mirror of
https://github.com/imayushsaini/Bombsquad-Ballistica-Modded-Server.git
synced 2025-11-14 17:46:03 +00:00
Safe read and write opearation with file lock.
Added file locks. - avoiding data leaks. file handle module - To handle file opearations safly. Added filelock.py - licensed file. linted the pdata.py - type checking and formating.
This commit is contained in:
parent
7c67998dec
commit
9f4be5eb84
6 changed files with 694 additions and 264 deletions
2
dist/ba_root/mods/custom_hooks.py
vendored
2
dist/ba_root/mods/custom_hooks.py
vendored
|
|
@ -83,7 +83,7 @@ def bootstraping():
|
||||||
|
|
||||||
# import features
|
# import features
|
||||||
if settings["whitelist"]:
|
if settings["whitelist"]:
|
||||||
pdata.loadWhitelist()
|
pdata.load_white_list()
|
||||||
|
|
||||||
import_discord_bot()
|
import_discord_bot()
|
||||||
import_games()
|
import_games()
|
||||||
|
|
|
||||||
89
dist/ba_root/mods/file_handle.py
vendored
Normal file
89
dist/ba_root/mods/file_handle.py
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
"""Module to handle operations with file."""
|
||||||
|
|
||||||
|
# ba_meta require api 6
|
||||||
|
# (see https://ballistica.net/wiki/meta-tag-system)
|
||||||
|
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
__all__ = ["OpenJson", "JsonFile", "PathNotExistsError"]
|
||||||
|
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
from filelock import FileLock
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PathNotExistsError(Exception):
|
||||||
|
"""Error telling path does not exits."""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class JsonFile:
|
||||||
|
"""Object to handle simple operations with json file."""
|
||||||
|
|
||||||
|
path: str
|
||||||
|
|
||||||
|
def load(self, **kw) -> dict:
|
||||||
|
"""Loads the json file."""
|
||||||
|
if not os.path.exists(self.path):
|
||||||
|
PathNotExistsError(f"Path does not exists. {self.path}")
|
||||||
|
|
||||||
|
with FileLock(self.path):
|
||||||
|
with open(self.path, mode="r", encoding="utf-8") as json_file:
|
||||||
|
try:
|
||||||
|
data = json.load(json_file, **kw)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print(f"Could not load json. {self.path}", end="")
|
||||||
|
print("Creating json in the file.", end="")
|
||||||
|
data = {}
|
||||||
|
self.dump(data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def dump(self, data: dict, **kw) -> None:
|
||||||
|
"""Dumps the json file."""
|
||||||
|
if not os.path.exists(self.path):
|
||||||
|
PathNotExistsError(f"Path does not exists. {self.path}")
|
||||||
|
|
||||||
|
with FileLock(self.path):
|
||||||
|
with open(self.path, mode="w", encoding="utf-8") as json_file:
|
||||||
|
json.dump(data, json_file, **kw)
|
||||||
|
|
||||||
|
def format(self, data: dict) -> None:
|
||||||
|
"""Dumps the json file."""
|
||||||
|
if not os.path.exists(self.path):
|
||||||
|
PathNotExistsError(f"Path does not exists. {self.path}")
|
||||||
|
|
||||||
|
with FileLock(self.path):
|
||||||
|
output = json.dumps(data, indent=4)
|
||||||
|
output2 = re.sub(r'": \[\s+', '": [', output)
|
||||||
|
output3 = re.sub(r'",\s+', '", ', output2)
|
||||||
|
output4 = re.sub(r'"\s+\]', '"]', output3)
|
||||||
|
|
||||||
|
with open(self.path, mode="w", encoding="utf-8") as json_file:
|
||||||
|
json_file.write(output4)
|
||||||
|
|
||||||
|
|
||||||
|
class OpenJson:
|
||||||
|
"""Context manager to open json files.
|
||||||
|
|
||||||
|
Json files opened with this will be file locked. If
|
||||||
|
json file is not readable then It will create new dict."""
|
||||||
|
|
||||||
|
def __init__(self, path: str) -> None:
|
||||||
|
self.json_obj = JsonFile(path)
|
||||||
|
|
||||||
|
def __enter__(self) -> JsonFile:
|
||||||
|
return self.json_obj
|
||||||
|
|
||||||
|
def __exit__(self, _type, value, traceback):
|
||||||
|
if traceback:
|
||||||
|
print(traceback)
|
||||||
103
dist/ba_root/mods/filelock.py
vendored
Normal file
103
dist/ba_root/mods/filelock.py
vendored
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
# Copyright (c) 2009, Evan Fosmark
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
# list of conditions and the following disclaimer.
|
||||||
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
#
|
||||||
|
# The views and conclusions contained in the software and documentation are those
|
||||||
|
# of the authors and should not be interpreted as representing official policies,
|
||||||
|
# either expressed or implied, of the FreeBSD Project.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import errno
|
||||||
|
|
||||||
|
|
||||||
|
class FileLockException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class FileLock(object):
|
||||||
|
"""A file locking mechanism that has context-manager support so
|
||||||
|
you can use it in a with statement. This should be relatively cross
|
||||||
|
compatible as it doesn't rely on msvcrt or fcntl for the locking.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, file_name, timeout=10, delay=0.05):
|
||||||
|
"""Prepare the file locker. Specify the file to lock and optionally
|
||||||
|
the maximum timeout and the delay between each attempt to lock.
|
||||||
|
"""
|
||||||
|
self.is_locked = False
|
||||||
|
self.lockfile = os.path.join(os.getcwd(), "%s.lock" % file_name)
|
||||||
|
self.file_name = file_name
|
||||||
|
self.timeout = timeout
|
||||||
|
self.delay = delay
|
||||||
|
|
||||||
|
def acquire(self):
|
||||||
|
"""Acquire the lock, if possible. If the lock is in use, it check again
|
||||||
|
every `wait` seconds. It does this until it either gets the lock or
|
||||||
|
exceeds `timeout` number of seconds, in which case it throws
|
||||||
|
an exception.
|
||||||
|
"""
|
||||||
|
start_time = time.time()
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||||
|
break
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
if (time.time() - start_time) >= self.timeout:
|
||||||
|
raise FileLockException("Timeout occured.")
|
||||||
|
time.sleep(self.delay)
|
||||||
|
self.is_locked = True
|
||||||
|
|
||||||
|
def release(self):
|
||||||
|
"""Get rid of the lock by deleting the lockfile.
|
||||||
|
When working in a `with` statement, this gets automatically
|
||||||
|
called at the end.
|
||||||
|
"""
|
||||||
|
if self.is_locked:
|
||||||
|
os.close(self.fd)
|
||||||
|
os.unlink(self.lockfile)
|
||||||
|
self.is_locked = False
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Activated when used in the with statement.
|
||||||
|
Should automatically acquire a lock to be used in the with block.
|
||||||
|
"""
|
||||||
|
if not self.is_locked:
|
||||||
|
self.acquire()
|
||||||
|
print(f"{self.file_name.split('/')[-1]} locked")
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type, value, traceback):
|
||||||
|
"""Activated at the end of the with statement.
|
||||||
|
It automatically releases the lock if it isn't locked.
|
||||||
|
"""
|
||||||
|
if self.is_locked:
|
||||||
|
self.release()
|
||||||
|
print(f"{self.file_name.split('/')[-1]} unlocked")
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
"""Make sure that the FileLock instance doesn't leave a lockfile
|
||||||
|
lying around.
|
||||||
|
"""
|
||||||
|
self.release()
|
||||||
744
dist/ba_root/mods/playersData/pdata.py
vendored
744
dist/ba_root/mods/playersData/pdata.py
vendored
|
|
@ -1,302 +1,540 @@
|
||||||
# Released under the MIT License. See LICENSE for details.
|
"""Module to manage players data."""
|
||||||
import _ba, os, json
|
|
||||||
from serverData import serverdata
|
# ba_meta require api 6
|
||||||
|
# (see https://ballistica.net/wiki/meta-tag-system)
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
import _thread
|
import _thread
|
||||||
roles = {}
|
|
||||||
data = {}
|
from serverData import serverdata
|
||||||
custom = {}
|
from file_handle import OpenJson
|
||||||
whitelist=[]
|
import _ba # pylint: disable=import-error
|
||||||
data_path = os.path.join(_ba.env()['python_directory_user'],"playersData" + os.sep)
|
|
||||||
|
|
||||||
|
|
||||||
# ============== player data =======================
|
if TYPE_CHECKING:
|
||||||
def get_info(id):
|
pass
|
||||||
with open(data_path+'profiles.json', 'r') as f:
|
|
||||||
profiles = json.load(f)
|
|
||||||
if id in profiles:
|
|
||||||
|
|
||||||
return profiles[id]
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_profiles():
|
|
||||||
with open(data_path+'profiles.json', 'r') as f:
|
|
||||||
|
|
||||||
profiles = json.load(f)
|
|
||||||
return profiles
|
|
||||||
def commit_profiles(profiles):
|
|
||||||
with open(data_path+'profiles.json', 'w') as f:
|
|
||||||
|
|
||||||
json.dump(profiles,f,indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
def add_profile(id,display_string,currentname,age):
|
|
||||||
f=open(data_path+"profiles.json","r")
|
|
||||||
profiles=json.load(f)
|
|
||||||
f.close()
|
|
||||||
profiles[id]={"display_string":display_string,
|
|
||||||
"profiles":[],
|
|
||||||
"name":currentname,
|
|
||||||
"isBan":False,
|
|
||||||
"isMuted":False,
|
|
||||||
"accountAge":age,
|
|
||||||
"registerOn":time.time(),
|
|
||||||
"canStartKickVote":True,
|
|
||||||
"spamCount":0,
|
|
||||||
"lastSpam":time.time(),
|
|
||||||
"totaltimeplayer":0,
|
|
||||||
"lastseen":0}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
f=open(data_path+"profiles.json","w")
|
|
||||||
json.dump(profiles,f,indent=4)
|
|
||||||
serverdata.clients[id]=profiles[id]
|
|
||||||
serverdata.clients[id]["warnCount"]=0
|
|
||||||
serverdata.clients[id]["lastWarned"]=time.time()
|
|
||||||
serverdata.clients[id]["verified"]=False
|
|
||||||
serverdata.clients[id]["rejoincount"]=1
|
|
||||||
serverdata.clients[id]["lastJoin"]=time.time()
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def update_displayString(id,display_string):
|
|
||||||
profiles=get_profiles()
|
|
||||||
if id in profiles:
|
|
||||||
profiles[id]["display_string"]=display_string
|
|
||||||
commit_profiles(profiles)
|
|
||||||
|
|
||||||
|
|
||||||
def update_profile(id,display_string=None,allprofiles=[],name=None):
|
|
||||||
f=open(data_path+"profiles.json","r")
|
|
||||||
profiles=json.load(f.read())
|
|
||||||
f.close()
|
|
||||||
if id in profiles:
|
|
||||||
if display_string != None and display_string not in profiles[id]['display_string']:
|
|
||||||
profiles[id]['display_string'].append(display_string)
|
|
||||||
if profiles!=[]:
|
|
||||||
for profile in allprofiles:
|
|
||||||
if profile not in profiles[id]['profiles']:
|
|
||||||
profiles[id]['profiles'].append(profile)
|
|
||||||
if name != None:
|
|
||||||
profiles[id]['name']=name
|
|
||||||
|
|
||||||
|
|
||||||
f=open(data_path+"profiles.json","w")
|
PLAYERS_DATA_PATH = os.path.join(
|
||||||
json.dump(profiles,f,indent=4)
|
_ba.env()["python_directory_user"], "playersData" + os.sep
|
||||||
f.close()
|
)
|
||||||
|
|
||||||
def ban_player(id):
|
|
||||||
profiles= get_profiles()
|
|
||||||
if id in profiles:
|
|
||||||
profiles[id]['isBan']=True
|
|
||||||
_thread.start_new_thread(commit_profiles,(profiles,))
|
|
||||||
# commit_profiles(profiles)
|
|
||||||
|
|
||||||
def mute(id):
|
|
||||||
profiles=get_profiles()
|
|
||||||
if id in profiles:
|
|
||||||
|
|
||||||
profiles[id]["isMuted"]=True
|
|
||||||
_thread.start_new_thread(commit_profiles,(profiles,))
|
|
||||||
# commit_profiles(profiles)
|
|
||||||
|
|
||||||
def unmute(id):
|
|
||||||
profiles=get_profiles()
|
|
||||||
if id in profiles:
|
|
||||||
profiles[id]["isMuted"]=False
|
|
||||||
_thread.start_new_thread(commit_profiles,(profiles,))
|
|
||||||
# commit_profiles(profiles)
|
|
||||||
|
|
||||||
def updateSpam(id,spamCount,lastSpam):
|
|
||||||
profiles=get_profiles()
|
|
||||||
if id in profiles:
|
|
||||||
profiles[id]["spamCount"]=spamCount
|
|
||||||
profiles[id]["lastSpam"]=lastSpam
|
|
||||||
|
|
||||||
commit_profiles(profiles)
|
|
||||||
|
|
||||||
|
|
||||||
|
class CacheData: # pylint: disable=too-few-public-methods
|
||||||
|
"""Stores the cache data."""
|
||||||
|
|
||||||
|
roles: dict = {}
|
||||||
|
data: dict = {}
|
||||||
|
custom: dict = {}
|
||||||
|
whitelist: list[str] = []
|
||||||
|
|
||||||
|
|
||||||
#================ ROLES ==========================
|
def get_info(account_id: str) -> dict | None:
|
||||||
|
"""Returns the information about player.
|
||||||
|
|
||||||
def commit_roles(data):
|
Parameters
|
||||||
global roles
|
----------
|
||||||
if data == {}:
|
account_id : str
|
||||||
return
|
account_id of the client
|
||||||
output=json.dumps(data,indent=4)
|
|
||||||
import re
|
Returns
|
||||||
output2 = re.sub(r'": \[\s+', '": [', output)
|
-------
|
||||||
output3 = re.sub(r'",\s+', '", ', output2)
|
dict | None
|
||||||
output4 = re.sub(r'"\s+\]', '"]', output3)
|
information of client
|
||||||
with open(data_path+'roles.json','w') as f:
|
"""
|
||||||
f.write(output4)
|
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
|
profiles = profiles_file.load()
|
||||||
|
if account_id in profiles:
|
||||||
|
return profiles[account_id]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_roles():
|
def get_profiles() -> dict:
|
||||||
global roles
|
"""Returns the profiles of all players.
|
||||||
if roles == {}:
|
|
||||||
with open(data_path+'roles.json', 'r') as f:
|
Returns
|
||||||
roles = json.load(f)
|
-------
|
||||||
return roles
|
dict
|
||||||
|
profiles of the players
|
||||||
|
"""
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
|
profiles = profiles_file.load()
|
||||||
|
return profiles
|
||||||
|
|
||||||
|
|
||||||
def create_role(role):
|
def commit_profiles(profiles: dict) -> None:
|
||||||
global roles
|
"""Commits the given profiles in the database.
|
||||||
_roles = get_roles()
|
|
||||||
if role not in _roles:
|
Parameters
|
||||||
_roles[role] = {
|
----------
|
||||||
"tag":role,
|
profiles : dict
|
||||||
"tagcolor":[1,1,1],
|
profiles of all players
|
||||||
"commands":[],
|
"""
|
||||||
"ids":[]
|
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
}
|
profiles_file.dump(profiles, indent=4)
|
||||||
roles = _roles
|
|
||||||
commit_roles(_roles)
|
|
||||||
return
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def add_player_role(role, id):
|
def add_profile(
|
||||||
global roles
|
account_id: str,
|
||||||
_roles = get_roles()
|
display_string: str,
|
||||||
if role in _roles:
|
current_name: str,
|
||||||
|
account_age: int,
|
||||||
if id not in _roles[role]["ids"]:
|
) -> None:
|
||||||
_roles[role]["ids"].append(id)
|
"""Adds the profile in database.
|
||||||
roles =_roles
|
|
||||||
commit_roles(_roles)
|
Parameters
|
||||||
|
----------
|
||||||
else:
|
account_id : str
|
||||||
print("no role such")
|
account id of the client
|
||||||
|
display_string : str
|
||||||
|
display string of the client
|
||||||
|
current_name : str
|
||||||
|
name of the client
|
||||||
|
account_age : int
|
||||||
|
account_age of the account
|
||||||
|
"""
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
|
profiles = profiles_file.load()
|
||||||
|
profiles[account_id] = {
|
||||||
|
"display_string": display_string,
|
||||||
|
"profiles": [],
|
||||||
|
"name": current_name,
|
||||||
|
"isBan": False,
|
||||||
|
"isMuted": False,
|
||||||
|
"accountAge": account_age,
|
||||||
|
"registerOn": time.time(),
|
||||||
|
"canStartKickVote": True,
|
||||||
|
"spamCount": 0,
|
||||||
|
"lastSpam": time.time(),
|
||||||
|
"totaltimeplayer": 0,
|
||||||
|
"lastseen": 0,
|
||||||
|
}
|
||||||
|
commit_profiles(profiles)
|
||||||
|
|
||||||
|
serverdata.clients[account_id] = profiles[account_id]
|
||||||
|
serverdata.clients[account_id]["warnCount"] = 0
|
||||||
|
serverdata.clients[account_id]["lastWarned"] = time.time()
|
||||||
|
serverdata.clients[account_id]["verified"] = False
|
||||||
|
serverdata.clients[account_id]["rejoincount"] = 1
|
||||||
|
serverdata.clients[account_id]["lastJoin"] = time.time()
|
||||||
|
|
||||||
|
|
||||||
def remove_player_role(role, id):
|
def update_display_string(account_id: str, display_string: str) -> None:
|
||||||
global roles
|
"""Updates the display string of the account.
|
||||||
_roles = get_roles()
|
|
||||||
if role in _roles:
|
Parameters
|
||||||
_roles[role]["ids"].remove(id)
|
----------
|
||||||
roles =_roles
|
account_id : str
|
||||||
commit_roles(_roles)
|
account id of the client
|
||||||
return "removed from "+role
|
display_string : str
|
||||||
return "role not exists"
|
new display string to be updated
|
||||||
|
"""
|
||||||
|
profiles = get_profiles()
|
||||||
|
if account_id in profiles:
|
||||||
|
profiles[account_id]["display_string"] = display_string
|
||||||
|
commit_profiles(profiles)
|
||||||
|
|
||||||
|
|
||||||
|
def update_profile(
|
||||||
|
account_id: str,
|
||||||
|
display_string: str = None,
|
||||||
|
allprofiles: list[str] = None,
|
||||||
|
name: str = None,
|
||||||
|
) -> None:
|
||||||
|
"""Updates the profile of client.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
display_string : str, optional
|
||||||
|
display string of the account, by default None
|
||||||
|
allprofiles : list[str], optional
|
||||||
|
all profiles of the client, by default None
|
||||||
|
name : str, optional
|
||||||
|
name to be updated, by default None
|
||||||
|
"""
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
|
profiles = profiles_file.load()
|
||||||
|
|
||||||
|
if profiles is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if account_id in profiles and display_string is not None:
|
||||||
|
if display_string not in profiles[account_id]["display_string"]:
|
||||||
|
profiles[account_id]["display_string"].append(display_string)
|
||||||
|
|
||||||
|
if allprofiles is not None:
|
||||||
|
for profile in allprofiles:
|
||||||
|
if profile not in profiles[account_id]["profiles"]:
|
||||||
|
profiles[account_id]["profiles"].append(profile)
|
||||||
|
|
||||||
|
if name is not None:
|
||||||
|
profiles[account_id]["name"] = name
|
||||||
|
|
||||||
|
commit_profiles(profiles)
|
||||||
|
|
||||||
|
|
||||||
def add_command_role(role, command):
|
def ban_player(account_id: str) -> None:
|
||||||
global roles
|
"""Bans the player.
|
||||||
_roles = get_roles()
|
|
||||||
if role in _roles:
|
Parameters
|
||||||
if command not in _roles[role]["commands"]:
|
----------
|
||||||
_roles[role]["commands"].append(command)
|
account_id : str
|
||||||
roles =_roles
|
account id of the player to be banned
|
||||||
commit_roles(_roles)
|
"""
|
||||||
return "command added to "+role
|
profiles = get_profiles()
|
||||||
return "command not exists"
|
if account_id in profiles:
|
||||||
|
profiles[account_id]["isBan"] = True
|
||||||
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
def remove_command_role(role, command):
|
def mute(account_id: str) -> None:
|
||||||
global roles
|
"""Mutes the player.
|
||||||
_roles = get_roles()
|
|
||||||
if role in _roles:
|
Parameters
|
||||||
if command in _roles[role]["commands"]:
|
----------
|
||||||
_roles[role]["commands"].remove(command)
|
account_id : str
|
||||||
roles =_roles
|
acccount id of the player to be muted
|
||||||
commit_roles(_roles)
|
"""
|
||||||
return "command added to "+role
|
profiles = get_profiles()
|
||||||
return "command not exists"
|
if account_id in profiles:
|
||||||
|
profiles[account_id]["isMuted"] = True
|
||||||
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
def change_role_tag(role, tag):
|
def unmute(account_id: str) -> None:
|
||||||
global roles
|
"""Unmutes the player.
|
||||||
_roles = get_roles()
|
|
||||||
if role in _roles:
|
Parameters
|
||||||
_roles[role]['tag'] = tag
|
----------
|
||||||
roles = _roles
|
account_id : str
|
||||||
commit_roles(_roles)
|
acccount id of the player to be unmuted
|
||||||
return "tag changed"
|
"""
|
||||||
return "role not exists"
|
profiles = get_profiles()
|
||||||
|
if account_id in profiles:
|
||||||
|
profiles[account_id]["isMuted"] = False
|
||||||
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
def get_player_roles(acc_id):
|
def update_spam(account_id: str, spam_count: int, last_spam: float) -> None:
|
||||||
|
"""Updates the spam time and count.
|
||||||
_roles = get_roles()
|
|
||||||
roles=[]
|
Parameters
|
||||||
for role in _roles:
|
----------
|
||||||
if acc_id in _roles[role]["ids"]:
|
account_id : str
|
||||||
roles.append(role)
|
account id of the client
|
||||||
return roles
|
spam_count : int
|
||||||
|
spam count to be added
|
||||||
|
last_spam : float
|
||||||
|
last spam time
|
||||||
|
"""
|
||||||
|
profiles = get_profiles()
|
||||||
|
if account_id in profiles:
|
||||||
|
profiles[account_id]["spamCount"] = spam_count
|
||||||
|
profiles[account_id]["lastSpam"] = last_spam
|
||||||
|
|
||||||
|
commit_profiles(profiles)
|
||||||
|
|
||||||
|
|
||||||
##### those ups done will clean it in future
|
def commit_roles(data: dict) -> None:
|
||||||
|
"""Commits the roles in database.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : dict
|
||||||
|
data to be commited
|
||||||
|
"""
|
||||||
|
if not data:
|
||||||
|
return
|
||||||
|
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "roles.json") as roles_file:
|
||||||
|
roles_file.format(data)
|
||||||
|
|
||||||
|
|
||||||
|
def get_roles() -> dict:
|
||||||
|
"""Returns the roles.
|
||||||
|
|
||||||
#======================= CUSTOM EFFECTS/TAGS ===============
|
Returns
|
||||||
|
-------
|
||||||
|
dict
|
||||||
|
roles
|
||||||
|
"""
|
||||||
|
if CacheData.roles == {}:
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "roles.json") as roles_file:
|
||||||
|
roles = roles_file.load()
|
||||||
|
return roles
|
||||||
|
return CacheData.roles
|
||||||
|
|
||||||
|
|
||||||
def get_custom():
|
def create_role(role: str) -> None:
|
||||||
global custom
|
"""Ceates the role.
|
||||||
if custom=={}:
|
|
||||||
with open(data_path+"custom.json","r") as f:
|
Parameters
|
||||||
custom = json.loads(f.read())
|
----------
|
||||||
return custom
|
role : str
|
||||||
return custom
|
role to be created
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
|
||||||
|
if role in roles:
|
||||||
|
return
|
||||||
|
|
||||||
|
roles[role] = {
|
||||||
|
"tag": role,
|
||||||
|
"tagcolor": [1, 1, 1],
|
||||||
|
"commands": [],
|
||||||
|
"ids": [],
|
||||||
|
}
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
|
||||||
|
|
||||||
def set_effect(effect, id):
|
def add_player_role(role: str, account_id: str) -> None:
|
||||||
global custom
|
"""Adds the player to the role.
|
||||||
_custom = get_custom()
|
|
||||||
_custom['customeffects'][id] = effect
|
Parameters
|
||||||
custom = _custom
|
----------
|
||||||
commit_c()
|
role : str
|
||||||
|
role to be added
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
|
||||||
|
if role in roles:
|
||||||
|
if account_id not in roles[role]["ids"]:
|
||||||
|
roles[role]["ids"].append(account_id)
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("no role such")
|
||||||
|
|
||||||
|
|
||||||
def set_tag(tag, id):
|
def remove_player_role(role: str, account_id: str) -> str:
|
||||||
|
"""Removes the role from player.
|
||||||
global custom
|
|
||||||
_custom = get_custom()
|
Parameters
|
||||||
_custom['customtag'][id] = tag
|
----------
|
||||||
custom = _custom
|
role : str
|
||||||
commit_c()
|
role to br removed
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
status of the removing role
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
if role in roles:
|
||||||
|
roles[role]["ids"].remove(account_id)
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
return "removed from " + role
|
||||||
|
return "role not exists"
|
||||||
|
|
||||||
|
|
||||||
def remove_effect(id):
|
def add_command_role(role: str, command: str) -> str:
|
||||||
global custom
|
"""Adds the command to the role.
|
||||||
_custom = get_custom()
|
|
||||||
_custom['customeffects'].pop(id)
|
Parameters
|
||||||
custom = _custom
|
----------
|
||||||
commit_c()
|
role : str
|
||||||
|
role to add the command
|
||||||
|
command : str
|
||||||
|
command to be added
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
status of the adding command
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
if role in roles:
|
||||||
|
if command not in roles[role]["commands"]:
|
||||||
|
roles[role]["commands"].append(command)
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
return "command added to " + role
|
||||||
|
return "command not exists"
|
||||||
|
|
||||||
|
|
||||||
def remove_tag(id):
|
def remove_command_role(role: str, command: str) -> str:
|
||||||
global custom
|
"""Removes the command from the role.
|
||||||
_custom = get_custom()
|
|
||||||
_custom['customtag'].pop(id)
|
Parameters
|
||||||
custom = _custom
|
----------
|
||||||
commit_c()
|
role : str
|
||||||
|
role to remove command from
|
||||||
|
command : str
|
||||||
|
command to be removed
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
status of the removing command
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
if role in roles:
|
||||||
|
if command in roles[role]["commands"]:
|
||||||
|
roles[role]["commands"].remove(command)
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
return "command added to " + role
|
||||||
|
return "command not exists"
|
||||||
|
|
||||||
|
|
||||||
|
def change_role_tag(role: str, tag: str) -> str:
|
||||||
|
"""Changes the tag of the role.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
role : str
|
||||||
|
role to chnage the tag
|
||||||
|
tag : str
|
||||||
|
tag to be added
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
status of the adding tag
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
if role in roles:
|
||||||
|
roles[role]["tag"] = tag
|
||||||
|
CacheData.roles = roles
|
||||||
|
commit_roles(roles)
|
||||||
|
return "tag changed"
|
||||||
|
return "role not exists"
|
||||||
|
|
||||||
|
|
||||||
|
def get_player_roles(account_id: str) -> list[str]:
|
||||||
|
"""Returns the avalibe roles of the account.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
list[str]
|
||||||
|
list of the roles
|
||||||
|
"""
|
||||||
|
|
||||||
|
roles = get_roles()
|
||||||
|
have_roles = []
|
||||||
|
for role in roles:
|
||||||
|
if account_id in roles[role]["ids"]:
|
||||||
|
have_roles.append(role)
|
||||||
|
return have_roles
|
||||||
|
|
||||||
|
|
||||||
|
def get_custom() -> dict:
|
||||||
|
"""Returns the custom effects.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
dict
|
||||||
|
custom effects
|
||||||
|
"""
|
||||||
|
if CacheData.custom == {}:
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "custom.json") as custom_file:
|
||||||
|
custom = custom_file.load()
|
||||||
|
return custom
|
||||||
|
return CacheData.custom
|
||||||
|
|
||||||
|
|
||||||
|
def set_effect(effect: str, accout_id: str) -> None:
|
||||||
|
"""Sets the costum effect for the player.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
effect : str
|
||||||
|
effect to be added to the player
|
||||||
|
accout_id : str
|
||||||
|
account id of the client
|
||||||
|
"""
|
||||||
|
custom = get_custom()
|
||||||
|
custom["customeffects"][accout_id] = effect
|
||||||
|
CacheData.custom = custom
|
||||||
|
commit_c()
|
||||||
|
|
||||||
|
|
||||||
|
def set_tag(tag: str, account_id: str) -> None:
|
||||||
|
"""Sets the custom tag to the player.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
tag : str
|
||||||
|
tag to be added to the player
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
"""
|
||||||
|
custom = get_custom()
|
||||||
|
custom["customtag"][account_id] = tag
|
||||||
|
CacheData.custom = custom
|
||||||
|
commit_c()
|
||||||
|
|
||||||
|
|
||||||
|
def remove_effect(account_id: str) -> None:
|
||||||
|
"""Removes the effect from player.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
"""
|
||||||
|
custom = get_custom()
|
||||||
|
custom["customeffects"].pop(account_id)
|
||||||
|
CacheData.custom = custom
|
||||||
|
commit_c()
|
||||||
|
|
||||||
|
|
||||||
|
def remove_tag(account_id: str) -> None:
|
||||||
|
"""Removes the tag from the player
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
account_id : str
|
||||||
|
account id of the client
|
||||||
|
"""
|
||||||
|
custom = get_custom()
|
||||||
|
custom["customtag"].pop(account_id)
|
||||||
|
CacheData.custom = custom
|
||||||
|
commit_c()
|
||||||
|
|
||||||
|
|
||||||
def commit_c():
|
def commit_c():
|
||||||
global custom
|
"""Commits the custom data into the custom.json."""
|
||||||
with open(data_path+"custom.json",'w') as f:
|
with OpenJson(PLAYERS_DATA_PATH + "custom.json") as custom_file:
|
||||||
json.dump(custom,f,indent=4)
|
custom_file.dump(CacheData.custom, indent=4)
|
||||||
|
|
||||||
def update_toppers(topperlist):
|
|
||||||
global roles
|
|
||||||
_roles = get_roles()
|
|
||||||
if "top5" not in _roles:
|
|
||||||
create_role("top5")
|
|
||||||
roles["top5"]["ids"]=topperlist
|
|
||||||
commit_roles(roles)
|
|
||||||
|
|
||||||
|
|
||||||
def loadWhitelist():
|
def update_toppers(topper_list: list[str]) -> None:
|
||||||
global whitelist
|
"""Updates the topper list into top5 role.
|
||||||
with open(data_path+"whitelist.json","r") as f:
|
|
||||||
data=json.loads(f.read())
|
Parameters
|
||||||
for id in data:
|
----------
|
||||||
whitelist.append(id)
|
topper_list : list[str]
|
||||||
|
list of the topper players
|
||||||
|
"""
|
||||||
|
roles = get_roles()
|
||||||
|
if "top5" not in roles:
|
||||||
|
create_role("top5")
|
||||||
|
CacheData.roles["top5"]["ids"] = topper_list
|
||||||
|
commit_roles(roles)
|
||||||
|
|
||||||
|
|
||||||
|
def load_white_list() -> None:
|
||||||
|
"""Loads the whitelist."""
|
||||||
|
with OpenJson(PLAYERS_DATA_PATH + "whitelist.json") as whitelist_file:
|
||||||
|
data = whitelist_file.load()
|
||||||
|
for account_id in data:
|
||||||
|
CacheData.whitelist.append(account_id)
|
||||||
|
|
|
||||||
16
dist/ba_root/mods/playersData/roles.json
vendored
16
dist/ba_root/mods/playersData/roles.json
vendored
|
|
@ -23,14 +23,6 @@
|
||||||
"commands": [],
|
"commands": [],
|
||||||
"ids": []
|
"ids": []
|
||||||
},
|
},
|
||||||
"top5": {
|
|
||||||
"tag": "top5", "tagcolor": [1,
|
|
||||||
1,
|
|
||||||
1
|
|
||||||
],
|
|
||||||
"commands": [],
|
|
||||||
"ids": ["pb-IF4VAk4a"]
|
|
||||||
},
|
|
||||||
"smoothy": {
|
"smoothy": {
|
||||||
"tag": "smoothy", "tagcolor": [1,
|
"tag": "smoothy", "tagcolor": [1,
|
||||||
1,
|
1,
|
||||||
|
|
@ -46,5 +38,13 @@
|
||||||
],
|
],
|
||||||
"commands": [],
|
"commands": [],
|
||||||
"ids": []
|
"ids": []
|
||||||
|
},
|
||||||
|
"top5": {
|
||||||
|
"tag": "top5", "tagcolor": [1,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"commands": [],
|
||||||
|
"ids": ["pb-IF5XUm9eAg==", "pb-IF43VUwlAg==", "pb-IF4iVUc5Cg==", "pb-IF4vNnMJ", "pb-IF4TVWwZUQ=="]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
4
dist/ba_root/mods/tools/servercheck.py
vendored
4
dist/ba_root/mods/tools/servercheck.py
vendored
|
|
@ -90,7 +90,7 @@ class checkserver(object):
|
||||||
|
|
||||||
return
|
return
|
||||||
if settings["whitelist"] and ros["account_id"]!=None:
|
if settings["whitelist"] and ros["account_id"]!=None:
|
||||||
if ros["account_id"] not in pdata.whitelist:
|
if ros["account_id"] not in pdata.CacheData.whitelist:
|
||||||
_ba.screenmessage("Not in whitelist,contact admin",color=(1,0,0),transient=True,clients=[ros['client_id']])
|
_ba.screenmessage("Not in whitelist,contact admin",color=(1,0,0),transient=True,clients=[ros['client_id']])
|
||||||
logger.log(d_str+"||"+ros["account_id"]+" | kicked > not in whitelist")
|
logger.log(d_str+"||"+ros["account_id"]+" | kicked > not in whitelist")
|
||||||
_ba.disconnect_client(ros['client_id'])
|
_ba.disconnect_client(ros['client_id'])
|
||||||
|
|
@ -321,7 +321,7 @@ def save_age(age, pb_id,display_string):
|
||||||
def save_ids(ids,pb_id,display_string):
|
def save_ids(ids,pb_id,display_string):
|
||||||
|
|
||||||
|
|
||||||
pdata.update_displayString(pb_id,ids)
|
pdata.update_display_string(pb_id,ids)
|
||||||
|
|
||||||
|
|
||||||
if display_string not in ids:
|
if display_string not in ids:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue