-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate_perk_list.py
163 lines (128 loc) · 5.24 KB
/
generate_perk_list.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
from operator import attrgetter
import vdf
from io import TextIOWrapper
from itertools import starmap
from typing import Mapping, NamedTuple
from pathlib import Path
# relative paths do not work on GitLab well (leading slash after URL makes a difference)
REPO_ROOT = "https://github.com/Phil25/RTD/blob/master"
class PerkDocs(NamedTuple):
class _Setting(NamedTuple):
name: str
value: str
default: str
description: str
def __str__(self) -> str:
if self.value.startswith("<"):
suffix = f" _(default: `{self.default}`)_"
else:
suffix = " _(default)_" if self.value == self.default else ""
return f'* `"{self.name}" "{self.value}"` — {self.description}{suffix}'
index: int
is_good: bool
name: str
token: str
brief: str
description: str
time: str
tags: str
settings: list[_Setting]
notes: list[str]
@property
def circle(self):
return "🟢" if self.is_good else "🟣"
@property
def square(self):
return "🟩" if self.is_good else "🟪"
@property
def filepath(self):
return f"{REPO_ROOT}/scripting/rtd/perks/{self.token}.sp"
@property
def header(self):
return f"# {self.square} {self.name} [^](#perk-briefs)"
@property
def anchor(self):
escaped = self.name.replace(" ", "-").lower()
return f"#-{escaped}-"
def write_brief(self, out: TextIOWrapper):
out.write(f'{self.index} | **[{self.name}]({self.anchor} "Navigate to {self.name}")** | {self.circle} | {self.brief}\n')
def write_full(self, out: TextIOWrapper):
run_time = {
"-1": "Perk is **instant**, no timer is run.",
"0": "Perk runs for the **default time**.",
}.get(self.time, f"Perk runs for custom time of **{self.time} seconds**.")
settings_brief = "_none_"
if len(self.settings) > 0:
settings_brief = "{`" + "`, `".join(set(map(attrgetter("name"), self.settings))) + "`}"
tags = "{`" + self.tags.replace(", ", "`, `") + "`}"
out.writelines([
f"{self.header}\n",
"**ID** | **TOKEN** | **SETTINGS** | **TAGS** | **SOURCE**\n-:|:-:|:-:|:-:|:-:\n",
f'{self.index} | `{self.token}` | {settings_brief} | {tags} | [{self.token}.sp]({self.filepath} "View source code of {self.name}")\n',
"### Description\n",
self.description.replace(". ", ".\n") + "\n",
f"### Time\n{run_time}\n",
self._get_settings(),
self._get_notes(),
])
def _get_settings(self) -> str:
if len(self.settings) == 0:
return ""
return "\n".join(["### Settings"] + [*map(str, self.settings)]) + "\n"
def _get_notes(self) -> str:
if len(self.notes) == 0:
return ""
return "### Additional notes\n* " + "\n* ".join(self.notes) + "\n"
@classmethod
def from_config(cls, index: int, perk: Mapping):
docs = perk["docs"]
settings = []
if "settings" in docs:
for name, values in docs["settings"].items():
for value, description in values.items():
default = perk["settings"][name]
settings.append(cls._Setting(name, value, default, description))
extra_notes = []
if perk.get("no_medieval", "0") != "0":
extra_notes.append("Perk is disabled in Medieval Mode.")
if (limit_team := perk.get("limit_team", "0")) != "0":
plural = "use" if limit_team == "1" else "uses"
extra_notes.append(f"Perk is limited to {limit_team} active {plural} per team.")
if (limit_global := perk.get("limit_global", "0")) != "0":
plural = "use" if limit_global == "1" else "uses"
extra_notes.append(f"Perk is limited to {limit_global} active {plural}.")
return cls(
index,
perk["good"] == "1",
perk["name"],
perk["token"],
docs["brief"].replace("''", "\""),
docs["description"].replace("''", "\""),
perk["time"],
perk["tags"],
settings,
extra_notes + docs["notes"].replace("''", "\"").split(";") if "notes" in docs else []
)
def main(output: Path):
with open(Path.cwd() / "configs" / "rtd2_perks.default.cfg", encoding="utf-8") as f:
perk_ids = vdf.load(f)["Effects"]
perks_docs = list(starmap(PerkDocs.from_config, perk_ids.items()))
with open(output, "w", encoding="utf-8") as out:
out.writelines([
f"> Automatically generated from [rtd2_perks.default.cfg]({REPO_ROOT}/configs/rtd2_perks.default.cfg). Please edit that file for any updates.\n",
"# Perk Briefs\n",
"**ID** | **NAME** | | **SHORT DESCRIPTION**\n",
"-:|-:|-|-\n",
])
for doc in perks_docs:
doc.write_brief(out)
out.write("\n***\n")
for doc in perks_docs:
out.write("***\n\n")
doc.write_full(out)
out.write("\n")
print(f"Written to \"{output}\"", flush=True)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
main((Path.cwd() / sys.argv[1]).with_suffix(".md"))