def new_game(self, request): """Creates new game""" player_one = User.query(User.name == request.player_one_name).get() player_two = User.query(User.name == request.player_two_name).get() if not player_one: raise endpoints.NotFoundException('Player one "' + request.player_one_name + '" does not exist!') if not player_two: raise endpoints.NotFoundException('Player two "' + request.player_two_name + '" does not exist!') game = Game.new_game(player_one.key, player_two.key, request.freak_factor) # Increment active count of games taskqueue.add(url='/tasks/increment_active_games') message = "Game created successfully" game_history = GameHistory(parent=game.key) game_history.history.append(game) game_history.messages.append(message) game_history.put() return game.to_form(message)
def _cache_game_move(game, position, message, urlsafe_game_key): """Populates memcache with the actual moves of the Game""" if not game.game_over: history = memcache.get(MEMCACHE_GAME_HISTORY_PREFIX + urlsafe_game_key) or [] game_history = GameHistory(username=game.current_player, position=position, message=message) history.append(game_history) memcache.set(MEMCACHE_GAME_HISTORY_PREFIX + urlsafe_game_key, history)
def make_a_move(self, request): """ This function enables a player to move during the race. Input: game safe url key, player username, number render by the Dice""" if request.dice not in diceValue: raise endpoints.NotFoundException('Invalid Dice Number') game = utils.get_by_urlsafe(request.game_urlsafekey, Game) if game.status != "start": raise endpoints.NotFoundException( 'This game is either over or save!! Resume if saved') player = Position.query( Position.game == game.key, Position.player == utils.get_by_username( request.player).key).get() if not player.isPlayingNow: raise endpoints.NotFoundException( 'This player does not have token to play now') # Who is Player and who is Opponent ? Current user is always the # PLAYER if game.player == player.player: opponent = Position.query(Position.game == game.key, Position.player == game.opponent).get() elif game.opponent == player.player: opponent = Position.query(Position.game == game.key, Position.player == game.player).get() # IMPORTANT !!!! # player and opponent are key objects in POSITION model # change position player.position = utils.move(player.position, request.dice) self._updateScore(game, player) # remove token player.isPlayingNow = False player.put() game_status = "Token False. Time for your opponent to play!" # Opponent is given token to play opponent.isPlayingNow = True msg = "Player %s played %s; Distance Moved:%s; Points earned: %s " % ( request.player, request.dice, player.position, player.position) GameHistory(game=game.key, message=msg).put() # Opponent loose its position as first if player.position == opponent.position: opponent.position -= 1 # GAME Over if player.position == 100: opponent.isPlayingNow = False game.gameOver(player) GameHistory(game=game.key, message="Player %s WON" % request.player).put() game_status = "GAME OVER, YOU WON THE RACE!!!!!" opponent.put() return MakeMoveForm( player=request.player, dice=request.dice, position=player.position, score=Score.query(Score.game == game.key, Score.player == player.player).get().score, status=game_status)