-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics-parser.py
executable file
·193 lines (179 loc) · 4.98 KB
/
statistics-parser.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
#!/usr/bin/python
import re
import traceback
import argparse
from commands import getoutput as cmd_
from datetime import timedelta
from display import stats as stats_display
from display import achievements as achi_display
def cmd(s):
r = cmd_(s)
# print r
return r
import sys
import json
display = {}
display["stats"] = stats_display
display["achievements"] = achi_display
def minutesToTime(minutes):
try:
min = int(minutes)
except:
return "error"
return timedelta(seconds=min).__str__()
def import_player_info(world_dir,player_name):
try:
json_data=open(world_dir + "/stats/" + player_name + ".json")
player_info = json.load(json_data)
json_data.close()
stats = {}
achievements = {}
for i in player_info:
indexes = i.split(".")
if indexes[0] == "stat":
if(len(indexes) == 2):
stats[indexes[1]] = player_info[i]
else: # subitem
stats[indexes[1] + indexes[2]] = player_info[i]
elif indexes[0] == "achievement":
achievements[indexes[1]] = player_info[i]
else:
print("unsed item : " + i )
info = {}
info["stats"] = stats
info["achievements"] = achievements
info["name"] = player_name
return info
except:
traceback.print_exc(file=sys.stdout)
print "error !!! "
sys.exit(-1)
#this only is used to create the auxiliary names file
def create_display(info,key):
display = {}
for i in info:
for j in i[key]:
if j not in display.keys() :
display[j] = raw_input("Name for \"" + j + "\":\n")
print display
return display
def CmToDistance(value):
st = ""
val = int(value)
if val > 100000: #kilometers
return str(val/ 100000) + "," + str((val - (val / 100000)*100000)/100) + " Km"
else: #meters
return str(val/100) + "," + str(val - (val/100)*100) + " m"
return st
def join(info,key):
full = {}
for i in info:
for j in i[key]:
try:
if j not in full.keys():
full[j] = {}
if "vector" not in full[j].keys():
full[j]["vector"] = [None]*len(info)
if j == "playOneMinute" :
full[j]["vector"][i["player_id"]] = minutesToTime(i[key][j])
elif j == "exploreAllBiomes":#TODO: print this better
full[j]["vector"][i["player_id"]] = i[key][j]
elif j[len(j) - 2:len(j)] == "Cm":
full[j]["vector"][i["player_id"]] = CmToDistance(i[key][j])
else:
full[j]["vector"][i["player_id"]] = i[key][j]
full[j]["name"] = display[key][j]
except:
print "error in " + j
del full[j]
return full
class maxx:
def __init__(self):
self.max_size =0
return
def max(self,b):
try:
tmp = max(len(b),self.max_size)
except:
return
self.max_size = tmp
return
def value(self):
return self.max_size
def print_table(table,size):
for i in range(len(table)):
for j in range(len(table[i])):
try:
print str(table[i][j]).rjust(size) + " | ",
except:
print "".rjust(size) + " | ",
print
return
def html_table(table):
html = "<table border=\"1\" style=\"width:1500px\">"
for i in range(len(table)):
html += "<tr>\n"
for j in range(len(table[i])):
html += "\t<td>"
try:
html += str(table[i][j])
except:
html+= ""
html += "</td>\n"
html += "</tr>\n"
html+= "</table>"
return html
def display_info(info,players):
cols = len(players) + 1
table = []
table.append([""]*cols)
m = maxx()
for i in range(0,len(players)):
table[0][i+1] = players[i]
m.max( players[i])
for i in info:
if info[i]["name"] != "":
table.append([""]*cols)
table[len(table)-1][0] = info[i]["name"]
for j in range(0,len(info[i]["vector"])):
table[len(table)-1][j+1] = info[i]["vector"][j]
m.max(info[i]["vector"][j])
if table[len(table)-1][j+1] == None:
table[len(table)-1][j+1] = ""
print " ---- " + str(m.value())
table.sort()
return table
def generate_html(st,ac,filename):
f = open(filename,"w")
f.write("<!DOCTYPE html>\n")
f.write("<html>\n")
f.write("<body>\n")
f.write("\n")
f.write("<h1>Minecraft Statistics</h1><p><p>\n")
f.write("<h2>Statistics </h2>\n")
f.write(html_table(st))
f.write("<h2>Achievements </h2>\n")
f.write(html_table(ac))
f.write("</body>\n")
f.write("</html>\n")
f.close()
def parse_all(world_dir):
lst = cmd("ls " + world_dir + "/stats/").replace(".json","").splitlines()
info = []
for player_id in range(0,len(lst)):
info.append(import_player_info(world_dir,lst[player_id]))
info[len(info)-1]["player_id"] = player_id
info[len(info)-1]["player_id"] = player_id
st = display_info(join(info,"stats"),lst)
ac = display_info(join(info,"achievements"),lst)
return (st,ac)
if __name__ == "__main__" :
parser = argparse.ArgumentParser(description = "Creates Minecraft player statistics in a given server")
parser.add_argument("-O","--output", help = " Html output file", dest = "output", default = "")
parser.add_argument("-S","--standard", help = "Print table in command line",dest = "cmd", )
parser.add_argument("-I","--input", help = "Input world directory", dest = "input", default = "")
args = parser.parse_args()
if args.input == "":
print "invalid input"
st,ac = parse_all(args.input)
generate_html(st,ac,args.output)