Skip to content

Commit

Permalink
feat: map sm flags to torchlight levels in config
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime1907 committed Feb 25, 2024
1 parent bbc91cf commit 3f49606
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 257 deletions.
36 changes: 16 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
RUN --mount=type=cache,sharing=locked,id=pipcache,mode=0777,target=/root/.cache/pip/http \
pip install --no-compile build==$BUILD_VERSION

FROM build as build-production

# hadolint ignore=DL3042
RUN --mount=type=secret,id=pipconf,dst="/root/.config/pip/pip.conf" \
--mount=type=cache,sharing=locked,id=pipcache,mode=0777,target=/root/.cache/pip/http \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
pip wheel --no-deps --wheel-dir /app/wheels -r requirements.txt
FROM build as build-common

COPY src/ ./src/

Expand All @@ -33,22 +27,22 @@ RUN --mount=type=secret,id=pipconf,dst="/root/.config/pip/pip.conf" \
--mount=type=bind,source=VERSION,target=VERSION \
python -m build --sdist

FROM build as build-development
FROM build as build-production

# hadolint ignore=DL3042
RUN --mount=type=secret,id=pipconf,dst="/root/.config/pip/pip.conf" \
--mount=type=cache,sharing=locked,id=pipcache,mode=0777,target=/root/.cache/pip/http \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
--mount=type=bind,source=requirements-dev.txt,target=requirements-dev.txt \
pip wheel --no-deps --wheel-dir /app/wheels -r requirements.txt -r requirements-dev.txt \
pip wheel --no-deps --wheel-dir /app/wheels -r requirements.txt

COPY src/ ./src/
FROM build as build-development

# hadolint ignore=DL3042
RUN --mount=type=secret,id=pipconf,dst="/root/.config/pip/pip.conf" \
--mount=type=cache,sharing=locked,id=pipcache,mode=0777,target=/root/.cache/pip/http \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=VERSION,target=VERSION \
python -m build --sdist
--mount=type=bind,source=requirements.txt,target=requirements.txt \
--mount=type=bind,source=requirements-dev.txt,target=requirements-dev.txt \
pip wheel --no-deps --wheel-dir /app/wheels -r requirements.txt -r requirements-dev.txt

FROM python:3.10-slim-bookworm@sha256:9a97ede5d731252b42541a5d3ec60f6d4cd03747ca75315adc784ed864651c0e as runtime

Expand Down Expand Up @@ -94,9 +88,10 @@ FROM runtime as development
USER root

RUN --mount=type=bind,from=build-development,source=/app/wheels,target=/wheels \
--mount=type=bind,from=build-development,source=/app/dist,target=/dist \
pip install --no-cache-dir --no-compile --prefer-binary /wheels/* \
&& pip install --no-cache-dir --no-compile --prefer-binary /dist/*
pip install --no-cache-dir --no-compile --prefer-binary /wheels/*

RUN --mount=type=bind,from=build-common,source=/app/dist,target=/dist \
pip install --no-cache-dir --no-compile --prefer-binary /dist/*

USER rootless

Expand All @@ -108,9 +103,10 @@ FROM runtime as production
USER root

RUN --mount=type=bind,from=build-production,source=/app/wheels,target=/wheels \
--mount=type=bind,from=build-production,source=/app/dist,target=/dist \
pip install --no-cache-dir --no-compile --prefer-binary /wheels/* \
&& pip install --no-cache-dir --no-compile --prefer-binary /dist/*
pip install --no-cache-dir --no-compile --prefer-binary /wheels/*

RUN --mount=type=bind,from=build-common,source=/app/dist,target=/dist \
pip install --no-cache-dir --no-compile --prefer-binary /dist/*

USER rootless

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.1
1.6.0
6 changes: 0 additions & 6 deletions config/access.json

This file was deleted.

9 changes: 9 additions & 0 deletions config/admins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"admins": [
{
"name": "BotoX",
"unique_id": "[U:1:51174697]",
"level": 100
}
]
}
53 changes: 44 additions & 9 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,50 @@
"AdStop": 10
},

"AccessLevel":
{
"Root": 6,
"EventManager": 4,
"Admin": 3,
"DonatedAdmin": 2,
"VIP": 2,
"Player": 0
},
"SourcemodGroups":
[
{
"name": "Root",
"flags": [
"z",
"m"
],
"level": 6
},
{
"name": "Event Manager",
"flags": [
"r"
],
"level": 4
},
{
"name": "Server admin",
"flags": [
"d"
],
"level": 3
},
{
"name": "Donated admin",
"flags": [
"b"
],
"level": 2
},
{
"name": "VIP",
"flags": [
"o"
],
"level": 2
},
{
"name": "Player",
"flags": [],
"level": 0
}
],

"Command":
{
Expand Down
106 changes: 106 additions & 0 deletions config/flags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"SM_RESERVED_SLOT": {
"value": "a",
"display": "Reserved Slot"
},

"SM_GENERIC": {
"value": "b",
"display": "Generic Admin"
},

"SM_KICK": {
"value": "c",
"display": "Kick"
},

"SM_BAN": {
"value": "d",
"display": "Ban"
},

"SM_UNBAN": {
"value": "e",
"display": "Unban"
},

"SM_SLAY": {
"value": "f",
"display": "Slay"
},

"SM_MAP": {
"value": "g",
"display": "Map Change"
},

"SM_CVAR": {
"value": "h",
"display": "Change ConVars"
},

"SM_CONFIG": {
"value": "i",
"display": "Run Configs"
},

"SM_CHAT": {
"value": "j",
"display": "Admin Chat"
},

"SM_VOTE": {
"value": "k",
"display": "Start Votes"
},

"SM_PASSWORD": {
"value": "l",
"display": "Password Server"
},

"SM_RCON": {
"value": "m",
"display": "RCON"
},

"SM_CHEATS": {
"value": "n",
"display": "Enable Cheats"
},

"SM_ROOT": {
"value": "z",
"display": "ALL Permissions (Root)"
},

"SM_CUSTOM1": {
"value": "o",
"display": "Custom Flag 1"
},

"SM_CUSTOM2": {
"value": "p",
"display": "Custom Flag 2"
},

"SM_CUSTOM3": {
"value": "q",
"display": "Custom Flag 3"
},

"SM_CUSTOM4": {
"value": "r",
"display": "Custom Flag 4"
},

"SM_CUSTOM5": {
"value": "s",
"display": "Custom Flag 5"
},

"SM_CUSTOM6": {
"value": "t",
"display": "Custom Flag 6"
}
}
73 changes: 48 additions & 25 deletions src/torchlight/AccessManager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import copy
import json
import logging
import os
from collections import OrderedDict

from torchlight.Config import ConfigAccess
from torchlight.Player import Player
from torchlight.Sourcemod import SourcemodAdmin


class AccessManager:
def __init__(
self, config_folder: str, config_filename: str = "access.json"
self, config_folder: str, config_filename: str = "admins.json"
) -> None:
self.logger = logging.getLogger(self.__class__.__name__)
self.config_folder = os.path.abspath(config_folder)
Expand All @@ -18,44 +18,67 @@ def __init__(
os.path.join(config_folder, config_filename)
)
self.access_dict: OrderedDict = OrderedDict()
self.config_access_list: dict[str, ConfigAccess] = {}
self.admins: list[SourcemodAdmin] = []

def Load(self) -> None:
self.logger.info(f"Loading access from {self.config_filepath}")

with open(self.config_filepath) as fp:
self.access_dict = json.load(fp, object_pairs_hook=OrderedDict)
for unique_id, access in self.access_dict.items():
self.config_access_list[unique_id] = ConfigAccess(
name=access["name"],
level=access["level"],
uniqueid=unique_id,
for admin_dict in self.access_dict["admins"]:
self.admins.append(
SourcemodAdmin(
name=admin_dict["name"],
level=admin_dict["level"],
unique_id=admin_dict["unique_id"],
flag_bits=0,
groups=[],
)
)

self.logger.info(f"Loaded {self.config_access_list}")
self.logger.info(f"Loaded {self.admins}")

def Save(self) -> None:
self.logger.info(f"Saving access to {self.config_filepath}")

for unique_id, access in self.config_access_list.items():
self.access_dict[unique_id] = {
"name": access.name,
"level": access.level,
for admin in self.admins:
admin_cfg = {
"name": admin.name,
"level": admin.level,
"unique_id": admin.unique_id,
}

self.access_dict = OrderedDict(
sorted(
self.access_dict.items(),
key=lambda x: x[1]["level"],
reverse=True,
)
index = 0
while index < len(self.access_dict["admins"]):
admin_dict = self.access_dict["admins"][index]
if admin.unique_id == admin_dict["unique_id"]:
break
index += 1

if index > len(self.access_dict["admins"]):
self.access_dict["admins"].append(admin_cfg)
else:
self.access_dict["admins"][index] = admin_cfg

self.access_dict["admins"] = sorted(
self.access_dict["admins"], key=lambda x: x["level"], reverse=True
)

with open(self.config_filepath, "w") as fp:
json.dump(self.access_dict, fp, indent="\t")

def get_access(self, player: Player) -> ConfigAccess:
for unique_id, access in self.config_access_list.items():
if unique_id == player.unique_id:
return access
return player.access
def get_admin(self, *, unique_id: str) -> SourcemodAdmin | None:
admin: SourcemodAdmin | None = None
for admin in self.admins:
if admin.unique_id == unique_id:
return admin
return admin

def set_admin(self, unique_id: str, admin: SourcemodAdmin) -> None:
admin_copy = copy.deepcopy(admin)
if self.get_admin(unique_id=unique_id) is None:
self.admins.append(admin_copy)
else:
for index, admin in enumerate(self.admins):
if admin.unique_id == unique_id:
self.admins[index] = admin_copy
Loading

0 comments on commit 3f49606

Please sign in to comment.