From a321a184a40c0749585454c49355f8e0a26169b3 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sun, 25 Aug 2024 17:19:25 +0200 Subject: [PATCH] GameStatistic now returns games and results seperately, results might not be a board game attribute --- src/classes/statistic_classes.py | 5 +-- src/main.py | 9 ++++++ src/modules/data_connection.py | 7 ++--- src/modules/statistic_creator.py | 53 ++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/classes/statistic_classes.py b/src/classes/statistic_classes.py index 4288877..d6bfc03 100644 --- a/src/classes/statistic_classes.py +++ b/src/classes/statistic_classes.py @@ -2,7 +2,7 @@ from pydantic import BaseModel from typing import Union, Dict from datetime import date -from src.classes import boardgame_classes, player_classes +from src.classes import boardgame_classes class StatisticBase(BaseModel): name: str @@ -14,7 +14,8 @@ class PlayerStatistic(StatisticBase): result: Dict[str, float] class GamesStatistic(StatisticBase): - result: list[boardgame_classes.BoardGamePublic] + games: list[boardgame_classes.BoardGamePublicNoPlays] + result: Dict[int, Union[int,float]] model_config = { 'validate_assignment':True diff --git a/src/main.py b/src/main.py index 1005c60..f09cc89 100644 --- a/src/main.py +++ b/src/main.py @@ -30,6 +30,8 @@ def refresh_data(): data_connection.get_user_owned_collection(session) data_connection.get_user_wishlist_collection(session) data_connection.get_plays(session) + + print('DONE') is_refreshing = False @@ -207,6 +209,13 @@ def get_most_expensive_games(query: BoardgameFilterParams = Depends(), top_amoun return statistic_to_return +@app.get('/statistics/cheapest_per_play', response_model=statistic_classes.GamesStatistic) +def get_cheapest_per_play_games(query: BoardgameFilterParams = Depends(), top_amount: int = 10, session: Session = Depends(get_session)): + + statistic_to_return = statistic_creator.get_cheapest_per_play_games(session, query, top_amount) + + return statistic_to_return + @app.get('/statistics/shelf_of_shame', response_model=statistic_classes.GamesStatistic) def get_shelf_of_shame(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)): diff --git a/src/modules/data_connection.py b/src/modules/data_connection.py index 109b6cd..de32318 100644 --- a/src/modules/data_connection.py +++ b/src/modules/data_connection.py @@ -74,12 +74,11 @@ def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) - wishlisted_boardgames_from_db = db_connection.get_wishlist_boardgames(session) - to_return_boardgames = wishlisted_boardgames_from_db - + print(wishlisted_boardgames_from_db) if wishlist_priority != 0: - to_return_boardgames = list(filter(lambda game: game.wishlist_info.wishlist_priority == wishlist_priority, to_return_boardgames)) + wishlisted_boardgames_from_db = list(filter(lambda game: game.wishlist_info.wishlist_priority == wishlist_priority, wishlisted_boardgames_from_db)) - return to_return_boardgames + return wishlisted_boardgames_from_db def get_plays(session: Session) -> list[play_classes.Play]: diff --git a/src/modules/statistic_creator.py b/src/modules/statistic_creator.py index 52ffccf..205a2ac 100644 --- a/src/modules/statistic_creator.py +++ b/src/modules/statistic_creator.py @@ -190,9 +190,15 @@ def get_most_expensive_games(session: Session, filtering_query: BoardgameFilterP most_expensive_games = most_expensive_games[0:top_amount] + result_dict = {} + + for boardgame in most_expensive_games: + result_dict[boardgame.id] = boardgame.owned_info.price_paid + statistic_dict = { "name":statistic_name, - "result":most_expensive_games + "games":most_expensive_games, + "result":result_dict } statistic_to_return = statistic_classes.GamesStatistic.model_validate(statistic_dict) @@ -201,6 +207,43 @@ def get_most_expensive_games(session: Session, filtering_query: BoardgameFilterP return statistic_to_return +def get_cheapest_per_play_games(session: Session, filtering_query: BoardgameFilterParams = None, top_amount: int = 10) -> statistic_classes.GamesStatistic: + statistic_name = 'Cheapest plays' + + md5hash, cached_statistic = get_from_cache({**locals()}) + + if cached_statistic != None: + return cached_statistic + + owned_games = data_connection.get_user_owned_collection(session) + + owned_games = filtering_query.do_filtering(owned_games) + + owned_games = list(filter(lambda x: len(x.plays) != 0 and x.owned_info.price_paid != 0,owned_games)) + + owned_games.sort(key=lambda x: x.owned_info.price_paid / len(x.plays)) + + cheapest_per_play_games = owned_games[0:top_amount] + + result_dict = {} + + for boardgame in cheapest_per_play_games: + result_dict[boardgame.id] = boardgame.owned_info.price_paid / len(boardgame.plays) + + statistic_dict = { + "name":statistic_name, + "games":cheapest_per_play_games, + "result":result_dict + } + + statistic_to_return = statistic_classes.GamesStatistic.model_validate(statistic_dict) + + cached_statistics[md5hash] = statistic_to_return + + return statistic_to_return + + + def get_shelf_of_shame(session: Session, filtering_query: BoardgameFilterParams = None) -> statistic_classes.GamesStatistic: @@ -229,9 +272,15 @@ def get_shelf_of_shame(session: Session, filtering_query: BoardgameFilterParams owned_boardgames_no_plays.sort(key=lambda x: x.name) + result_dict = {} + + for boardgame in owned_boardgames_no_plays: + result_dict[boardgame.id] = len(boardgame.plays) + statistic_dict = { "name":statistic_name, - "result":owned_boardgames_no_plays + "games":owned_boardgames_no_plays, + "result":result_dict } statistic_to_return = statistic_classes.GamesStatistic.model_validate(statistic_dict)