예제 #1
0
def register(username: str, password: str):
    profile = Profile.get_or_none(Profile.username == username)
    if profile is not None:
        raise BadRequest('A user with this username already exists')
    hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
    profile = Profile.create(
        username=username,
        hashed_password=hashed,
    )
    global_game = Game.get_or_none(Game.name == 'Global Indefinite')
    GameProfile.create(game=global_game,
                       profile=profile,
                       cash=global_game.starting_cash)
    global_game = Game.get_or_none(Game.name == 'Global Timed')
    GameProfile.create(game=global_game,
                       profile=profile,
                       cash=global_game.starting_cash)
    send_notification(profile, 'Welcome to Fortune!')
    send_notification(
        profile,
        'Click the Play button in the menubar on top to create a new game.')
    send_notification(
        profile,
        'To adjust account options, see achiements, and add friends, click on your username on the top and select Profile'
    )
    return create_auth_token(profile)
예제 #2
0
def active_games_at_page(profile_id, page_number, keyword, criteria):
    """ Returns
        - active games in the requested page that matches the keyword (sorted by criteria)
        - number of active games matching the keyword
    """

    games_query = Game.select() \
        .join(GameProfile) \
        .where((GameProfile.profile == profile_id)
               & Game.name.contains(keyword)
               & (Game.ends_at > datetime.utcnow()))  # Note: This line always filters out Global Indef.

    # LOOKATME: Depending on the keyword, the call above might return EVERY games from the database that belongs to
    # the player. If that is expensive, optimize this.

    games = [gm for gm in games_query]
    global_indef = Game.get_or_none(Game.id == 1)

    include_global_indef = False
    if keyword.lower() in global_indef.name.lower(
    ):  # Cannot append global_indef to games yet,
        include_global_indef = True  # else sorting by endTime will break

    total_games = len(games) + 1 if include_global_indef else len(games)
    max_page = int(ceil(total_games / PAGE_SIZE))

    # Verify page is within range
    page_number = max(1, page_number)
    page_number = max_page if page_number > max_page else page_number

    from operator import attrgetter
    if criteria['titleAscending']:
        games.append(global_indef)
        games.sort(key=attrgetter("name"))
    elif criteria['titleDescending']:
        games.append(global_indef)
        games.sort(key=attrgetter("name"), reverse=True)
    elif criteria['endTimeAscending']:
        games.sort(key=attrgetter("ends_at"))
        games.append(global_indef)
    elif criteria['endTimeDescending']:
        games.sort(key=attrgetter("ends_at"), reverse=True)
        games.insert(0, global_indef)
    else:
        games.insert(0, global_indef)

    first_index_in_page = (page_number - 1) * PAGE_SIZE
    games = games[first_index_in_page:first_index_in_page + PAGE_SIZE]

    return games, total_games, PAGE_SIZE
예제 #3
0
def update_game(
    game_id,
    name,
    starting_cash,
    ends_at,
    active_coins,
):
    game = Game.get_or_none(Game.id == game_id)
    if not game:
        raise BadRequest(f'A game with id {game_id} does not exist')
    game.name = name
    game.starting_cash = starting_cash
    game.ends_at = ends_at
    # delete all GameCoins for this game and just re-create
    GameCoin.delete().where(Game.id == game_id)
    create_gamecoins_for_game(game, active_coins)
    return game
예제 #4
0
def create_chat_message(profile_id, game_id, message):
    try:
        int(profile_id)
        int(game_id)
        assert len(message) > 0
    except:
        raise BadRequest("Invalid parameters to create chat message")

    message = Message.create(
        game=game_id,
        profile=profile_id,
        content=message,
        # use default value for created_on
    )

    game = Game.get_or_none(Game.id == game_id)
    if game is not None:
        print("Broadcaseting to game")
        broadcast_in_a_game(game, 'chat', '', False)

    return message
예제 #5
0
def create_game(log, platform: str, category: str, name: str, gist_file: GistFile) -> bool:
    # Проверяем, что игры с такой категорией нет в базе
    game = Game.get_or_none(name=name, platform=platform, category=category)
    if game:
        return False

    finish_datetime = None

    # Если игра уже добавлена как завершенная
    if is_finished(category):
        finish_datetime = gist_file.committed_at

    log.info(f'Added {name!r} ({platform} / {category})')
    Game.create(
        name=name,
        platform=platform,
        category=category,
        append_datetime=gist_file.committed_at,
        finish_datetime=finish_datetime,
    )

    return True
# В какой-то момент поменял платформу с PS на PS1, нужно играм с PS1 указать
# на те же игры с PS
# В другой момент писал 'PS 2', после поменял название категории на PS2
old_by_new_platforms = {
    'PS': 'PS1',
    'PS 2': 'PS2',
    'PS 3': 'PS3',
    'PS 4': 'PS4',
}
for old_platform, new_platform in old_by_new_platforms.items():
    # Ищем игры с указанной платформой и без заданного root_alias
    for game in Game.select().where(Game.platform == new_platform,
                                    Game.root_alias.is_null()):
        root_game = Game.get_or_none(Game.platform == old_platform,
                                     Game.name == game.name,
                                     Game.category == game.category)
        # Если не найдено или если у найденной id больше, чем у требуемой игры
        if not root_game or root_game.id > game.id:
            continue

        print(f'In game #{game.id} setted root_alias from #{root_game.id}')
        game.root_alias = root_game
        game.save()

# У некоторых игр арабские цифры в названии были заменены на римские
for root_game in Game.select():
    m = re.search(r'\d+', root_game.name)
    if not m:
        continue
예제 #7
0
def get_game_by_id(game_id):
    game = Game.get_or_none(Game.id == game_id)
    if not game:
        raise BadRequest('Game not found')
    return game
예제 #8
0
def main() -> bool:
    changed = False
    t = time.perf_counter()

    last_committed_at = Settings.get_value('last_committed_at')
    log.info(f'Last committed at: {last_committed_at}')

    query = GistFile.select().order_by(GistFile.committed_at.asc())
    if last_committed_at:
        query = query.where(GistFile.committed_at > last_committed_at)

    for gist_file in query:
        log.info(gist_file)
        last_committed_at = gist_file.committed_at

        platforms = parse_played_games(gist_file.content, silence=True)

        for platform, category, name in iter_parse_played_games(platforms):
            # Пропускаем, если игра уже есть в базе
            game = Game.get_or_none(name=name, platform=platform, category=category)
            if game:
                continue

            # Попробуем найти игру по предыдущей категории с учетом типа
            need_category = None
            if category == FINISHED_GAME:
                need_category = NOT_FINISHED_GAME
            elif category == FINISHED_WATCHED:
                need_category = NOT_FINISHED_WATCHED

            if need_category:
                game = Game.get_or_none(name=name, platform=platform, category=need_category)
            else:
                game = Game.get_or_none(name=name, platform=platform)

            # Если игра еще не добавлена
            if not game:
                changed = create_game(log, platform, category, name, gist_file)
                continue

            # Если у игры в базе тип категории отличается от текущего типа категории
            # Пример: игра была ранее просмотрена, а теперь сыграна
            if is_gamed(category) != is_gamed(game.category):
                changed = create_game(log, platform, category, name, gist_file)
                continue

            # Если уже завершена, то работа с игрой закончена
            if is_finished(game.category):
                continue

            # Тип категории совпадает
            is_equals_type_category = (
                is_gamed(category) == is_gamed(game.category)
                or
                is_watched(category) == is_watched(game.category)
            )

            # Если статус поменялся
            # Пример: NOT_FINISHED_GAME -> FINISHED_GAME
            # Но не:  NOT_FINISHED_GAME -> FINISHED_WATCHED
            if is_equals_type_category and game.category != category:
                log.info(f'Updated {name!r} ({platform}). {game.category} -> {category}')
                game.category = category
                game.ignored = False  # На всякий случай

                # Если игра стала завершенной
                if is_finished(game.category):
                    game.finish_datetime = gist_file.committed_at

                game.save()
                changed = True

        Settings.set_value('last_committed_at', last_committed_at)

    log.info(f'Elapsed {int(time.perf_counter() - t)} secs')
    return changed
예제 #9
0
def get_game_with_code(code: str):
    game = Game.get_or_none(Game.shareable_code == code)
    return game