/
api.py
269 lines (222 loc) · 10.7 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-`
'''
Hangman Game API
By: Ryan Dunton
Date: May 26, 2016'''
import re
import logging
import endpoints
from protorpc import remote, messages, message_types
from google.appengine.ext import ndb
from models import User, Game, Score, Word
from models import StringMessage, NewGameForm, GameForm, MakeMoveForm, \
ScoreForms, UserGamesForm, GameForms, HighScores, IntegerMessage
from models import UserForms
from utils import get_by_urlsafe
NEW_GAME_REQUEST = endpoints.ResourceContainer(NewGameForm)
GET_GAME_REQUEST = endpoints.ResourceContainer(
urlsafe_game_key=messages.StringField(1),)
MAKE_MOVE_REQUEST = endpoints.ResourceContainer(
MakeMoveForm,
urlsafe_game_key=messages.StringField(1),)
USER_REQUEST = endpoints.ResourceContainer(user_name=messages.StringField(1),
email=messages.StringField(2))
GET_ALL_GAMES_REQUEST = endpoints.ResourceContainer(user_name=messages.StringField(1),) # NOQA
HIGH_SCORES_REQUEST = endpoints.ResourceContainer(
number_of_results=messages.IntegerField(1))
MEMCACHE_MOVES_REMAINING = 'MOVES_REMAINING'
@endpoints.api(name='hangman', version='v1')
class HangmanApi(remote.Service):
"""Game API"""
@endpoints.method(request_message=USER_REQUEST,
response_message=StringMessage,
path='user',
name='create_user',
http_method='POST')
def create_user(self, request):
"""Create a User. Requires a unique username"""
if User.query(User.name == request.user_name).get():
raise endpoints.ConflictException(
'A User with that name already exists!')
user = User(name=request.user_name, email=request.email)
user.put()
return StringMessage(message='User {} created!'.format(
request.user_name))
@endpoints.method(request_message=NEW_GAME_REQUEST,
response_message=GameForm,
path='game',
name='new_game',
http_method='POST')
def new_game(self, request):
"""Creates new game"""
user = User.query(User.name == request.user_name).get()
if not user:
raise endpoints.NotFoundException(
'A User with that name does not exist!')
word = Word.query(Word.word_to_guess == request.word).get()
if not word:
word = Word(word_to_guess=request.word)
word.put()
try:
game = Game.new_game(user.key,
word.key.urlsafe())
return game.to_form('Good luck playing Hangman!')
except:
raise
@endpoints.method(request_message=MAKE_MOVE_REQUEST,
response_message=GameForm,
path='game/{urlsafe_game_key}',
name='make_move',
http_method='PUT')
def make_move(self, request):
"""Makes a move. Returns a game state with message"""
game = get_by_urlsafe(request.urlsafe_game_key, Game)
# Verify Game
if not game:
raise endpoints.NotFoundException("We could not find your game!")
# Verify game is active
if game.game_over:
return game.to_form('That game is over, sorry')
# Verify guess is a letter
if (request.guess.isalpha()):
game_word = game.get_word()
game.history.append("(User's guess was " + request.guess + ")")
# Allow user to guess the whole world
if len(request.guess) == len(game_word):
if request.guess == game_word:
game.word_so_far = request.guess
respond_to_user = 'You guessed the word! You won!'
game.end_game(won=True)
return game.to_form(respond_to_user)
# If user guesses too long let them know and take away attempt
if len(request.guess) > len(game_word):
game.attempts_remaining -= 1
respond_to_user = 'Your guess is too long!'
return game.to_form(respond_to_user)
# Verify guess is just one letter
if len(request.guess) == 1:
if request.guess in game.letters_guessed:
respond_to_user = 'You already guessed that letter'
respond_to_user = respond_to_user.format(request.guess)
return game.to_form(respond_to_user)
if request.guess not in game_word:
game.letters_guessed += request.guess
game.attempts_remaining -= 1
if game.attempts_remaining == 0:
game.end_game(won=False)
respond_to_user = 'You lose, the word was {}'
respond_to_user = respond_to_user.format(game_word)
else:
respond_to_user = 'The letter {} is not in the word'
respond_to_user = respond_to_user.format(request.guess)
game.put()
return game.to_form(respond_to_user)
else:
game.letters_guessed += request.guess
for (index, letter) in enumerate(game_word):
if letter == request.guess:
game.word_so_far = game.word_so_far[0:index] + request.guess + game.word_so_far[index + 1:]
if game.word_so_far == game_word:
game.end_game(won=True)
respond_to_user = 'Great job! You won!!! You guessed {}'
respond_to_user = respond_to_user.format(game_word)
else:
respond_to_user = 'Good guess! {} is in the word!'
respond_to_user = respond_to_user.format(request.guess)
game.put()
return game.to_form(respond_to_user)
# This occurs when user guesses more than one letter and length of guess is
# less than the word
else:
game.attempts_remaining -= 1
respond_to_user = 'That is not the word. Try to guess the whole word again or guess one letter at a time'
return game.to_form(respond_to_user)
else:
#game.history.append("User guessed " + request.guess + ', not a valid guess!')
raise endpoints.BadRequestException('Guess is not a letter!')
@endpoints.method(request_message=GET_GAME_REQUEST,
response_message=GameForm,
path='game/{urlsafe_game_key}',
name='get_game',
http_method='GET')
def get_game(self, request):
"""Return the current game state."""
game = get_by_urlsafe(request.urlsafe_game_key, Game)
if game:
return game.to_form('Go and make a move!')
else:
raise endpoints.NotFoundException('Could not find your game!')
@endpoints.method(request_message=GET_ALL_GAMES_REQUEST,
response_message=GameForms,
path='game/user/{user_name}',
name='get_user_games',
http_method='GET')
def get_user_games(self, request):
'''Get all active games a user is playing'''
user = User.query(User.name == request.user_name).get()
if not user:
raise endpoints.NotFoundException('''A user with that name does not exist''')
# attempt to match user and user key
qry1 = Game.query(Game.user == user.key)
games = qry1.filter(Game.game_over == False)
return GameForms(items=[game.to_form("Active Game!") for game
in games])
@endpoints.method(request_message=GET_GAME_REQUEST,
response_message=StringMessage,
path='game/{urlsafe_game_key}/history',
name='gamehistory',
http_method='GET')
def get_game_history(self, request):
'''Returns all moves made in game'''
game = get_by_urlsafe(request.urlsafe_game_key, Game)
if not game:
raise endpoints.NotFoundException('Game not found!')
return StringMessage(message=str(game.history))
@endpoints.method(response_message=ScoreForms,
path='scores',
name='get_scores',
http_method='GET')
def get_scores(self, request):
"""Return all scores"""
return ScoreForms(items=[score.to_form() for score in Score.query()])
@endpoints.method(request_message=GET_GAME_REQUEST,
response_message=StringMessage,
path='cancel/{urlsafe_game_key}',
name='cancel_game',
http_method='DELETE')
def cancel_game(self, request):
'''Cancel an active game'''
game = get_by_urlsafe(request.urlsafe_game_key, Game)
if not game:
raise endpoints.NotFoundException("We could not find your game!")
if game.game_over == False: # NOQA
game.key.delete()
return StringMessage(message="Game cancelled!")
else:
return StringMessage(message="You can't delete a completed game!")
@endpoints.method(
response_message=UserForms,
path='user_rankings',
name='get_user_rankings',
http_method='GET')
def get_user_rankings(self, request):
'''Rank users by scores'''
qry1 = User.query(User.score >= 0)
users = qry1.order(-User.score)
return UserForms(items=[user.to_form() for user in users])
@endpoints.method(request_message=HIGH_SCORES_REQUEST,
response_message=ScoreForms,
path='scores/leaderboard',
name='high_score_leaderboard',
http_method='GET')
def get_high_scores(self, request):
'''Order scores by attempts remaining for high score list'''
scores = Score.query()
scores = scores.order(-Score.attempts_remaining)
if request.number_of_results:
number = int(request.number_of_results)
scores = scores.fetch(number)
return ScoreForms(items=[score.to_form() for score in scores])
else:
return ScoreForms(items=[score.to_form() for score in scores])
api = endpoints.api_server([HangmanApi])