-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTimeline.py
58 lines (39 loc) · 1.86 KB
/
GameTimeline.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
from __future__ import print_function
import json
class GameTimeline(object):
def __init__(self, filepath="GameTimeline.json"):
self.filepath = filepath
self.events = []
def appendStateActionPair(self, state, action):
self.events.append({"state": state, "action": action})
def export(self):
serializedEvents = json.dumps(self.events, sort_keys=True)
print("exported " + str(len(self.events)), " state - action pairs")
self.events[:] = []
filepath = self.filepath
with open(filepath, 'w') as f:
print(serializedEvents, file=f)
# #######################################
# # Example Usage
# #######################################
# #######################################
# # Initialization.
# #######################################
# gameTimeline = GameTimeline()
# #######################################
# # Append state - action pairs for the whole simulation.
# #######################################
# state = {
# "treasures": [{"id": "SomeEntity", "pos": {"x": 2, "y": 4}}, {"id": "SomeEntity", "pos": {"x": 4, "y": 4}}],
# "traps": [{"id": "SomeEntity", "pos": {"x": 1, "y": 5}}, {"id": "SomeEntity", "pos": {"x": 2, "y": 5}}],
# "allies": [{"id": "SomeEntity", "pos": {"x": 3, "y": 4}}, {"id": "SomeEntity", "pos": {"x": 5, "y": 7}}],
# "enemies": [{"id": "SomeEntity", "pos": {"x": 1, "y": 6}}, {"id": "SomeEntity", "pos": {"x": 3, "y": 6}}]
# }
# gameTimeline.appendStateActionPair(state, {"text": "something happened", "pos": {"x": 2, "y": 4}})
# state["treasures"][0]["pos"]["x"] = 1
# gameTimeline.appendStateActionPair(state, {"text": "something happened", "pos": {"x": 1, "y": 4}})
# #######################################
# # Export everything at the end of the simulation.
# # The timeline is cleaned after export.
# #######################################
# gameTimeline.export()