-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONDumper.py
85 lines (75 loc) · 3.04 KB
/
JSONDumper.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
import json
# JSON Format:
# root: dictionary of map and turn data
# map: 2D([row][col]) list of integer
# turn data: list (index = turn no) of turn details
# turn details: dict(), consists of unitData, baseData and deadData
# unitData: list of unit details
# unitDetails: [row, col, owner ID]
# baseData: list of hive details
# hiveDetails: [row, col, owner ID] (-1 for no owner)
# deadData: list of dead details, positions where units died in this turn
# deadDetails: [row, col, owner ID] (100 for multiple owners' units death)
# hiveScore: list 0..playernum (int) of no. of hives they control
# unitScore: list 0..playernum (int) of no. of hives they control
# winner: [int playerId, string winningReason] denoting victor and reason behind victory. PlayerId = -1 if tied
#
# example:
# {
# map: [[0,0,0,0][0,1,1,0][9,0,0,9]]
# turnData:
# [
# {
# unitData: [[0,0,1], [2,2,0], [4,0,0]]
# baseData: [[5,5,0], [10,5,1], [9,9,-1]]
# deadData: [[7,7,0], [7,8,1]]
# hiveScore: [1,1]
# unitScore: [2,1]
# },
# {
# unitData: [[0,1,1], [2,3,1], [4,1,0]]
# baseData: [[5,5,0], [10,5,1], [9,9,0]]
# deadData: []
# hiveScore: [0,1]
# unitScore: [1,2]
# },
# ]
# winner: [0, "by Wipeout"]
# }
class JSONDumper:
def __init__(self):
self.data = dict()
def InitializeMap(self, terrainMap):
self.data["map"] = terrainMap
self.turnData = []
def Update(self, unitDictionary, hiveList, gridDeathPos, hiveScore, unitScore):
newUnitList = []
for key in unitDictionary.keys():
pid = unitDictionary[key].GetPlayerID()
row = unitDictionary[key].GetRow()
col = unitDictionary[key].GetCol()
newUnitList.append([row,col,pid])
newHiveList = []
for hive in hiveList:
pid = hive.GetPlayerID()
row = hive.GetRow()
col = hive.GetCol()
newHiveList.append([row,col,pid])
newDeadList = []
for r in range(len(gridDeathPos)):
for c in range(len(gridDeathPos[r])):
if (gridDeathPos[r][c] != -1):
pid = gridDeathPos[r][c]
newDeadList.append([r, c, pid])
newTurnData = dict()
newTurnData["unitData"] = newUnitList
newTurnData["baseData"] = newHiveList
newTurnData["deadData"] = newDeadList
newTurnData["hiveScore"] = hiveScore
newTurnData["unitScore"] = unitScore
self.turnData.append(newTurnData)
def SetWinner(self, playerID, winReason):
self.data["winnerData"] = [playerID, winReason]
def GetDump(self):
self.data["turnData"] = self.turnData
return json.dumps(self.data)