def processMessageCoin(game, isHeads, author): log.debug("Processing coin toss message: {}".format(str(isHeads))) utils.setGamePlayed(game) if isHeads == utils.coinToss(): log.debug("User won coin toss, asking if they want to defer") game.status.waitingAction = Action.DEFER game.status.waitingOn.set(False) utils.setWaitingId(game, 'return') game.dirty = True if utils.isGameOvertime(game): questionString = "do you want to **defend** or **attack**?" else: questionString = "do you want to **receive** or **defer**?" message = "{}, {} won the toss, {}".format(string_utils.getCoachString(game, False), game.away.name, questionString) return True, string_utils.embedTableInMessage(message, utils.getActionTable(game, Action.DEFER)) else: log.debug("User lost coin toss, asking other team if they want to defer") game.status.waitingAction = Action.DEFER game.status.waitingOn.set(True) utils.setWaitingId(game, 'return') game.dirty = True if utils.isGameOvertime(game): questionString = "do you want to **defend** or **attack**?" else: questionString = "do you want to **receive** or **defer**?" message = "{}, {} won the toss, {}".format(string_utils.getCoachString(game, True), game.home.name, questionString) return True, string_utils.embedTableInMessage(message, utils.getActionTable(game, Action.DEFER))
def setBlock(game,turnover): current = game['status']['possession'] utils.addStat(game,'blocks', 1, utils.reverseHomeAway(current)) shotType = utils.coinToss() if shotType: sub2Pt(game, False, False) else: sub3Pt(game, False, False)
def overtimeTurnover(game): log.debug("Running overtime turnover") if game.status.overtimePossession == 1: log.debug("End of first overtime possession, starting second") game.status.overtimePossession = 2 setStateOvertimeDrive(game, game.status.possession.negate()) return "End of the drive. {} has possession now".format(string_utils.flair(game.team(game.status.possession))) elif game.status.overtimePossession == 2: if game.status.state(T.home).points == game.status.state(T.away).points: if game.status.quarterType == QuarterType.OVERTIME_TIME and game.status.quarter >= 6: log.debug("End of 6th quarter in a time forced overtime, flipping coin for victor") if utils.coinToss(): log.debug("Home has won") victor = HomeAway(T.home) else: log.debug("Away has won") victor = HomeAway(T.away) output = utils.endGame(game, game.team(victor).name) return "It is the end of the 6th quarter in an overtime forced by the game clock and the score is still tied. " \ "I'm flipping a coin to determine the victor. {} has won!\n\n{}".format(string_utils.flair(game.team(victor)), output) else: log.debug("End of second overtime possession, still tied, starting new quarter") game.status.overtimePossession = 1 game.status.quarter += 1 setStateOvertimeDrive(game, game.status.receivingNext) game.status.receivingNext.reverse() return "It's still tied! Going to the {} quarter.".format(string_utils.getNthWord(game.status.quarter)) else: log.debug("End of game") if game.status.state(T.home).points > game.status.state(T.away).points: victor = HomeAway(T.home) else: victor = HomeAway(T.away) output = utils.endGame(game, game.team(victor).name) return "That's the end of the game. {} has won!\n\n".format(string_utils.flair(game.team(victor)), output) else: log.warning("Something went wrong. Invalid overtime possession: {}".format(game.status.overtimePossession))
def processMessageCoin(game, isHeads, author): log.debug("Processing coin toss message: {}".format(str(isHeads))) if isHeads == utils.coinToss(): log.debug("User won coin toss, asking if they want to defer") game['waitingAction'] = 'defer' game['waitingOn'] = 'home' game['waitingId'] = 'return' game['dirty'] = True message = "{}, {} won the toss, do you want to **receive** or **defer**?".format( utils.getCoachString(game, 'home'), game['home']['name']) return True, utils.embedTableInMessage(message, {'action': 'defer'}) else: log.debug( "User lost coin toss, asking other team if they want to defer") game['waitingAction'] = 'defer' game['waitingOn'] = 'away' game['waitingId'] = 'return' game['dirty'] = True message = "{}, {} won the toss, do you want to **receive** or **defer**?".format( utils.getCoachString(game, 'away'), game['away']['name']) return True, utils.embedTableInMessage(message, {'action': 'defer'})
def executePlay(game, play, number, numberMessage): startingPossessionHomeAway = game['status']['possession'] actualResult = None resultMessage = "Something went wrong, I should never have reached this" diffMessage = None success = True timeMessage = None fouled = False log.debug("starting to execute Play with play being {} and number being {}".format(play, number)) if game['status']['free'] != False: result = "FREE" if number == -1: log.debug("Trying to shoot a free throw play, but didn't have a number") resultMessage = numberMessage success = False return False, resultMessage elif number > -1: game['status']['frees'] -= 1 utils.addStat(game,'FTAttempted',1,startingPossessionHomeAway) numberResult, diffMessage = getNumberDiffForGame(game, number) freeResult = getFreeThrowResult(game,numberResult) if freeResult: game['play']['result'] = 'Free Made' log.debug("Successful Free Throw") tempMessage = ["{}'s Free throw was successful.\n\n ".format(game[game['status']['possession']]['name'])] utils.addStat(game,'FTMade',1,startingPossessionHomeAway) scoreFreeThrow(game, startingPossessionHomeAway) tempMessage.append("\nThe score is now **{}**: {}, **{}**: {}".format(game['away']['name'],game['score']['away'], game['home']['name'], game['score']['home'])) resultMessage = ''.join(tempMessage) if game['status']['free'] == '1and1Start': game['status']['frees'] = 1 game['status']['free'] = True else: game['play']['result'] = 'Free Missed' log.debug("failed free throw") resultMessage = "You missed the free throw." if game['status']['frees'] == 0: game['status']['free'] = False game['waitingAction'] = 'play' game['play']['playResult'] = 'freeDone' game['freeThrows']['freeType'] = None result = "FREEDONE" else: pass ## database.clearDefensiveNumber(game['dataID']) else: resultMessage = "It looks like /]you're trying to get the extra point after a touchdown, but this isn't a valid play" success = False elif game['status']['ifoul']: resultMessage = setFouls(game, 0) game['play']['playResult'] = 'fouled' else: if play in globals.offPlays: utils.addStat(game,'possessions',1,startingPossessionHomeAway) if number == -1: log.debug("Trying to execute a normal play, but didn't have a number") resultMessage = numberMessage success = False else: numberResult, diffMessage = getNumberDiffForGame(game, number) log.debug("numberResult was {}".format(numberResult)) log.debug("Executing normal play: {}".format(play)) result = getPlayResult(game, play, numberResult) game['play']['result'] = result['result'] playResultName = result['result'].lower() log.debug('playResultName is {}'.format(playResultName)) ptf = re.search('2|3', playResultName) log.debug("ptf is {}".format(ptf)) if ptf: pointsTriedFor = int(ptf.group(0)) log.debug("this shot as an attempt for {} points".format(pointsTriedFor)) if playResultName.lower() in globals.pointResults : if 'points' not in result: log.warning("Result is a score, but I couldn't find any points") resultMessage = "Result of play is a number of points, but something went wrong and I couldn't find what number" success = False else: if playResultName in globals.foulPlays: ##After this play we will be waiting on the current devensive ##team since they will have to send a defensive number fouled = True points = result['points'] tempMessage = ["{}'s Basket is good for {} points.\n\n".format(game[game['status']['possession']]['name'],points)] if fouled: tempMessage.append('AND ONE. Player is fouled.\n\n') if points == 2: sub2Pt(game, True, fouled) game['play']['playDesc'] = '2' game['play']['playResult'] = 'made' game['status']['scored'] = True elif points == 3: sub3Pt(game, True, fouled) game['play']['playResult'] = 'made' game['play']['playDesc'] = '3' game['status']['scored'] = True log.debug("Result is a gain of {} points".format(points)) tempMessage.append("\nThe score is now **{}**: {}, **{}**: {}".format(game['away']['name'],game['score']['away'], game['home']['name'], game['score']['home'])) resultMessage = ''.join(tempMessage) elif playResultName in globals.foulMissPlays: log.debug("In foul Miss Plays") ##get numbers to see how many free thors we will shoot setFouls(game, pointsTriedFor) resultMessage = 'Fouled on a {0}pt shot. The shot is missed. Going to shoot {0} free throws.'.format(pointsTriedFor) game['play']['playResult'] = 'fouled' elif playResultName in globals.nonShootingFoul: ##This sets possession in the bonus check resultMessage = setFouls(game, 0) game['play']['playResult'] = 'fouled' elif playResultName in globals.missPlays: log.debug('In miss plays') game['play']['playResult'] = 'miss'##No need to change who we are waitingon here if pointsTriedFor == 2: sub2Pt(game, False, False) elif pointsTriedFor == 3: sub3Pt(game, False, False) else: return False, "Could not detect a number like we think it should. We must pay with our " resultMessage = "Missed a {} point shot.".format(pointsTriedFor) elif playResultName in globals.offRebounds: game['play']['playResult'] = 'offrebound' shotType = utils.coinToss() if shotType: log.debug('By random choice this was a 2 point shot') sub2Pt(game, False, False, True) else: log.debug('By random choice this was a 3 point shot') sub3Pt(game, False, False, True) resultMessage = "Missed a shot but got the offensive rebound" elif playResultName in globals.turnovers: game['play']['playResult'] = 'turnover' setTurnovers(game, playResultName.lower()) if playResultName == "Steal": resultMessage = "Hark, a steal" else: resultMessage = 'Turned the ball over' elif playResultName == 'block': resultMessage = "the shot was BLOCKED" game['play']['playResult'] = 'block' setBlock(game, 'block') elif playResultName == globals.stealDunk: tempMessage = ['The ball was stolen by {} and dunked for two points\n\n'.format(game[utils.reverseHomeAway(game['status']['possession'])]['name'])] setTurnovers(game, 'steal') utils.addStat(game, 'possessions',1, utils.reverseHomeAway(startingPossessionHomeAway)) game['status']['possession'] = utils.reverseHomeAway(startingPossessionHomeAway) log.debug('{} had the ball stolen and {} is about to score'.format(startingPossessionHomeAway, utils.reverseHomeAway(startingPossessionHomeAway))) sub2Pt(game, True, False) game['play']['playResult'] = 'made' game['waitingOn'] = utils.reverseHomeAway(startingPossessionHomeAway) tempMessage.append("\nThe score is now **{}**: {}, **{}**: {}".format(game['away']['name'],game['score']['away'], game['home']['name'], game['score']['home'])) resultMessage = ''.join(tempMessage) elif playResultName == globals.steal3Pt: tempMessage = ['The ball was stolen by {} and shot for a 3 PT.\n\n '.format(game[utils.reverseHomeAway(game['status']['possession'])]['name'])] setTurnovers(game, 'steal') utils.addStat(game, 'possessions',1, utils.reverseHomeAway(startingPossessionHomeAway)) game['status']['possession'] = utils.reverseHomeAway(startingPossessionHomeAway) log.debug('{} had the ball stolen and {} is about to score'.format(startingPossessionHomeAway, utils.reverseHomeAway(startingPossessionHomeAway))) sub3Pt(game, True, False) game['play']['playResult'] = 'made' game['waitingOn'] = utils.reverseHomeAway(startingPossessionHomeAway) tempMessage.append("\nThe score is now **{}**: {}, **{}**: {}".format(game['away']['name'],game['score']['away'], game['home']['name'], game['score']['home'])) resultMessage = ''.join(tempMessage) database.clearDefensiveNumber(game['dataID']) else: resultMessage = "{} isn't a valid play at the moment".format(play) success = False messages = [resultMessage] if resultMessage is not None: if success: if game['status']['ifoul']: game['status']['ifoul'] = False diffMessage = None result = None timeMessage = updateTime(game, play, result, startingPossessionHomeAway) if timeMessage is None: timeMessage = updateTime(game, play, result, startingPossessionHomeAway) messages.append(timeMessage) if diffMessage is not None: messages.append(diffMessage) log.debug("Finishing execution of play") log.debug("We will be waiting on {} for next number".format(game['waitingOn'])) log.debug("messages: resultMessage: {}, timeMessage:{}, diffMessage:{}".format(resultMessage, timeMessage, diffMessage)) ##determine here if we need to change possession changePossession(game) return success, '\n\n'.join(messages)