def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game.game_over: return game.to_form('Game already over!') """Hangman Game Logic""" word = list(request.guess)[0] target = list(game.target) miss = list(game.miss) current = list(game.current) valid = False inMissList = False message = '' game_history = GameHistory.query(GameHistory.game == game.key).get() """Check whether the input word is in miss list""" for index,t in enumerate(miss): if t == word: inMissList = True game.attempts_remaining -= 1 message = 'You already guessed that letter and it is not right! Try again, but a new one this time.' break """Check whether the input word is in target list""" for index,t in enumerate(target): if word == t: valid = True message = 'Correct!' current[index] = word break game.current = ''.join(current) """Update miss list""" if valid == False and inMissList == False: game.attempts_remaining -= 1 miss.append(word) game.miss =''.join(miss) game.put() message = 'Nice try! But it is not a valid word' """Compare between current and target""" if game.attempts_remaining < 1: game_history.addHistory(valid,word,'lose') game.end_game(False) message = 'Game over!' else: if target == current: game.end_game(True) game_history.addHistory(valid,word,'win') message = 'You Win!' else: game_history.addHistory(valid,word,'pending') game.put() return game.to_form(message)
def get_game_history(self,request): """Return game history for a specific game""" game = get_by_urlsafe(request.urlsafe_game_key,Game) if not game: raise endpoints.NotFoundException('Game not found!') gameHistory = GameHistory.query(GameHistory.game == game.key).get() return HistoryForms(items=[history.to_form() for history in gameHistory.histories])
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game.game_over: return game.to_form('Game already over!') if (game.whos_turn == 1 and game.player_one.get().name != request.player_name) or \ (game.whos_turn == 2 and game.player_two.get().name != request.player_name): return game.to_form('Please wait your turn!') try: game.move(request.move_row, request.move_col) game.put() except ValueError as error: return game.to_form(error.message) if game.game_over is True: taskqueue.add(url='/tasks/decrement_active_games') message = "Thank you for playing. " + \ request.player_name + " has won the game." else: message = "Move accepted. Please wait for your next turn." game_history = GameHistory.query(ancestor=game.key).get() game_history.history.append(game) game_history.messages.append(message) game_history.put() return game.to_form(message)
def cancel_game(self, request): game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found!') if game.game_over is True: raise endpoints.ForbiddenException( 'You can not cancel this game as it is already over!') # Save the game. # A game with no winner and with game_over=True is a cancelled game. game.game_over = True game.put() # Decrement active games taskqueue.add(url='/tasks/decrement_active_games') message = "The game was cancelled successfuly." # Update game history game_history = GameHistory.query(ancestor=game.key).get() game_history.history.append(game) game_history.messages.append(message) game_history.put() return StringMessage(message=message)
def get_game_history(self, request): """Return game history for a specific game""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found!') gameHistory = GameHistory.query(GameHistory.game == game.key).get() return HistoryForms( items=[history.to_form() for history in gameHistory.histories])
def get_game_history(game): # query by kind query = GameHistory.query(ancestor=game.key) # order by timestamp query.order(GameHistory.step_timestamp) return query.fetch()
def get_game_history(self, request): """ This function returns the chronological game history based on a url safe game key. The 'turn' value uses computer counting, so the first move done will have a turn value of 0. """ histories = GameHistory.query(GameHistory.game_url_safekey == request.urlsafe_game_key).order(GameHistory.turn).fetch() return GameHistories(items=[history.to_form() for history in histories])
def get_game_history(self, request): """Return every moves of the game""" game_histories = GameHistory.query(ancestor=ndb.Key( urlsafe=request.urlsafe_game_key)).fetch() if not game_histories: raise endpoints.NotFoundException("No Game Histories found") return GameHistoryForms( items=[history.to_form() for history in game_histories])
def getUserGameHistory(self, request): """ returns all moves made and corresponding game state per move for a given user """ game_history = GameHistory.query(GameHistory.game_id == request.game_id, GameHistory.user_id == request.user_id).fetch() game_history_forms = GameHistoryForms(moves = [ move.to_form() for move in game_history ]) if len(game_history) == 0: game_history_forms = [GameHistoryForm(user_id="There's No Games For Selected User ID")] return GameHistoryForms(moves=game_history_forms) return game_history_forms
def cancel_game(self, request): """This function delete a game. Input: Game safe url key""" game = utils.get_by_urlsafe(request.urlsafekey, Game) if game.status == "over": raise endpoints.ConflictException('Cannot delete a Game over') for hist in GameHistory.query(GameHistory.game == game.key): hist.key.delete() game.key.delete() return MessageForm(message=" Game cancelled")
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # Check if the game is finished if game.game_over: return game.to_form('Game already finished!') # Only alphabetic character is allowed if not request.guess.isalpha(): return game.to_form('Only alphabetic character is allowed!') guess = request.guess.upper() # Only one character is allowed if len(guess) != 1: return game.to_form('Only one character allowed') # We will check if the character is already used game_key = ndb.Key(Game, request.urlsafe_game_key) ls = GameHistory.query(ancestor=game_key).filter( GameHistory.guess == guess).count() if ls != 0: return game.to_form('Character already used' + guess) found = False temp = list(game.guess_word) temp_wimp = list(game.word_in_progress) for i, val in enumerate(temp): if val == guess: temp_wimp[i] = guess found = True msg = 'Key found ' + guess GameHistory.create_game_history(request.urlsafe_game_key, guess, found, i, 'Found') game.word_in_progress = ''.join(temp_wimp) if found is False: GameHistory.create_game_history(request.urlsafe_game_key, guess, found, -1, 'Not found') game.attempts_remaining -= 1 msg = 'Character not found in word: ' + request.guess if game.word_in_progress == game.guess_word: game.end_game(True) msg = 'You win' if game.attempts_remaining < 1: game.end_game(False) GameHistory.create_game_history(request.urlsafe_game_key, guess, found, -1, 'Not attempts remaining you lose') return game.to_form(msg + ' Game over!') else: game.put() return game.to_form(msg)
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game.game_over: return game.to_form('Game already over!') """Hangman Game Logic""" word = list(request.guess)[0] target = list(game.target) miss = list(game.miss) current = list(game.current) valid = False inMissList = False message = '' game_history = GameHistory.query(GameHistory.game == game.key).get() """Check whether the input word is in miss list""" for index, t in enumerate(miss): if t == word: inMissList = True game.attempts_remaining -= 1 message = 'You already guessed that letter and it is not right! Try again, but a new one this time.' break """Check whether the input word is in target list""" for index, t in enumerate(target): if word == t: valid = True message = 'Correct!' current[index] = word break game.current = ''.join(current) """Update miss list""" if valid == False and inMissList == False: game.attempts_remaining -= 1 miss.append(word) game.miss = ''.join(miss) game.put() message = 'Nice try! But it is not a valid word' """Compare between current and target""" if game.attempts_remaining < 1: game_history.addHistory(valid, word, 'lose') game.end_game(False) message = 'Game over!' else: if target == current: game.end_game(True) game_history.addHistory(valid, word, 'win') message = 'You Win!' else: game_history.addHistory(valid, word, 'pending') game.put() return game.to_form(message)
def game_history(self, request): game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found!') game_history = GameHistory.query(ancestor=game.key).get() items = [] for index, item in enumerate(game_history.history): items.append(item.to_form(game_history.messages[index])) return GameForms(items=items)
def get_game_history(self, request): """Return the game history.""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # If the game dose't exist throw exception. if not game: raise endpoints.NotFoundException('Game not found!') game_key = ndb.Key(Game, request.urlsafe_game_key) return GameHistoryForms(items=[ game_history.to_form() for game_history in GameHistory.query( ancestor=game_key).order(GameHistory.date_created) ])
def get_game_history(self, request): """Returns the move history for a specified game.""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game: game_history = (GameHistory.query(GameHistory.game == game.key) .order(GameHistory.turn)) if game_history: return GameHistory.to_form(game_history) else: raise endpoints.NotFoundException( 'No bids have been raised for this game yet') else: raise endpoints.NotFoundException('Game not found!')
def cancel_game(self, request): """Delete a game from the database""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if game: if not game.game_over: game_history = GameHistory.query(ancestor=game.key).fetch() for h in game_history: h.key.delete() game.key.delete() return StringMessage(message="The game has been deleted!") else: raise endpoints.ForbiddenException( "Not allowed to delete a completed game") else: raise endpoints.NotFoundException("Game not found")
def get_game_history(self,request): """Return moves for specified game""" game = get_by_urlsafe(request.urlsafe_game_key, Game) return GameHistoryForms(items = [record.to_form() for record in \ GameHistory.query(GameHistory.game == game.key).order(GameHistory.movecount)])
def get_game_history(self, request): """This function returns all moves in a game.""" game = utils.get_by_urlsafe(request.game_urlsafekey, Game) games = GameHistory.query(GameHistory.game == game.key).order( GameHistory.date) return ListGameHistory(history=[game.toForm() for game in games])