-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake_event_quest.py
More file actions
150 lines (125 loc) · 4.25 KB
/
make_event_quest.py
File metadata and controls
150 lines (125 loc) · 4.25 KB
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
#!/usr/bin/env python3
# イベントCSVデータをJSONファイルに変換
import argparse
import csv
import dataclasses
import json
import logging
import unicodedata
from pathlib import Path
# from make_freequest import questId2dropItemNum
from make_freequest import (
DropItem,
FgoQuest,
alias2id,
fetch_free_quests,
id2dropPriority,
id2name,
id2type,
)
url = "https://api.atlasacademy.io/export/JP/nice_war.json"
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")
logger = logging.getLogger(__name__)
csv_dir = Path(__file__).resolve().parent / Path("data/csv/")
json_dir = Path(__file__).resolve().parent / Path("data/json/")
@dataclasses.dataclass(frozen=True)
class FgoEventQuest(FgoQuest):
shortname: str
def open_file_with_utf8(filename):
"""utf-8 のファイルを BOM ありかどうかを自動判定して読み込む"""
is_with_bom = is_utf8_file_with_bom(filename)
encoding = "utf-8-sig" if is_with_bom else "utf-8"
return open(filename, encoding=encoding)
def is_utf8_file_with_bom(filename):
"""utf-8 ファイルが BOM ありかどうかを判定する"""
line_first = open(filename, encoding="utf-8").readline()
return line_first[0] == "\ufeff"
def list2dic(quest_list):
free_quests = fetch_free_quests(url)
quest_output = []
for quest in quest_list:
# ドロップを作成
drop = []
for item in quest.keys():
if item.startswith("item"):
if quest[item] != "":
quest[item] = unicodedata.normalize("NFKC", quest[item])
if quest[item] in alias2id.keys():
item_id = alias2id[quest[item]]
else:
logger.error("Error: 変換できません: %s", quest[item])
exit(1)
name = id2name[alias2id[quest[item]]]
drop.append(
DropItem(
item_id,
name,
id2type[item_id],
id2dropPriority[item_id],
),
)
drop = sorted(drop, key=lambda x: x.dropPriority, reverse=True)
questId = int(quest["id"])
q = free_quests[questId]
if q["recommendLv"] == "90++":
qp = 13536
elif q["recommendLv"] == "90+++":
qp = 16243
elif q["recommendLv"] == "90+":
qp = 11280
elif q["recommendLv"] == "120★★★":
qp = 558187
else:
qp = int(q["recommendLv"]) * 100 + 400
spotname = q["name"]
logger.debug("drop: %s", drop)
# try:
# dropItemNum = questId2dropItemNum[questId]
# except:
# logger.warning("ドロップ枠数が取得できません")
# dropItemNum = -1
dropItemNum = -1
event_quest = FgoEventQuest(
int(quest["id"]),
quest["quest"],
"",
"",
"",
qp,
drop,
dropItemNum,
quest["shortname"],
)
if quest["quest"] != spotname:
logger.warning("場所名が異なります: %s %s", quest["quest"], spotname)
quest_output.append(dataclasses.asdict(event_quest))
return quest_output
def main(args):
file = Path(args.csv)
if file.exists() is False:
logger.critical("File not found: %s", file)
exit(1)
with open_file_with_utf8(file) as f:
reader = csv.DictReader(f)
quest_list = [row for row in reader]
quest_dic = list2dic(quest_list)
outfile = json_dir / (file.stem + ".json")
with open(outfile, "w", encoding="UTF-8") as f:
f.write(json.dumps(quest_dic, ensure_ascii=False, indent=4))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"csv",
help="input csv file",
)
parser.add_argument(
"--loglevel",
choices=("DEBUG", "INFO", "WARNING"),
default="WARNING",
help="loglevel [default: WARNING]",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
logger.setLevel(args.loglevel)
main(args)