From 9f5aa4a92712b2ca3af2639ec5ce1f716e18fbe9 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Mon, 14 Oct 2024 18:33:24 +0200 Subject: [PATCH] Added playercount votes to boardgames --- src/classes/boardgame_classes.py | 17 +++++++++++++++++ src/modules/bgg_connection.py | 24 +++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/classes/boardgame_classes.py b/src/classes/boardgame_classes.py index 348679d..65383bb 100644 --- a/src/classes/boardgame_classes.py +++ b/src/classes/boardgame_classes.py @@ -15,6 +15,19 @@ class BoardgameType(Enum): PREORDEREDBOARDGAME = 'preorderedboargame' PREORDEREDBOARDGAMEEXPANSION = 'preorderedboargameexpansion' +class PlayerCountVotesBase(SQLModel): + playercount: str + best: int + recommended: int + not_recommended: int + +class PlayerCountVotes(PlayerCountVotesBase, table=True): + id: int = Field(primary_key=True) + boardgame_id: int = Field(default=None, foreign_key="boardgame.id") + boardgame: "BoardGame" = Relationship(back_populates='playercount_votes') + +class PlayerCountVotesPublic(PlayerCountVotesBase): + pass class BoardGameBase(SQLModel): name: str @@ -49,6 +62,8 @@ class BoardGame(BoardGameBase, table=True): plays: list["Play"] = Relationship(back_populates='boardgame') + playercount_votes: list[PlayerCountVotes] = Relationship(back_populates="boardgame") + model_config = { 'arbitrary_types_allowed':True } @@ -61,6 +76,7 @@ class BoardGamePublic(BoardGameBase): owned_info: Optional["OwnedInfoPublicNoGame"] wishlist_info: Optional["WishlistInfoPublicNoGame"] preordered_info: Optional["PreorderedInfoPublicNoGame"] + playercount_votes: list[PlayerCountVotesPublic] plays: list["PlayPublicNoGame"] class BoardGamePublicNoPlays(BoardGameBase): @@ -71,6 +87,7 @@ class BoardGamePublicNoPlays(BoardGameBase): owned_info: Optional["OwnedInfoPublicNoGame"] wishlist_info: Optional["WishlistInfoPublicNoGame"] preordered_info: Optional["PreorderedInfoPublicNoGame"] + playercount_votes: list[PlayerCountVotesPublic] class ExpansionInfo(SQLModel, table=True): diff --git a/src/modules/bgg_connection.py b/src/modules/bgg_connection.py index 4420cda..b556565 100644 --- a/src/modules/bgg_connection.py +++ b/src/modules/bgg_connection.py @@ -108,6 +108,27 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.Boa else: thumbnail_url = "https://cf.geekdo-images.com/zxVVmggfpHJpmnJY9j-k1w__thumb/img/Tse35rOD2Z8Pv9EOUj4TfeMuNew=/fit-in/200x150/filters:strip_icc()/pic1657689.jpg" + playercount_poll = boardgame_xml.find('poll') + + playercount_votes_list = [] + + for playercount_vote_results in playercount_poll.findall('results'): + playercount = playercount_vote_results.get('numplayers') + best_votes = 0 + recommended_votes = 0 + not_recommended_votes = 0 + + for playercount_vote_result in playercount_vote_results: + if playercount_vote_result.get('value') == "Best": + best_votes = playercount_vote_result.get('numvotes') + elif playercount_vote_result.get('value') == "Recommended": + recommended_votes = playercount_vote_result.get('numvotes') + elif playercount_vote_result.get('value') == "Not Recommended": + not_recommended_votes = playercount_vote_result.get('numvotes') + + playercount_votes = boardgame_classes.PlayerCountVotes(boardgame_id=int(boardgame_xml.get('id')), playercount=playercount, best=best_votes, recommended=recommended_votes, not_recommended=not_recommended_votes) + playercount_votes_list.append(playercount_votes) + boardgame_dict = { "id" : int(boardgame_xml.get('id')), @@ -123,7 +144,8 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.Boa "max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')), "min_age" : int(boardgame_xml.find('minage').get('value')), "designers" : designers, - "artists" : artists + "artists" : artists, + "playercount_votes" : playercount_votes_list } boardgame = boardgame_classes.BoardGame(**boardgame_dict)