-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
343 lines (291 loc) · 11 KB
/
runner.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import actions
import cgi
import json
import logging
import zoo
from constants import directions
from constants import column_range
from constants import max_beats
from constants import monster_spawn_rate
from constants import ability_codes
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from util import template_path
from util import get_game
from util import get_account
from util import drop_powerup
from random import choice
from random import randint
def authorize(scope):
user = users.get_current_user()
if not user:
scope.redirect(users.create_login_url(scope.request.uri))
return None
return user
class results(webapp.RequestHandler):
path = template_path('results.html')
def get(self):
user = authorize(self)
account = get_account(user)
game = get_game(account.game_id)
if game.is_dead != 1:
self.redirect('/')
return
player = json.loads(game.player)
turns_lasted = game.turn_count
last_heartrate = player['heartrate']
last_heartbeats = player['heartbeats']
monsters = json.loads(game.monsters)
active_monsters = json.loads(game.active_monsters)
stupid = 'true' if player['stupid'] else 'false'
px = player['x']
py = player['y']
killer = None
for a in active_monsters:
m = monsters[a]
if m['x'] == px and m['y'] == py:
killer = a
break
clean_up_game(game)
self.response.out.write(
template.render(self.path, {
'heartrate': last_heartrate,
'heartbeat': last_heartbeats if last_heartbeats > 0 else 0,
'killer': killer,
'turns': turns_lasted,
'stupid': stupid
})
)
def clean_up_game(game):
# get stuff
active_monsters = json.loads(game.active_monsters)
powerups = json.loads(game.powerups)
player = json.loads(game.player)
player['heartrate'] = 50 # reset heartrate
player['heartbeats'] = max_beats # reset heartbeats
player['stupid'] = False
active_monsters = [] # reset active monsters
powerups = [] # clear all powerups
for a in player['abilities']: # reset timedown on abilities
a = 0
game.active_monsters = json.dumps(active_monsters)
game.powerups = json.dumps(powerups)
game.player = json.dumps(player)
game.is_dead = 0
game.turn_count = 0
# save stuff
game.put()
logging.info('Saved!')
class home(webapp.RequestHandler):
path = template_path('index.html')
def get(self):
self.response.out.write(
template.render(self.path, {})
)
class display_game(webapp.RequestHandler):
path = template_path('game.html')
def get(self):
user = authorize(self)
account = get_account(user)
game = get_game(account.game_id)
# check for death condition
if game.is_dead == 1:
self.redirect('/results')
tiles = []
for row in json.loads(game.tiles):
newRow = []
for cell in row:
newRow.append(directions[cell])
tiles.append(newRow)
monsters = json.loads(game.monsters)
monster_details = []
for monster in json.loads(game.active_monsters):
m = monsters[monster]
monster_details.append({
'name': monster,
'x': m['x'],
'y': m['y']
})
p = json.loads(game.player)
self.response.out.write(
template.render(self.path, {
'tiles': tiles,
'columns': column_range,
'player': p,
'abilities': p['abilities'],
'max_beats': max_beats,
'monsters': monster_details
})
)
class action(webapp.RequestHandler):
def post(self):
# authenticate user
user = authorize(self)
# get world state (from user)
game = get_game(get_account(user).game_id)
world = json.loads(game.tiles)
monsters = json.loads(game.monsters)
active_monsters = json.loads(game.active_monsters)
powerups = json.loads(game.powerups)
player = json.loads(game.player)
# establish monster positions
m_grid = []
for i in range(10):
row = []
for j in range(10):
row.append(None)
m_grid.append(row)
for a in active_monsters:
m = monsters[a]
m_grid[m['y']][m['x']] = m
# establish powerup positions and update countdown on powerups
p_grid = []
for i in range(10):
row = []
for j in range(10):
row.append(None)
p_grid.append(row)
new_powerups = []
for a in powerups:
a['t'] -= 1
if a['t']>=0: # if the powerup is still active, add to grid
new_powerups.append(a)
p_grid[a['y']][a['x']] = a['type']
powerups = new_powerups
# get action params from POST
params = json.loads(self.request.body)
# FIRST update allowed actions (i.e. a turn has passed for them) if they used an actual ability
# get action key
action_key = params['action']
action_code = ability_codes[action_key] if action_key in ability_codes else None
if action_code is not None:
for i in range(len(player['abilities'])):
if player['abilities'][i] > 0:
player['abilities'][i] -= 1
player['abilities'][action_code] = 2
# update countdown on powerups
# THEN perform new action && make it invalid
# calculate action on the world
changes = actions.perform[action_key](world, params)
changes['is_dead'] = False
# loop through changes and apply
for change in changes.keys():
if change == 'world':
for tile in changes[change]:
world[tile['y']][tile['x']] = tile['direction']
tile['direction'] = directions[
tile['direction']
]
elif change == 'monsters':
continue #Futureproofing
elif change == 'player':
continue #Futureproofing
# move player & pick up any powerups
player_changes = zoo.move_player(world, player, m_grid)
player = player_changes['player']
if player_changes['died']:
game.is_dead = 1
changes['is_dead'] = True
player['stupid'] = True
# powerups effect
new_powerups = []
for pu in powerups:
if (pu['x'] == player['x'] and pu['y'] == player['y']): # if there is a powerup on the square
p_grid[pu['y']][pu['x']] = None # pick it up
# blanket
if pu['type'] == 'blanket':
# loop through active monsts and remove close ones
safe_monsters = []
for m in active_monsters:
if abs(monsters[m]['x'] - pu['x']) > 2 or abs(monsters[m]['y'] - pu['y']) > 2:
safe_monsters.append(m)
active_monsters = safe_monsters
# candy
elif pu['type'] == 'candy':
# move player again
player_changes = zoo.move_player(world, player, m_grid)
player = player_changes['player']
if player_changes['died']:
game.is_dead = 1
changes['is_dead'] = True
player['stupid'] = True
else:
new_powerups.append(pu)
powerups = new_powerups
# move monsters
monster_changes = zoo.move_monsters(
world,
monsters,
player,
m_grid,
active_monsters,
p_grid,
powerups)
monsters = monster_changes['monsters']
m_grid = monster_changes['m_grid']
world = monster_changes['world']
m_change = monster_changes['changes']
p_grid = monster_changes['p_grid']
powerups = monster_changes['powerups']
# add tile changes caused by monsters
changes['world'].extend(m_change['world'])
changes['is_dead'] = changes['is_dead'] or m_change['is_dead']
player = m_change['player']
# board updates!
# tile randomising (non-vital)
# monster spawning
if game.turn_count % monster_spawn_rate == 0:
spawn_results = zoo.spawn_monster(
monsters,
active_monsters,
player,
m_grid,
powerups,
p_grid)
monsters = spawn_results['monsters']
active_monsters = spawn_results['active_monsters']
p_grid = monster_changes['p_grid']
powerups = monster_changes['powerups']
# powerup drop
powerup_changes = drop_powerup(game.drop_chance, powerups, m_grid, player, p_grid)
powerups = powerup_changes['powerups']
game.drop_chance = powerup_changes['drop_chance']
# not updating p_grid because not used again
# check death conditions
# no more heartbeats
if player['heartbeats'] < 1 or player['heartrate'] > 199 or changes['is_dead']:
game.is_dead = 1
changes['is_dead'] = True
# save the changed world
game.tiles = json.dumps(world)
# save the new monster positions
game.monsters = json.dumps(monsters)
game.active_monsters = json.dumps(active_monsters)
# save the new powerup positions
game.powerups = json.dumps(powerups)
# save the updated health
game.player = json.dumps(player)
game.turn_count+=1
game.put()
monster_details = {}
for monster in active_monsters:
monster_details[monster] = monsters[monster]
changes['monsters'] = monster_details
changes['player'] = player
changes['powerups'] = powerups
# response: send changes!
self.response.headers['Content-Type'] = "application/json"
self.response.out.write(json.dumps(changes))
urls = [
('/', home),
('/game', display_game),
('/action', action),
('/results', results)
]
app = webapp.WSGIApplication(urls, debug=True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()