8 def __init__(self, game, save_file='savefile'):
9 from plomrogue.parser import Parser
10 self.parser = Parser(game)
12 self.save_file = save_file
16 """Handle commands coming through queue q, run game, send results back."""
19 command, connection_id = q.get(timeout=0.001)
20 self.handle_input(connection_id, command)
25 """Start game loop, set up self.queue to communicate with it.
27 The game loop works sequentially through game commands received
28 via self.queue from connected servers' clients."""
29 self.queue = queue.Queue()
30 c = threading.Thread(target=self.loop, args=(self.queue,))
33 def start_server(self, port, server_class):
34 """Start server of server_class in talk with game loop.
36 The server communicates with the game loop via self.queue.
38 server = server_class(self.queue, port)
39 self.servers += [server]
40 c = threading.Thread(target=server.serve_forever)
43 def handle_input(self, input_, connection_id=None, god_mode=False):
44 """Process input_ to command grammar, call command handler if found.
46 Command handlers that have no connectin_i argument in their
47 signature will only be called if god_mode is set.
50 from inspect import signature
51 from plomrogue.errors import GameError, ArgError, PlayError
52 from plomrogue.misc import quote
54 def answer(connection_id, msg):
56 self.send(msg, connection_id)
61 command, args = self.parser.parse(input_)
63 answer(connection_id, 'UNHANDLED_INPUT')
65 if 'connection_id' in list(signature(command).parameters):
66 command(*args, connection_id=connection_id)
69 #if store and not hasattr(command, 'dont_save'):
70 # with open(self.game_file_name, 'a') as f:
71 # f.write(input_ + '\n')
73 answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
74 except PlayError as e:
75 answer(connection_id, 'PLAY_ERROR ' + quote(str(e)))
76 except GameError as e:
77 answer(connection_id, 'GAME_ERROR ' + quote(str(e)))
79 def send(self, msg, connection_id=None):
80 """Send message msg to servers' client(s).
82 If a specific client is identified by connection_id, only
83 sends msg to that one. Else, sends it to all client sessions.
87 for server in self.servers:
88 if connection_id in server.clients:
89 client = server.clients[connection_id]
92 for c_id in self.game.sessions:
93 for server in self.servers:
94 if c_id in server.clients:
95 client = server.clients[c_id]