2024-08-08 18:41:18 +02:00
|
|
|
from typing import Union
|
2024-08-15 11:07:11 +02:00
|
|
|
from sqlmodel import Session
|
2024-08-15 10:29:54 +02:00
|
|
|
|
|
|
|
|
from threading import Lock
|
|
|
|
|
critical_function_lock = Lock()
|
2024-08-08 18:41:18 +02:00
|
|
|
|
2024-08-03 15:42:19 +02:00
|
|
|
from src.modules import bgg_connection, db_connection
|
2024-08-24 18:13:32 +02:00
|
|
|
from src.classes import boardgame_classes, play_classes, people_classes, player_classes
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
def get_db_engine():
|
|
|
|
|
return db_connection.get_engine()
|
|
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
def get_boardgame(session: Session, boardgame_id: int) -> boardgame_classes.BoardGame:
|
2024-08-03 10:44:52 +02:00
|
|
|
#Will check if it already exists in db, then it will get it from there
|
2024-08-08 17:56:22 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
boardgame_in_db = db_connection.get_boardgame(session, boardgame_id=boardgame_id)
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-08 17:56:22 +02:00
|
|
|
to_return_boardgame = None
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-12 12:08:08 +02:00
|
|
|
if boardgame_in_db != None:
|
|
|
|
|
to_return_boardgame = boardgame_in_db
|
2024-08-08 18:13:18 +02:00
|
|
|
else:
|
2024-08-08 17:56:22 +02:00
|
|
|
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
2024-08-15 11:07:11 +02:00
|
|
|
db_connection.add_boardgame(session, to_return_boardgame)
|
|
|
|
|
to_return_boardgame = db_connection.get_boardgame(session, boardgame_id)
|
2024-08-19 13:50:27 +02:00
|
|
|
|
2024-08-08 17:56:22 +02:00
|
|
|
|
|
|
|
|
return to_return_boardgame
|
2024-08-03 10:44:52 +02:00
|
|
|
|
|
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[boardgame_classes.BoardGame]:
|
2024-08-15 11:07:11 +02:00
|
|
|
boardgames_in_db, boardgame_ids_missing = db_connection.get_multiple_boardgames(session, boardgame_ids=boardgame_ids)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-14 10:02:25 +02:00
|
|
|
if len(boardgame_ids_missing) != 0:
|
|
|
|
|
missing_boardgames = bgg_connection.get_multiple_boardgames(boardgame_ids_missing)
|
2024-08-22 13:56:22 +02:00
|
|
|
db_connection.upsert_multiple_boardgames(session, missing_boardgames)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
boardgames_in_db, boardgame_ids_missing = db_connection.get_multiple_boardgames(session, boardgame_ids=boardgame_ids)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-14 10:02:25 +02:00
|
|
|
return boardgames_in_db
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
def get_user_collection(session: Session) -> list[boardgame_classes.BoardGame]:
|
2024-08-22 09:35:53 +02:00
|
|
|
boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session)
|
2024-08-15 10:29:54 +02:00
|
|
|
|
2024-08-22 09:35:53 +02:00
|
|
|
if len(boardgames_from_db) == 0:
|
2024-08-15 10:29:54 +02:00
|
|
|
boardgames = bgg_connection.get_user_collection()
|
2024-08-19 13:50:27 +02:00
|
|
|
|
2024-08-22 13:56:22 +02:00
|
|
|
db_connection.upsert_multiple_boardgames(session, boardgames)
|
2024-08-15 10:29:54 +02:00
|
|
|
|
2024-08-22 09:35:53 +02:00
|
|
|
boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session)
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-22 09:35:53 +02:00
|
|
|
return boardgames_from_db
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
def get_user_owned_collection(session: Session) -> list[boardgame_classes.BoardGame]:
|
2024-08-12 12:08:08 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
owned_boardgames_from_db = db_connection.get_owned_boardgames(session)
|
2024-08-11 19:58:22 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
if len(owned_boardgames_from_db) == 0:
|
2024-08-08 18:13:18 +02:00
|
|
|
owned_boardgames = bgg_connection.get_user_owned_collection()
|
2024-08-22 13:56:22 +02:00
|
|
|
db_connection.upsert_multiple_boardgames(session, owned_boardgames)
|
2024-08-11 19:58:22 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
owned_boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_owned_boardgames(session)
|
2024-08-11 19:58:22 +02:00
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
return owned_boardgames_from_db
|
2024-08-03 10:44:52 +02:00
|
|
|
|
|
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) -> list[boardgame_classes.BoardGame]:
|
2024-08-11 15:58:07 +02:00
|
|
|
|
2024-08-22 09:35:53 +02:00
|
|
|
wishlisted_boardgames_from_db = db_connection.get_wishlist_boardgames(session)
|
2024-08-21 21:03:49 +02:00
|
|
|
|
|
|
|
|
if len(wishlisted_boardgames_from_db) == 0:
|
2024-08-11 15:58:07 +02:00
|
|
|
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
2024-08-22 13:56:22 +02:00
|
|
|
db_connection.upsert_multiple_boardgames(session, wishlisted_boardgames)
|
2024-08-11 15:58:07 +02:00
|
|
|
|
2024-08-22 09:35:53 +02:00
|
|
|
wishlisted_boardgames_from_db = db_connection.get_wishlist_boardgames(session)
|
2024-08-11 15:58:07 +02:00
|
|
|
|
2024-08-21 21:03:49 +02:00
|
|
|
to_return_boardgames = wishlisted_boardgames_from_db
|
2024-08-11 15:58:07 +02:00
|
|
|
|
|
|
|
|
if wishlist_priority != 0:
|
2024-08-22 09:35:53 +02:00
|
|
|
to_return_boardgames = list(filter(lambda game: game.wishlist_info.wishlist_priority == wishlist_priority, to_return_boardgames))
|
2024-08-11 15:58:07 +02:00
|
|
|
|
|
|
|
|
return to_return_boardgames
|
2024-08-08 18:13:18 +02:00
|
|
|
|
2024-08-03 10:44:52 +02:00
|
|
|
|
2024-08-15 13:47:50 +02:00
|
|
|
def get_plays(session: Session) -> list[play_classes.Play]:
|
2024-08-11 21:27:59 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
plays_from_db = db_connection.get_plays(session)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
if len(plays_from_db) == 0:
|
|
|
|
|
all_plays = bgg_connection.get_plays()
|
|
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
db_connection.add_multiple_plays(session, all_plays)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
plays_from_db = db_connection.get_plays(session)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
2024-08-12 20:11:34 +02:00
|
|
|
#Making sure all played board games are in table 'boardgames'
|
|
|
|
|
#list + set to remove duplicates
|
|
|
|
|
played_boardgame_ids = list(set([play.boardgame_id for play in plays_from_db]))
|
2024-08-14 09:12:20 +02:00
|
|
|
|
|
|
|
|
#Remove None's (played board games don't have expansion id and vice versa)
|
|
|
|
|
played_boardgame_ids = list(filter(lambda x: x != None, played_boardgame_ids))
|
|
|
|
|
|
|
|
|
|
assert len(list(filter(lambda x: x == None, played_boardgame_ids))) == 0, plays_from_db
|
2024-08-12 20:11:34 +02:00
|
|
|
|
2024-08-22 09:55:31 +02:00
|
|
|
#Make sure to add all board games that are played
|
|
|
|
|
get_multiple_boardgames(session, played_boardgame_ids)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
return plays_from_db
|
|
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
def get_players_from_play(session: Session, play_id: int) -> list[play_classes.PlayPlayer]:
|
|
|
|
|
players_from_db = db_connection.get_players_from_play(session, play_id)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
if len(players_from_db) == 0:
|
|
|
|
|
all_plays = bgg_connection.get_plays()
|
|
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
db_connection.add_multiple_plays(session, all_plays)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
2024-08-15 11:07:11 +02:00
|
|
|
players_from_db = db_connection.get_players_from_play(session, play_id)
|
2024-08-11 21:27:59 +02:00
|
|
|
|
|
|
|
|
return players_from_db
|
2024-08-03 15:42:19 +02:00
|
|
|
|
2024-08-24 18:13:32 +02:00
|
|
|
def get_all_players(session: Session) -> list[player_classes.Player]:
|
|
|
|
|
players_from_db = db_connection.get_all_players(session)
|
|
|
|
|
|
|
|
|
|
if len(players_from_db) == 0:
|
|
|
|
|
all_plays = bgg_connection.get_plays()
|
|
|
|
|
|
|
|
|
|
db_connection.add_multiple_plays(session, all_plays)
|
|
|
|
|
|
|
|
|
|
players_from_db = db_connection.get_all_players(session)
|
|
|
|
|
|
|
|
|
|
return players_from_db
|
|
|
|
|
|
|
|
|
|
def get_player(player_name: str, session: Session) -> player_classes.Player:
|
2024-08-24 18:25:52 +02:00
|
|
|
player_from_db = db_connection.get_player(player_name.title(), session)
|
2024-08-24 18:13:32 +02:00
|
|
|
return player_from_db
|
|
|
|
|
|
2024-08-22 09:18:18 +02:00
|
|
|
def get_designers(session: Session) -> list[people_classes.Designer]:
|
|
|
|
|
designers_from_db = db_connection.get_all_designers(session)
|
|
|
|
|
return designers_from_db
|
|
|
|
|
|
2024-08-08 16:50:52 +02:00
|
|
|
def delete_database():
|
|
|
|
|
db_connection.delete_database()
|
2024-08-03 15:42:19 +02:00
|
|
|
|
|
|
|
|
def create_db_and_tables():
|
|
|
|
|
db_connection.create_db_and_tables()
|