Compare commits

..

1 Commits

Author SHA1 Message Date
665d21e7bf Add LICENSE.md 2026-06-01 16:59:12 +02:00
7 changed files with 50 additions and 598 deletions

9
LICENSE.md Normal file
View File

@@ -0,0 +1,9 @@
/*
* ------------------------------------------------------------
* "THE BEERWARE LICENSE" (Revision 42):
* DerGrubengräber / Carl Hinze wrote this code. As long as you retain this
* notice, you can do whatever you want with this stuff. If we
* meet someday, and you think this stuff is worth it, you can
* buy me a beer in return.
* ------------------------------------------------------------
*/

View File

@@ -1,25 +0,0 @@
from discord.ext import commands
import discord
import threading
import os
from dotenv import load_dotenv
_started = False
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
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)

View File

@@ -1,108 +0,0 @@
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))

38
main.py
View File

@@ -1,15 +1,19 @@
import discord import discord
import os import os
from discord.ext import tasks from discord.ext import tasks, commands
from dotenv import load_dotenv
from dom5game import Dom5game from dom5game import Dom5game
from webui import create_ui from servermanager import create_server
from bot_instance import bot, start_bot
from nicegui import ui intents = discord.Intents.default()
import asyncio bot = commands.Bot(command_prefix="!", intents=intents)
load_dotenv()
TOKEN = os.getenv("TOKEN")
bot.tracked_games = []
def reload_games(): def reload_games():
with bot.tracked_games_lock:
bot.tracked_games = [] bot.tracked_games = []
for _, _, files in os.walk("games"): for _, _, files in os.walk("games"):
for name in files: for name in files:
@@ -19,15 +23,8 @@ def reload_games():
) )
def main(): create_server("Amogus", 7777, 831955362646851617)
create_ui() reload_games()
loop = asyncio.get_event_loop()
loop.create_task(start_bot())
ui.run(reload=False)
if __name__ == "__main__":
main()
@bot.event @bot.event
@@ -36,11 +33,10 @@ async def on_ready():
await bot.load_extension("cogs.slash_commands") 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():
task_loop.start() task_loop.start()
@tasks.loop(seconds=5) @tasks.loop(seconds=2)
async def task_loop(): async def task_loop():
await bot.change_presence( await bot.change_presence(
activity=discord.Activity( activity=discord.Activity(
@@ -50,11 +46,8 @@ async def task_loop():
) )
for game in bot.tracked_games: for game in bot.tracked_games:
channel = bot.get_channel(game.channelId) channel = bot.get_channel(game.channelId)
if channel is None:
continue
# handle turn ticks # handle turn ticks
new_turn = game.get_turn() if game.get_turn() != game.turn and game.get_turn().isdigit():
if new_turn != game.turn and new_turn.isdigit():
game.update_turn() game.update_turn()
pingstr = "" pingstr = ""
if game.members: if game.members:
@@ -98,3 +91,6 @@ async def task_loop():
) )
game.update_players() game.update_players()
game.to_json() game.to_json()
bot.run(TOKEN)

View File

@@ -1,56 +1,18 @@
aiofiles==25.1.0
aiohappyeyeballs==2.6.1 aiohappyeyeballs==2.6.1
aiohttp==3.13.5 aiohttp==3.13.5
aiosignal==1.4.0 aiosignal==1.4.0
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
attrs==26.1.0 attrs==26.1.0
audioop-lts==0.2.2 audioop-lts==0.2.2
bidict==0.23.1
certifi==2026.2.25
click==8.3.2
discord==2.3.2 discord==2.3.2
discord.py==2.7.1 discord.py==2.7.1
docutils==0.22.4
dotenv==0.9.9 dotenv==0.9.9
fastapi==0.135.3
frozenlist==1.8.0 frozenlist==1.8.0
h11==0.16.0
httpcore==1.0.9
httptools==0.7.1
httpx==0.28.1
idna==3.11 idna==3.11
ifaddr==0.2.0
itsdangerous==2.2.0
Jinja2==3.1.6
lxml==6.0.2
lxml_html_clean==0.4.4
markdown2==2.5.5
MarkupSafe==3.0.3
multidict==6.7.1 multidict==6.7.1
nicegui==3.10.0
numpy==2.4.4 numpy==2.4.4
orjson==3.11.8
pandas==3.0.2 pandas==3.0.2
propcache==0.4.1 propcache==0.4.1
pydantic==2.12.5
pydantic_core==2.41.5
Pygments==2.20.0
python-dateutil==2.9.0.post0 python-dateutil==2.9.0.post0
python-dotenv==1.2.2 python-dotenv==1.2.2
python-engineio==4.13.1
python-multipart==0.0.24
python-socketio==5.16.1
PyYAML==6.0.3
simple-websocket==1.1.0
six==1.17.0 six==1.17.0
starlette==1.0.0
typing-inspection==0.4.2
typing_extensions==4.15.0
uvicorn==0.44.0
uvloop==0.22.1
watchfiles==1.1.1
websockets==16.0
wsproto==1.3.2
yarl==1.23.0 yarl==1.23.0

View File

@@ -2,140 +2,29 @@ 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: str, port: int, channel: int, command: str): def create_server(name, port, channel):
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)
game = Dom5game(name, channel, [], 0, {}) # print(SERVER_PATH)
game.to_json() # print(server_command_builder(name, port, channel))
try: try:
p = subprocess.Popen( subprocess.Popen(
command, server_command_builder(name, port, channel),
stdout=subprocess.DEVNULL, stdin=None,
stderr=subprocess.STDOUT, stdout=None,
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 get_nations(): def server_command_builder(name, port, channel):
# 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",
@@ -144,109 +33,10 @@ def server_command_builder(
str(port), str(port),
"--statuspage", "--statuspage",
os.getcwd() + "/games/" + name + "/turnstats.html", os.getcwd() + "/games/" + name + "/turnstats.html",
" --era ",
str(era),
] ]
if team_game: game = Dom5game(name, channel, [], 0, {})
for team in teams: game.to_json()
# TODO: Can team have more than one disciple and pretender? return command
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:

172
webui.py
View File

@@ -1,172 +0,0 @@
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]))