def processMessageDefer(game, isDefer, author): log.debug("Processing defer message: {}".format(str(isDefer))) authorHomeAway = utils.getHomeAwayString(utils.isCoachHome(game, author)) if isDefer: log.debug("User deferred, {} is receiving".format( utils.reverseHomeAway(authorHomeAway))) state.setStateTouchback(game, utils.reverseHomeAway(authorHomeAway)) game['receivingNext'] = authorHomeAway game['waitingOn'] = utils.reverseHomeAway(game['waitingOn']) game['dirty'] = True utils.sendDefensiveNumberMessage(game) return True, "{} deferred and will receive the ball in the second half. The game has started!\n\n{}\n\n{}".format( game[authorHomeAway]['name'], utils.getCurrentPlayString(game), utils.getWaitingOnString(game)) else: log.debug( "User elected to receive, {} is receiving".format(authorHomeAway)) state.setStateTouchback(game, authorHomeAway) game['receivingNext'] = utils.reverseHomeAway(authorHomeAway) game['waitingOn'] = utils.reverseHomeAway(game['waitingOn']) game['dirty'] = True utils.sendDefensiveNumberMessage(game) return True, "{} elected to receive. The game has started!\n\n{}\n\n{}".format( game[authorHomeAway]['name'], utils.getCurrentPlayString(game), utils.getWaitingOnString(game))
def processMessageTip(game, message): number, error = utils.extractPlayNumber(message.body) if error is not None: return False, "Didn't send me a number. Reply to the original message and send me a number this time" author = str(message.author).lower() log.debug('author is {}'.format(author)) log.debug('number is {}'.format(number)) log.debug("Processing tip ball where game is dirty is {}".format(game['dirty'])) log.debug('is author in {} or {}'.format(game['away']['coaches'], game['home']['coaches'])) if author in game['away']['coaches']: resultMessage, worked = state.tipResults(game, 'away', number) elif author in game['home']['coaches']: resultMessage, worked = state.tipResults(game, 'home', number) else: return False, 'ooops' log.debug('Now checking if game is dirty and tip off is complete') log.debug('Dirty is {}'.format(game['dirty'])) if game['tip']['awayTip'] and game['tip']['homeTip']: awayTip = int(database.getTipById(game['dataID'],'awayTip')) homeTip = int(database.getTipById(game['dataID'],'homeTip')) botTip = utils.rngNumber() tipWinner = utils.getTipWinner(awayTip, homeTip, botTip) game['status']['wonTip'] = tipWinner game['waitingOn'] = utils.reverseHomeAway(tipWinner) game['status']['possession'] = tipWinner game['play']['defensiveNumber'] = True game['play']['offensiveNumber'] = False log.debug("sending initial defensive play comment to {}".format(game['waitingOn'])) resultMessage = "/u/{} has won the tippoff . /u/{} Will get a DM to start the action. \ \naway tip number: {}\ \nhome tip number: {}\ \nbot tip number: {}".format( game[tipWinner]['coaches'][0], game[game['waitingOn']]['coaches'][0], awayTip, homeTip, botTip ) defMessage = "You lost the tipoff . Please send me a number to start the game getween **1** and **{}**".format(globals.maxRange) log.debug('defensive message is {}'.format(defMessage)) game['dirty'] = True utils.sendDefensiveNumberMessage(game, defMessage) game['waitingAction'] = 'play' game['tip']['justTipped'] = True return True, resultMessage return worked, resultMessage
def processMessageDefer(game, isDefer, author): log.debug("Processing defer message: {}".format(str(isDefer))) authorHomeAway = utils.coachHomeAway(game, author) utils.setGamePlayed(game) if utils.isGameOvertime(game): if isDefer: log.debug("User deferred, {} is attacking".format(authorHomeAway.negate().name())) state.setStateOvertimeDrive(game, authorHomeAway.negate()) game.status.receivingNext = authorHomeAway.copy() game.status.waitingOn.reverse() game.dirty = True utils.sendDefensiveNumberMessage(game) return True, "{} deferred and will attack next. Overtime has started!\n\n{}\n\n{}".format( game.team(authorHomeAway).name, string_utils.getCurrentPlayString(game), string_utils.getWaitingOnString(game)) else: log.debug("User elected to attack, {} is attacking".format(authorHomeAway)) state.setStateOvertimeDrive(game, authorHomeAway) game.status.receivingNext = authorHomeAway.negate() game.status.waitingOn.reverse() game.dirty = True utils.sendDefensiveNumberMessage(game) return True, "{} elected to attack. Overtime has started!\n\n{}\n\n{}".format( game.team(authorHomeAway).name, string_utils.getCurrentPlayString(game), string_utils.getWaitingOnString(game)) else: if isDefer: log.debug("User deferred, {} is receiving".format(authorHomeAway.negate())) state.setStateKickoff(game, authorHomeAway) game.status.receivingNext = authorHomeAway.copy() game.status.waitingOn.reverse() game.dirty = True utils.sendDefensiveNumberMessage(game) return True, "{} deferred and will receive the ball in the second half. The game has started!\n\n{}\n\n{}".format( game.team(authorHomeAway).name, string_utils.getCurrentPlayString(game), string_utils.getWaitingOnString(game)) else: log.debug("User elected to receive, {} is receiving".format(authorHomeAway)) state.setStateKickoff(game, authorHomeAway.negate()) game.status.receivingNext = authorHomeAway.negate() game.status.waitingOn.reverse() game.dirty = True utils.sendDefensiveNumberMessage(game) return True, "{} elected to receive. The game has started!\n\n{}\n\n{}".format( game.team(authorHomeAway).name, string_utils.getCurrentPlayString(game), string_utils.getWaitingOnString(game))
log.debug("Game past playclock: {}".format(game.thread)) utils.cycleStatus(game, None) game.status.state(game.status.waitingOn).playclockPenalties += 1 penaltyMessage = "{} has not sent their number in over 24 hours, playclock penalty. This is their {} penalty.".format( string_utils.getCoachString(game, game.status.waitingOn), string_utils.getNthWord(game.status.state(game.status.waitingOn).playclockPenalties)) if game.status.state(game.status.waitingOn).playclockPenalties >= 3: log.debug("3 penalties, game over") result = utils.endGame(game, game.team(game.status.waitingOn.negate()).name) resultMessage = "They forfeit the game. {} has won!\n\n{}".format(string_utils.flair(game.team(game.status.waitingOn.negate())), result) elif game.status.waitingOn == game.status.possession: log.debug("Waiting on offense, turnover") if utils.isGameOvertime(game): resultMessage = state.overtimeTurnover(game) if game.status.waitingAction != Action.END: utils.sendDefensiveNumberMessage(game) else: state.turnover(game) game.status.waitingOn = game.status.possession.negate() utils.sendDefensiveNumberMessage(game) resultMessage = "Turnover, {} has the ball.".format(string_utils.flair(game.team(game.status.waitingOn))) else: 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)
def processMessageOffensePlay(game, message, author): log.debug("Processing offense number message") timeoutMessageOffense = None timeoutMessageDefense = None if "timeout" in message: if game.status.state(game.status.possession).timeouts > 0: game.status.state(game.status.possession).requestedTimeout = TimeoutOption.REQUESTED else: timeoutMessageOffense = "The offense requested a timeout, but they don't have any left" if game.forceChew: timeOption = TimeOption.CHEW else: timeOption = TimeOption.NORMAL if any(x in message for x in ['chew the clock', 'milk the clock', 'chew clock']): timeOption = TimeOption.CHEW elif any(x in message for x in ['hurry up', 'no huddle', 'no-huddle']): timeOption = TimeOption.HURRY elif any(x in message for x in ['normal']): timeOption = TimeOption.NORMAL normalOptions = ["run", "pass", "punt", "field goal", "kneel", "spike"] conversionOptions = ["two point", "pat"] kickoffOptions = ["normal", "squib", "onside"] if game.status.waitingAction == Action.PLAY: playSelected = utils.findKeywordInMessage(normalOptions, message) elif game.status.waitingAction == Action.CONVERSION: playSelected = utils.findKeywordInMessage(conversionOptions, message) elif game.status.waitingAction == Action.KICKOFF: playSelected = utils.findKeywordInMessage(kickoffOptions, message) else: return False, "Something went wrong, invalid waiting action: {}".format(game.status.waitingAction) if playSelected == "run": play = Play.RUN elif playSelected == "pass": play = Play.PASS elif playSelected == "punt": play = Play.PUNT elif playSelected == "field goal": play = Play.FIELD_GOAL elif playSelected == "kneel": play = Play.KNEEL elif playSelected == "spike": play = Play.SPIKE elif playSelected == "two point": play = Play.TWO_POINT elif playSelected == "pat": play = Play.PAT elif playSelected == "normal": play = Play.KICKOFF_NORMAL elif playSelected == "squib": play = Play.KICKOFF_SQUIB elif playSelected == "onside": play = Play.KICKOFF_ONSIDE elif playSelected == "mult": log.debug("Found multiple plays") return False, "I found multiple plays in your message. Please repost it with just the play and number." else: log.debug("Didn't find any plays") return False, "I couldn't find a play in your message" number, numberMessage = utils.extractPlayNumber(message) if play not in classes.timePlays and number == -1: log.debug("Trying to execute a {} play, but didn't have a number".format(play)) return False, numberMessage success, resultMessage = state.executePlay(game, play, number, timeOption) if game.status.state(game.status.possession).requestedTimeout == TimeoutOption.USED: timeoutMessageOffense = "The offense is charged a timeout" elif game.status.state(game.status.possession).requestedTimeout == TimeoutOption.REQUESTED: timeoutMessageOffense = "The offense requested a timeout, but it was not used" game.status.state(game.status.possession).requestedTimeout = TimeoutOption.NONE if game.status.state(game.status.possession.negate()).requestedTimeout == TimeoutOption.USED: timeoutMessageDefense = "The defense is charged a timeout" elif game.status.state(game.status.possession.negate()).requestedTimeout == TimeoutOption.REQUESTED: timeoutMessageDefense = "The defense requested a timeout, but it was not used" game.status.state(game.status.possession.negate()).requestedTimeout = TimeoutOption.NONE result = [resultMessage] if timeoutMessageOffense is not None: result.append(timeoutMessageOffense) if timeoutMessageDefense is not None: result.append(timeoutMessageDefense) if not game.status.timeRunoff: result.append("The clock is stopped.") game.status.waitingOn.reverse() game.dirty = True utils.setGamePlayed(game) if game.status.waitingAction in classes.playActions: utils.sendDefensiveNumberMessage(game) elif game.status.waitingAction == Action.OVERTIME: log.debug("Starting overtime, posting coin toss comment") message = "Overtime has started! {}, you're away, call **heads** or **tails** in the air.".format( string_utils.getCoachString(game, False)) comment = utils.sendGameComment(game, message, utils.getActionTable(game, Action.COIN)) utils.setWaitingId(game, comment.fullname) game.status.waitingAction = Action.COIN game.status.waitingOn = classes.HomeAway(False) return success, string_utils.embedTableInMessage('\n\n'.join(result), utils.getActionTable(game, game.status.waitingAction))
def processMessageOffensePlay(game, message, author): log.debug("Processing offense number message") number, numberMessage = utils.extractPlayNumber(message) timeoutMessageOffense = None timeoutMessageDefense = None if message.find("timeout") > 0: if game['status']['timeouts'][game['status']['possession']] > 0: game['status']['requestedTimeout'][game['status'] ['possession']] = 'requested' else: timeoutMessageOffense = "The offense requested a timeout, but they don't have any left" playOptions = [ 'run', 'pass', 'punt', 'field goal', 'kneel', 'spike', 'two point', 'pat' ] playSelected = utils.findKeywordInMessage(playOptions, message) play = "default" if playSelected == "run": play = "run" elif playSelected == "pass": play = "pass" elif playSelected == "punt": play = "punt" elif playSelected == "field goal": play = "fieldGoal" elif playSelected == "kneel": play = "kneel" elif playSelected == "spike": play = "spike" elif playSelected == "two point": play = "twoPoint" elif playSelected == "pat": play = "pat" elif playSelected == "mult": log.debug("Found multiple plays") return False, "I found multiple plays in your message. Please repost it with just the play and number." else: log.debug("Didn't find any plays") return False, "I couldn't find a play in your message" success, resultMessage = state.executePlay(game, play, number, numberMessage) if game['status']['requestedTimeout'][game['status'] ['possession']] == 'used': timeoutMessageOffense = "The offense is charged a timeout" elif game['status']['requestedTimeout'][game['status'] ['possession']] == 'requested': timeoutMessageOffense = "The offense requested a timeout, but it was not used" game['status']['requestedTimeout'][game['status']['possession']] = 'none' if game['status']['requestedTimeout'][utils.reverseHomeAway( game['status']['possession'])] == 'used': timeoutMessageDefense = "The defense is charged a timeout" elif game['status']['requestedTimeout'][utils.reverseHomeAway( game['status']['possession'])] == 'requested': timeoutMessageDefense = "The defense requested a timeout, but it was not used" game['status']['requestedTimeout'][utils.reverseHomeAway( game['status']['possession'])] = 'none' result = [resultMessage] if timeoutMessageOffense is not None: result.append(timeoutMessageOffense) if timeoutMessageDefense is not None: result.append(timeoutMessageDefense) game['waitingOn'] = utils.reverseHomeAway(game['waitingOn']) game['dirty'] = True if game['waitingAction'] == 'play': utils.sendDefensiveNumberMessage(game) return success, utils.embedTableInMessage( '\n\n'.join(result), {'action': game['waitingAction']})
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)