def test_from_json_bad_command(self): """Raises GameActionError if the valid JSON doesn't represent a valid Command. """ c_json = '{"game":"notagame", "number": 0, "action":{"action": 0, "args": [true]}}' with self.assertRaises(GameActionError): a = Command.from_json(c_json) c_json = '{"game":"0", "number": 0, "action":{"args": [true]}}' with self.assertRaises(ParsingError): a = Command.from_json(c_json)
def test_from_json_bad_command(self): """Raises GameActionError if the valid JSON doesn't represent a valid Command. """ c_json = '{"game":"notagame", "action":{"action": 0, "args": [true]}}' with self.assertRaises(GameActionError): a = Command.from_json(c_json) c_json = '{"game":"0", "action":{"args": [true]}}' with self.assertRaises(ParsingError): a = Command.from_json(c_json)
def test_from_bad_json(self): """Failure to convert bad JSON raises ParsingError. """ c_json = '{"act]]]}' # not valid JSON with self.assertRaises(ParsingError): c = Command.from_json(c_json)
def test_from_json(self): """Convert JSON dictionary to Command. """ c_json = '{"game":1, "action":{"action": 0, "args": [true]}}' c = Command.from_json(c_json) self.assertEqual(c.action.action, message.THINKERORLEAD) self.assertEqual(c.game, 1) self.assertEqual(c.action.args, [True])
def test_from_json(self): """Convert JSON dictionary to Command. """ c_json = '{"game":1, "number": 0, "action":{"action": 0, "args": [true]}}' c = Command.from_json(c_json)[0] self.assertEqual(c.action.action, message.THINKERORLEAD) self.assertEqual(c.game, 1) self.assertEqual(c.action.args, [True])
def test_multiple_commands_to_json_non_sequential_numbers(self): a0 = GameAction(message.THINKERORLEAD, True) a1 = GameAction(message.THINKERTYPE, True) c0 = Command(1, 0, a0) c1 = Command(1, 3, a1) c0_json, c1_json = [c.to_json() for c in (c0,c1)] list_json = '[{0},{1}]'.format(c0_json, c1_json) with self.assertRaises(ParsingError): commands = Command.from_json(list_json)
def test_multiple_commands_from_json(self): """Convert a list of Commands to JSON. """ a0 = GameAction(message.THINKERORLEAD, True) a1 = GameAction(message.THINKERTYPE, True) c0 = Command(1, 0, a0) c1 = Command(1, 1, a1) c0_json, c1_json = [c.to_json() for c in (c0,c1)] list_json = '[{0},{1}]'.format(c0_json, c1_json) commands = Command.from_json(list_json) self.assertEqual(len(commands), 2) for c, c_orig in zip(commands, (c0,c1)): self.assertEqual(c_orig.action.action, c.action.action) self.assertEqual(c_orig.action.args, c.action.args) self.assertEqual(c_orig.number, c.number) self.assertEqual(c_orig.game, c.game)
def on_message(self, message): lg.debug('WS handler received message: '+str(message)) try: commands = Command.from_json(message) except ParsingError as e: self.message_error_count += 1 if self.message_error_count >= self.MESSAGE_ERROR_THRESHOLD: self.close() else: self.send_command( Command(None, None, GameAction(cloaca.message.SERVERERROR, 'Error parsing message: '+e.message))) return else: user_id = self.current_user['user_id'] game_id = commands[0].game if commands[0].action.action == cloaca.message.LOGIN: lg.debug('Ignoring deprecated LOGIN message.') elif commands[0].action.action == cloaca.message.REQGAMESTATE: lg.debug('Received request for game {0!s}.'.format(game_id)) try: game_encoded = yield self.server.get_game_data(user_id, game_id) except GTRError as e: lg.debug('Sending error') self.send_error(e.message) else: resp = Command(game_id, None, GameAction(cloaca.message.GAMESTATE, game_encoded)) lg.debug('Sending game '+str(game_id)) self.send_command(resp) elif commands[0].action.action == cloaca.message.REQGAMELOG: lg.debug('Received request for log game {0!s}.'.format(game_id)) n_messages, n_start = commands[0].action.args yield self.server.retrieve_and_send_log_messages( user_id, game_id, n_messages, n_start) else: yield self.server.handle_game_actions( game_id, user_id, [[c.number, c.action] for c in commands])