Spaces:
Sleeping
Sleeping
pratikshahp
commited on
Create randomselection.py
Browse files- randomselection.py +36 -0
randomselection.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def randomly_select_from_json(world_data):
|
2 |
+
world_name = world_data["name"]
|
3 |
+
world_description = world_data["description"]
|
4 |
+
|
5 |
+
valid_kingdoms = {
|
6 |
+
k_name: k_data
|
7 |
+
for k_name, k_data in world_data["kingdoms"].items()
|
8 |
+
if any("npcs" in town_data and town_data["npcs"] for town_data in k_data["towns"].values())
|
9 |
+
}
|
10 |
+
if not valid_kingdoms:
|
11 |
+
raise ValueError("No kingdoms with valid towns and NPCs found.")
|
12 |
+
|
13 |
+
kingdom_name, kingdom_data = random.choice(list(valid_kingdoms.items()))
|
14 |
+
kingdom_description = kingdom_data["description"]
|
15 |
+
|
16 |
+
valid_towns = {
|
17 |
+
t_name: t_data
|
18 |
+
for t_name, t_data in kingdom_data["towns"].items()
|
19 |
+
if "npcs" in t_data and t_data["npcs"]
|
20 |
+
}
|
21 |
+
if not valid_towns:
|
22 |
+
raise ValueError(f"No towns with NPCs found in kingdom: {kingdom_name}")
|
23 |
+
|
24 |
+
town_name, town_data = random.choice(list(valid_towns.items()))
|
25 |
+
town_description = town_data["description"]
|
26 |
+
|
27 |
+
npcs = town_data["npcs"]
|
28 |
+
character_name, character_data = random.choice(list(npcs.items()))
|
29 |
+
character_description = character_data["description"]
|
30 |
+
|
31 |
+
return {
|
32 |
+
"world": {"name": world_name, "description": world_description},
|
33 |
+
"kingdom": {"name": kingdom_name, "description": kingdom_description},
|
34 |
+
"town": {"name": town_name, "description": town_description},
|
35 |
+
"character": {"name": character_name, "description": character_description},
|
36 |
+
}
|