/
models.py
513 lines (407 loc) · 17.6 KB
/
models.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import webapp2
import random
import csv
import json
import math
from collections import deque
from google.appengine.ext import db
from google.appengine.api import users
from basegame.models import *
from profile.models import *
class TerritoryMapper():
def __init__(self):
self.territory_indexes = {}
self.territory_labels = {}
self.territory_connections = {}
self.territory_continent = {}
self.continent_total = {}
self.continent_bonus = {}
index_mapping_data = csv.DictReader(
open("data/indexmapping.csv")
)
for territory in index_mapping_data:
index = int(territory["index"])-1
continent = int(territory["continent"])-1
label = territory["label"].lower().replace(" ", "-")
continentTotal = int(territory["continentTotal"])
continentBonus = int(territory["continentBonus"])
self.territory_indexes[label] = index
self.territory_labels[index] = label
#map territories to continents
self.territory_continent[index] = continent
#map continent to total number if teritories in it
self.continent_total[continent] = continentTotal
#map continent to its bonus
self.continent_bonus[continent] = continentBonus
border_data_json = open('data/borders.json')
border_data = json.load(border_data_json)
border_data_json.close()
for border in border_data:
self.territory_connections[border["id"]] = border["borders"]
def get_territory_index(self, label):
return self.territory_indexes[label]
def get_territory_label(self, index):
return self.territory_labels[index]
def get_size(self):
return len(self.territory_indexes)
def is_connected(self, a, b):
return b in self.territory_connections[a]
def get_territory_continent(self, territory):
return self.territory_continent[territory]
def get_continent_total(self, continent):
return self.continent_total[continent]
def get_continent_bonus(self, continent):
return self.continent_bonus[continent]
def get_territory_mapper():
app = webapp2.get_app()
territory_mapper = app.registry.get('conquest.territory_mapper')
if not territory_mapper:
territory_mapper = TerritoryMapper()
app.registry['conquest.territory_mapper'] = territory_mapper
return territory_mapper
def attacking_phase_roll_dice(attack_armies, defend_armies):
attack_dice = [random.randint(1, 6) for i in range(attack_armies)]
attack_dice.sort()
attack_dice.reverse()
defend_dice = [random.randint(1, 6) for i in range(defend_armies)]
defend_dice.sort()
defend_dice.reverse()
return [attack_dice, defend_dice]
class ConquestGameState(BaseGameState):
possible_states = BaseGameState.possible_states | set([
'place','reinforce','attack','attack_victory','fortify'])
# State required here for possible_states support
state = db.StringProperty(choices=possible_states, default='init')
territory_units = db.ListProperty(int)
territory_player = db.ListProperty(int)
attack_victory_origin = db.IntegerProperty()
attack_victory_destination = db.IntegerProperty()
users_placed = db.IntegerProperty()
def get_info_dict(self, target=None):
""" Fill info about state into a dict. """
if target is None:
target = dict()
BaseGameState.get_info_dict(self, target)
territory_mapper = get_territory_mapper()
territories = []
for i in range(len(self.territory_units)):
label = territory_mapper.get_territory_label(i)
data = {'id':label,
'index':i,
'player':self.territory_player[i],
'units':self.territory_units[i]
}
territories.append(data)
target['territories'] = territories
return target
def setup(self, players):
""" Setup the initial state. """
self.check_state('init')
for x in range(42):
self.territory_player.append(0)
player_territories_owned = [0] * self.total_players
player = 0
territories = range(42)
random.shuffle(territories)
for territory in territories:
self.territory_player[territory] = player
player_territories_owned[player] += 1
player = (player + 1) % self.total_players
self.territory_units.append(1)
for player in players:
player.unit_pool = 50 - 5 * self.total_players - player_territories_owned[player.play_index]
self.users_placed = 0
self.state = 'place'
def give_units(self, player):
""" Give units to player based on teritories owned"""
territories_owned = 0
player.unit_pool = 0
for territory in self.territory_player:
if territory == player.play_index:
territories_owned += 1
if territories_owned < 9:
units_given = 3
else:
units_given = math.floor(territories_owned/3)
#Give units to player based on wholy owned continents
territory_mapper = get_territory_mapper()
self.continent_noOfPlayerOwnedTerritories = [0]*6
for territory, playerID in enumerate(self.territory_player):
if playerID == player.play_index:
continent = territory_mapper.get_territory_continent(territory)
self.continent_noOfPlayerOwnedTerritories[continent] += 1
continent_units_given = 0
for continent, numberOwned in enumerate(self.continent_noOfPlayerOwnedTerritories):
if numberOwned == territory_mapper.get_continent_total(continent):
continent_units_given += territory_mapper.get_continent_bonus(continent)
continent_units_given += 1
player.unit_pool += int(units_given)
player.unit_pool += int(continent_units_given)
player.put()
def place_action(self, request):
""" Place the units at the requested territory """
self.check_state('place')
self.is_current_player()
placements = request['placements']
territory_mapper = get_territory_mapper()
player_data = self.get_current_player_data()
action = PlaceAction(parent = self)
self.add_action(action)
total_units_placed = 0
for i in range(territory_mapper.get_size()):
action.placed_units.append(0)
for placement in placements:
placement_index = territory_mapper.get_territory_index(placement['id'])
units = placement['units']
total_units_placed += units
action.placed_units[placement_index] = units
self.territory_units[placement_index] = units
self.territory_player[placement_index] = self.current_player_index
if total_units_placed > player_data.unit_pool:
raise InvalidActionParametersException()
player_data.unit_pool = 0;
self.users_placed += 1
self.end_turn()
if self.users_placed == self.total_players:
action.new_state = 'reinforce'
self.state = 'reinforce'
self.give_units(self.get_current_player_data())
player_data.put()
return action
def reinforce_action(self, request):
""" Add the units to the territory """
self.check_state('reinforce')
self.is_current_player()
placements = request['placements']
territory_mapper = get_territory_mapper()
player_data = self.get_current_player_data()
action = PlaceAction(parent = self)
self.add_action(action)
total_units_placed = 0
for i in range(territory_mapper.get_size()):
action.placed_units.append(0)
for placement in placements:
placement_index = territory_mapper.get_territory_index(placement['id'])
units = placement['units']
total_units_placed += units
action.placed_units[placement_index] = units
if self.territory_player[placement_index] != self.current_player_index:
raise InvalidActionParametersException()
self.territory_units[placement_index] += units
if total_units_placed > player_data.unit_pool:
raise InvalidActionParametersException()
player_data.unit_pool -= total_units_placed;
action.new_state = 'attack'
self.state = 'attack'
player_data.put()
return action
def attack_action(self, request):
""" Atack a territory """
self.check_state('attack')
self.is_current_player()
territory_mapper = get_territory_mapper()
origin = territory_mapper.get_territory_index(request['origin'])
destination = territory_mapper.get_territory_index(request['destination'])
attackers = int(request['attackers'])
if not territory_mapper.is_connected(origin, destination):
raise InvalidActionParametersException()
if self.territory_player[origin] != self.current_player_index:
raise InvalidActionParametersException()
if self.territory_player[destination] == self.current_player_index:
raise InvalidActionParametersException()
possible_attackers = min(self.territory_units[origin] - 1, 3)
possible_defenders = min(self.territory_units[destination], 2)
if attackers > possible_attackers:
raise InvalidActionParametersException
defenders = min(possible_defenders, attackers)
attack_rolls, defend_rolls = attacking_phase_roll_dice(attackers, defenders)
attack_dice = deque(attack_rolls)
defend_dice = deque(defend_rolls)
win_rolls = 0
loose_rolls = 0
while len(attack_dice) > 0 and len(defend_dice) > 0:
if attack_dice.popleft() > defend_dice.popleft():
win_rolls += 1
else:
loose_rolls += 1
self.territory_units[origin] -= loose_rolls
self.territory_units[destination] -= win_rolls
action = AttackAction(parent = self)
self.add_action(action)
if self.territory_units[destination] <= 0:
old_owner = self.territory_player[destination]
self.territory_player[destination] = self.current_player_index
self.attack_victory_origin = origin
self.attack_victory_destination = destination
action.new_state = 'attack_victory'
self.state = 'attack_victory'
remaining_territories = 0
for owner in self.territory_player:
if owner == old_owner:
remaining_territories += 1
if remaining_territories == 0:
old_owner_data = self.basegameplayer_set.filter('play_index', old_owner).get()
old_owner_data.eliminated = True
old_owner_data.put()
if self.basegameplayer_set.filter('eliminate =', True).count(2) <= 1:
action.new_state = 'finished'
self.state = 'finished'
action.origin = origin
action.destination = destination
action.attackers = attackers
action.attack_rolls = attack_rolls
action.defend_rolls = defend_rolls
action.win_rolls = win_rolls
action.loose_rolls = loose_rolls
return action
def attack_victory_action(self, request):
""" Move after win attack """
self.check_state('attack_victory')
self.is_current_player()
units = int(request['units'])
if units < 1:
raise InvalidActionParametersException
action = MoveAction(parent = self)
self.add_action(action)
action.origin = self.attack_victory_origin
action.destination = self.attack_victory_destination
self.territory_units[action.origin] -= units
self.territory_units[action.destination] = units
action.units = units
action.new_state = 'attack'
self.state = 'attack'
return action
def end_attack_action(self, request):
""" End attack move """
self.check_state('attack')
self.is_current_player()
action = StateChangeAction(parent = self)
self.add_action(action)
action.new_state = 'fortify'
self.state = 'fortify'
return action
def move_action(self, request):
""" Move units from one territory to another """
self.check_state('fortify')
self.is_current_player()
territory_mapper = get_territory_mapper()
origin = territory_mapper.get_territory_index(request['origin'])
destination = territory_mapper.get_territory_index(request['destination'])
units = int(request['units'])
if not territory_mapper.is_connected(origin, destination):
raise InvalidActionParametersException()
if self.territory_player[origin] != self.current_player_index:
raise InvalidActionParametersException
if self.territory_player[destination] != self.current_player_index:
raise InvalidActionParametersException
units_at_origin = self.territory_units[origin]
if units >= units_at_origin:
raise InvalidActionParametersException
self.territory_units[origin] -= units
self.territory_units[destination] += units
action = MoveAction(parent = self)
self.add_action(action)
action.origin = origin
action.destination = destination
action.units = units
if self.end_turn():
action.new_state = 'reinforce'
self.state = 'reinforce'
self.give_units(self.get_current_player_data())
else:
action.new_state = 'finished'
self.state = 'finished'
return action
def end_move_action(self, request):
""" End attack move """
self.check_state('fortify')
self.is_current_player()
action = StateChangeAction(parent = self)
self.add_action(action)
if self.end_turn():
action.new_state = 'reinforce'
self.state = 'reinforce'
self.give_units(self.get_current_player_data())
else:
action.new_state = 'finished'
self.state = 'finished'
return action
class ConquestGamePlayer(BaseGamePlayer):
unit_pool = db.IntegerProperty()
def get_info_dict(self, target=None):
""" Fill info about player into a dict. """
if target is None:
target = dict()
BaseGamePlayer.get_info_dict(self, target)
target['unit_pool'] = self.unit_pool
return target
class ConquestGameInstance(BaseGameInstance):
game_name = 'conquest'
human_name = 'Conquest'
description = """
Battle your enemies and bring your foes to their knees in this
exhilarating turn based conquest for world domination
"""
info_redirect = "conquestgameinfo"
play_redirect = "conquestgameplay"
min_players = 2
max_players = 6
game_player_type = ConquestGamePlayer
game_state_type = ConquestGameState
class PlaceAction(BaseGameAction):
placed_units = db.ListProperty(int)
action_type = 'place'
def get_info_dict(self, target=None):
""" Fill info about state into a dict """
if target is None:
target = {}
BaseGameAction.get_info_dict(self, target)
territory_mapper = get_territory_mapper()
placements = []
for i,placement in enumerate(self.placed_units):
if placement > 0:
label = territory_mapper.get_territory_label(i)
data = {'id':label,
'units':placement
}
placements.append(data)
target['placed_units'] = placements
return target
class AttackAction(BaseGameAction):
origin = db.IntegerProperty()
destination = db.IntegerProperty()
attackers = db.IntegerProperty()
attack_rolls = db.ListProperty(int)
defend_rolls = db.ListProperty(int)
win_rolls = db.IntegerProperty()
loose_rolls = db.IntegerProperty()
action_type = 'attack'
def get_info_dict(self, target=None):
""" Fill info about state into a dict """
if target is None:
target = {}
BaseGameAction.get_info_dict(self, target)
territory_mapper = get_territory_mapper()
target['origin'] = territory_mapper.get_territory_label(self.origin)
target['destination'] = territory_mapper.get_territory_label(self.destination)
target['attackers'] = self.attackers
target['attack_rolls'] = self.attack_rolls
target['defend_rolls'] = self.defend_rolls
target['win_rolls'] = self.win_rolls
target['loose_rolls'] = self.loose_rolls
return target
class MoveAction(BaseGameAction):
origin = db.IntegerProperty()
destination = db.IntegerProperty()
units = db.IntegerProperty()
action_type = 'move'
def get_info_dict(self, target=None):
""" Fill info about state into a dict """
if target is None:
target = {}
BaseGameAction.get_info_dict(self, target)
territory_mapper = get_territory_mapper()
target['origin'] = territory_mapper.get_territory_label(self.origin)
target['destination'] = territory_mapper.get_territory_label(self.destination)
target['units'] = self.units
return target