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 16e5b87
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 228 deletions.
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"
}
}
52 changes: 33 additions & 19 deletions src/torchlight/AccessManager.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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,30 +19,36 @@ 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 index, admin_dict in enumerate(self.access_dict["admins"]):
for admin in self.admins:
if admin.unique_id == admin_dict["unique_id"]:
self.access_dict[index] = {
"name": admin.name,
"level": admin.level,
}

self.access_dict = OrderedDict(
sorted(
Expand All @@ -54,8 +61,15 @@ def Save(self) -> None:
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, player: Player) -> SourcemodAdmin:
for admin in self.admins:
if admin.unique_id == player.unique_id:
return admin
return player.admin

def set_admin(self, unique_id: str, admin: SourcemodAdmin) -> bool:
for index, admin in enumerate(self.admins):
if admin.unique_id == unique_id:
self.admins[index] = copy.deepcopy(admin)
return True
return False
72 changes: 0 additions & 72 deletions src/torchlight/Admin.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/torchlight/AntiSpam.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def CheckAntiSpam(self, player: Player) -> bool:
if (
self.disabled_time
and self.disabled_time > self.torchlight.loop.time()
and player.access.level < self.config["ImmunityLevel"]
and player.admin.level < self.config["ImmunityLevel"]
):
self.torchlight.SayPrivate(
player,
Expand Down
2 changes: 1 addition & 1 deletion src/torchlight/AudioClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(
self.last_position: int = 0
self.stops: set[int] = set()

self.level = self.player.access.level
self.level = self.player.admin.level

self.audio_player.AddCallback("Play", self.OnPlay)
self.audio_player.AddCallback("Stop", self.OnStop)
Expand Down
Loading

0 comments on commit 16e5b87

Please sign in to comment.