Fix some things

- @adventure doesn't think an adventure is currently running when the
adventure process has already exited
- added status command
This commit is contained in:
Joscha 2018-08-08 22:10:48 +00:00
parent 17c808ba26
commit fd24db53e0

View file

@ -48,6 +48,9 @@ class AdventureWrapper:
self.lines = [] self.lines = []
return b"".join(lines).decode("utf8") # Might result in an exception if in the middle of a character return b"".join(lines).decode("utf8") # Might result in an exception if in the middle of a character
def running(self):
return self.process.poll()
def _run(self): def _run(self):
while True: while True:
#line = self.process.stdout.readline() #line = self.process.stdout.readline()
@ -72,7 +75,8 @@ class Adventure:
SHORT_DESCRIPTION = "play the classic text adventure 'adventure'" SHORT_DESCRIPTION = "play the classic text adventure 'adventure'"
DESCRIPTION = "'adventure' can play the classic text adventure aptly named 'adventure'.\n" DESCRIPTION = "'adventure' can play the classic text adventure aptly named 'adventure'.\n"
COMMANDS = ( COMMANDS = (
"!adventure [start|stop|restart] - start/stop/restart the adventure\n" "!adventure start|stop|restart - start/stop/restart the adventure\n"
"!adventure status - check if there's currently an adventure running\n"
"> your command here - send a command to the adventure, if currently running\n" "> your command here - send a command to the adventure, if currently running\n"
) )
AUTHOR = "Created by @Garmy using github.com/Garmelon/yaboli\n" AUTHOR = "Created by @Garmy using github.com/Garmelon/yaboli\n"
@ -91,46 +95,45 @@ class Adventure:
if len(args) == 1: if len(args) == 1:
arg = args[0] arg = args[0]
if arg == "start": if arg == "start":
if room.roomname in self.adventures: adv = self.adventures.get(room.roomname)
if adv and adv.running():
await room.send("Adventure already running.", message.mid) await room.send("Adventure already running.", message.mid)
else: else:
adv = AdventureWrapper() adv = AdventureWrapper()
self.adventures[room.roomname] = adv self.adventures[room.roomname] = adv
send = parallel(room.send("Adventure started.", message.mid)) await room.send("Adventure started.", message.mid)
await asyncio.sleep(self.DELAY) await asyncio.sleep(self.DELAY)
text = adv.read() text = adv.read()
await room.send(text, message.mid) await room.send(text, message.mid)
await send
elif arg == "stop": elif arg == "stop":
try: adv = self.adventures.get(room.roomname)
adv = self.adventures.pop(room.roomname) if adv and adv.running:
adv.stop() adv.stop()
self.adventures.pop(room.roomname)
await room.send("Adventure stopped.", message.mid) await room.send("Adventure stopped.", message.mid)
except KeyError: else:
await room.send("Not adventure currently running.", message.mid) await room.send("Adventure not running.", message.mid)
elif arg == "restart": elif arg == "restart":
try: adv = self.adventures.get(room.roomname)
adv = self.adventures.pop(room.roomname) if adv and adv.running():
adv.stop() adv.stop()
except KeyError: await room.send("Adventure stopped.", message.mid)
adv = AdventureWrapper()
self.adventures[room.roomname] = adv
send = parallel(room.send("Adventure started.", message.mid))
else:
adv = AdventureWrapper()
self.adventures[room.roomname] = adv
send = parallel(room.send("Adventure restarted.", message.mid))
adv = AdventureWrapper()
self.adventures[room.roomname] = adv
await room.send("Adventure started.", message.mid)
await asyncio.sleep(self.DELAY) await asyncio.sleep(self.DELAY)
text = adv.read() text = adv.read()
await room.send(text, message.mid) await room.send(text, message.mid)
await send elif arg == "status":
adv = self.adventures.get(room.roomname)
if adv and adv.running():
await room.send("Adventure running.", message.mid)
else:
await room.send("Adventure not running.", message.mid)
else: else:
text = f"Unknown command: {arg!r}\n{self.COMMANDS}" text = f"Unknown command: {arg!r}\n{self.COMMANDS}"