def endGame(self, statePassedIn): ''' endGame State handler used to sum of the scores of a round and add them to the total score value in the state JSON. Also decides if the game is over or just the round is over. Note about the sleep timers: Google, get your shit together and serialize your datastore. This is just ridiculous. Parameters: statePassedIn, the (current) state of the game that has been passed in by the client side (view) ajax call. Returns: newState, the new state of the game as delinated by the statePassedIn and the user's choices. ''' logging.info("Made it to the endgame state") # set the state to endgame so the view knows what to do statePassedIn['state'] = "endGame" # make a new object to interact with the datastore newModel = DatastoreInteraction(statePassedIn['sessionId']) # update the number of rounds played in the game newModel.updateGameRounds() # what is the total score of each player's hand? pScore = 0 cScore = 0 # check to see if the deck has enough cards in it to accomodate swapping all potential power cards # also, remove all power cards, so they cannot be distributed again numberCards = [ [0]* 4, [1]*4, [2]*4, [3]*4, [4]*4, [5]*4, [6]*4, [7]*4, [8]*4, [9]*9 ] cardReplace = sum(numberCards, []) shuffle(cardReplace) # set the cards to visible, get the score, and swap out any power cards for pCard in statePassedIn['playCard']: cardVal = int(pCard['image']) # if the card is a power card, replace it if(cardVal >= 10): pCard['image'] = cardReplace.pop() # add to running total and set visible pScore += cardVal pCard['visible'] = 1 for cCard in statePassedIn['compCard']: cardVal = int(cCard['image']) # if the card is a power card, replace it if(cardVal >= 10): cCard['image'] = cardReplace.pop() cScore += cardVal cCard['visible'] = 1 logging.info("This is the player score") logging.info(pScore) logging.info("This is the computer score") logging.info(cScore) # who wins the round? if (pScore < cScore): # player wins logging.info("player wins") statePassedIn["win"] = 1 # update the fact that the player won a round, and played a round newModel.updateRoundsWonTotal() time.sleep(1) elif (pScore > cScore): # computer wins logging.info("computer wins") statePassedIn["win"] = 0 newModel.updateRoundsLostTotal() time.sleep(1) else: # tie logging.info("tie!!") statePassedIn["win"] = 2 newModel.updateRoundsPlayedTotal() time.sleep(1) # use sleep to prevent the datastore from overwriting itself time.sleep(1) # add the player score to the running total of their score for all games so far in the database newModel.updatePlayerScore(pScore) time.sleep(1) # add the computer score to the running total for HAL newModel.updateComputerScore(cScore) time.sleep(1) # add the players score to the running total for the current game newModel.updateGameScore(pScore) time.sleep(1) # what is the player and computer's total score now for this specific game? playerTotalScore, computerTotalScore = newModel.getTotalGameScore() time.sleep(1) # is the game over? If yes... if(playerTotalScore >= self.ENDGAME_SCORE or computerTotalScore >= self.ENDGAME_SCORE): logging.info("Game is now over") newModel.updateGames() time.sleep(1) # update the json to reflect that the game is now over statePassedIn['gameOver'] = 1 # is the player's score, retrieved from the database, greater than the computer's score? Who won? if (playerTotalScore > computerTotalScore): # player loses logging.info("Player Loses") gameText = "You Lose," statePassedIn["win"] = 0 newModel.updateGameLose() newModel.updateRoundsLostTotal() time.sleep(1) elif(computerTotalScore > playerTotalScore): # player wins logging.info("Player Wins") gameText = "You Win," statePassedIn["win"] = 1 newModel.updateGameWin() newModel.updateRoundsWonTotal() time.sleep(1) else: # a tie logging.info("A tie has occurred") gameText = "It was a tie," statePassedIn["win"] = 2 newModel.updateGameLose() newModel.updateRoundsPlayedTotal() time.sleep(1) time.sleep(1) statePassedIn['message']['text'] = "Game Over. " + gameText + str(playerTotalScore) + " to " + str(computerTotalScore) + ". Would you like to start a new game?" return statePassedIn # if the game is not over, only the rounds is. Therefore, we begin a new round else: time.sleep(1) logging.info("Starting a new round") statePassedIn['message']['text'] = "The round is over! Your score for the round was: " + str(pScore) + ". The computer's score was: " + str(cScore) + ". Would you like to continue playing?" return statePassedIn