Пример #1
0
def make_move(request):
    game = get_by_urlsafe(request.urlsafe_game_key, Game)
    game.number_of_guess += 1
    word = Word.query(Word.key == game.word).get()
    split_word = list(word.word)
    guess = request.guess
    game_history = History.query(History.game == game.key)
    letters_guessed_so_far = []

    for history in game_history.iter():
        letters_guessed_so_far.append(history.guess)

    if game.game_over is True:
        msg = 'Game already over!'
    elif guess == '' or guess.isdigit() or len(guess) > 1:
        msg = "Please enter a single alpha character only"
    elif guess in letters_guessed_so_far:
        msg = "You have already used that letter"
    elif guess not in split_word:
        msg = "letter isn't in word"
        game.guesses_remaining -= 1
        save_history(game.key, game.number_of_guess, guess, False)
    else:
        msg = "Letter is in word!"
        save_history(game.key, game.number_of_guess, guess, True)

    # Added in a sleep because of latency writing to the datastore.
    # http://stackoverflow.com/questions/9137214/writing-then-reading-entity-does-not-fetch-entity-from-datastore
    time.sleep(0.1)

    count_of_success = History.query(History.status == True).filter(History.game == game.key).count()

    if len(word.word) == count_of_success:
        msg = "You've won! The word was {}".format(word.word)
        game.end_game(True)
    elif game.guesses_remaining == 0:
        msg = 'You have run out of guesses! The word was {}'.format(word.word)
        game.end_game()
    taskqueue.add(url='/tasks/cache_average_attempts')
    game.put()

    return game, msg
Пример #2
0
 def history(self):
     q = History.query(ancestor=self.key)
     q = q.order(-History.creation_time)
     return [h.get() for h in q.iter(limit=10, keys_only=True)]
Пример #3
0
 def history(self):
     q = History.query(ancestor=self.key)
     q = q.order(-History.creation_time)
     return [h.get() for h in q.iter(limit=10, keys_only=True)]
Пример #4
0
 def get_game_history(self, request):
     """Get history for a game"""
     game = get_game(request)
     return HistoryForms(game=[history.to_form() for history in History.query(History.game == game.key)])