-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbingo_test.py
More file actions
executable file
·306 lines (298 loc) · 16.5 KB
/
bingo_test.py
File metadata and controls
executable file
·306 lines (298 loc) · 16.5 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import time
from events.models.tasks import TaskType, BaseTask
from utils.format import convert_from_ms, format_number
from utils.redis import redis_client
from events.models import EventTask, BingoGameModel, EventTeamModel, BingoBoardTile, BingoBoardModel
from events.generators.BingoBoardGen import BingoBoard
import os
from db.models import ItemList, session, NpcList
from events.helpers.ids import get_item_id, get_npc_id
real = False
def chunk_list(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
async def regenerate_board(bingo_game: BingoGameModel, team_id: int = None):
tasks = bingo_game.event.tasks
# task_locations would be stored in a list of dicts, with the task_id as the key and the location as the value
## i,e [{"id": 1, "loc": (i, j)}]
location_config = [config.config_value for config in bingo_game.event.configurations if config.config_key == "task_locations"]
board = BingoBoard(size=5)
for task in tasks:
if task.task_type == TaskType.ITEM_COLLECTION:
if task.task_config["requires"] == "set":
sets: list[list[str]] = task.task_config["sets"]
ids = [[get_item_id(item) for item in set] for set in sets]
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "FULL SET")
elif task.task_config["requires"] == "points":
ids = [get_item_id(item) for item in task.task_config["items"]]
# Chunk into rows of 5 for better display
ids = list(chunk_list(ids, 5))
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "POINTS")
elif task.task_config["requires"] == "all":
items = {}
for item, value in task.task_config["required_items"].items():
items[get_item_id(item)] = value
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_items_with_extras(i, j, items, task.id, task.name, "ALL ITEMS")
elif task.task_config["requires"] == "any":
ids = [get_item_id(item) for item in task.task_config["required_items"]]
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "ANY ITEM")
else:
print(f"Task {task.name} has no required items and is not a set-based task")
elif task.task_type == TaskType.XP_TARGET:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_skill_with_extras(i, j, [task.task_config["skill_name"]], task.id, task.name, "XP TARGET")
elif task.task_type == TaskType.KC_TARGET:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
kc_str = format_number(task.task_config["target_kc"]) + " "
board.set_cell_npc_with_extras(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, "KC TARGET", kc_value=kc_str)
elif task.task_type == TaskType.EHP_TARGET or task.task_type == TaskType.EHB_TARGET:
if task.task_config.get("target_ehp", None) is not None:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_skill_with_extras(i, j, ["ehp"], task.id, task.name, "EHP TARGET")
elif task.task_config.get("target_ehb", None) is not None:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_skill_with_extras(i, j, ["ehb"], task.id, task.name, "EHB TARGET")
elif task.task_type == TaskType.LOOT_VALUE:
gp_required = task.task_config.get("target_value", None)
if gp_required is not None:
gp_str = format_number(gp_required) + " "
else:
gp_str = ""
if task.task_config.get("source_npcs", None) is not None:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
board.set_cell_npc_gp_target(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, f"TOTAL LOOT", gp_str)
else:
if real:
location_data = [loc for loc in location_config if loc["id"] == task.id]
i,j = location_data[0]["loc"]
board.set_cell_items_with_extras(i, j, [1004], task.id, task.name, f"TOTAL LOOT")
elif task.task_type == TaskType.CUSTOM:
pass
i += 1
if i >= 5:
i = 0
j += 1
if j >= 5:
break
if team_id is not None:
## Mark tiles completed for tasks this team has already finished
team = session.query(EventTeamModel).filter(EventTeamModel.team_id == team_id).first()
if team is not None:
assigned_tasks = team.assigned_tasks
for task in assigned_tasks:
if task.status == "completed":
if task.task_id in location_config:
i,j = location_config[task.task_id]["loc"]
board.mark_cell_completed(i, j)
show_free_space = [config.config_value for config in bingo_game.event.configurations if config.config_key == "show_free_space"]
if show_free_space:
board.draw_free_space_tile()
if team_id:
board.save(f"static/assets/img/bingo_board_{team_id}.png")
else:
board.save(f"static/assets/img/bingo_board.png")
if __name__ == "__main__":
board = BingoBoard(size=5)
num = 64
while os.path.exists("static/assets/img/bingo_board_{num}.png"):
num += 1
tasks = session.query(BaseTask).all()
# Initialize grid position
i, j = 0, 0
# For a real event,
real = True
if real:
print("Generating real board")
bingo_game = session.query(BingoGameModel).filter(BingoGameModel.event_id == 3).first()
board_model = session.query(BingoBoardModel).filter(BingoBoardModel.event_id == bingo_game.event_id).first()
tiles = session.query(BingoBoardTile).filter(BingoBoardTile.board_id == board_model.board_id).all()
for tile in tiles:
print(f"Using tile {tile.task_id} for tile {tile.position_x}, {tile.position_y}")
task = tile.task
if task.task_type == TaskType.ITEM_COLLECTION:
if task.task_config["requires"] == "set":
sets: list[list[str]] = task.task_config["sets"]
ids = [[get_item_id(item) for item in set] for set in sets]
j, i = tile.position_x, tile.position_y
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "FULL SET")
elif task.task_config["requires"] == "points":
ids = [get_item_id(item) for item in task.task_config["items"]]
# Chunk into rows of 5 for better display
ids = list(chunk_list(ids, 5))
j, i = tile.position_x, tile.position_y
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "POINTS")
elif task.task_config["requires"] == "all":
ids = [get_item_id(item) for item in task.task_config["required_items"]]
j, i = tile.position_x, tile.position_y
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "ALL ITEMS")
elif task.task_config["requires"] == "any":
ids = [get_item_id(item) for item in task.task_config["required_items"]]
j, i = tile.position_x, tile.position_y
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "ANY ITEM")
else:
print(f"Task {task.name} has no required items and is not a set-based task")
elif task.task_type == TaskType.XP_TARGET:
j, i = tile.position_x, tile.position_y
board.set_cell_skill_with_extras(i, j, [task.task_config["skill_name"]], task.id, task.name, "XP TARGET")
elif task.task_type == TaskType.KC_TARGET:
j, i = tile.position_x, tile.position_y
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
kc_str = format_number(task.task_config["target_kc"]) + " "
board.set_cell_npc_with_extras(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, "KC TARGET", kc_value=kc_str)
elif task.task_type == TaskType.EHP_TARGET or task.task_type == TaskType.EHB_TARGET:
if task.task_config.get("target_ehp", None) is not None:
j, i = tile.position_x, tile.position_y
board.set_cell_skill_with_extras(i, j, ["ehp"], task.id, task.name, "EHP TARGET")
elif task.task_config.get("target_ehb", None) is not None:
j, i = tile.position_x, tile.position_y
board.set_cell_skill_with_extras(i, j, ["ehb"], task.id, task.name, "EHB TARGET")
elif task.task_type == TaskType.LOOT_VALUE:
gp_required = task.task_config.get("target_value", None)
if gp_required is not None:
gp_str = format_number(gp_required) + " "
else:
gp_str = ""
if task.task_config.get("source_npcs", None) is not None:
j, i = tile.position_x, tile.position_y
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
board.set_cell_npc_gp_target(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, f"TOTAL LOOT", gp_str)
else:
j, i = tile.position_x, tile.position_y
board.set_cell_items_with_extras(i, j, [1004], task.id, task.name, f"TOTAL LOOT")
elif task.task_type == TaskType.KILL_TIME:
j, i = tile.position_x, tile.position_y
board.set_cell_npc_time_target(i, j, [get_npc_id(task.task_config['target_npc'])], task.id, task.name, "KILL TIME", time_value=convert_from_ms(task.task_config["target_time"]))
elif task.task_type == TaskType.CUSTOM:
pass
i += 1
if i >= 5:
i = 0
j += 1
if j >= 5:
break
board.draw_free_space_tile()
board.save(f"static/assets/img/bingo_board_{num}.png")
print(f"Generated real board from database with ID {num}")
exit()
for task in tasks:
if i == 2 and j == 2:
i += 1
if i >= 5:
i = 0
j += 1
if j >= 5:
break
continue
if task.task_type == TaskType.ITEM_COLLECTION:
if task.task_config["requires"] == "set":
sets: list[list[str]] = task.task_config["sets"]
ids = [[get_item_id(item) for item in set] for set in sets]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "FULL SET")
elif task.task_config["requires"] == "points":
ids = [get_item_id(item) for item in task.task_config["items"]]
# Chunk into rows of 5 for better display
ids = list(chunk_list(ids, 5))
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "POINTS")
elif task.task_config["requires"] == "all":
ids = [get_item_id(item) for item in task.task_config["required_items"]]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "ALL ITEMS")
elif task.task_config["requires"] == "any":
ids = [get_item_id(item) for item in task.task_config["required_items"]]
board.set_cell_items_with_extras(i, j, ids, task.id, task.name, "ANY ITEM")
else:
print(f"Task {task.name} has no required items and is not a set-based task")
elif task.task_type == TaskType.XP_TARGET:
board.set_cell_skill_with_extras(i, j, [task.task_config["skill_name"]], task.id, task.name, "XP TARGET")
elif task.task_type == TaskType.KC_TARGET:
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
kc_str = format_number(task.task_config["target_kc"]) + " "
board.set_cell_npc_with_extras(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, "KC TARGET", kc_value=kc_str)
elif task.task_type == TaskType.EHP_TARGET or task.task_type == TaskType.EHB_TARGET:
if task.task_config.get("target_ehp", None) is not None:
board.set_cell_skill_with_extras(i, j, ["ehp"], task.id, task.name, "EHP TARGET")
elif task.task_config.get("target_ehb", None) is not None:
board.set_cell_skill_with_extras(i, j, ["ehb"], task.id, task.name, "EHB TARGET")
elif task.task_type == TaskType.LOOT_VALUE:
gp_required = task.task_config.get("target_value", None)
if gp_required is not None:
gp_str = format_number(gp_required) + " "
else:
gp_str = ""
if task.task_config.get("source_npcs", None) is not None:
npcs = []
for npc in task.task_config["source_npcs"]:
if any(existing_npc[:7] == npc[:7] for existing_npc in npcs):
print("Skipping npc", npc)
continue
npcs.append(npc)
board.set_cell_npc_gp_target(i, j, [get_npc_id(npc) for npc in npcs], task.id, task.name, f"TOTAL LOOT", gp_str)
else:
board.set_cell_items_with_extras(i, j, [1004], task.id, task.name, f"TOTAL LOOT")
elif task.task_type == TaskType.KILL_TIME:
board.set_cell_npc_time_target(i, j, [get_npc_id(task.task_config['target_npc'])], task.id, task.name, "KILL TIME", time_value=convert_from_ms(task.task_config["target_time"]))
elif task.task_type == TaskType.CUSTOM:
pass
i += 1
if i >= 5:
i = 0
j += 1
if j >= 5:
break
board.draw_free_space_tile()
board.save(f"static/assets/img/bingo_board_{num}.png")
board.mark_cell_completed(0,0)
board.mark_cell_completed(0,1)
board.mark_cell_completed(0,3)
board.mark_cell_completed(4,4)
board.save(f"static/assets/img/bingo_board_completed_{num}.png")
print(f"Saved number {num}")