-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame_db_editor.py
executable file
·268 lines (214 loc) · 7.57 KB
/
game_db_editor.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
from typing import List, Optional, Tuple
import os
import sys
from builtins import input
from click import Choice, prompt
from CYLGame.Database import GameDB
gamedb: Optional[GameDB] = None
cur_school: Optional[str] = None
cur_comp: Optional[str] = None
def clear():
os.system("clear")
def pause(prompt="Enter any input: "):
get_input(prompt, lambda x: 1)
def get_input(prompt="", validator=lambda x: x, error_msg=""):
inp = input(prompt)
while not validator(inp):
print(error_msg)
inp = input(prompt)
return inp
def print_menu(options: List[Tuple], title: str, enable_no_selection=True) -> Optional:
"""
options should be in the form [(name, value)] example: [("School #1", "ABC")]
"""
clear()
print(title)
if enable_no_selection:
print("0: Return without selecting an option")
for index, (option, value) in enumerate(options):
print(str(index + 1) + ":", option)
start_index = 0 if enable_no_selection else 1
choices = Choice(list(map(str, range(start_index, len(options) + 1))))
choice = prompt("Select an item", type=choices)
if choice == "0":
print("Selecting None")
return None
print(options)
return options[int(choice) - 1][1]
def clear_selection():
global cur_comp, cur_school
cur_comp = None
cur_school = None
def add_competition():
global gamedb, cur_comp
clear()
comp_name = get_input("Enter New Competition Name: ")
# TODO(derpferd): add school selection
# TODO(derpferd): add token selection from school
clear_selection()
cur_comp = gamedb.add_new_competition(comp_name)
# # select the top scoring bots from each school
# for school in gamedb.get_school_tokens():
# top_bot = None
# top_score = -1
# for user in gamedb.get_tokens_for_school(school):
# score = gamedb.get_avg_score(user)
# if score is None:
# score = 0
# if score > top_score:
# top_score = score
# top_bot = user
# if top_bot is not None:
# gamedb.set_token_for_comp(token, top_bot, school)
print("Don't forget to run competition sim script with the following token:", cur_comp)
pause()
def select_competition():
global gamedb, cur_comp
options = []
for i, comp_tk in enumerate(gamedb.get_comp_tokens()):
options += [(gamedb.get_name(comp_tk), comp_tk)]
clear_selection()
cur_comp = print_menu(options, "Select Competition")
print("Current Competition Set to:", cur_comp)
def add_school_to_comp():
global gamedb, cur_comp
options = []
for i, school_tk in enumerate(gamedb.get_school_tokens()):
options += [(gamedb.get_name(school_tk), school_tk)]
selection = print_menu(options, "Select School")
if selection is None:
print("No School added")
else:
gamedb.add_school_to_comp(cur_comp, selection)
print("School added")
print("Don't forget to run competition sim script with the following token:", cur_comp)
pause()
def list_schools_in_comp():
global gamedb, cur_comp
clear()
print("Schools")
for token in gamedb.get_schools_in_comp(cur_comp):
print(gamedb.get_name(token))
pause()
# TODO(derpferd): add function to remove a school
def add_school():
global gamedb, cur_school
clear()
school_name = get_input("Enter New School Name: ")
clear_selection()
cur_school = gamedb.add_new_school(school_name)
print("Current School Set to:", cur_school)
def select_school():
global gamedb, cur_school
options = []
for i, school_tk in enumerate(gamedb.get_school_tokens()):
options += [(gamedb.get_name(school_tk), school_tk)]
clear_selection()
cur_school = print_menu(options, "Select School")
print("Current School Set to:", cur_school)
def get_new_tokens():
global gamedb, cur_school
clear()
count = int(get_input("How many tokens would you like: ", lambda x: x.isdigit(), "Please enter a number."))
clear()
print("New tokens")
for _ in range(count):
print(gamedb.get_new_token(cur_school))
pause()
def list_tokens():
global gamedb, cur_school
clear()
print("Tokens")
for token in gamedb.get_tokens_for_school(cur_school):
print(token)
pause()
def view_exceptions():
global gamedb
clear()
options = []
for token in gamedb.get_exception_tokens():
options.append((token, token))
selection = print_menu(options, "Select Exception")
clear()
if selection:
print(gamedb.get_exception(selection))
pause()
def view_code_from_hash():
global gamedb
clear()
code_hash = get_input("Enter Code hash: ")
code = gamedb.get_code_by_hash(code_hash)
if isinstance(code, list):
options = [(x, x) for x in code]
option = print_menu(options, "There were multiple matches, select one:")
if not option:
return
code = gamedb.get_code_by_hash(option)
clear()
print(code)
pause()
def get_main_menu_options():
global cur_school
options = ["Add New School", "Select School", "Add New Competition", "Select Competition"]
if cur_school is not None:
options += ["Get new Tokens", "List current Tokens"]
if cur_comp is not None:
# TODO(derpferd): implement
options += ["Add School to Competition", "List Schools in Competition"]
options.append("View Exceptions")
options.append("Code from Hash")
return options + ["Quit"]
def get_main_menu_title():
global gamedb, cur_school, cur_comp
title = "Main Menu"
if cur_school is not None:
title += "\n\nSelected School: " + gamedb.get_name(cur_school) + " (" + cur_school + ")"
if cur_comp is not None:
title += "\n\nSelected Competition: " + gamedb.get_name(cur_comp) + " (" + cur_comp + ")"
return title
def main():
global gamedb
print("Welcome to the GameDB Editor")
print("!!!!WARNING!!!!!!")
print("If you do NOT know what you are doing. Please exit now!!!")
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
game_path = os.path.abspath(sys.argv[1])
print("You choose", game_path, "as a game path.")
else:
game_path = os.path.abspath(
get_input(
"Your current dir is '" + os.path.abspath(os.curdir) + "'\nEnter path to game dir: ",
lambda x: os.path.exists(x),
error_msg="Invalid Game Directory. Try Again.",
)
)
gamedb = GameDB(game_path)
option = ""
while option != "Quit":
options = get_main_menu_options()
options = [(x, x) for x in options]
option = print_menu(options, get_main_menu_title(), enable_no_selection=False)
print("You selected:", option)
if option == "Select School":
select_school()
elif option == "Select Competition":
select_competition()
elif option == "Add New Competition":
add_competition()
elif option == "Add School to Competition":
add_school_to_comp()
elif option == "List Schools in Competition":
list_schools_in_comp()
elif option == "Add New School":
add_school()
elif option == "Get new Tokens":
get_new_tokens()
elif option == "List current Tokens":
list_tokens()
elif option == "View Exceptions":
view_exceptions()
elif option == "Code from Hash":
view_code_from_hash()
if __name__ == "__main__":
main()