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 = 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) utils.saveGameObject(game) return result
def processMessageAcceptGame(dataTable, author): log.debug("Processing accept game message") if 'opponent' not in dataTable: log.warning("Couldn't find opponent in datatable") return False, "I couldn't figure out which opponent you were accepting. This shouldn't happen, please let /u/Watchful1 know" coachNum, result = utils.verifyCoaches([author, dataTable['opponent']]) if coachNum != -1: log.debug("Coaches not verified, {} : {}".format(coachNum, result)) return False, "Something went wrong, someone is no longer an acceptable coach. Please try to start the game again" homeTeam = wiki.getTeamByCoach(dataTable['opponent'].lower()) awayTeam = wiki.getTeamByCoach(author.lower()) for team in [homeTeam, awayTeam]: team['yardsPassing'] = 0 team['yardsRushing'] = 0 team['yardsTotal'] = 0 team['turnoverInterceptions'] = 0 team['turnoverFumble'] = 0 team['fieldGoalsScored'] = 0 team['fieldGoalsAttempted'] = 0 team['posTime'] = 0 game = utils.newGameObject(homeTeam, awayTeam) gameThread = utils.getGameThreadText(game) gameTitle = "[GAME THREAD] {} @ {}".format(game['away']['name'], game['home']['name']) threadID = str( reddit.submitSelfPost(globals.SUBREDDIT, gameTitle, gameThread)) game['thread'] = threadID log.debug("Game thread created: {}".format(threadID)) gameID = database.createNewGame(threadID) game['dataID'] = gameID log.debug("Game database record created: {}".format(gameID)) for user in game['home']['coaches']: database.addCoach(gameID, user, True) log.debug("Coach added to home: {}".format(user)) for user in game['away']['coaches']: database.addCoach(gameID, user, False) log.debug("Coach added to away: {}".format(user)) log.debug("Game started, posting coin toss comment") message = "The game has started! {}, you're away, call **heads** or **tails** in the air.".format( utils.getCoachString(game, 'away')) comment = utils.sendGameComment(game, message, {'action': 'coin'}) game['waitingId'] = comment.fullname log.debug("Comment posted, now waiting on: {}".format(game['waitingId'])) utils.updateGameThread(game) log.debug("Returning game started message") return True, "Game started. Find it [here]({}).".format( utils.getLinkToThread(threadID))
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 tipResults(game, homeaway,number): tipKey = '{}Tip'.format(homeaway) if not game['tip'][tipKey]: success = database.saveTipNumber(game['dataID'],number, tipKey) if success: game['tip'][tipKey] = True else: return 'could not save this number', False else: return 'Already sent a number', False log.debug('hometip is {} and awaytip is {}'.format(game['tip']['homeTip'], game['tip']['awayTip'])) if game['tip']['homeTip'] and game['tip']['awayTip']: log.debug('Setting dirty to True') game['dirty'] = True log.debug('game dirty is {} right after update'.format(game['dirty'])) else: return 'You sent {}'.format(number), True utils.updateGameThread(game) log.debug('game is dirty is {}. this is after updating game thread'.format(game['dirty'])) return 'result time', True
log.debug("Waiting on defense, touchdown") if utils.isGameOvertime(game): state.forceTouchdown(game, game.status.possession) resultMessage = state.overtimeTurnover(game) if game.status.waitingAction != Action.END: utils.sendDefensiveNumberMessage(game) else: state.forceTouchdown(game, game.status.possession) state.setStateTouchback(game, game.status.possession.negate()) game.status.waitingOn.reverse() utils.sendDefensiveNumberMessage(game) resultMessage = "Automatic 7 point touchdown, {} has the ball.".format(string_utils.flair(game.team(game.status.waitingOn))) utils.sendGameComment(game, "{}\n\n{}".format(penaltyMessage, resultMessage), None, False) utils.setGamePlayed(game) utils.updateGameThread(game) utils.clearLogGameID() for game in index.getGamesPastPlayclockWarning(): warningText = "This is a warning that your [game]({}) is waiting on a reply from you to " \ "this {}. You have 12 hours until a delay of game penalty."\ .format(string_utils.getLinkToThread(game.thread), string_utils.getLinkFromGameThing(game.thread, utils.getPrimaryWaitingId(game.status.waitingId))) results = reddit.sendMessage(recipients=game.team(game.status.waitingOn).coaches, subject="{} vs {} 12 hour warning".format(game.away.name, game.home.name), message=warningText) log.debug("12 hour warning sent to {} for game {}: {}" .format( string_utils.getCoachString(game, game.status.waitingOn), game.thread,
def processMessage(message, force=False): if isinstance(message, praw.models.Message): isMessage = True log.debug("Processing a message from /u/{} : {}".format(str(message.author), message.id)) else: isMessage = False log.debug("Processing a comment from /u/{} : {}".format(str(message.author), message.id)) response = None success = None updateWaiting = True dataTable = None if message.parent_id is not None and (message.parent_id.startswith("t1") or message.parent_id.startswith("t4")): if isMessage: parent = reddit.getMessage(message.parent_id[3:]) else: parent = reddit.getComment(message.parent_id[3:]) if parent is not None and str(parent.author).lower() == globals.ACCOUNT_NAME: dataTable = string_utils.extractTableFromMessage(parent.body) if dataTable is not None: if 'action' not in dataTable or 'thread' not in dataTable: dataTable = None else: dataTable['source'] = parent.fullname log.debug("Found a valid datatable in parent message: {}".format(str(dataTable))) body = message.body.lower() author = str(message.author) game = None appendMessageId = False if dataTable is not None: game = index.reloadAndReturn(dataTable['thread']) if game is not None: utils.cycleStatus(game, message.fullname) utils.setLogGameID(game.thread, game) waitingOn = utils.isGameWaitingOn(game, author, dataTable['action'], dataTable['source'], force) if waitingOn is not None: response = waitingOn success = False updateWaiting = False elif game.errored: log.debug("Game is errored, skipping") response = "This game is currently in an error state, /u/{} has been contacted to take a look".format( globals.OWNER) success = False updateWaiting = False else: if dataTable['action'] == Action.COIN and not isMessage: keywords = ["heads", "tails"] keyword = utils.findKeywordInMessage(keywords, body) if keyword == "heads": success, response = processMessageCoin(game, True, author) elif keyword == "tails": success, response = processMessageCoin(game, False, author) elif keyword == "mult": success = False response = "I found both {} in your message. Please reply with just one of them.".format( ' and '.join(keywords)) elif dataTable['action'] == Action.DEFER and not isMessage: if utils.isGameOvertime(game): keywords = ["defend", "attack"] else: keywords = ["defer", "receive"] keyword = utils.findKeywordInMessage(keywords, body) if keyword == "defer" or keyword == "defend": success, response = processMessageDefer(game, True, author) elif keyword == "receive" or keyword == "attack": success, response = processMessageDefer(game, False, author) elif keyword == "mult": success = False response = "I found both {} in your message. Please reply with just one of them.".format( ' and '.join(keywords)) elif dataTable['action'] in classes.playActions and isMessage: success, response = processMessageDefenseNumber(game, body, author) appendMessageId = not success elif dataTable['action'] in classes.playActions and not isMessage: success, response = processMessageOffensePlay(game, body, author) else: log.debug("Couldn't get a game for /u/{}".format(author)) else: log.debug("Parsing non-datatable message") if isMessage and str(message.author).lower() in wiki.admins: if body.startswith("newgame"): response = processMessageNewGame(message.body, str(message.author)) elif body.startswith("kick"): response = processMessageKickGame(message.body) elif body.startswith("pause"): response = processMessagePauseGame(message.body) elif body.startswith("abandon"): response = processMessageAbandonGame(message.body) elif body.startswith("status"): response = processMessageGameStatus(message.body) elif body.startswith("reindex"): response = processMessageReindex(message.body) elif body.startswith("chew"): response = processMessageDefaultChew(message.body) elif body.startswith("gamelist"): response = processMessageGameList(message.body) message.mark_read() if response is not None: if success is not None and not success and dataTable is not None and string_utils.extractTableFromMessage( response) is None: log.debug("Embedding datatable in reply on failure") response = string_utils.embedTableInMessage(response, dataTable) if updateWaiting and game is not None: if appendMessageId: utils.addWaitingId(game, 'return') else: utils.setWaitingId(game, 'return') resultMessage = reddit.replyMessage(message, response) if resultMessage is None: log.warning("Could not send message") elif game is not None and 'return' in game.status.waitingId: if appendMessageId: utils.clearReturnWaitingId(game) utils.addWaitingId(game, resultMessage.fullname) else: utils.setWaitingId(game, resultMessage.fullname) game.dirty = True log.debug("Message/comment replied, now waiting on: {}".format(game.status.waitingId)) else: if isMessage: log.debug("Couldn't understand message") resultMessage = reddit.replyMessage(message, "I couldn't understand your message, please try again or message /u/Watchful1 if you need help.") if resultMessage is None: log.warning("Could not send message") if game is not None and game.dirty: log.debug("Game is dirty, updating thread") utils.updateGameThread(game)
def processMessage(message): if isinstance(message, praw.models.Message): isMessage = True log.debug("Processing a message from /u/{} : {}".format( str(message.author), message.id)) else: isMessage = False log.debug("Processing a comment from /u/{} : {}".format( str(message.author), message.id)) response = None success = None updateWaiting = True dataTable = None if message.parent_id is not None and (message.parent_id.startswith("t1") or message.parent_id.startswith("t4")): if isMessage: parent = reddit.getMessage(message.parent_id[3:]) else: parent = reddit.getComment(message.parent_id[3:]) if parent is not None and str( parent.author).lower() == globals.ACCOUNT_NAME: dataTable = utils.extractTableFromMessage(parent.body) if dataTable is not None: if 'action' not in dataTable: dataTable = None else: dataTable['source'] = parent.fullname log.debug( "Found a valid datatable in parent message: {}".format( str(dataTable))) body = message.body.lower() author = str(message.author) game = None if dataTable is not None: if dataTable['action'] == 'newgame' and isMessage: keywords = ['accept', 'reject'] keyword = utils.findKeywordInMessage(keywords, body) if keyword == "accept": success, response = processMessageAcceptGame( dataTable, str(message.author)) elif keyword == "reject": success, response = processMessageRejectGame( dataTable, str(message.author)) elif keyword == 'mult': success = False response = "I found both {} in your message. Please reply with just one of them.".format( ' and '.join(keywords)) else: game = utils.getGameByUser(author) if game is not None: utils.setLogGameID(game['thread'], game['dataID']) waitingOn = utils.isGameWaitingOn(game, author, dataTable['action'], dataTable['source']) if waitingOn is not None: response = waitingOn success = False updateWaiting = False elif game['errored']: log.debug("Game is errored, skipping") response = "This game is currently in an error state, /u/{} has been contacted to take a look".format( globals.OWNER) success = False updateWaiting = False else: if dataTable['action'] == 'coin' and not isMessage: keywords = ['heads', 'tails'] keyword = utils.findKeywordInMessage(keywords, body) if keyword == "heads": success, response = processMessageCoin( game, True, str(message.author)) elif keyword == "tails": success, response = processMessageCoin( game, False, str(message.author)) elif keyword == 'mult': success = False response = "I found both {} in your message. Please reply with just one of them.".format( ' and '.join(keywords)) elif dataTable['action'] == 'defer' and not isMessage: keywords = ['defer', 'receive'] keyword = utils.findKeywordInMessage(keywords, body) if keyword == "defer": success, response = processMessageDefer( game, True, str(message.author)) elif keyword == "receive": success, response = processMessageDefer( game, False, str(message.author)) elif keyword == 'mult': success = False response = "I found both {} in your message. Please reply with just one of them.".format( ' and '.join(keywords)) elif dataTable['action'] == 'play' and isMessage: success, response = processMessageDefenseNumber( game, body, str(message.author)) elif dataTable['action'] == 'play' and not isMessage: success, response = processMessageOffensePlay( game, body, str(message.author)) else: log.debug("Couldn't get a game for /u/{}".format(author)) else: log.debug("Parsing non-datatable message") if "newgame" in body and isMessage: response = processMessageNewGame(body, str(message.author)) if "kick" in body and isMessage and str( message.author).lower() == globals.OWNER: response = processMessageKickGame(body) message.mark_read() if response is not None: if success is not None and not success and dataTable is not None and utils.extractTableFromMessage( response) is None: log.debug("Embedding datatable in reply on failure") response = utils.embedTableInMessage(response, dataTable) if updateWaiting and game is not None: game['waitingId'] = 'return' resultMessage = reddit.replyMessage(message, response) if game is not None and game['waitingId'] == 'return': game['waitingId'] = resultMessage.fullname game['dirty'] = True log.debug("Message/comment replied, now waiting on: {}".format( game['waitingId'])) else: if isMessage: log.debug("Couldn't understand message") reddit.replyMessage( message, "I couldn't understand your message, please try again or message /u/Watchful1 if you need help." ) if game is not None and game['dirty']: log.debug("Game is dirty, updating thread") utils.updateGameThread(game)
def processMessage(message): ## Determine if comment or dm if isinstance(message, praw.models.Message): isMessage = True else: isMessage = False log.debug("Processing a comment from /u/{} with id {} and body {}".format(str(message.author), message.id,message.body)) response = None success = None updateWaiting = True dataTable = None resultMessage = None tipped = False if message.parent_id is not None and (message.parent_id.startswith("t1") or message.parent_id.startswith("t4")): if isMessage: parent = reddit.getMessage(message.parent_id[3:]) else: parent = reddit.getComment(message.parent_id[3:]) if parent is not None and str(parent.author).lower() == globals.ACCOUNT_NAME: dataTable = utils.extractTableFromMessage(parent.body) if dataTable is not None: if 'action' not in dataTable: dataTable = None else: dataTable['source'] = parent.fullname log.debug("Found a valid datatable in parent message: {}".format(str(dataTable))) body = message.body.lower() author = str(message.author) game = None if dataTable is not None: game = utils.getGameByUser(author) log.debug('game is {}'.format(game)) if game is not None: utils.setLogGameID(game['thread'], game['dataID']) print('the action is {}'.format(dataTable['action'])) waitingOn = utils.isGameWaitingOn(game, author, dataTable['action'], dataTable['source']) log.debug("waitingOn is {}".format(waitingOn)) if waitingOn is not None: response = waitingOn success = False updateWaiting = False elif game['errored']: log.debug("Game is errored, skipping") response = "This game is currently in an error state, /u/{} has been contacted to take a look".format(globals.OWNER) success = False updateWaiting = False else: log.debug('Trying to process tip, offense, or defense') ##this is where we start the game basically if dataTable['action'] == 'tip' and isMessage: log.debug('About to process tip message from {}'.format(str(message.author))) success, response = processMessageTip(game, message) if success: game['dirty'] = True log.debug("The tip message's success was {} and the message's content reads {}".format(success, response)) elif dataTable['action'] == 'play' and isMessage: success, response = processMessageDefenseNumber(game, body, str(message.author)) elif dataTable['action'] == 'play' and not isMessage: pos = game['status']['possession'] success, response = processMessageOffensePlay(game, body, str(message.author)) utils.sendPlayResultMessage(utils.reverseHomeAway(pos),game, response) else: log.debug("Couldn't get a game for /u/{}".format(author)) else: log.debug("Parsing non-datatable message with body {} from author {}".format(body, str(message.author).lower())) if "newgame" in body and isMessage: response = processMessageNewGame(message.body, str(message.author)) if "kick" in body and isMessage and str(message.author).lower() == globals.OWNER: response = processMessageKickGame(message.body) if "pause" in body and isMessage and str(message.author).lower() in wiki.admins: response = processMessagePauseGame(message.body) if "abandon" in body and isMessage and str(message.author).lower() in wiki.admins: log.debug('Going to abandon a game') response = processMessageAbandonGame(message.body) if "refresh" in body and isMessage and str(message.author).lower() in wiki.admins: response = utils.processRefresh() message.mark_read() if response is not None: if success is not None and not success and dataTable is not None and utils.extractTableFromMessage(response) is None: log.debug("Embedding datatable in reply on failure") response = utils.embedTableInMessage(response, dataTable) if updateWaiting and game is not None: game['waitingId'] = 'return' log.debug("About to send reply Message") if game is not None: log.debug("game is not none") if game['tip']['justTipped']: log.debug('sending the winning tip message to the game thread') ##send the tip update to the game thread instead of to the last ## person who sent a tip number resultMessage = utils.sendGameCommentAfterTip(game, response) game['tip']['justTipped'] = False game['tip']['tipped'] = True else: resultMessage = reddit.replyMessage(message, response) else: resultMessage = reddit.replyMessage(message, response) log.debug("result of sending reply message was {}".format(resultMessage)) if resultMessage is None: log.warning("Could not send message") elif game is not None and game['waitingId'] == 'return': game['waitingId'] = resultMessage.fullname log.debug('About to send. WaitingID is {} when waitingID was return'.format(game['waitingId'])) game['dirty'] = True log.debug("Message/comment replied, now waiting on: {}".format(game['waitingId'])) else: if isMessage: log.debug("Couldn't understand message") resultMessage = reddit.replyMessage(message, "Could not understand you. Please try again or message /u/zenverak if you need help.") if resultMessage is None: log.warning("Could not send message") if game is not None: if game['status']['sendDef']: utils.sendDefensiveNumberMessage(game) game['status']['sendDef'] = False if game is not None and game['dirty']: log.debug("Game is dirty, updating thread") utils.updateGameThread(game)