def post(self): data = self._get_data() game = BingoGame(name=data['name'], user1=self.user) if 'metric' in data: game.user1_metric = data['metric'] game.save() self.write(game.to_dict())
def get(self, game_id): game = BingoGame.get(game_id) game_dict = game.to_dict() game_dict['called_no'] = [t.called for t in game.turns if t.called] if game.status == models.FINISHED: game_dict['user1_metric'] = game.user1_metric game_dict['user2_metric'] = game.user2_metric self.write(game_dict)
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 put(self, game_id): game = BingoGame.get(game_id) data = self._get_data() curr_user = self.user if curr_user == game.user1: game.user1_metric = data['metric'] elif curr_user == game.user2: game.user2_metric = data['metric'] elif not game.user2: game.user2 = self.user if 'metric' in data: game.user2_metric = data['metric'] game.status = models.PREPARING else: raise HTTPError(400, 'game_full:%s' % game_id, reason='game_full') if game.user1_metric and game.user2_metric: # game start game.status = models.PLAYING turn = BingoTurn(game, 1, game.user1) turn.save() game.turns.append(turn) game.save()
def get(self): games = BingoGame.list() data = {'data': g.to_dict for g in games} self.write(data)