Example #1
0
def win_game(game=None, winner=None, owner=None):
    """
    Makes winner user win the game
    :param game: game that winner won
    :param winner: winner of the game
    :param owner: owner of the game
    """
    if game is not None and winner is not None and owner is not None and (
            game.owner.key() == owner.key() or owner.role == "admin"):
        game.winner = winner
        game.is_active = False
        Game.save(game)
        return game
Example #2
0
def reopen_game(game=None, user=None):
    """
    Removes game winner and reopens the game
    :param game_id: id of the game to reopen
    :param user: user that reopens the game, must be the owner of the game
    """

    if game is not None and user is not None and (
            game.owner.key() == user.key() or user.role == "admin"):
        game.winner = None
        game.is_active = True
        for treasure in game.treasures:
            for image in treasure.images:
                Snapshot.delete(image)
        Game.save(game)
Example #3
0
def register_game(game: RegisterGameRequest):
    black, white = (player_repository.find_by_name(game.black_player_name),
                    player_repository.find_by_name(game.white_player_name))

    winner, loser = (white, black) if game.result == 'WHITE' else (black,
                                                                   white)

    winner.elo, loser.elo = rate_1vs1(winner.elo,
                                      loser.elo,
                                      drawn=game.result == 'DRAW')

    game = Game(white_player_id=white.id,
                white_player_name=white.name,
                white_player_elo=white.elo,
                black_player_id=black.id,
                black_player_name=black.name,
                black_player_elo=black.elo,
                result=game.result,
                time=datetime.now())

    game = game_repository.save(game)
    player_repository.update(winner)
    player_repository.update(loser)

    return game
Example #4
0
def get_or_insert_game(zone=None,
                       treasures=None,
                       owner=None,
                       name=None,
                       is_active=True):
    """Get or insert the game with the information provided

        Args:
            :param is_active: Allows knowing if the game is active or not.
                :type: Boolean
            :param name: The name that receives the game.
                :type: String
            :param owner: The user who is the owner of the game
                :type: User
            :param treasures: The list of treasures the participants need to find to win the game
                :type: [Treasure]
            :param zone: The zone where the treasures are located
                :type: Zone
        Raises:
            Exception: if the required parameters are not present or are None
        Returns:
            Game: The game from db or the one which was just created
    """
    if name is None or owner is None or owner.email is None:
        return None
    game = Game.get_or_insert(key_name=owner.email + "_" + name,
                              is_active=is_active,
                              name=name,
                              zone=zone,
                              treasures=treasures,
                              owner=owner,
                              participants=None,
                              winner=None)
    return game
Example #5
0
def get_created_games_by_user(user=None):
    """

    :return: all games in the database that were created by the provided user
    """
    if user is not None:
        return filter(lambda x: owning_game(element=x, user=user), Game.all())
    return list()
Example #6
0
def get_game_by_id(game_id=None):
    """Get the game from db with the id provided
        Args:
            :param _id: The id of the game to search in db
                :type: Game
        Returns:
            Game: The game if it is already stored, None in other case
    """
    if game_id is None:
        return None
    return Game.get(game_id)
Example #7
0
def get_game_by_owner_and_name(owner=None, game_name=None):
    """Get the game from db whose name is "game_name" and the owner is "owner"
        Args:
            :param owner: The owner of the game to obtain
                :type: User
            :param game_name: The name of the game to obtain
                :type: String
    """
    if owner is None or owner.email is None or game_name is None:
        return None
    return Game.get_by_key_name(key_names=owner.email + "_" + game_name)
Example #8
0
def exists_game(game_name=None, user=None):
    """Get a boolean value corresponding with the evidence of an existing game provided in db
        Args:
            :param game: The game to search in db
                :type: Game
        Returns:
            Boolean: Specify if the game is already stored in db
    """
    if game_name is None or user is None:
        return False
    return Game.get_by_key_name(key_names=user.email + "_" +
                                game_name) is not None
Example #9
0
def join_game(game_id=None, user=None):
    """

    :param game_id: idof the game that user joins
    :param user: user what joins the game
    """

    if user is not None and game_id is not None:
        game = Game.get(game_id)
        Participant.get_or_insert(key_name=user.email + "_" + game_id,
                                  game=game,
                                  user=user)
Example #10
0
def delete_game(game_id=None, user=None):
    """Delete the game from db

        Args:
            :param game_id: The game id which is going to be deleted from db
                :type: Game
    """
    if user is not None and game_id is not None:
        game = Game.get(game_id)
        if game.owner.key() == user.key() or user.role == "admin":
            for treasure in game.treasures:
                db.delete(treasure.images)
                db.delete(treasure)
            db.delete(game)
Example #11
0
def get_games_not_joined_by_user(user=None):
    """

    :return: all games in the database that are actives and the user hasn't joined yet
    """
    if user is not None:
        result = list()
        for game in Game.all():
            if game not in get_active_games_by_user(
                    user=user) and game not in get_created_games_by_user(
                        user=user) and game not in get_completed_games_by_user(
                            user=user):
                result.append(game)
        return result
    return list()
Example #12
0
def write_game_json(game=None, user=None, map_json=None):
    """
    Updates map property in game entity
    :param game: game to update
    :param user: user that made the update. Must be the owner of the game
    :param map_json: json to write in map property of game
    :return: game updated
    """
    if game is not None and user is not None and (
            user.key() == game.owner.key()
            or user.role == "admin") and map_json is not None:
        create_treasures_with_json(game=game,
                                   user=user,
                                   map_json=json.loads(map_json))
        print(map_json)
        game.map = json.dumps(map_json)
        return Game.put(game)
    else:
        return None