Compare commits

..

40 Commits

Author SHA1 Message Date
9a1c18f815 Added name symbol validation. 2026-06-05 12:06:28 +02:00
bb2388c914 Added multiplier options. 2026-06-05 12:01:55 +02:00
f76d0ce63a Dark mode, maybe? 2026-06-05 11:19:21 +02:00
3beb9b67f7 Fixed master select binding both ways. 2026-06-05 11:15:53 +02:00
2bf7898720 Added comment. 2026-06-05 11:09:26 +02:00
64dd7cc3ae Changed table layout and added set all button for ai. 2026-06-05 11:08:41 +02:00
49e090b3c9 Fixed typo in command name. 2026-06-03 11:00:42 +02:00
efc9145f80 Fixed AI validation. 2026-06-02 17:33:31 +02:00
bfb3f74342 No comment. We do not speak of this. 2026-06-02 17:29:39 +02:00
d6a56f5084 I must have been high while writing that. 2026-06-02 17:23:29 +02:00
fac0b12c21 Added TODO comments. 2026-06-02 17:16:19 +02:00
fe53cba904 Fixed nation id being captured too late. 2026-06-02 17:14:04 +02:00
e8b6edb0d8 Added checkbox and toggle options. 2026-06-01 16:13:06 +02:00
965fea2227 Added string generation for testing. 2026-06-01 15:22:38 +02:00
9f0059e833 Added missing space between -TS and game name. 2026-06-01 15:22:16 +02:00
65cd0906a4 Added era and ai level selector. 2026-06-01 15:10:12 +02:00
31e4ec08c4 Merged ai_slots and closed_slots into one. 2026-06-01 15:07:41 +02:00
6e52242483 Added Era selection. 2026-06-01 13:51:21 +02:00
29df7b2a53 fixed port input validation 2026-06-01 13:48:36 +02:00
6ca35c12e2 Added name and port fields for game creation. 2026-05-30 16:31:30 +02:00
06514cc4c2 Testing game creator page. 2026-05-30 16:05:21 +02:00
caba8d42bd nvm i'm stupid 2026-05-30 16:01:54 +02:00
84b84f45b6 Added function to check if folder exists. 2026-05-30 14:40:52 +02:00
6d1ff496dd Added duplicate Ai check. 2026-05-03 01:35:34 +02:00
c62eef559d Minor fixes. 2026-05-03 01:32:02 +02:00
6aeb7fd303 Forgor to actually add ai. 2026-05-03 01:23:01 +02:00
ca7283d57a Fixed command formatting having too many spaces. 2026-05-03 01:12:35 +02:00
36be931e9f Added all (for now) features to the command builder 2026-05-03 00:59:25 +02:00
e8bba85cd2 Continued work on the command builder. 2026-04-28 18:01:33 +02:00
fbb434563d Added input validation for team nations. 2026-04-28 17:36:27 +02:00
54653282aa Fixed get_nations returning nothing 2026-04-21 18:21:04 +02:00
c9811a9a01 First attempt of modular nation list. 2026-04-21 18:16:50 +02:00
01fb7dee2c forgor 2026-04-21 17:42:21 +02:00
f0eb658913 Started work on implementing most server args. 2026-04-15 17:31:46 +02:00
92b2f653cb Added links to the main page table. 2026-04-12 14:18:17 +02:00
0300f19727 Testing sub pages. 2026-04-12 13:52:21 +02:00
35df0e8263 Forgor. 2026-04-11 01:54:08 +02:00
c34c768dcb Changed everything, gonna clean up later. 2026-04-11 01:53:46 +02:00
aaff712a04 Changed details command to exclude AI. 2026-04-10 14:49:57 +02:00
305ae44b66 fixed bot starting multiple times. 2026-04-10 14:19:41 +02:00
5 changed files with 586 additions and 48 deletions

View File

@@ -1,7 +1,25 @@
from discord.ext import commands from discord.ext import commands
import discord import discord
import threading
import os
from dotenv import load_dotenv
_started = False
intents = discord.Intents.default() intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents) bot = commands.Bot(command_prefix="!", intents=intents)
bot.tracked_games = [] bot.tracked_games = []
bot.tracked_games_lock = threading.Lock()
async def start_bot():
global _started
if _started:
return
_started = True
load_dotenv()
TOKEN = os.getenv("TOKEN")
if not TOKEN:
raise ValueError("No token found")
await bot.start(TOKEN)

108
cogs/slash_commands.py Normal file
View File

@@ -0,0 +1,108 @@
import discord
from discord import app_commands
from discord.ext import commands
from dom5game import Dom5game
class SlashCommands(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
def game_autocomplete(self, current):
options = []
for game in self.bot.tracked_games:
options.append(game.name)
return [
app_commands.Choice(name=option, value=option)
for option in options
if option.lower().startswith(current.lower())
][:25]
@app_commands.command(name="ping", description="Check bot latency")
async def ping(self, interaction: discord.Interaction):
await interaction.response.send_message(
f"Pong! {round(self.bot.latency * 1000)}ms"
)
@app_commands.command(
name="dom5-addgame",
description="Adds a game that is already running but not tracked by the bot yet.",
)
async def addgame(self, interaction: discord.Interaction, name: str):
try:
if any(game.name == name for game in self.bot.tracked_games):
await interaction.response.send_message("Game already tracked.")
return
game = Dom5game(name, interaction.channel_id, [], 0, {})
game.update_turn()
game.update_players()
game.to_json()
self.bot.tracked_games.append(game)
await interaction.response.send_message("Added the game.")
except:
await interaction.response.send_message(
"Something went wrong. Are you sure the name is correct and the game exists?"
)
@app_commands.command(name="dom5-creategame", description="Creates a new game.")
async def creategame(self, interaction: discord.Interaction, name: str, port: int):
print("a")
@app_commands.command(
name="dom5-pingme",
description="Signs you up to be pinged for a game. Run the command again to not get pinged anymore.",
)
async def pingme(self, interaction: discord.Interaction, name: str):
game = Dom5game.get_game_by_name(name, self.bot.tracked_games)
if game == None:
await interaction.response.send_message("Game does not exist.")
return
try:
if interaction.user.id in game.members:
game.members.remove(interaction.user.id)
await interaction.response.send_message(
'You will no longer receive turn notifications for game: **"'
+ name
+ '"**.'
)
else:
game.members.append(interaction.user.id)
await interaction.response.send_message(
'You will now receive turn notifications for game: **"'
+ name
+ '"**.'
)
except:
await interaction.response.send_message("Something went wrong.")
@pingme.autocomplete("name")
async def pingme_autocomplete(self, interaction: discord.Interaction, current: str):
return self.game_autocomplete(current)
@app_commands.command(
name="dom5-details", description="Shows the details of an ongoing game."
)
async def details(self, interaction: discord.Interaction, name: str):
game = Dom5game.get_game_by_name(name, self.bot.tracked_games)
if game == None:
await interaction.response.send_message("Game does not exist.")
return
embed = discord.Embed(title=name)
for player in game.players.keys():
if game.players[player] not in {"AI", "Eliminated"}:
embed.add_field(name=player, value=game.players[player])
await interaction.response.send_message(embed=embed)
@details.autocomplete("name")
async def details_autocomplete(
self, interaction: discord.Interaction, current: str
):
return self.game_autocomplete(current)
async def setup(bot: commands.Bot):
await bot.add_cog(SlashCommands(bot))

30
main.py
View File

@@ -1,20 +1,12 @@
import discord import discord
import os import os
from discord.ext import tasks from discord.ext import tasks
from dotenv import load_dotenv
from dom5game import Dom5game from dom5game import Dom5game
from servermanager import create_server
from nicegui import ui
from webui import create_ui from webui import create_ui
from bot_instance import bot from bot_instance import bot, start_bot
from nicegui import ui
import asyncio import asyncio
bot_started = False # prolly only need this in dev mode
load_dotenv()
TOKEN = os.getenv("TOKEN")
if not TOKEN:
raise ValueError("No token found")
def reload_games(): def reload_games():
with bot.tracked_games_lock: with bot.tracked_games_lock:
@@ -27,34 +19,28 @@ def reload_games():
) )
async def start_discord_bot():
await bot.load_extension("cogs.slash_commands")
await bot.start(TOKEN)
def main(): def main():
global bot_started
create_ui() create_ui()
if not bot_started: loop = asyncio.get_event_loop()
bot_started = True loop.create_task(start_bot())
ui.timer(0, lambda: asyncio.create_task(start_discord_bot()), once=True) ui.run(reload=False)
ui.run()
if __name__ in {"__main__", "__mp_main__"}: if __name__ == "__main__":
main() main()
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f"Logged in as {bot.user}") print(f"Logged in as {bot.user}")
await bot.load_extension("cogs.slash_commands")
# await bot.tree.sync() # await bot.tree.sync()
reload_games() reload_games()
if not task_loop.is_running(): if not task_loop.is_running():
task_loop.start() task_loop.start()
@tasks.loop(seconds=2) @tasks.loop(seconds=5)
async def task_loop(): async def task_loop():
await bot.change_presence( await bot.change_presence(
activity=discord.Activity( activity=discord.Activity(

View File

@@ -2,29 +2,140 @@ import subprocess
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
from dom5game import Dom5game from dom5game import Dom5game
import re
load_dotenv() load_dotenv()
SERVER_PATH = os.getenv("SERVERPATH") SERVER_PATH = os.getenv("SERVERPATH")
def create_server(name, port, channel): def create_server(name: str, port: int, channel: int, command: str):
if is_port_in_use(port): if is_port_in_use(port):
return "ERROR_PORT_IN_USE" return "ERROR_PORT_IN_USE"
# os.mkdir("games/" + name) os.mkdir("games/" + name)
# print(SERVER_PATH) game = Dom5game(name, channel, [], 0, {})
# print(server_command_builder(name, port, channel)) game.to_json()
try: try:
subprocess.Popen( p = subprocess.Popen(
server_command_builder(name, port, channel), command,
stdin=None, stdout=subprocess.DEVNULL,
stdout=None, stderr=subprocess.STDOUT,
stderr=None,
) )
except Exception as e: except Exception as e:
return "EXCEPTION OCCURED: " + str(e) return "EXCEPTION OCCURED: " + str(e)
return "SUCCESS"
# p.wait()
def server_command_builder(name, port, channel): def get_nations():
# TODO mod support
nation_string = subprocess.check_output([SERVER_PATH, "--listnations"]).decode()
nations = {}
current_era = None
for line in nation_string.splitlines():
line = line.strip()
era_match = re.match(r"-+ Era (\d+) -+", line)
if era_match:
current_era = f"{era_match.group(1)}"
nations[current_era] = {}
continue
entry_match = re.match(r"(\d+)\s+(.*?),\s+(.*)", line)
if entry_match and current_era:
nation_id = int(entry_match.group(1))
name = entry_match.group(2), entry_match.group(3)
nations[current_era][nation_id] = {"name": name}
return nations
# This is terrible.
def server_command_builder(
name: str,
port: int,
era: int, # 1 EA, 2 MA, 3 LA
ai_slots: list[
tuple[int, int]
] = [], # list of nations that will be ai tuple format: NationID, AI Level (1-6), 0 being closed
client_start: bool = True,
teams: list[tuple[int, int, int]] = [], # list of teams with format nation id,
clustered_start: bool = False,
team_game: bool = False,
mapfile: str = "",
random_map: int = 0,
research_difficulty: int = 2,
random_start_research: bool = True,
hof_size: int = 10,
global_slots: int = 5,
inde_strength: int = 5,
magic_sites: int = 40,
event_rarity: int = 1,
richness: int = 100,
resources: int = 100,
recruitment: int = 100,
supplies: int = 100,
masterpass: str = "",
start_prov: int = 1,
renaming: bool = True,
score_graphs: bool = False,
no_nation_info: bool = False,
no_cheat_det: bool = False,
no_artifact_rest: bool = False,
story_events: int = 1,
new_ai_lvl: int = 2,
no_new_ai: bool = False,
conq_all: bool = False,
thrones: tuple[int, int, int] = (1, 1, 1),
required_apoints: int = 0,
cataclysm: int = 0,
vwrap: bool = True,
hwrap: bool = True,
):
available_nations = get_nations()
# TODO change < ranges to =<
if (not ((random_map == 0) ^ (mapfile == ""))) or random_map not in {0, 10, 15, 20}:
return "ERROR_MAP"
if not (0 < era < 4):
return "ERROR_ERA"
ai_indexes = [t[0] for t in ai_slots]
if not (len(ai_indexes) == len(set(ai_indexes))):
return "ERROR_AIS"
# if bool(set(closed_slots) & set(ai_indexes)):
# return "ERROR_CLOSED_AIS"
if required_apoints == 0:
required_apoints = thrones[0] + (2 * thrones[1]) + (3 * thrones[2]) - 1
if not (0 <= research_difficulty < 5):
return "ERROR_RESEARCH"
if not (4 < hof_size < 16):
return "ERROR_HOF"
if not (2 < global_slots < 10):
return "ERROR_GLOBALS"
if not (0 <= inde_strength <= 9):
return "ERROR_INDEP"
if not (0 <= magic_sites <= 75):
return "ERROR_MAGICSITES"
if not (1 <= event_rarity <= 2):
return "ERROR_EVENTS"
if not (50 <= richness <= 300):
return "ERROR_RICHNESS"
if not (50 <= recruitment <= 300):
return "ERROR_RECRUITMENT"
if not (50 <= resources <= 300):
return "ERROR_RESOURCES"
if not (50 <= supplies <= 300):
return "ERROR_SUPPLIES"
if not (1 <= start_prov <= 9):
return "ERROR_STARTPROV"
if not (0 <= story_events <= 2):
return "ERROR_STORYEVENTS"
if not (1 <= new_ai_lvl <= 6):
return "ERROR_NEWAIS"
command = [ command = [
SERVER_PATH, SERVER_PATH,
" -TS ", " -TS ",
@@ -33,10 +144,109 @@ def server_command_builder(name, port, channel):
str(port), str(port),
" --statuspage ", " --statuspage ",
os.getcwd() + "/games/" + name + "/turnstats.html", os.getcwd() + "/games/" + name + "/turnstats.html",
" --era ",
str(era),
] ]
game = Dom5game(name, channel, [], 0, {}) if team_game:
game.to_json() for team in teams:
return command # TODO: Can team have more than one disciple and pretender?
if team[0] not in available_nations[str(era)]:
return "ERROR_INVALID_NATION"
command.append(
" --team " + str(team[0]) + " " + str(team[1]) + " " + str(team[2])
)
if clustered_start:
command.append(" --clustered")
if not client_start:
command.append(" --noclientstart")
if cataclysm > 0:
command.append(" --cataclysm " + str(cataclysm))
if conq_all:
command.append(" --conqall")
if not research_difficulty == 2:
command.append(" --research " + str(research_difficulty))
if not random_start_research:
command.append(" --norandres")
if not hof_size == 10:
command.append(" --hof_size " + str(hof_size))
if not global_slots == 5:
command.append(" --globals " + str(global_slots))
if not inde_strength == 5:
command.append(" --indepstr " + str(inde_strength))
if not magic_sites == 40:
command.append(" --magisites " + str(magic_sites))
command.append(" --eventrarity " + str(event_rarity))
if not richness == 100:
command.append(" --richness " + str(richness))
if not resources == 100:
command.append(" --resources" + str(resources))
if not recruitment == 100:
command.append(" --recruitment" + str(recruitment))
if not supplies == 100:
command.append(" --supplies" + str(supplies))
if not masterpass == "":
command.append(" --masterpass " + masterpass)
# i am going insane
if not start_prov == 1:
command.append(" --startprov " + str(start_prov))
if renaming:
command.append(" --renaming")
if score_graphs:
command.append(" --scoregraphs")
if no_nation_info:
command.append(" --nonationinfo")
if no_cheat_det:
command.append(" --nocheatdet")
if no_artifact_rest:
command.append(" --noartrest")
if story_events == 1:
command.append(" --storyevents")
elif story_events == 2:
command.append(" --allstoryevents")
command.append(" --newailvl " + str(new_ai_lvl))
if no_new_ai:
command.append(" --nonewai")
if conq_all:
command.append(" --conqall")
command.append(
" --thrones " + str(thrones[0]) + " " + str(thrones[1]) + " " + str(thrones[2])
)
command.append(" --requiredap " + str(required_apoints))
if not cataclysm == 0:
command.append(" --cataclysm " + str(cataclysm))
if vwrap:
command.append(" --vwrap")
if not hwrap:
command.append(" --nohwrap")
if ai_slots:
added_ais = []
for ai in ai_slots:
if ai[0] not in available_nations[str(era)]:
return "ERROR_INVALID_NATION"
if ai[0] in added_ais:
return "ERROR_DUPLICATE_AI"
match ai[1]:
case 0:
command.append(" --closed " + str(ai[0]))
case 1:
command.append(" --easyai " + str(ai[0]))
case 2:
command.append(" --normai " + str(ai[0]))
case 3:
command.append(" --diffai " + str(ai[0]))
case 4:
command.append(" --mightyai " + str(ai[0]))
case 5:
command.append(" --masterai " + str(ai[0]))
case 6:
command.append(" --impai " + str(ai[0]))
case _:
return "ERROR_INVALID_AI"
added_ais.append(ai[0])
return "".join(command)
def is_port_in_use(port: int) -> bool: def is_port_in_use(port: int) -> bool:

236
webui.py
View File

@@ -1,17 +1,233 @@
from nicegui import ui from nicegui import ui
from bot_instance import bot from bot_instance import bot
import re
# reminder: from dom5game import Dom5game
# tracked_games etc erst in andere variable kopieren und lock benutzen. import servermanager
import os
from dotenv import load_dotenv
def create_ui(): def create_ui():
ui.label("Amogus") pages = ui.sub_pages()
with bot.tracked_games_lock:
games = list(bot.tracked_games)
rows = [] rows = []
for game in games: for game in bot.tracked_games:
rows.append({"Name": game.name}) pages.add(f"/{game.name}", lambda name=game.name: game_page(name))
rows.append({"Name": game.name, "Turn": game.turn})
ui.table(rows=rows) pages.add("/", lambda: main_page(rows))
pages.add("/create", lambda: creator_page())
# ui.dark_mode().enable()
ui.run(reload=False, show=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)
),
"Name contains an invalid symbol": lambda value: bool(
re.match("^[A-Za-z0-9_-]*$", 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()
# TODO: Are these containers still needed?
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:")
with ui.row():
ui.label("Gold Multiplier:").classes("w-36")
richness = ui.input(
validation={
"Value must be between 50 and 300": lambda value: value.isdigit()
and 50 <= int(value) <= 300,
},
value="100",
)
with ui.row():
ui.label("Resource Multiplier:").classes("w-36")
resources = ui.input(
validation={
"Value must be between 50 and 300": lambda value: value.isdigit()
and 50 <= int(value) <= 300,
},
value="100",
)
with ui.row():
ui.label("Recruitment Multiplier:").classes("w-36")
recruitment = ui.input(
validation={
"Value must be between 50 and 300": lambda value: value.isdigit()
and 50 <= int(value) <= 300,
},
value="100",
)
with ui.row():
ui.label("Supplies Multiplier:").classes("w-36")
supplies = ui.input(
validation={
"Value must be between 50 and 300": lambda value: value.isdigit()
and 50 <= int(value) <= 300,
},
value="100",
)
# 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=int(random_map.value),
event_rarity=int(event_rarity.value),
hwrap=hwrap.value,
vwrap=vwrap.value,
no_new_ai=no_new_ai.value,
no_artifact_rest=no_artifact_rest.value,
no_cheat_det=no_cheat_det.value,
renaming=renaming.value,
score_graphs=score_graphs.value,
random_start_research=random_start_research.value,
clustered_start=clustered_start.value,
team_game=team_game.value,
no_nation_info=no_nation_info.value,
recruitment=int(recruitment.value),
resources=int(resources.value),
richness=int(richness.value),
supplies=int(supplies.value),
)
),
)
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-104")
ui.label("AI Level")
ui.separator()
with ui.row().classes("items-center, w-full"):
ui.label("All").classes("w-124")
master_select = ui.select(
AI_LEVELS,
value="Human",
)
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 + ", " + title).classes("w-104")
# TODO: Random nations?
# Although that logic should probably go into servermanager.py
ui.select(
AI_LEVELS,
value="Human",
on_change=lambda e, nid=nation_id: set_ai_level(
e.value, nid, ai_slots
),
).bind_value_from(master_select)
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]))