-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
216 lines (189 loc) · 8.72 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
# -*- coding: utf-8 -*-`
"""api.py - Create and configure the Game API exposing the resources.
This can also contain game logic. For more complex games it would be wise to
move game logic to another file. Ideally the API will be simple, concerned
primarily with communication to/from the API's users."""
import endpoints
from protorpc import remote, messages
from google.appengine.api import taskqueue
from models import User, Game
from models import StringMessage, NewGameForm, GameForm, MakeMoveForm,\
GamesForm, UserRankingsForm, GameHistoryForm
from utils import get_by_urlsafe
import kalah
NEW_GAME_REQUEST = endpoints.ResourceContainer(NewGameForm)
GET_GAME_REQUEST = endpoints.ResourceContainer(
urlsafe_game_key=messages.StringField(1),)
GAME_HISTORY_REQUEST = endpoints.ResourceContainer(
urlsafe_game_key=messages.StringField(1),
verbose=messages.BooleanField(2, default=False))
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),
active_only=messages.BooleanField(3, default=True))
@endpoints.api(name='kalah', version='v1')
class KalahApi(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"""
north_user = self.get_user_or_error(request.north_user_name)
south_user = self.get_user_or_error(request.south_user_name)
game = Game.new_game(north_user.key, south_user.key)
return game.to_form('Good luck playing Kalah!')
@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:
if game.active:
return game.to_form('Time to make a move!')
else:
return game.to_form('')
else:
raise endpoints.NotFoundException('Game not found!')
@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)
# Check if game is finished or canceled.
if game.game_over:
return game.to_form('Game already over.')
if game.canceled:
return game.to_form('Cannot move because Game has been canceled.')
# Check player exists
moving_user = self.get_user_or_error(request.user_name)
moving_user_id = moving_user.key.id()
# Check player a participant in game
if moving_user_id != game.north_user.id():
if moving_user_id != game.south_user.id():
return game.to_form('Player not a participant in this game.')
# Check the move is made by the player who's turn it is
if game.game_state[0] == 'N':
legitimate_moving_user_id = game.north_user.id()
else:
legitimate_moving_user_id = game.south_user.id()
if moving_user_id != legitimate_moving_user_id:
return game.to_form('Player moved out of turn.')
# Try making the move, return error message if invalid
try:
game = game.move(request.house)
except ValueError:
return game.to_form('Invalid move.')
# Check if the game is over, and create appropriate message
msg = ''
if game.game_over:
msg = 'Game over! '
if game.north_final_score > game.south_final_score:
winner_name = game.north_user.get().name
msg += '{} wins!'.format(winner_name)
elif game.south_final_score > game.north_final_score:
winner_name = game.south_user.get().name
msg += '{} wins!'.format(winner_name)
else:
msg += "Draw!"
else: # If the game isn't over
# send a reminder to the next player
taskqueue.add(url='/tasks/send_reminder',
params={'urlsafe_key': game.key.urlsafe()})
# Create an appropriate message
msg_params = (("North", 7, 12) if game.game_state[0] == 'N'
else ("South", 0, 5))
msg = "{} player's turn. Enter an integer between {} and {}."
msg = msg.format(*msg_params)
return game.to_form(msg)
def get_user_or_error(self, user_name):
"""Get user with given user name or raise API error"""
user = User.query(User.name == user_name).get()
if not user:
raise endpoints.NotFoundException(
'A User with the name {} does not exist!'.format(
user_name))
return user
# = = = Task 3: Extend Your API = = = = = = = = =
@endpoints.method(request_message=USER_REQUEST,
response_message=GamesForm,
path='user/games',
name='get_user_games',
http_method='GET')
def get_user_games(self, request):
"""Get a user's active games."""
user = self.get_user_or_error(request.user_name)
games = user.get_games(active_only=request.active_only)
return GamesForm(games=[game.to_form() for game in games])
@endpoints.method(request_message=GET_GAME_REQUEST,
response_message=StringMessage,
path='game/{urlsafe_game_key}/cancel',
name='cancel_game',
http_method='PUT')
def cancel_game(self, request):
"""Cancels the specified game, returning an error
if the game is already over or canceled."""
game = get_by_urlsafe(request.urlsafe_game_key, Game)
if game:
try:
game.cancel()
return StringMessage(message='Game successfully canceled.')
except AttributeError as e:
raise endpoints.ForbiddenException(e.message)
else:
raise endpoints.NotFoundException('Game not found!')
@endpoints.method(response_message=UserRankingsForm,
path='rankings',
name='get_user_rankings',
http_method='GET')
def get_user_rankings(self, request):
"""Retrieve user rankings, according to win:loss ratio,
with ties broken by greatest number of draws."""
return User.rankings()
@endpoints.method(request_message=GAME_HISTORY_REQUEST,
response_message=GameHistoryForm,
path='history/{urlsafe_game_key}',
name='get_game_history',
http_method='GET')
def get_game_history(self, request):
"""Retrieve move history for a particular Game."""
game = get_by_urlsafe(request.urlsafe_game_key, Game)
if game:
return game.to_history_form(request.verbose)
else:
raise endpoints.NotFoundException('Game not found!')
# = = = Extra endpoints in response to comments = = = = = = = = =
@endpoints.method(response_message=GamesForm,
path='games/completed',
name='get_completed_games',
http_method='GET')
def get_completed_games(self, request):
"""Retrieve all completed games."""
games = Game.query(Game.game_over == True).fetch()
return GamesForm(games=[game.to_form() for game in games])
api = endpoints.api_server([KalahApi])