1
- import base64
2
1
import enum
3
2
import os
4
3
import random
5
4
import re
6
5
import string
7
- import struct
8
6
from datetime import datetime
9
7
10
8
import shortuuid
11
9
from github import Auth , Github , InputFileContent
12
10
13
11
14
12
class ArgFormat (enum .Enum ):
15
- V110 = '-noui -seed={seed_name} -permalink={permalink}'
16
- V111 = '--noui --dry --seed={seed_name} --permalink={permalink}'
17
- RS14 = '--randobot --noui --dry --seed={seed_name} --permalink={permalink}'
13
+ V110 = "-noui -seed={seed_name} -permalink={permalink}"
14
+ V111 = "--noui --dry --seed={seed_name} --permalink={permalink}"
15
+ VS7 = "--noui --dry --seed={seed_name} --permalink={permalink} --modifiers={modifiers}"
16
+ RS14 = "--randobot --noui --dry --seed={seed_name} --permalink={permalink}"
18
17
19
18
20
19
class Generator :
21
- def __init__ (self , github_token ):
20
+ def __init__ (self , github_token : str ):
22
21
self .github_token = github_token
23
22
24
- def generate_seed (self , randomizer_path , permalink , username , generate_spoiler_log ,
25
- args_format : ArgFormat = ArgFormat .V110 ):
26
- trimmed_name = re .sub (r'\W+' , '' , username )[:12 ]
23
+ def generate_seed (
24
+ self ,
25
+ randomizer_path : str ,
26
+ permalink : str ,
27
+ username : str ,
28
+ generate_spoiler_log : bool ,
29
+ modifiers : str = "" ,
30
+ args_format : ArgFormat = ArgFormat .V110 ,
31
+ ) -> dict [str , str | None ]:
32
+ trimmed_name = re .sub (r"\W+" , "" , username )[:12 ]
27
33
random_suffix = shortuuid .ShortUUID ().random (length = 10 )
28
34
seed_name = f"{ trimmed_name } { random_suffix } "
29
35
file_name = "" .join (random .choice (string .digits ) for _ in range (6 ))
30
36
31
37
os .system (
32
- f"/venv/{ randomizer_path } /bin/python { randomizer_path } /wwrando.py " +
33
- args_format .value .format (seed_name = seed_name , permalink = permalink )
38
+ f"/venv/{ randomizer_path } /bin/python { randomizer_path } /wwrando.py "
39
+ + args_format .value .format (seed_name = seed_name , permalink = permalink , modifiers = modifiers )
34
40
)
35
41
36
42
permalink_file_name = f"permalink_{ seed_name } .txt"
37
- permalink_file = open (permalink_file_name , "r" )
38
- permalink = permalink_file .read ()
39
- permalink_file .close ()
43
+ with open (permalink_file_name , "r" ) as permalink_file :
44
+ permalink = permalink_file .read ()
40
45
os .remove (permalink_file_name )
41
46
42
47
seed_hash_file_name = f"seed_hash_{ seed_name } .txt"
43
- seed_hash_file = open (seed_hash_file_name , "r" )
44
- seed_hash = seed_hash_file .read ()
45
- seed_hash_file .close ()
48
+ with open (seed_hash_file_name , "r" ) as seed_hash_file :
49
+ seed_hash = seed_hash_file .read ()
46
50
os .remove (seed_hash_file_name )
47
51
48
52
if generate_spoiler_log :
49
53
spoiler_log_file_name = f"spoiler_log_{ seed_name } .txt"
50
- spoiler_log_file = open (spoiler_log_file_name , "r" )
51
- spoiler_log = spoiler_log_file .read ()
52
- spoiler_log_file .close ()
54
+ with open (spoiler_log_file_name , "r" ) as spoiler_log_file :
55
+ spoiler_log = spoiler_log_file .read ()
53
56
os .remove (spoiler_log_file_name )
54
57
55
58
timestamp = datetime .now ().strftime ("%Y-%m-%d-%H:%M:%S" )
@@ -59,7 +62,7 @@ def generate_seed(self, randomizer_path, permalink, username, generate_spoiler_l
59
62
gist = gh_auth_user .create_gist (
60
63
public = False ,
61
64
files = {f"spoiler_log_{ timestamp } .txt" : InputFileContent (spoiler_log )},
62
- description = "The Wind Waker Randomizer Spoiler Log"
65
+ description = "The Wind Waker Randomizer Spoiler Log" ,
63
66
)
64
67
spoiler_log_url = gist .html_url
65
68
else :
@@ -69,106 +72,5 @@ def generate_seed(self, randomizer_path, permalink, username, generate_spoiler_l
69
72
"file_name" : file_name ,
70
73
"permalink" : permalink ,
71
74
"seed_hash" : seed_hash ,
72
- "spoiler_log_url" : spoiler_log_url
75
+ "spoiler_log_url" : spoiler_log_url ,
73
76
}
74
-
75
- @staticmethod
76
- def encode_permalink (version , seed , bit_array ):
77
- # NOTE: This function only works with the official 1.10 version of the randomizer!
78
-
79
- # Start the permalink with the version number and seed name
80
- permalink = b""
81
- permalink += version
82
- permalink += b"\0 "
83
- permalink += seed
84
- permalink += b"\0 "
85
-
86
- # Unpack the bytes object as a bit array
87
- bit_array = bit_array [::- 1 ]
88
- bytes_array = [bit_array [i : i + 8 ] for i in range (0 , len (bit_array ), 8 )]
89
- for byte in bytes_array :
90
- byte = int ("" .join (byte [::- 1 ]), 2 )
91
- permalink += struct .pack (">B" , byte )
92
-
93
- return base64 .b64encode (permalink ).decode ("ascii" )
94
-
95
- @staticmethod
96
- def decode_permalink (base64_encoded_permalink ):
97
- # NOTE: This function only works with the official 1.10 version of the randomizer!
98
-
99
- # Decode permalink into a bytes object
100
- permalink = base64 .b64decode (base64_encoded_permalink )
101
-
102
- # Split the permalink into the version, seed name, and option bits
103
- version , seed , options_bytes = permalink .split (b"\0 " , 2 )
104
-
105
- # Unpack the option bytes object as a bit array
106
- option_bytes = struct .unpack (f">{ 'B' * len (options_bytes )} " , options_bytes )
107
- bit_array = []
108
- for byte in option_bytes :
109
- bit_array .extend (list (bin (byte )[2 :].zfill (8 ))[::- 1 ])
110
- bit_array = bit_array [::- 1 ]
111
-
112
- return version , seed , bit_array
113
-
114
- @staticmethod
115
- def apply_ra_modifier (base_permalink , modifier ):
116
- # NOTE: This function only works with the official 1.10 version of the randomizer!
117
-
118
- # Convert the permalink to a bit array
119
- version , seed , bit_array = Generator .decode_permalink (base_permalink )
120
-
121
- # Modify bit array depending on modifier
122
- match modifier :
123
- case "4drm" :
124
- bit_array [87 ] = "0"
125
- bit_array [88 ] = "1"
126
- bit_array [89 ] = "1"
127
- case "nosword" :
128
- bit_array [98 ] = "0"
129
- bit_array [99 ] = "1"
130
- case "der" :
131
- bit_array [142 ] = "0"
132
- bit_array [143 ] = "0"
133
- bit_array [144 ] = "1"
134
- case "keys" :
135
- bit_array [145 ] = "1"
136
- case "tingle" :
137
- bit_array [149 ] = "1"
138
- case "expen" :
139
- bit_array [151 ] = "1"
140
- case "subs" :
141
- bit_array [156 ] = "1"
142
- case "minis" :
143
- bit_array [160 ] = "1"
144
- case "combat" :
145
- bit_array [164 ] = "1"
146
-
147
- # Encode updated bit array back into a permalink
148
- updated_permalink = Generator .encode_permalink (version , seed , bit_array )
149
-
150
- return updated_permalink
151
-
152
- @staticmethod
153
- def update_hint_distribution_for_ra (base_permalink ):
154
- # NOTE: This function only works with the official 1.10 version of the randomizer!
155
-
156
- # Convert the permalink to a bit array
157
- version , seed , bit_array = Generator .decode_permalink (base_permalink )
158
-
159
- # Set the number of location hints to 8
160
- bit_array [119 ] = "1"
161
- bit_array [120 ] = "0"
162
- bit_array [121 ] = "0"
163
- bit_array [122 ] = "0"
164
-
165
- # Set the number of barren hints to 5
166
- bit_array [123 ] = "0"
167
- bit_array [124 ] = "1"
168
- bit_array [125 ] = "0"
169
- bit_array [126 ] = "1"
170
-
171
- # Encode updated bit array back into a permalink
172
- updated_permalink = Generator .encode_permalink (version , seed , bit_array )
173
-
174
- return updated_permalink
0 commit comments