forked from blixt/monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
283 lines (230 loc) · 9.48 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: cp1252 -*-
#
# Copyright (c) 2008-2010 Andreas Blixt <[email protected]>
# Project homepage: <http://github.com/blixt/monkey>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Google App Engine entry point for the MoNKey! game.
Registers the WSGI web application with request handlers, also defined
in this file.
"""
from google.appengine.api import users
from google.appengine.ext import db, webapp
import wsgiref.handlers
from datetime import datetime, timedelta
import monkey, re, util
class Error(Exception):
"""Base of all exceptions in the MoNKey! game interface."""
pass
class GameService(util.ServiceHandler):
"""Methods that can be called through HTTP (intended to be called by
JavaScript through an XmlHttpRequest object.)
"""
def add_cpu_player(self, game):
"""Adds a CPU player to a game.
"""
if not isinstance(game, monkey.Game):
game = monkey.Game.get_by_id(game)
if not game:
raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
if not player.key() in game.players:
raise Error('You cannot add a CPU player to a game you\'re not in.')
cpu = monkey.CpuPlayer()
cpu.join(game)
return self.get_game_status(game)
def change_nickname(self, nickname):
"""Changes the nickname of the current player.
"""
player = monkey.Player.get_current(self)
player.rename(nickname)
return self.get_player_info()
def cpu_battle(self, rule_set):
"""Creates a new game with only CPU players.
"""
if not isinstance(rule_set, monkey.RuleSet):
rule_set = monkey.RuleSet.get_by_id(rule_set)
if not rule_set: raise ValueError('Invalid rule set id.')
game = monkey.Game(rule_set = rule_set)
game.put()
for i in xrange(rule_set.num_players):
cpu = monkey.CpuPlayer()
cpu.join(game)
return game.key().id()
def create_game(self, rule_set):
"""Creates a new game.
"""
if not isinstance(rule_set, monkey.RuleSet):
rule_set = monkey.RuleSet.get_by_id(rule_set)
if not rule_set: raise ValueError('Invalid rule set id.')
player = monkey.Player.get_current(self)
game = monkey.Game(rule_set = rule_set)
game.put()
player.join(game)
return game.key().id()
def create_rule_set(self, name, m, n, k, p = 1, q = 1, num_players = 2):
"""Creates a new rule set.
"""
if not re.match('^[\\w]([\\w&\'\\- ]{0,28}[\\w\'!])$', name):
raise ValueError('Invalid name.')
rule_set = monkey.RuleSet(name = name,
author = monkey.Player.get_current(self),
num_players = num_players,
m = m, n = n, k = k,
p = p, q = q)
rule_set.put()
return rule_set.key().id()
def get_game_status(self, game, turn = None):
"""Gets the status of game.
"""
if not isinstance(game, monkey.Game):
game = monkey.Game.get_by_id(game)
if not game: raise ValueError('Invalid game id.')
if turn != None and game.turn == turn: return False
pkey = monkey.Player.get_current(self).key()
if pkey in game.players:
playing_as = game.players.index(pkey) + 1
else:
playing_as = 0
status = {
'players': game.player_names,
'board': game.unpack_board(),
'playing_as': playing_as,
'current_player': game.current_player,
'state': game.state,
'turn': game.turn,
'rule_set_id': game.rule_set.key().id() }
game.handle_cpu()
return status
def get_games(self, mode = 'play'):
"""Returns a list of games relevant to the current player.
Modes:
play - Returns games that the player is playing or can join.
view - Returns games that other players are playing.
past - Returns recent games that the player has played.
"""
pkey = monkey.Player.get_current(self).key()
if mode == 'play':
playing = monkey.Game.all()
playing.filter('state =', 'playing')
playing.filter('players =', pkey)
playing.order('-last_update')
waiting = monkey.Game.all()
waiting.filter('state =', 'waiting')
waiting.order('-last_update')
results = list(playing) + waiting.fetch(10)
elif mode == 'view':
playing = monkey.Game.all()
playing.filter('state =', 'playing')
playing.order('-last_update')
results = playing.fetch(10)
elif mode == 'past':
history = monkey.Game.gql('WHERE state IN :1 AND '
'players = :2 ORDER BY last_update DESC',
['aborted', 'win', 'draw'], pkey)
results = history.fetch(10)
else:
raise ValueError('Invalid mode.')
now = datetime.utcnow()
games = []
for game in results:
# Don't include games that can be considered abandoned.
# - Games that are waiting for players are considered abandoned
# after six hours.
# - Games that are in play are considered abandoned after 48 hours.
age = now - game.last_update
age = age.seconds / 3600.0 + age.days * 24.0
if ((game.state == 'waiting' and age > 6) or
(game.state == 'playing' and age > 48)):
# Abort the game to speed up future queries.
game.abort()
# break instead of continue so that only one game is aborted per
# request (spreads out load between requests if many games have
# timed out since last request.)
break
# Determine the position of the player if the player is in the game.
if pkey in game.players:
playing_as = game.players.index(pkey) + 1
else:
playing_as = 0
games.append({
'id': game.key().id(),
'players': game.player_names,
'current_player': game.current_player,
'playing_as': playing_as,
'rule_set_id': game.rule_set.key().id(),
'state': game.state })
return games
def get_player_info(self):
"""Gets information about the currently logged in player.
"""
user = users.get_current_user()
if user:
log_url = users.create_logout_url('/')
else:
log_url = users.create_login_url('/')
player = monkey.Player.get_current(self)
return { 'nickname': player.nickname,
'anonymous': player.is_anonymous(),
'log_url': log_url,
'wins': player.wins,
'losses': player.losses,
'draws': player.draws }
def get_rule_sets(self):
"""Gets all rule sets.
"""
rule_sets = []
for rule_set in monkey.RuleSet.all().order('name'):
rule_sets.append({ 'id': rule_set.key().id(),
'name': rule_set.name,
'num_games': rule_set.num_games,
'num_players': rule_set.num_players,
'exact': rule_set.exact,
'm': rule_set.m, 'n': rule_set.n,
'k': rule_set.k, 'p': rule_set.p,
'q': rule_set.q })
return rule_sets
def join_game(self, game):
"""Joins an existing game.
"""
if not isinstance(game, monkey.Game):
game = monkey.Game.get_by_id(game)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
player.join(game)
return self.get_game_status(game)
def leave_game(self, game):
"""Leaves an existing game.
"""
if not isinstance(game, monkey.Game):
game = monkey.Game.get_by_id(game)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
player.leave(game)
def put_tile(self, game, x, y):
"""Places a tile on the board of the specified game.
"""
if not isinstance(game, monkey.Game):
game = monkey.Game.get_by_id(game)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
game.move(player, x, y)
return self.get_game_status(game)
def main():
application = webapp.WSGIApplication([
('/game/(\\w*)', GameService)
])
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()