/
main.py
230 lines (187 loc) · 7.47 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
# -*- coding: cp1252 -*-
#
# Copyright (c) 2008 Andreas Blixt <andreas@blixt.org>
# Project homepage: <http://code.google.com/p/monkey-web/>
#
# 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
import monkey, re, util
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_id):
"""Adds the CPU player to a game.
"""
game = monkey.Game.get_by_id(game_id)
if not game: raise ValueError('Invalid game id.')
cpu = monkey.CpuPlayer()
cpu.player.join(game)
return self.status(game_id)
def change_nickname(self, nickname):
player = monkey.Player.get_current(self)
player.rename(nickname)
return self.get_player_info()
def create(self, rule_set_id):
"""Creates a new game.
"""
rule_set = monkey.RuleSet.get_by_id(rule_set_id)
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 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 join(self, game_id):
"""Joins an existing game.
"""
game = monkey.Game.get_by_id(game_id)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
player.join(game)
return self.status(game_id)
def leave(self, game_id):
"""Leaves an existing game.
"""
game = monkey.Game.get_by_id(game_id)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
player.leave(game)
def list(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 = filter(lambda g: pkey not in g.players, 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.')
games = []
for game in results:
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 move(self, game_id, x, y):
"""Places a tile on the board of the specified game.
"""
game = monkey.Game.get_by_id(game_id)
if not game: raise ValueError('Invalid game id.')
player = monkey.Player.get_current(self)
game.move(player, x, y)
return self.status(game_id)
def new_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 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 status(self, game_id, turn = None):
"""Gets the status of game.
"""
game = monkey.Game.get_by_id(game_id)
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
return {
'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() }
class HomePage(util.ExtendedHandler):
def get(self):
self.template('home.html')
def main():
application = webapp.WSGIApplication([
('/game/.*', GameService)
])
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()