From e70481b664ba1d93d109e05ef4caf0dad537e49c Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 10 Oct 2024 09:07:12 +0200 Subject: [PATCH] Added h-index statistic --- src/main.py | 5 ++++ src/modules/statistic_creator.py | 47 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index d92b36e..5bdbcd1 100644 --- a/src/main.py +++ b/src/main.py @@ -185,6 +185,11 @@ def get_players_from_play(play_id: int, session: Session = Depends(get_session)) return requested_players +@app.get('/statistics/h_index', response_model=statistic_classes.NumberStatistic) +def get_h_index(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)): + statistic_to_return = statistic_creator.get_h_index(session, query) + return statistic_to_return + @app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic) def get_amount_of_games(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)): diff --git a/src/modules/statistic_creator.py b/src/modules/statistic_creator.py index 6659012..fb577ab 100644 --- a/src/modules/statistic_creator.py +++ b/src/modules/statistic_creator.py @@ -27,7 +27,52 @@ def get_from_cache(argument_dict: dict): if md5hash in cached_statistics: return md5hash, cached_statistics[md5hash] else: - return md5hash, None + return md5hash, None + +def get_h_index(session: Session, filtering_query: BoardgameFilterParams) -> statistic_classes.NumberStatistic: + + statistic_name = 'Total h-index' + + md5hash, cached_statistic = get_from_cache({**locals()}) + + if cached_statistic != None: + return cached_statistic + + owned_collection = data_connection.get_user_owned_collection(session) + + if filtering_query != None: + owned_collection = filtering_query.do_filtering(owned_collection) + + #{Amount of plays: how many boardgames that were played this much} + amount_of_plays_counter = {} + + for owned_boardgame in owned_collection: + times_boardgame_played = len(owned_boardgame.plays) + for x in range(1, times_boardgame_played + 1): + if x in amount_of_plays_counter: + amount_of_plays_counter[x] += 1 + else: + amount_of_plays_counter[x] = 1 + + h_index = 0 + + for amount_played, games_played_that_amount in amount_of_plays_counter.items(): + if games_played_that_amount >= amount_played: + h_index = amount_played + else: + break + + statistic_dict = { + "name":statistic_name, + "result":h_index + } + + statistic_to_return = statistic_classes.NumberStatistic.model_validate(statistic_dict) + + cached_statistics[md5hash] = statistic_to_return + + return statistic_to_return + def get_total_owned_games(session: Session, filtering_query: BoardgameFilterParams) -> statistic_classes.NumberStatistic: