def get_by_game_id(game_id):
    """ This is to find a game entity by an assigned game_id """
    """ if no game is found None is returned """
    if game_id:
        games = OthelloGame.query(OthelloGame.game_id == game_id)
        if games:
            game = [g for g in games if g.game_id == int(game_id)]
            print "GAME:", game
            if game:
                return game[0]
    return game
    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))
    def _newOthelloGame(self, request):
        """ Logic for creating a new Othello game.
        Create ndb types that will store the game
        logic using the OthelloLogic class
        implementation """

        # set game mode
        if request.player_two:
            playerNames = [ request.player_one, request.player_two ]
        else:
            playerNames = [ request.player_one ]

        if len(playerNames) == 1:
            game_mode = SINGLE_PLAYER_MODE
        else:
            game_mode = TWO_PLAYER_MODE

        # get OthelloPlayer keys for this game
        # assert : all keys have been verified to exist
        othello_players_keys = [ndb.Key(
            OthelloPlayer, name, parent=ndb.Key(
                User, name)) for name in playerNames]

        # initialize logic
        new_game_logic = OthelloLogic(game_mode=game_mode)
        # initialize game object with
        # explicit array name containers
        json_board = json.dumps(new_game_logic.B)
        json_array_x = json.dumps(new_game_logic.X)
        json_array_y = json.dumps(new_game_logic.Y)
        json_array_n = json.dumps(new_game_logic.N)
        json_array_move = json.dumps(new_game_logic.D)
        json_check_move = json.dumps(new_game_logic.C)
        game_logic = OthelloDatastoreLogic(
                board=json_board,
                player_turn=new_game_logic.CP,
                check_move=json_check_move,
                array_move=json_array_move,
                array_x=json_array_x,
                array_y=json_array_y,
                array_n=json_array_n,
                game_mode=new_game_logic.game_mode)

        new_game = OthelloGame(
                status="ACTIVE",
                starttime=datetime.today(),
                gamelogic=game_logic,
                userKeys=othello_players_keys)

        # link new game to creator, in this case, first user in player list
        g_id = OthelloGame.allocate_ids(size=1,
                                        parent=othello_players_keys[0])[0]
        g_key = ndb.Key('OthelloGame', g_id, parent=othello_players_keys[0])
        new_game.key = g_key
        # add generated id as game id
        new_game.game_id = g_id

        # create game history entity
        # adding scoreboard entry
        new_game.put()
        print "Game created"
        gh_id = OthelloGameHistory.allocate_ids(size=1, parent=g_key)[0]
        gh_key = ndb.Key(OthelloGameHistory, gh_id, parent=g_key)
        game_history = OthelloGameHistory(key=gh_key)

        game_history.put()

        # load OtheloPlayer entities
        players = ndb.get_multi(othello_players_keys)
        for p in players:
            p.gameKeys.append(g_key)
        ndb.put_multi(players)
        print "Added game to player game keys"

        return new_game.game_id