def cancel_game(self, request): """ Cancel game. Game state and history is preserved in datastore. Only requestor of game (parent) can cancel it""" # try to get game by safe_url or by game_id game = get_by_game_id(request.game_id) if not game: # game cannot be found raise endpoints.NotFoundException('Game not found!') if game.status == "CANCELLED": raise endpoints.BadRequestException('Game has already been' ' cancelled.') if not game.status == "ACTIVE": raise endpoints.BadRequestException('And ended game cannot' ' be cancelled.') if game: creator = OthelloPlayer.query( ancestor=ndb.Key(User, request.user_name)).get() if not creator: raise endpoints.BadRequestException('User does not exist.') else: if game.key.parent() != creator.key: raise endpoints.BadRequestException('User is not game' ' creator') # cancel game game.status = "CANCELLED" game.put() return SimpleMessage(message='Found and cancelled Othello game.')
def get_user_games(self, request): """ Returns all games where a user is active """ user_key = ndb.Key(User, request.user_name) print "User key", user_key player = OthelloPlayer.query(ancestor=user_key).get() if not player: raise endpoints.NotFoundException( 'A User with that name is not registered for Othello!') print "Getting games with keys", player.gameKeys games = ndb.get_multi(player.gameKeys) if not games: return OthelloGameForms(message='No games found for ' + request.user_name) # in memory filter for active games # instead of querying all active games with user in userKeys # it is probably faster to load game keys for user and filter in # memory only ACTIVE games. if games: active_games = [ag for ag in games if ag.status == "ACTIVE"] if active_games: return OthelloGameForms( othello_games=[self._copyOthelloGameToForm(game) for game in active_games]) else: return OthelloGameForms(message='No active games found for ' + request.user_name)
def _createOthelloPlayerObject(self, request): if User.query(User.name == request.user_name).get(): print "Found in User" else: # user not found, adding both to User user_key = ndb.Key(User, request.user_name) user = User(key=user_key, name=request.user_name, email=request.email) user.put() print "Added to User" u_key = ndb.Key(User, request.user_name) # check if it's OthelloPlayer if OthelloPlayer.query(ancestor=u_key).get(): raise endpoints.ConflictException( 'An Othello user with that name already exists!') # use user_name as key to get game date using creator user_name ou_key = ndb.Key(OthelloPlayer, request.user_name, parent=u_key) print "Got new key for othello player" data = {} data['key'] = ou_key # adding scoreboard entry sc_id = OthelloGame.allocate_ids(size=1, parent=ou_key)[0] sc_key = ndb.Key(OthelloScoreBoardEntry, sc_id, parent=ou_key) player_score_entry = OthelloScoreBoardEntry( key=sc_key, points=0, wins=0, winning_streak=0, score_difference_average=0) # Adding entities to datastore # Added to OthelloPlayer OthelloPlayer(**data).put() # Added to ScoreBoardEntry player_score_entry.put() return SimpleMessage(message='User {} created!'.format( request.user_name))