def put(self, game_id, turn_no): turn_id = BingoTurn.turn_id(game_id, turn_no) turn = BingoTurn.get(turn_id) if self.user == turn.player: game = BingoGame.get(game_id) data = self._get_data() turn.call(data['call']) turn.save() called_no = [t.turn_no for t in game.turns] user1_won, user1_cond = game.won(game.user1_metric, called_no) user2_won, user2_cond = game.won(game.user1_metric, called_no) if any((user1_won, user2_won)): # XXX win the same time? if user1_won: game.winner = game.user1 game.condition = user1_cond elif user2_won: game.winner = game.user2 game.condition = user2_cond game.status = models.FINISHED game.end_time = datetime.utcnow() else: # next turn if turn.player == game.user1: next_user = game.user2 else: next_user = game.user1 next_turn = BingoTurn(game, turn.turn_no + 1, next_user) next_turn.save() game.turns.append(next_turn) game.save() else: raise HTTPError(400, 'not_your_turn:%s,%s' % (turn_id,self.user), reason='not_your_turn')
def get(self, game_id, turn_no): turn_id = BingoTurn.turn_id(game_id, turn_no) turn = BingoTurn.get(turn_id) self.write(turn.to_dict())