-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_json2xls
executable file
·293 lines (256 loc) · 10.9 KB
/
level_json2xls
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
#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
from __future__ import print_function
import sys
import os
import getopt
import time
import json
import openpyxl
from openpyxl.styles import Font, NamedStyle, Alignment, Border, Side, PatternFill
def print_usage():
print("python3 %s <输入playData.json文件> [-o] <输出xls文件>" % sys.argv[0])
print("-o: 指定输出文件路径")
def load_json(path):
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
f.close()
return data
def calc_width(value, font_height):
str_val = str(value)
num = len(str_val.encode('utf-16')) / 2 - 1
# print('\'%s\'字符个数: %d' % (str_val, num))
num *= 2 * font_height / 10.0
if num > 40:
num = 40
return int(num)
def add_styles(wb):
if 'head_style' not in wb.named_styles:
head_style = NamedStyle(name='head_style')
head_style.font = Font(name='宋体', size=16, bold=True)
head_style.alignment = Alignment(horizontal='center', vertical='center')
head_style.border = Border(bottom=Side(style=openpyxl.styles.borders.BORDER_THICK))
wb.add_named_style(head_style)
if 'reg_style' not in wb.named_styles:
reg_style = NamedStyle(name='reg_style')
reg_style.font = Font(name='宋体', size=12)
reg_style.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
sideline = Side(style=openpyxl.styles.borders.BORDER_THIN)
reg_style.border = Border(left=sideline, right=sideline, top=sideline, bottom=sideline)
wb.add_named_style(reg_style)
if 'reg_style_grey' not in wb.named_styles:
reg_style_grey = NamedStyle(name='reg_style_grey')
reg_style_grey.font = Font(name='宋体', size=12)
reg_style_grey.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
sideline = Side(style=openpyxl.styles.borders.BORDER_THIN)
reg_style_grey.border = Border(left=sideline, right=sideline, top=sideline, bottom=sideline)
reg_style_grey.fill = PatternFill('solid', '00D9D9D9')
wb.add_named_style(reg_style_grey)
def write_game_statistics_sheet(sheet, json_data, key_mapping):
col_width = [0]
i = 1
for vl in key_mapping.values():
cell = sheet.cell(1, i, vl)
cell.style = 'head_style'
i += 1
col_width.append(calc_width(vl, 16))
cur_line = 2
grey = False
for record in json_data:
cellStyle = 'reg_style'
if grey:
cellStyle = 'reg_style_grey'
grey = not grey
line_num = 1
col = 0
for key in key_mapping.keys():
col += 1
if key in record:
vl = record[key]
if isinstance(vl, list):
count = len(vl)
if count > 0:
sheet.cell(cur_line, col, vl[0]).style = cellStyle
col_len = calc_width(vl[0], 12)
if col_width[col] < col_len:
col_width[col] = col_len
for i in range(1, count):
sheet.cell(cur_line + i, col, vl[i]).style = cellStyle
col_len = calc_width(vl[i], 12)
if col_width[col] < col_len:
col_width[col] = col_len
if count > line_num:
line_num = count
else:
sheet.cell(cur_line, col).style = cellStyle
continue
else:
vl = ""
sheet.cell(cur_line, col, vl).style = cellStyle
col_len = calc_width(vl, 12)
if col_width[col] < col_len:
col_width[col] = col_len
# 合并非数组单元格
if line_num > 1:
col = 0
for key in key_mapping.keys():
col += 1
if key in record and isinstance(record[key], list):
continue
else:
sheet.merge_cells(start_row=cur_line, end_row=cur_line + line_num - 1,
start_column=col, end_column=col)
sheet.cell(cur_line, col).style = cellStyle
cur_line += line_num
for i in range(1, len(col_width)):
if col_width[i] > 10:
col_letter = openpyxl.utils.get_column_letter(i)
sheet.column_dimensions[col_letter].width = col_width[i] + 1
def exportPlayData(path, playRec_jsonData, upgrade_jsonData):
wb = openpyxl.Workbook();
add_styles(wb)
recordList = []
if isinstance(playRec_jsonData, dict):
keys = list(playRec_jsonData.keys())
keys.sort(key=lambda lv_key: int(lv_key[5:]))
# length = len(playRec_jsonData)
# for i in range(1, length + 1):
# levelKey = 'level{}'.format(i)
for levelKey in keys:
if levelKey in playRec_jsonData:
lv = int(levelKey[5:])
record = {'Id': lv, 'PlayScores': [], 'Requirements': [], 'ContinueCount': [], 'ConsumeProps': [], 'ConsumePremiumIngredients': [], 'ServeCustomerCount': [], 'Time': [], 'Coin': [],
'Cash': []}
dataList = playRec_jsonData[levelKey]
for data in dataList:
record['PlayScores'].append(data['Scores'])
if 'Requirement' in data:
record['Requirements'].append(data['Requirement'])
if 'ContinueCount' in data:
record['ContinueCount'].append(data['ContinueCount'])
if 'ConsumeProps' in data:
record['ConsumeProps'].append(data['ConsumeProps'])
if 'ConsumePremiumIngredients' in data:
record['ConsumePremiumIngredients'].append(data['ConsumePremiumIngredients'])
if 'ServeCustomerCount' in data:
record['ServeCustomerCount'].append(data['ServeCustomerCount'])
if 'Time' in data.keys():
tl = time.localtime(int(data['Time']))
format_time = time.strftime("%Y-%m-%d %H:%M:%S", tl)
record['Time'].append(format_time)
if 'Coin' in data.keys():
record['Coin'].append(data['Coin'])
if 'Cash' in data.keys():
record['Cash'].append(data['Cash'])
recordList.append(record)
keyMapping = {
'Id': '关卡ID',
'PlayScores': '得分',
'Requirements': '条件',
'ContinueCount': '继续关卡次数',
'ConsumeProps': '消耗道具',
'ConsumePremiumIngredients': '消耗有机食材',
'ServeCustomerCount': '服务顾客数量',
'Coin': 'Coin',
'Cash': 'Cash',
'Time': '时间',
}
sheet = wb.active
sheet.title = '关卡统计'
write_game_statistics_sheet(sheet, recordList, keyMapping)
sheet.freeze_panes = 'B2'
# 升级情况
if upgrade_jsonData:
upgradeList = []
for itemName in upgrade_jsonData.keys():
itemList = upgrade_jsonData.get(itemName)
for up in itemList:
upgrade = {}
upgrade['name'] = itemName
upgrade['upgradeLevel'] = up['upgradeLevel']
upgrade['level'] = up['level']
upgrade['spentCoin'] = up['spentCoin']
upgrade['leftCoin'] = up['leftCoin']
upgrade['spentCash'] = up['spentCash']
upgrade['leftCash'] = up['leftCash']
if 'time' in up:
tl = time.localtime(int(up['time']))
format_time = time.strftime("%Y-%m-%d %H:%M:%S", tl)
upgrade['time'] = format_time
else:
upgrade['time'] = 0
upgradeList.append(upgrade)
# def takeLevel(ele):
# return ele['level']
upgradeList.sort(key=lambda ele: ele['level'])
keyMapping = {
'name': '关卡ID',
'level': '当前关卡',
'upgradeLevel': '购买等级',
'spentCoin': '花费金币',
'leftCoin': '剩余金币',
'spentCash': '花费Cash',
'leftCash': '剩余Cash',
'time': '时间'
}
sheet = wb.create_sheet('厨具升级')
write_game_statistics_sheet(sheet, upgradeList, keyMapping)
sheet.freeze_panes = 'A2'
# sheet.cell(1, 1, "项目").style = 'head_style'
# sheet.cell(1, 2, "结余").style = 'head_style'
# row = 2
# items = json_data['items']
# for key, val in items.items():
# if key in keyMapping:
# key = keyMapping[key]
# sheet.cell(row, 1, key).style = 'reg_style'
# sheet.cell(row, 2, val).style = 'reg_style'
# row += 1
# sheet.freeze_panes = 'A2'
wb.save(path)
print("\033[1;34m已经输出至文件:%s\n\033[0m" % path)
def main():
if len(sys.argv) < 4:
print_usage()
sys.exit(1)
try:
opts, args = getopt.getopt(sys.argv[2:], "ho:", ["help", "output="])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
print_usage()
sys.exit(2)
jsonPath = sys.argv[1]
outPath = None
for o, a in opts:
if o in ("-o", "--output"):
outPath = a
elif o in ("-h", "--help"):
print_usage()
sys.exit(0)
outfile = os.path.splitext(outPath)
if not outfile[-1] == '.xlsx':
fname = outfile[0]
outPath = fname + ".xlsx"
try:
print("\033[1;34m\n开始读取json文件: %s\033[0m" % jsonPath)
rootData = load_json(jsonPath)
except Exception as e:
print('\033[1;31m读取json文件%s出错: %s\033[0m' % (jsonPath, e))
sys.exit(1)
if rootData is not None:
count = len(rootData)
if count <= 0:
print("\033[1;31m无效的json文件!\033[0m")
exit(1)
# print('rootData:', rootData)
playRecordData = rootData['Level'] if 'Level' in rootData.keys() else None
# print('playRecordData:', playRecordData)
upgradeData = rootData['Upgrade'] if 'Upgrade' in rootData.keys() else None
# print('upgradeData:', upgradeData)
exportPlayData(outPath, playRecordData, upgradeData)
else:
print("\033[1;31m无法读取json文件: %s\033[0m" % jsonPath)
sys.exit(1)
if __name__ == "__main__":
main()