[docs]defgame_list_to_pandas(games:List[dict])->Optional[pd.DataFrame]:ifnotgames:# no gamesreturnNonedataframe=pd.DataFrame(games)# convert to dataframelisting=pd.DataFrame()# holder for new dataframe# Building the listingdataframe["win_status"]=dataframe["win_status"].replace({True:"W",False:"L",None:" "}# win, lost, continue)# change the win status to human readable formatlisting["Mode"]=dataframe["game_mode"]listing["Dimension"]=(dataframe["number_of_colors"].astype(str)+"x"+dataframe["number_of_dots"].astype(str))# express the colors and dots together as a dimensionlisting["Attempts"]=(dataframe["win_status"].astype(str)+" "+dataframe["amount_attempted"].astype(str)+"/"+dataframe["amount_allowed"].astype(str))returnlisting
[docs]classGameHistoryManager:"""Store and retrieve game history."""
[docs]@staticmethoddefgenerate_meta_data(game:Game)->dict:"""Generate meta data for the game."""return{"game_mode":game._state.GAME_MODE,"number_of_dots":game._state.number_of_dots,"number_of_colors":game._state.number_of_colors,"amount_attempted":len(game),"amount_allowed":game._state.MAXIMUM_ATTEMPTS,"win_status":game._state.win_status,"guesses":game._board._guesses,"feedback":game._board._feedbacks,**({"game":game}ifgame._state.win_statusisNoneelse{}),}
[docs]@staticmethoddefsave_game(game:Game)->None:"""Save the game to a file."""if"saved_games"notinuserdata:# if the list is emptyuserdata.saved_games=[]# initialize the listuserdata.saved_games.append(GameHistoryManager.generate_meta_data(game))# store the meta data