133 lines
3.9 KiB
Python
133 lines
3.9 KiB
Python
from nicegui import ui
|
|
from bot_instance import bot
|
|
from dom5game import Dom5game
|
|
import servermanager
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
def create_ui():
|
|
pages = ui.sub_pages()
|
|
rows = []
|
|
for game in bot.tracked_games:
|
|
pages.add(f"/{game.name}", lambda name=game.name: game_page(name))
|
|
rows.append({"Name": game.name, "Turn": game.turn})
|
|
|
|
pages.add("/", lambda: main_page(rows))
|
|
pages.add("/create", lambda: creator_page())
|
|
|
|
ui.run(reload=False)
|
|
|
|
|
|
def main_page(rows):
|
|
game_table = ui.table(rows=rows, title="Currently Running Games")
|
|
with game_table.add_slot("body-cell-Name"):
|
|
with game_table.cell("Name"):
|
|
ui.link().props(":href=props.value :innerHTML=props.value")
|
|
|
|
|
|
def game_page(game_name: str):
|
|
rows = []
|
|
game = Dom5game.get_game_by_name(game_name, bot.tracked_games)
|
|
for player in game.players.keys():
|
|
if game.players[player] not in {"AI", "Eliminated"}:
|
|
rows.append({"Player": player, "Status": game.players[player]})
|
|
ui.table(rows=rows, title=game_name)
|
|
|
|
|
|
# hell
|
|
def creator_page():
|
|
# Game Name
|
|
name = ui.input(
|
|
label="Game Name",
|
|
validation={
|
|
"Game with that name already exists": lambda value: not (
|
|
os.path.isdir("games/" + value)
|
|
)
|
|
},
|
|
).value
|
|
# Port
|
|
load_dotenv()
|
|
port_min = int(os.getenv("PORT_MIN", 1024))
|
|
port_max = int(os.getenv("PORT_MAX", 65535))
|
|
port = ui.input(
|
|
label="Port",
|
|
validation={
|
|
f"Port must be between {str(port_min)} and {str(port_max)}": lambda value: value.isdigit()
|
|
and port_min <= int(value) <= port_max,
|
|
"Port in use": lambda value: value.isdigit()
|
|
and not servermanager.is_port_in_use(int(value)),
|
|
},
|
|
)
|
|
# Era + AI
|
|
# This sucks but idk how else to move the radio selector above the table
|
|
ai_slots = []
|
|
era_container = ui.row()
|
|
table_container = ui.column()
|
|
with era_container:
|
|
era = ( # might not need the variable anymore
|
|
ui.radio({1: "Early Age", 2: "Middle Age", 3: "Late Age"})
|
|
.props("inline")
|
|
.on_value_change(
|
|
lambda e: create_ai_table(e.value, table_container, ai_slots)
|
|
)
|
|
)
|
|
|
|
|
|
def create_ai_table(era, table_container, ai_slots):
|
|
# TODO: move this somewhere else
|
|
AI_LEVELS = [
|
|
"Human",
|
|
"Easy",
|
|
"Normal",
|
|
"Difficult",
|
|
"Mighty",
|
|
"Master",
|
|
"Impossible",
|
|
"Closed",
|
|
]
|
|
table_container.clear()
|
|
nations = servermanager.get_nations()
|
|
with table_container:
|
|
with ui.card().classes("w-full"):
|
|
with ui.row().classes("font-bold w-full"):
|
|
ui.label("ID").classes("w-16")
|
|
ui.label("Nation").classes("w-40")
|
|
ui.label("Title").classes("w-64")
|
|
ui.label("AI Level")
|
|
ui.separator()
|
|
for nation_id, nation in nations[str(era)].items():
|
|
nation_name, title = nation["name"]
|
|
|
|
with ui.row().classes("items-center w-full"):
|
|
ui.label(str(nation_id)).classes("w-16")
|
|
ui.label(nation_name).classes("w-40")
|
|
ui.label(title).classes("w-64")
|
|
|
|
ui.select(
|
|
AI_LEVELS,
|
|
value="Human",
|
|
on_change=lambda e: set_ai_level(e.value, nation_id, ai_slots),
|
|
)
|
|
|
|
|
|
def set_ai_level(value, nation_id, ai_slots):
|
|
level_map = {
|
|
"Closed": 0,
|
|
"Easy": 1,
|
|
"Normal": 2,
|
|
"Difficult": 3,
|
|
"Mighty": 4,
|
|
"Master": 5,
|
|
"Impossible": 6,
|
|
}
|
|
ai_slots[:] = [
|
|
(nation_id, ai_level)
|
|
for nation_id, ai_level in ai_slots
|
|
if nation_id != nation_id
|
|
]
|
|
if value == "Human":
|
|
return
|
|
|
|
ai_slots.append((nation_id, level_map[value]))
|