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 verifyCoaches(coaches): coachSet = set() teamSet = set() for i, coach in enumerate(coaches): if coach in coachSet: return i, 'duplicate' coachSet.add(coach) team = wiki.getTeamByCoach(coach) if team is None: return i, 'team' if team.name in teamSet: return i, 'same' teamSet.add(team.name) return -1, None
def verifyCoaches(coaches): coachSet = set() for i, coach in enumerate(coaches): if coach in coachSet: return i, 'duplicate' coachSet.add(coach) team = wiki.getTeamByCoach(coach) if team is None: return i, 'team' game = database.getGameByCoach(coach) if game is not None: return i, 'game' return -1, None
def processMessageNewGame(body, author): log.debug("Processing new game message") users = re.findall('(?: /u/)([\w-]*)', body) if len(users) == 0: log.debug("Could not find an opponent in create game message") return "Please resend the message and specify an opponent" opponent = users[0] log.debug("Found opponent in message /u/{}".format(opponent)) i, result = utils.verifyCoaches([author, opponent]) if i == 0 and result == 'team': log.debug("Author does not have a team") return "It looks like you don't have a team, please contact the /r/FakeCollegeFootball moderators" if i == 0 and result == 'game': log.debug("Author already has a game") return "You're already playing a game, you can't challenge anyone else until that game finishes" if result == 'duplicate': log.debug("{} challenged themselves to a game".format(author)) return "You can't challenge yourself to a game" if i == 1 and result == 'team': log.debug("Opponent does not have a team") return "It looks like your opponent doesn't have a team, please contact the /r/FakeCollegeFootball moderators" if i == 1 and result == 'game': log.debug("Opponent already has a game") return "/u/{} is already playing a game".format(opponent) authorTeam = wiki.getTeamByCoach(author) message = "/u/{}'s {} has challenged you to a game! Reply **accept** or **reject**.".format( author, authorTeam['name']) data = {'action': 'newgame', 'opponent': author} embeddedMessage = utils.embedTableInMessage(message, data) log.debug( "Sending message to /u/{} that /u/{} has challenged them to a game". format(opponent, author)) if reddit.sendMessage(opponent, "Game challenge", embeddedMessage): return "I've let /u/{} know that you have challenged them to a game. I'll message you again when they accept".format( opponent) else: return "Something went wrong, I couldn't find that user"
def startGame(homeCoach, awayCoach, startTime=None, location=None, station=None, homeRecord=None, awayRecord=None, neutral=False): log.debug("Creating new game between /u/{} and /u/{}".format( homeCoach, awayCoach)) coachNum, result = verifyCoaches([homeCoach, awayCoach]) if coachNum != -1: log.debug("Coaches not verified, {} : {}".format(coachNum, result)) return "Something went wrong, someone is no longer an \ acceptable coach. Please try to start the game again" homeTeam = wiki.getTeamByCoach(homeCoach.lower()) awayTeam = wiki.getTeamByCoach(awayCoach.lower()) for team in [homeTeam, awayTeam]: team['2PtAttempted'] = 0 team['2PtMade'] = 0 team['3PtAttempted'] = 0 team['3PtMade'] = 0 team['turnovers'] = 0 team['FTAttempted'] = 0 team['FTMade'] = 0 team['posTime'] = 0 team['record'] = None team['playclockPenalties'] = 0 team['bonus'] = 'N' team['offRebound'] = 0 team['defRebound'] = 0 team['fouls'] = [0, 0] team['steals'] = 0 team['blocks'] = 0 team['offDiffs'] = [] team['defDiffs'] = [] team['possessions'] = 0 game = newGameObject(homeTeam, awayTeam) if startTime is not None: game['startTime'] = startTime if location is not None: game['location'] = location if station is not None: game['station'] = station if homeRecord is not None: homeTeam['record'] = homeRecord if awayRecord is not None: awayTeam['record'] = awayRecord game['neutral'] = neutral gameThread = getGameThreadText(game) gameTitle = "[GAME THREAD] {}{} @ {}{}".format( game['away']['name'], " {}".format(awayRecord) if awayRecord is not None else "", game['home']['name'], " {}".format(homeRecord) if homeRecord is not None else "") 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 tip ball comment") message = "The ball is thrown in the air! {}, {}, Respond to the DM message \ I sent you for a TIP number".format(getCoachString(game, 'home'), getCoachString(game, 'away')) sendGameComment(game, message, {'action': 'tip'}) log.debug("Comment posted, now waiting on both") updateGameThread(game) coaches = [game['home']['coaches'][0], game['away']['coaches'][0]] sendTipNumberMessages(game, coaches) log.debug("Returning game started message") return "Game started. Find it [here]({}).".format( getLinkToThread(threadID))