def get_game(self, request):
     """ Returns the current state of a certain game """
     game = Game.get_from_key(urlsafe_key=request.game_key)
     if game:
         return game.to_message()
     else:
         raise endpoints.NotFoundException("The game could not be found")
 def cancel_game(self, request):
     """ Delete a non-ended game """
     game = Game.get_from_key(urlsafe_key=request.game_key)
     if not game:
         raise endpoints.NotFoundException("The game could not be found")
     if game.game_over:
         raise endpoints.BadRequestException("The game is already over!")
     else:
         game.key.delete()
         return StringMessage(message="Game successfully canceled.")
 def get_game_history(self, request):
     """
     Gets the movement history for a specific game.
     Each moviement is composed of two fields separated by colon.
     The first is the origin point in the board.
     The second is the direction which can be 'u', 'd', 'l', 'r'
     for 'up', 'down', 'left' and 'right' respectively.
     """
     game = Game.get_from_key(urlsafe_key=request.game_key)
     if not game:
         raise endpoints.NotFoundException("The game could not be found")
     return game.to_historymessage()
 def give_up(self, request):
     """
     Gives up a game. The game will be ended and scores will be calculated.
     """
     game = Game.get_from_key(urlsafe_key=request.game_key)
     if not game:
         raise endpoints.NotFoundException("The game could not be found")
     if game.game_over:
         raise endpoints.BadRequestException("The game is already over")
     gamelogic.end_game(game)
     self.commit_game_end(game)
     return StringMessage(message="Game ended. Score: %s." % game.score)
 def make_move(self, request):
     """ Make move in the game, returning the new game state"""
     game = Game.get_from_key(urlsafe_key=request.game_key)
     if not game:
         raise endpoints.NotFoundException("The game could not be found")
     try:
         game = gamelogic.make_move(
                 game, (request.origin_point, request.direction))
     except (ValueError, gamelogic.InvalidMoveExpection) as e:
         raise endpoints.BadRequestException(e.message)
     if game.game_over:
         gamelogic.end_game(game)
         self.commit_game_end(game)
     else:
         game.put()
     return game.to_message()