-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tool.py
632 lines (537 loc) · 25.2 KB
/
main_tool.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import time
import htmlmin
import os
import pickle
import urllib.parse
from random import choices as ran_choices
from collections import OrderedDict
from jinja2 import UndefinedError
from functools import lru_cache
from main_cache import GenerationCache
__all__ = ["TOOLTIP_ID", "TOOLTIP_ID_RATIONAL", "INIT_TIME",
"JinjaTool", "TemplateTool", "get_curr_time_printable"]
TOOLTIP_ID = set()
TOOLTIP_CHAR_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
TOOLTIP_ID_RATIONAL = set() # 有理编号的tooltip
STATIC_BASE_URL = "/static/"
INIT_TIME = int(time.time_ns())
def get_curr_time_printable() -> str:
"""精确到毫秒的后四位"""
global INIT_TIME
return f"{(time.time_ns() - INIT_TIME) / 1e+6:0.4f}ms ({(time.time_ns() - INIT_TIME) / 1e+9:0.4f}s)"
class JinjaTool:
@staticmethod
def get_current_utc():
return time.time()
@staticmethod
def get_init_time():
return INIT_TIME
@staticmethod
def generate_tooltip_id(*args):
"""args 以 先type后id 的方式传递"""
def raise_exception(type: int = 2):
if type == 1:
raise ValueError(f"unknown data_type {args[0]}")
elif type == 2:
raise ValueError(args)
if len(args) != 0:
global TOOLTIP_ID_RATIONAL
data_type = args[0]
data_id = args[1] # 全部都是 uuid (通用),除了 npc=namespace[-1] (过于nt)
if len(args) == 2:
# track/background/character/album
if data_type in ["track", "background", "character", "album"]:
id_1 = f"{data_type}-{data_id}-all"
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
# story
elif data_type in ["story"]:
id_1 = f"story-{data_id}-all"
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
else:
raise_exception(1)
elif len(args) == 3:
if args[0] == "storypart":
id_1 = f"story-{args[1]}-part-{args[2]}"
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
elif args[0] == "storypart_source":
id_1 = f"story-{args[1]}-source-{args[2]}"
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
raise_exception(1)
elif len(args) == 4:
# data_type 取值如下:
# bg, bg_direct, char, track
first, second = (args[0], args[1]), (args[2], args[3])
if first[0] > second[0]:
second_, first_ = first, second
else:
second_, first_ = second, first
if first_[0] == "background_direct":
id_1 = f"char-{second_[1]}-bg"
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
# 针对 story -> track/character/background 情况
elif first_[0] == "story":
return JinjaTool.generate_tooltip_id(*second_)
elif second_[0] == "story":
return JinjaTool.generate_tooltip_id(*first_)
# 针对 char -> background (direct)
elif second_[0] == "character_bg_direct":
return JinjaTool.generate_tooltip_id(*first_)
# 其它
else:
id_1 = "-".join(first + second)
id_2 = "-".join(second + first)
# 检测tooltip id情况
if id_1 not in TOOLTIP_ID_RATIONAL:
# 如果第一个id不存在,检测第二个id
if id_2 not in TOOLTIP_ID_RATIONAL:
# 如果第二个id不存在,说明两个都不存在
# 即,是第一次创建这两数据的关系
# 返回第一个id
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
else:
# 否则,第二个id存在
# 第二个id,此时,也就是第一次创建关系时的id_1(这里是id_2)
return id_2
else:
# 谨防意外情况,虽然好像不该运行到这(?)
TOOLTIP_ID_RATIONAL.add(id_1)
return id_1
else:
raise ValueError(args)
else:
global TOOLTIP_ID
while True:
id = "".join(ran_choices(TOOLTIP_CHAR_SET, k=6))
if id not in TOOLTIP_ID:
TOOLTIP_ID.add(id)
return id
@staticmethod
def js_string_safe(value):
return (value.replace("&", "&").
replace("'", "\'").
replace("[", "[").
replace("]", "]"))
@staticmethod
def js_html_string_safe(value):
return value.replace("\n", "\\n")
@staticmethod
def page_minify_html(value):
return htmlmin.minify(value,
remove_empty_space=True,
remove_optional_attribute_quotes=False). \
replace("'", "'")
@staticmethod
@lru_cache
def get_outer_json(instance_id: str, instance_type: str):
def read_file(filepath):
with open(os.path.join("exported_data", filepath), mode="rb") as file:
return pickle.load(file)
# track
if instance_type == "track":
instance_id = instance_id.split("_")
track_type = instance_id[0]
track_no = int(instance_id[1])
if track_type == "OST":
return read_file(f"track/ost/{track_no}.json")
elif track_type == "other":
return read_file(f"track/other/{track_no}.json")
elif track_type == "animation":
return read_file(f"track/animation/{track_no}.json")
elif track_type == "short":
return read_file(f"track/short/{track_no}.json")
# background
elif instance_type == "background":
# instance_id = background filename
return read_file(f"background/{instance_id}.json")
elif instance_type == "character":
# instance_id = character.uuid
# 尝试student
try:
return read_file(f"character/student/{instance_id.lower()}/{instance_id.lower()}.json")
except FileNotFoundError:
return read_file(f"character/npc/{instance_id}/{instance_id}.json")
elif instance_type == "album":
return read_file(f"album/{instance_id}.json")
raise ValueError(f"{instance_id}, {instance_type}")
@staticmethod
@lru_cache()
def arg_check(arg, strict=False):
"""
:param arg:
:param strict: 严格模式,为True时只有未定义才会返回False
:return:
"""
# test if is undefined
if hasattr(arg, "_undefined_exception"):
exc = getattr(arg, "_undefined_exception")
if exc is UndefinedError:
return False
if not strict:
# other common cases
if arg == "" or arg == 0 or arg == "False":
return False
return True
@staticmethod
def storypart_extract_all_data(part, key_name):
temp = OrderedDict()
for segment in part["data"]:
for obj in segment[key_name]:
if obj["uuid"] not in temp.keys():
temp[obj["uuid"]] = obj
return [i for i in temp.values()]
@staticmethod
def storypart_extract_all_data_track(part):
return JinjaTool.storypart_extract_all_data(part, "track")
@staticmethod
def storypart_extract_all_data_background(part):
return JinjaTool.storypart_extract_all_data(part, "background")
@staticmethod
def storypart_extract_all_data_characters(part):
return JinjaTool.storypart_extract_all_data(part, "character")
@staticmethod
def convert_story_into_single_part(story):
ALL_KEY_NAME = ["background", "track", "character"]
all_data_uuid = []
all_data = {
"character": [],
"track": [],
"background": []
}
if story["filetype_sub"] == 0:
for part in story["part"]:
for seg in part["data"]:
for key_name in ALL_KEY_NAME:
for entry in seg[key_name]:
if entry["uuid"] not in all_data_uuid:
all_data_uuid.append(entry["uuid"])
all_data[key_name].append(entry)
elif story["filetype_sub"] == 1:
for key_name in ALL_KEY_NAME:
all_data[key_name] = list(story["part"]["all"][key_name].values())
else:
raise ValueError(story["filetype_sub"])
return {"data": [all_data]}
@staticmethod
@lru_cache()
def get_static(path):
global STATIC_BASE_URL
return urllib.parse.urljoin(STATIC_BASE_URL, path)
class TemplateTool:
@staticmethod
def get_character_instance_id(data: dict) -> str:
if data.get("filetype", "") == 52 or data.get("student", "None") != "None": # NPC
return data["namespace"][-1]
return data["uuid"]
@staticmethod
def get_character_data(data: dict):
"""tooltip_character 前半部分的 python 实现版"""
if data.get("filetype", -1) == 52:
# npc
char_data = JinjaTool.get_outer_json(data["namespace"][-1], "character")
else:
try:
char_data = JinjaTool.get_outer_json(data["uuid"], "character")
except Exception:
# for unknown case
char_data = JinjaTool.get_outer_json(data["student"]["uuid"], "character")
if char_data.get("filetype", -1) == 52:
# npc
char_info = char_data
char_type = "npc"
char_name = char_info["name"]
else:
# student
char_info = char_data["student"]
char_type = "stu"
char_name = char_info["name"]["name"]
outer_data = char_info["used_by"]
return {"char_data": char_data, "char_info": char_info,
"char_type": char_type, "char_name": char_name,
"outer_data": outer_data}
@staticmethod
def py_generate_story_url(lang: str, story: dict):
ft = story["filetype"]
if ft in [14, 15]:
return f"/{lang}/{'/'.join(story['namespace'])}.html"
else:
if ft == 11:
story_type = "main"
elif ft == 12:
story_type = "side"
elif ft == 13:
story_type = "short"
else:
story_type = "other"
return f"/{lang}/main/story/{'/'.join(story['namespace'][2:])}.html"
@staticmethod
def py_tooltip_background(background, instance_id, instance_type, extra_data):
"""返回一个 [[Story, [[Part, loop_index, seg_index, tooltip_id], ...]], ...]"""
all_story = []
# if instance_type == "story":
# cache_id = f"background-{background['uuid']}-story"
# if GenerationCache.check_for_cache("all", cache_id):
# return GenerationCache.get_from_cache("all", cache_id)
for story in extra_data.values():
story = story[0]
story_parts = []
if story["filetype_sub"] == 1:
# auto generated story
if instance_type == "story":
if background["filename"] in story["part"]["all"]["background"].keys():
all_story.append([story, []])
elif instance_type == "character":
if background["filename"] in story["part"]["bg_to_char"].keys():
if instance_id in story["part"]["bg_to_char"][background["filename"]]:
all_story.append([story, []])
elif instance_type == "track":
if background["filename"] in story["part"]["bg_to_track"].keys():
if instance_id in story["part"]["bg_to_track"][background["filename"]]:
all_story.append([story, []])
continue
for (index, part) in enumerate(story["part"], 1):
add_part, seg_index = 0, -1
is_by_comm, is_narrative = False, False
for (index2, segment) in enumerate(part["data"], 1):
is_exit = 0
for bg in segment["background"]:
if bg["uuid"] == background["uuid"]:
# bg-char
for char in segment["character"]:
if TemplateTool.get_character_instance_id(char) == instance_id:
if char["is_comm"]:
is_by_comm = True
if char["is_narrative"]:
is_narrative = True
# 其它代码
if instance_type == "story" or instance_type == "character":
is_exit = 1
break
elif instance_type == "track":
for track in segment["track"]:
if track["instance_id"] == instance_id:
is_exit = 1
break
if is_exit:
break
if is_exit:
seg_index = index2
break
if seg_index != -1:
story_parts.append([part, index, seg_index, 0, is_by_comm, is_narrative])
if len(story_parts) != 0:
all_story.append([story, story_parts])
# if instance_type == "story":
# GenerationCache.add_to_cache("all", cache_id, all_story)
return all_story
@staticmethod
def py_tooltip_track(track, instance_id, instance_type, extra_data):
all_story = []
# if instance_type == "story":
# cache_id = f"track-{track['uuid']}-story"
# if GenerationCache.check_for_cache("all", cache_id):
# return GenerationCache.get_from_cache("all", cache_id)
for story in extra_data["data_story"].values():
story = story[0]
related_parts = []
if story["filetype_sub"] == 1:
# auto generated story
if instance_type == "story":
if track["instance_id"] in story["part"]["all"]["track"].keys():
all_story.append([story, []])
elif instance_type == "character":
if track["instance_id"] in story["part"]["track_to_char"].keys():
if instance_id in story["part"]["track_to_char"][track["instance_id"]]:
all_story.append([story, []])
elif instance_type == "background":
if track["instance_id"] in story["part"]["track_to_bg"].keys():
if instance_id in story["part"]["track_to_bg"][track["instance_id"]]:
all_story.append([story, []])
continue
for (index, part) in enumerate(story["part"], 1):
seg_index = -1
is_by_comm, is_narrative = False, False
for (index2, segment) in enumerate(part["data"], 1):
is_exit = 0
for story_track in segment["track"]:
# print(story_track)
if story_track["instance_id"] == track["instance_id"]:
if instance_type == "story" or instance_type == "album":
is_exit = 1
break
elif instance_type == "character":
for char in segment["character"]:
# bg-char
if char["is_comm"]:
is_by_comm = True
if char["is_narrative"]:
is_narrative = True
if TemplateTool.get_character_instance_id(char) == instance_id:
is_exit = 1
break
elif instance_type == "background":
for bg in segment["background"]:
if bg["uuid"] == instance_id:
is_exit = 1
break
if is_exit:
break
if is_exit:
seg_index = index2
break
if seg_index != -1:
related_parts.append([part, index, seg_index, 0, is_by_comm, is_narrative])
if len(related_parts) != 0:
all_story.append([story, related_parts])
# if instance_type == "story":
# GenerationCache.add_to_cache("all", cache_id, all_story)
return all_story
@staticmethod
def py_tooltip_character(character, instance_id, instance_type, extra_data):
"""注意,此处的 extra_data 是 loop_data.data"""
if extra_data["filetype"] == 52:
char_data = extra_data
char_type = "npc"
else:
char_data = extra_data["student"]
char_type = "stu"
outer_data = char_data["used_by"]
character_id = TemplateTool.get_character_instance_id(character)
# if instance_type == "story":
# cache_id = f"character-{char_data['uuid']}-story"
# if GenerationCache.check_for_cache("all", cache_id):
# return GenerationCache.get_from_cache("all", cache_id)
all_story = []
for story in outer_data["data_story"].values():
story = story[0]
related_parts = []
if story["filetype_sub"] == 1:
# auto generated story
if instance_type == "story":
if character_id in story["part"]["all"]["character"].keys():
all_story.append([story, []])
elif instance_type == "character":
if character_id in story["part"]["char_to_char"].keys():
if instance_id in story["part"]["char_to_char"][character_id]:
all_story.append([story, []])
elif instance_type == "track":
if character_id in story["part"]["char_to_track"].keys():
if instance_id in story["part"]["char_to_track"][character_id]:
all_story.append([story, []])
elif instance_type == "background":
if character_id in story["part"]["char_to_bg"].keys():
if instance_id in story["part"]["char_to_bg"][character_id]:
all_story.append([story, []])
continue
for (index, part) in enumerate(story["part"], 1):
seg_index = -1
is_by_comm, is_narrative = False, False
# 针对 char-char (in-story) 设计,这部分只精确到part
if instance_type == "character":
is_exit = 0
for char2 in JinjaTool.storypart_extract_all_data_characters(part):
# 探测对方(传入的 character 变量)在不在里面
if TemplateTool.get_character_instance_id(char2) == character_id:
# 探测 character 自己在不在起头
for char3 in JinjaTool.storypart_extract_all_data_characters(part):
if TemplateTool.get_character_instance_id(char3) == instance_id:
if char2["is_comm"]:
is_by_comm = True
if char2["is_narrative"]:
is_narrative = True
is_exit = 1
break
if is_exit == 1:
break
if is_exit == 1:
break
if is_exit == 1:
related_parts.append([part, index, seg_index, 0, is_by_comm, is_narrative])
# 直接跳过剩下循环
continue
# 其它数据
for (index2, segment) in enumerate(part["data"], 1):
is_exit = 0
for char in segment["character"]:
if char["uuid"] == char_data["uuid"]:
if char["is_comm"]:
is_by_comm = True
if char["is_narrative"]:
is_narrative = True
if instance_type == "track":
for track in segment["track"]:
if track["instance_id"] == instance_id:
is_exit = 1
break
if is_exit == 1:
break
elif instance_type == "background":
for bg in segment["background"]:
if bg["uuid"] == instance_id:
is_exit = 1
break
if is_exit == 1:
break
else:
is_exit = 1
break
if is_exit:
seg_index = index2
if seg_index != -1:
related_parts.append([part, index, seg_index, 0, is_by_comm, is_narrative])
if len(related_parts) != 0:
all_story.append([story, related_parts])
# if instance_type == "story":
# GenerationCache.add_to_cache("all", cache_id, all_story)
return all_story
@staticmethod
def py_output_usedby_story(data_story: dict, instance_uuid, instance_key):
# cache_id = f"{instance_key}-{instance_uuid}-story"
all_story_with_filetype = OrderedDict()
_curr_filetype = 0
for key, value in data_story.items():
# 区分不同 filetype 故事
if _curr_filetype == 0:
_curr_filetype = value[0]["filetype"]
all_story_with_filetype[_curr_filetype] = []
else:
if _curr_filetype != value[0]["filetype"]:
_curr_filetype = value[0]["filetype"]
all_story_with_filetype[_curr_filetype] = []
value = value[0]
if value["filetype_sub"] == 1:
all_story_with_filetype[_curr_filetype].append([value, []])
continue
# appearedAt snippet
if instance_uuid:
related_parts = []
for (index, part) in enumerate(value["part"], 1):
seg_index = -1
is_by_comm, is_narrative = False, False # bg-char
for (index2, segment) in enumerate(part["data"], 1):
is_exit = 0
for obj in segment[instance_key]:
if obj["uuid"] == instance_uuid:
# bg-char
if instance_key == "character":
if obj["is_comm"]:
is_by_comm = True
if obj["is_narrative"]:
is_narrative = True
is_exit = 1
if is_exit:
seg_index = index2
if seg_index != -1:
related_parts.append(
[part, index, seg_index,
JinjaTool.generate_tooltip_id("storypart", value["instance_id"], index),
is_by_comm, is_narrative])
if len(related_parts) != 0:
all_story_with_filetype[_curr_filetype].append([value, related_parts])
# GenerationCache.add_to_cache("all", cache_id, [story for (i, j) in all_story_with_filetype.items() for story in j])
return all_story_with_filetype