def setupAIObject(self, sessionId): ''' setupAIObject This is used to retrieve all of the information from the datstore about the AI object and set the class variables equal to the values of the entitites retrieved in this way. This should be called whenever a new instance of HAL is created. Parameters: sessionId, the sessionId which identifies the HAL entity we are working with. ''' newModel = DatastoreInteraction(sessionId) valueDict = newModel.getHAL() self.pkSessionID = valueDict['pkSessionID'] self.estAIScore = valueDict['estAIScore'] self.estOppScore = valueDict['estOppScore'] self.opCardsMem = valueDict['opCardsMem'] self.aiCardsMem = valueDict['aiCardsMem'] tmpOP = valueDict['opCards'].encode('utf-8').replace("u","") tmpAI = valueDict['aiCards'].encode('utf-8').replace("u","") #Welcome to hack central. where python has type issues! self.realOpCards = eval(str(tmpOP)) self.realAiCards = eval(str(tmpAI)) self.opCards = tmpOP self.aiCards = tmpAI self.discardTopValue = valueDict['discardTopValue'] self.decayRate = valueDict['decayRate']
def get(self): ''' get You got your basic get method right here, but with a twist. This method perfoms the intialization of the game state, creating the json objects that represent the game and json encoding them. It then renders them on the game template with jinja. ''' # get the player avatar image from the datastore self.sessionId = self.request.get("sessionId") # use the gameModel to interact with the datastore newModel = DatastoreInteraction(self.sessionId) avatar = newModel.getAvatar() # set the right picture if(avatar == "character1"): self.thumbnailImage = "bettyThumb.png" elif(avatar == "character2"): self.thumbnailImage = "jasonThumb.png" elif(avatar == "character3"): self.thumbnailImage = "batRatThumb.png" elif(avatar == "character4"): self.thumbnailImage = "catLadyThumb.png" elif(avatar == "character5"): self.thumbnailImage = "lebowsCatThumb.png" else: self.thumbnailImage = "tommyCatThumb.png" # perform initial creation and encoding of JSON object newState = self.initEncode() # update the opcards and aicards in the hal object in the datastore halState = json.loads(newState) newModel = DatastoreInteraction(self.sessionId) newModel.updateAiCards(str(halState['playCard']), aiCards=str(halState['compCard'])) self.render("game.html", oldState='null', newState=newState, thumbnailImage=self.thumbnailImage)
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
#Should we knock?? #We should knock if we think our cards are higher than the players by some threshold state = self.shouldKnock(state) state['compCard'] = json.loads(self.aiCards) logging.info("ACTIONS") logging.info(self.actionsToTake) #Slowly forget what our cards are\ self.alzheimer() # we need to use the model to update the datastore # self.put() newModel = DatastoreInteraction(state['sessionId']) newModel.updateAiObject( self.pkSessionID, self.estAIScore, self.estOppScore, self.opCardsMem, self.aiCardsMem, self.opCards, self.aiCards, self.discardTopValue ) time.sleep(2) return state def drawTwo(self,state): ''' drawTwo: The AI decides what to do with a draw two card Parameters: state, the state of the game