def load_settings(self): mtga_logger.debug("loading settings") os.makedirs(self._settings_path, exist_ok=True) if not os.path.exists(self._settings_json_path): with open(self._settings_json_path, 'w') as wp: json.dump({"player_decks": {}, "player_id": None}, wp) try: with open(self._settings_json_path) as rp: settings = json.load(rp) except: mtga_logger.error( "had to move settings.json -> settings.json.bak, trying again..." ) mtga_watch_app.send_error( "Error loading settings; had to move settings.json -> settings.json.bak" ) os.rename(self._settings_json_path, self._settings_json_path + ".bak") return self.load_settings() if "player_id" in settings and settings["player_id"]: self.player_id = settings["player_id"] if "player_decks" in settings and settings["player_decks"]: for deck_id in settings["player_decks"]: self.player_decks[deck_id] = Deck.from_dict( settings['player_decks'][deck_id]) mtga_logger.debug("queue put from settings {}".format( id(decklist_change_queue))) new_dl = { k: v.to_serializable(transform_to_counted=True) for k, v in self.player_decks.items() } if not new_dl: new_dl = {"no_decks_defined": True} decklist_change_queue.put(new_dl)
def save_settings(self): mtga_logger.info("{}saving settings".format(util.ld())) with open(self._settings_json_path, 'w') as wp: write_obj = { "player_decks": {d.deck_id: d.to_serializable() for d in self.player_decks.values()}, "player_id": self.player_id, "collection": self.collection, } json.dump(write_obj, wp) mtga_logger.debug("{}queue put from settings {}".format(util.ld(), id(decklist_change_queue))) new_dl = {k: v.to_serializable(transform_to_counted=True) for k, v in self.player_decks.items()} if not new_dl: new_dl = {"no_decks_defined": True} decklist_change_queue.put(new_dl)
def json_blob_reader_task(in_queue, out_queue): def check_for_client_id(blob): if "authenticateResponse" in blob: if "clientId" in blob["authenticateResponse"]: # screw it, no one else is going to use this message, mess up the timestamp, who cares with mtga_watch_app.game_lock: if mtga_watch_app.player_id != blob[ "authenticateResponse"]['clientId']: mtga_watch_app.player_id = blob[ "authenticateResponse"]['clientId'] mtga_logger.debug( "{}check_for_client_id: got new clientId".format( util.ld())) mtga_watch_app.save_settings() general_output_queue.put( {"authenticateResponse": blob["authenticateResponse"]}) last_blob = None last_decklist = None error_count = 0 while all_die_queue.empty(): json_recieved = in_queue.get() if json_recieved is None: out_queue.put(None) break if last_blob == json_recieved: continue # don't double fire # check for decklist changes if mtga_watch_app.player_decks != last_decklist: last_decklist = mtga_watch_app.player_decks decklist_change_queue.put({ k: v.to_serializable(transform_to_counted=True) for k, v in last_decklist.items() }) # check for gamestate changes try: hero_library_hash = -1 opponent_hand_hash = -1 if mtga_watch_app.game: hero_library_hash = hash(mtga_watch_app.game.hero.library) opponent_hand_hash = hash(mtga_watch_app.game.opponent.hand) check_for_client_id(json_recieved) dispatchers.dispatch_blob(json_recieved) mtga_watch_app.last_blob = json_recieved error_count = 0 hero_library_hash_post = -1 opponent_hand_hash_post = -1 if mtga_watch_app.game: hero_library_hash_post = hash(mtga_watch_app.game.hero.library) opponent_hand_hash_post = hash( mtga_watch_app.game.opponent.hand) if hero_library_hash != hero_library_hash_post or opponent_hand_hash != opponent_hand_hash_post: game_state_change_queue.put(mtga_watch_app.game.game_state( )) # TODO: BREAKPOINT HERE if mtga_watch_app.game.final: game_state_change_queue.put({ "match_complete": True, "gameID": mtga_watch_app.game.match_id }) except: import traceback exc = traceback.format_exc() stack = traceback.format_stack() mtga_logger.error("{}Exception @ count {}".format( util.ld(True), mtga_watch_app.error_count)) mtga_logger.error(exc) mtga_logger.error(stack) mtga_watch_app.send_error( "Exception during check game state. Check log for more details" ) if error_count > 5: mtga_logger.error("{}error count too high; exiting".format( util.ld())) return last_blob = json_recieved