-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path_pcr_data.py
84 lines (65 loc) · 2.67 KB
/
_pcr_data.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
import os
import json
CHARA_NAME = {}
CHARA_PROFILE = {}
UnavailableChara = {}
class CharaMaster:
def __init__(self) -> None:
self.chara_name_path = os.path.join(os.path.dirname(__file__), 'CHARA_NAME.json')
self.chara_profile_path = os.path.join(os.path.dirname(__file__), 'CHARA_PROFILE.json')
self.unavailablechara_path = os.path.join(os.path.dirname(__file__), 'UnavailableChara.json')
self.__load_pcr_data()
self.__selfcheck()
def __selfcheck(self) -> None:
for chara_id in CHARA_NAME:
if "" in CHARA_NAME[chara_id]:
CHARA_NAME[chara_id].remove("")
self.__save_pcr_data()
def __load_pcr_data(self) -> None:
# load CHARA_NAME
with open(self.chara_name_path, 'r', encoding='utf-8') as f:
chara_name_str = json.load(f)
global CHARA_NAME
for id in chara_name_str:
CHARA_NAME[int(id)] = chara_name_str[id]
# load CHARA_PROFILE
with open(self.chara_profile_path, 'r', encoding='utf-8') as f:
chara_profile_str = json.load(f)
global CHARA_PROFILE
for id in chara_profile_str:
CHARA_PROFILE[int(id)] = chara_profile_str[id]
# load UnavailableChara
with open(self.unavailablechara_path, 'r', encoding='utf-8') as f:
unavailablechara_str = json.load(f)
global UnavailableChara
for id in unavailablechara_str:
UnavailableChara[int(id)] = unavailablechara_str[id]
def __save_pcr_data(self) -> None:
# save CHARA_NAME
with open(self.chara_name_path, 'w+', encoding='utf-8') as f:
json.dump(CHARA_NAME, f, indent=4, ensure_ascii=False)
# save CHARA_PROFILE
with open(self.chara_profile_path, 'w+' , encoding='utf-8') as f:
json.dump(CHARA_PROFILE, f, indent=4, ensure_ascii=False)
# save UnavailableChara
with open(self.unavailablechara_path, 'w+' , encoding='utf-8') as f:
json.dump(UnavailableChara, f, indent=4, ensure_ascii=False)
def check_nickname(self, id:int, nickname:str):
'''
Return true if nickname already existed.
'''
if id not in CHARA_NAME:
return None
nicknames = CHARA_NAME[id]
if nickname in nicknames:
return True
else:
return False
def add_chara(self, id:int, names:list) -> None:
CHARA_NAME[id] = names
self.__save_pcr_data()
def add_nickname(self, id:int, nickname:str) -> None:
CHARA_NAME[id].append(nickname)
self.__save_pcr_data()
# CHARA_NAME will be loaded while init
chara_master = CharaMaster()