GameStatistic now returns games and results seperately, results might not be a board game attribute

This commit is contained in:
Yarne Coppens 2024-08-25 17:19:25 +02:00
parent 93c5e65af5
commit a321a184a4
4 changed files with 66 additions and 8 deletions

View file

@ -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

View file

@ -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)):

View file

@ -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]:

View file

@ -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)