def processMessageKickGame(body): log.debug("Processing kick game message") threadIds = re.findall('([\da-z]{6})', body) if len(threadIds) < 1: log.debug("Couldn't find a thread id in message") return "Couldn't find a thread id in message" log.debug("Found thread id: {}".format(threadIds[0])) game = file_utils.loadGameObject(threadIds[0]) if game is None: return "Game not found: {}".format(threadIds[0]) game = index.reloadAndReturn(threadIds[0]) index.clearGameErrored(game) file_utils.saveGameObject(game) result = ["Kicked game: {}".format(threadIds[0])] statusIndex = re.findall('(?:revert:)(\d+)', body) if len(statusIndex) > 0: log.debug("Reverting to status: {}".format(statusIndex[0])) utils.revertStatus(game, int(statusIndex[0])) file_utils.saveGameObject(game) result.append("Reverted to status: {}".format(statusIndex[0])) messageFullname = re.findall('(?:message:)(t\d_[\da-z]{6,})', body) if len(messageFullname) > 0: log.debug("Reprocessing message/comment: {}".format(messageFullname[0])) message = reddit.getThingFromFullname(messageFullname[0]) if message is None: return "Something went wrong. Not valid fullname: {}".format(messageFullname[0]) processMessage(message, True) result.append("Reprocessed message: {}".format(messageFullname[0])) log.debug("Finished kicking game") return '\n\n'.join(result)
def updateGameThread(game): if game.thread is None: log.error("No thread ID in game when trying to update") game.dirty = False file_utils.saveGameObject(game) threadText = string_utils.renderGame(game) reddit.editThread(game.thread, threadText)
def addWinnerFieldToGames(): folder = globals.SAVE_FOLDER_NAME for fileName in os.listdir(folder): if not os.path.isfile(os.path.join(folder, fileName)): continue game = file_utils.loadGameObject(fileName) game.status.winner = None for status in game.previousStatus: status.winner = None file_utils.saveGameObject(game)
def processMessageAbandonGame(body): log.debug("Processing abandon game message") threadIds = re.findall('(?: )([\da-z]{6})', body) if len(threadIds) < 1: log.debug("Couldn't find a thread id in message") return "Couldn't find a thread id in message" log.debug("Found thread id: {}".format(threadIds[0])) game = file_utils.loadGameObject(threadIds[0]) if game is None: return "Game not found: {}".format(threadIds[0]) utils.endGame(game, "Abandoned", False) utils.updateGameThread(game) file_utils.saveGameObject(game) return "Game {} abandoned".format(threadIds[0])
def processMessagePauseGame(body): log.debug("Processing pause game message") threadIds = re.findall('([\da-z]{6})', body) if len(threadIds) < 1: log.debug("Couldn't find a thread id in message") return "Couldn't find a thread id in message" log.debug("Found thread id: {}".format(threadIds[0])) hours = re.findall('(\b\d{1,3}\b)', body) if len(hours) < 1: log.debug("Couldn't find a number of hours in message") return "Couldn't find a number of hours in message" log.debug("Found hours: {}".format(hours[0])) game = index.reloadAndReturn(threadIds[0]) utils.pauseGame(game, int(hours[0])) file_utils.saveGameObject(game) return "Game {} paused for {} hours".format(threadIds[0], hours[0])
def processMessageDefaultChew(body): log.debug("Processing default chew message") threadIds = re.findall('(?: )([\da-z]{6})', body) if len(threadIds) < 1: log.debug("Couldn't find a thread id in message") return "Couldn't find a thread id in message" log.debug("Found thread id: {}".format(threadIds[0])) game = file_utils.loadGameObject(threadIds[0]) if game is None: return "Game not found: {}".format(threadIds[0]) if "normal" in body: game.forceChew = False result = "Game changed to normal plays by default: {}".format(threadIds[0]) else: game.forceChew = True result = "Game changed to chew the clock plays by default: {}".format(threadIds[0]) utils.updateGameThread(game) file_utils.saveGameObject(game) return result
def init(): global games games = {} for gameFile in os.listdir(globals.SAVE_FOLDER_NAME): game = reloadAndReturn(gameFile) if game is not None: changed = False for team in [game.home, game.away]: wikiTeam = wiki.getTeamByTag(team.tag) if ((team.coaches > wikiTeam.coaches) - (team.coaches < wikiTeam.coaches)) != 0: log.debug( "Coaches for game {}, team {} changed from {} to {}". format(game.thread, team.tag, team.coaches, wikiTeam.coaches)) changed = True team.coaches = wikiTeam.coaches if changed: try: log.debug("Reverting status and reprocessing {}".format( game.previousStatus[0].messageId)) utils.revertStatus(game, 0) file_utils.saveGameObject(game) message = reddit.getThingFromFullname( game.status.messageId) if message is None: return "Something went wrong. Not valid fullname: {}".format( game.status.messageId) messages.processMessage(message, True) game = reloadAndReturn(game.thread) except Exception as err: log.warning(traceback.format_exc()) log.warning("Unable to revert game when changing coaches") games[game.thread] = game
while True: try: for message in reddit.getMessageStream(): startTime = time.perf_counter() log.debug("Processing message") wiki.loadPages() try: messages.processMessage(message) except Exception as err: log.warning("Error in main loop") log.warning(traceback.format_exc()) if globals.game is not None: log.debug("Setting game {} as errored".format(globals.game.thread)) index.setGameErrored(globals.game) file_utils.saveGameObject(globals.game) ownerMessage = string_utils.renderGameStatusMessage(globals.game) message.reply("This game has errored. Please wait for the bot owner to help.") else: ownerMessage = "Unable to process message from /u/{}, skipping".format(str(message.author)) try: reddit.sendMessage(globals.OWNER, "NCFAA game errored", ownerMessage) message.mark_read() except Exception as err2: log.warning("Error sending error message") log.warning(traceback.format_exc()) log.debug("Message processed after: %d", int(time.perf_counter() - startTime)) utils.clearLogGameID()