Files
dom5bot/webui.py

173 lines
5.4 KiB
Python

from nicegui import ui
from pandas.core.internals.blocks import external_values
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)
)
},
)
# 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 = (
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)
)
)
# client_start = ui.checkbox("Allow clients to start the game")
checkbox_container = ui.row()
with checkbox_container:
team_game = ui.checkbox("Disciple Game")
clustered_start = ui.checkbox("Clustered Spawns (Disciple games only)")
random_start_research = ui.checkbox("Random Start Research")
score_graphs = ui.checkbox("Score Graphs")
renaming = ui.checkbox("Allow Renaming", value=True)
no_nation_info = ui.checkbox("No Info About Other Nations")
no_cheat_det = ui.checkbox("Disable Anticheat")
no_artifact_rest = ui.checkbox("Disable Artifact Forge Limit")
no_new_ai = ui.checkbox("Disable Becoming AI Controlled")
vwrap = ui.checkbox("North South Wrapping")
hwrap = ui.checkbox("East West Wrapping")
description_container = ui.row()
toggle_container = ui.row()
with toggle_container:
event_rarity = ui.toggle({1: "Common", 2: "Rare"}, value=1)
random_map = ui.toggle([10, 15, 20], value=15)
with description_container:
ui.label("Event Rarity")
# TODO: Fix Padding
ui.label("Provinces per player:")
# TODO: REMOVE THIS it is just for testing
ui.button(
"Generate String",
on_click=lambda: print(
servermanager.server_command_builder(
name=name.value,
port=int(port.value),
era=int(era.value),
ai_slots=ai_slots,
random_map=15,
)
),
)
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")
# TODO: Set all button
# TODO: Random nations?
ui.select(
AI_LEVELS,
value="Human",
on_change=lambda e, nid=nation_id: set_ai_level(
e.value, nid, ai_slots
),
)
def set_ai_level(value, nid, ai_slots):
level_map = {
"Closed": 0,
"Easy": 1,
"Normal": 2,
"Difficult": 3,
"Mighty": 4,
"Master": 5,
"Impossible": 6,
}
for ai in ai_slots:
if ai[0] == nid:
ai_slots.remove(ai)
if value == "Human":
return
ai_slots.append((nid, level_map[value]))