def handle(self, player, game, play): if self.game.status is Game.WAITING: return JsonErrorResponse({'error': 'game has not started'}) if self.game.status is Game.COMPLETE: return JsonErrorResponse({'error': 'game has ended'}) if self.player not in self.game.players.all(): return JsonErrorResponse({'error': 'player is not in game'}) if self.game.status is Game.STATUS[2]: return JsonErrorResponse({'error': 'game is over'}) if play not in self.game.hand(self.player): return JsonErrorResponse({'error': 'illegal play'}) turn = self.game.turns.all()[0] if turn.plays.filter(player=self.player).exists(): return JsonErrorResponse({'error': 'already played this turn'}) turn.plays.create(player=self.player, play=play) if self.game.autoplay: bot = Player.bot_player() strategy = strategies[settings.BOT_STRATEGY] turn.plays.create(player=bot, play=strategy( self.game.to_json(mask_for_player=bot))) if turn.is_complete(): if self.game.is_complete(): self.game.finalize() self.game.save() else: self.game.add_turn() log.info("{} played {} in {}".format(self.player, play, self.game)) return redirect('show_game', self.player.pid, self.game.gid)
def handle(self, player, game): if self.game.status == Game.WAITING: self.game.autoplay = True self.game.players.add(Player.bot_player()) self.game.status = Game.PLAYING self.game.add_turn() self.game.save() log.info("{} set {} to autoplay".format(self.player, self.game)) return redirect('show_game', self.player.pid, self.game.gid) else: return JsonErrorResponse({'error': 'game already started'})