forked from ArchipelagoMW/AsyncTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateMystery.py
110 lines (90 loc) · 4.08 KB
/
GenerateMystery.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
import os
import yaml
from colorama import Fore
# Please don't look too closely at this code below. kthx -Phar
class MysterySettings(dict):
def __init__(self, name: str, description: str, requires: dict, game: dict):
super().__init__()
self["name"] = name
self["description"] = description
self["requires"] = requires
self["game"] = game
def __str__(self) -> str:
string = "{:<32} {:<4} {:>8}\n".format("GAME", "WGT", "%")
string += "-" * 46
string += "\n"
for game, percentage in sorted(self.weight_percentages().items(), key=lambda x:x[1], reverse=True):
string += "{:<32} {:<4} ~{:>6}\n".format(game, self["game"][game], "{:.1f}%".format(percentage * 100))
return string
def total_game_weights(self) -> int:
total = 0
for weight in self["game"].values():
total += weight
return total
def weight_percentages(self) -> dict:
total = self.total_game_weights()
games = {}
for game, weight in self["game"].items():
games[game] = weight / total
return games
def main():
# Create output directory, if it does not exist.
if not os.path.exists("output/"):
os.mkdir("output/")
# Delete any old mystery.yaml file, if it exists.
if os.path.exists("output/mystery.yaml"):
os.remove("output/mystery.yaml")
# Load meta data first.
mystery: dict
meta: dict = {
"meta_description": "Created by AsyncTools",
None: {
"progression_balancing": 0,
}
}
print("Loading meta player settings...")
try:
with open("games/__meta__.yaml") as file:
data = yaml.unsafe_load(file)
mystery = MysterySettings(data["name"], data["description"], data["requires"], data["game"])
except FileNotFoundError:
print(Fore.RED + "__meta__.yaml not found. Please ensure file exists and rerun generator.")
exit(3)
print(f"Estimated chance of a particular game being rolled...\n\n{mystery}")
print("Attempting to generate yaml file...")
# Merge together each game yaml into our mystery settings.
for game in mystery["game"].keys():
try:
print(f"Loading ./games/{game}.yaml...")
with open(f"games/{game}.yaml", encoding="utf-8-sig") as game_file:
game_settings: dict = yaml.unsafe_load(game_file)
# Ensure that game_settings only has one property which has the same name as the file.
if len(game_settings.items()) > 1:
raise ValueError(f"Found {len(game_settings)} top-level keys in `./games/{game}.yaml`")
if game not in game_settings:
raise ValueError(f"Could not find `{game}` top-level key in `./games/{game}.yaml`")
mystery.update(game_settings)
except FileNotFoundError:
print(Fore.RED + f"\nUnable to find game settings file `./games/{game}.yaml` in games directory.")
exit(1)
except ValueError as e:
print(Fore.RED + f"\nGame settings dict should only have 1 key named after the game.\n\t{e}")
exit(2)
meta_path = os.path.join("games", f"{game}.meta.yaml")
if os.path.exists(meta_path):
with open(meta_path) as meta_file:
meta_settings: dict = yaml.unsafe_load(meta_file)
if len(meta_settings.items()) > 1:
raise ValueError(f"Found {len(meta_settings)} top-level keys in `{meta_path}`")
if game not in meta_settings:
raise ValueError(f"Could not find `{game}` top-level key in `{meta_path}`")
meta.update(meta_settings)
with open("output/mystery.yaml", "w+") as file:
yaml.dump(dict(mystery), file)
print(Fore.GREEN + "\nOutputted settings file to `./output/mystery.yaml`")
meta_path = os.path.join("output", "meta.yaml")
with open(meta_path, "w+") as file:
yaml.dump(meta, file)
print(Fore.GREEN + f"\nOutputted meta file to `{meta_path}`")
if __name__ == "__main__":
main()