forked from brockuniera/PrestonTests2016
-
Notifications
You must be signed in to change notification settings - Fork 1
/
texttojson.py
83 lines (72 loc) · 2.87 KB
/
texttojson.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
# Prints named text file in arg1 to stdout as a json file
import sys
import json
def eprint(*args, **kwargs):
"""Print to stderr"""
print(*args, file=sys.stderr, **kwargs)
def prettydumps(obj):
return json.dumps(obj, indent=4)
def toints(line):
return (int(i) for i in line.split(" "))
def createconfig(phaseName, subjectName, playerMoveSpeed, objTriggerRadius, actionKey, scenes, pauseTime, lookSlerpTime):
return {
"scenes" : list(scenes),
"phaseName": phaseName,
"subjectName": subjectName,
"playerMoveSpeed": playerMoveSpeed,
"objTriggerRadius": objTriggerRadius,
"actionKey": actionKey,
"pauseTime": pauseTime,
"lookSlerpTime": lookSlerpTime,
}
def createcreateconfig(phaseName, subjectName, playerMoveSpeed, objTriggerRadius, pauseTime, lookSlerpTime, actionKey):
"""Returns a function that only needs a list of scenes to create the config"""
def f(scenes):
return createconfig(phaseName, subjectName, playerMoveSpeed, objTriggerRadius, actionKey, scenes, pauseTime, lookSlerpTime)
return f
def parsenormal(lines, infolines, configfunc):
def gen():
for info in map(list, zip(*map(toints, lines[7:17+1]))): # Grabbing ea column in text file
yield {
"mode": "normal",
"objShowIndex" : info[0],
"showTime" : info[1],
"greyScreenTime" : info[2],
"greyScreenTimeTwo" : info[3],
"envIndex" : info[4],
"envTime" : info[5],
"envTimeTwo" : info[6],
"objSpawnIndex" : info[7],
"showObjAlways" : info[8] == 1, # Show obj only if value is 1
"playerSpawnIndex" : info[9],
"landmarkSpawnIndex" : info[10],
}
return prettydumps(configfunc(list(gen())))
def parseexplore(alllines, infolines, configfunc):
return prettydumps(
configfunc([{
"mode": "explore",
"objShowIndex" : -1,
"showTime" : -1,
"greyScreenTime" : -1,
"greyScreenTimeTwo" : -1,
"envIndex" : infolines[0],
"envTime" : infolines[2],
"envTimeTwo" : -1,
"objSpawnIndex" : -1,
"showObjAlways" : False,
"playerSpawnIndex" : infolines[1],
"landmarkSpawnIndex" : -1,
}])
)
if __name__ == "__main__":
def err(lines):
eprint("'{}' is not a recognized mode".format(lines[0]))
handlers = {
"normal" : parsenormal,
"explore" : parseexplore,
}
filename = sys.argv[1]
with open(filename, "r") as f:
lines = [l.rstrip() for l in f.readlines()]
print(handlers.get(lines[0], err)(lines, lines[7:], createcreateconfig(*lines[0:7])))