diff options
Diffstat (limited to 'robots/server.py')
-rw-r--r-- | robots/server.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/robots/server.py b/robots/server.py index 421afd8..1b87392 100644 --- a/robots/server.py +++ b/robots/server.py @@ -8,6 +8,8 @@ import logbook import network +from robots.state import GameState + log = logbook.Logger(__name__) def n_required_args(argspec): @@ -113,6 +115,27 @@ class Server(object): log.debug('Created new bot instance %s' % instance_id) return success(instance_id=instance_id) + def next_move(self, instance_id, whoami, state): + try: + instance = self.instances[instance_id] + except KeyError: + return not_found('No instance %s on server.' % instance_id) + + state = GameState.from_json(state) + + try: + result = instance(whoami, state) + except Exception: + message = 'Exception running instance %s.' % instance_id + log.exception(message) + return server_error(message) + + return success(result=result) + + def destroy(self, instance_id): + self.instances.pop(instance_id, None) + return success() + def handle(self, request): try: action = request['action'] @@ -127,8 +150,9 @@ class Server(object): elif action == 'process': instance_id = request['instance_id'] + whoami = request['whoami'] state = request['state'] - return self.next_move(instance_id, state) + return self.next_move(instance_id, whoami, state) elif action == 'destroy': instance_id = request['instance_id'] |