mirror of
https://github.com/imayushsaini/Bombsquad-Ballistica-Modded-Server.git
synced 2025-11-07 17:36:15 +00:00
rework on profile.json handling
This commit is contained in:
parent
c776b40c5c
commit
5bd321c59c
2 changed files with 72 additions and 40 deletions
2
dist/ba_root/mods/custom_hooks.py
vendored
2
dist/ba_root/mods/custom_hooks.py
vendored
|
|
@ -73,6 +73,8 @@ def bootstraping():
|
||||||
|
|
||||||
# check for auto update stats
|
# check for auto update stats
|
||||||
_thread.start_new_thread(mystats.refreshStats,())
|
_thread.start_new_thread(mystats.refreshStats,())
|
||||||
|
pdata.load_cache()
|
||||||
|
_thread.start_new_thread(pdata.dump_cache,())
|
||||||
|
|
||||||
# import plugins
|
# import plugins
|
||||||
if settings["elPatronPowerups"]["enable"]:
|
if settings["elPatronPowerups"]["enable"]:
|
||||||
|
|
|
||||||
110
dist/ba_root/mods/playersData/pdata.py
vendored
110
dist/ba_root/mods/playersData/pdata.py
vendored
|
|
@ -13,7 +13,7 @@ import _thread
|
||||||
from serverData import serverdata
|
from serverData import serverdata
|
||||||
from tools.file_handle import OpenJson
|
from tools.file_handle import OpenJson
|
||||||
import _ba # pylint: disable=import-error
|
import _ba # pylint: disable=import-error
|
||||||
|
import json
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
pass
|
pass
|
||||||
|
|
@ -30,6 +30,7 @@ class CacheData: # pylint: disable=too-few-public-methods
|
||||||
roles: dict = {}
|
roles: dict = {}
|
||||||
data: dict = {}
|
data: dict = {}
|
||||||
custom: dict = {}
|
custom: dict = {}
|
||||||
|
profiles: dict = {}
|
||||||
whitelist: list[str] = []
|
whitelist: list[str] = []
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -46,10 +47,9 @@ def get_info(account_id: str) -> dict | None:
|
||||||
dict | None
|
dict | None
|
||||||
information of client
|
information of client
|
||||||
"""
|
"""
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
profiles=get_profiles()
|
||||||
profiles = profiles_file.load()
|
if account_id in profiles:
|
||||||
if account_id in profiles:
|
return profiles[account_id]
|
||||||
return profiles[account_id]
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,21 +61,25 @@ def get_profiles() -> dict:
|
||||||
dict
|
dict
|
||||||
profiles of the players
|
profiles of the players
|
||||||
"""
|
"""
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
if CacheData.profiles=={}:
|
||||||
profiles = profiles_file.load()
|
f=open(PLAYERS_DATA_PATH + "profiles.json","r")
|
||||||
|
profiles = json.load(f)
|
||||||
|
CacheData.profiles=profiles
|
||||||
|
f.close()
|
||||||
return profiles
|
return profiles
|
||||||
|
else:
|
||||||
|
return CacheData.profiles
|
||||||
|
|
||||||
|
|
||||||
def commit_profiles(profiles: dict) -> None:
|
def commit_profiles(data={}) -> None:
|
||||||
"""Commits the given profiles in the database.
|
"""Commits the given profiles in the database.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
profiles : dict
|
|
||||||
profiles of all players
|
profiles of all players
|
||||||
"""
|
"""
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
# with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
||||||
profiles_file.dump(profiles, indent=4)
|
# profiles_file.dump(CacheData.profiles, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def add_profile(
|
def add_profile(
|
||||||
|
|
@ -97,9 +101,8 @@ def add_profile(
|
||||||
account_age : int
|
account_age : int
|
||||||
account_age of the account
|
account_age of the account
|
||||||
"""
|
"""
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "profiles.json") as profiles_file:
|
profiles = get_profiles()
|
||||||
profiles = profiles_file.load()
|
profiles[account_id] = {
|
||||||
profiles[account_id] = {
|
|
||||||
"display_string": display_string,
|
"display_string": display_string,
|
||||||
"profiles": [],
|
"profiles": [],
|
||||||
"name": current_name,
|
"name": current_name,
|
||||||
|
|
@ -113,7 +116,8 @@ def add_profile(
|
||||||
"totaltimeplayer": 0,
|
"totaltimeplayer": 0,
|
||||||
"lastseen": 0,
|
"lastseen": 0,
|
||||||
}
|
}
|
||||||
commit_profiles(profiles)
|
CacheData.profiles=profiles
|
||||||
|
commit_profiles()
|
||||||
|
|
||||||
serverdata.clients[account_id] = profiles[account_id]
|
serverdata.clients[account_id] = profiles[account_id]
|
||||||
serverdata.clients[account_id]["warnCount"] = 0
|
serverdata.clients[account_id]["warnCount"] = 0
|
||||||
|
|
@ -136,7 +140,8 @@ def update_display_string(account_id: str, display_string: str) -> None:
|
||||||
profiles = get_profiles()
|
profiles = get_profiles()
|
||||||
if account_id in profiles:
|
if account_id in profiles:
|
||||||
profiles[account_id]["display_string"] = display_string
|
profiles[account_id]["display_string"] = display_string
|
||||||
commit_profiles(profiles)
|
CacheData.profiles=profiles
|
||||||
|
commit_profiles()
|
||||||
|
|
||||||
|
|
||||||
def update_profile(
|
def update_profile(
|
||||||
|
|
@ -158,25 +163,25 @@ def update_profile(
|
||||||
name : str, optional
|
name : str, optional
|
||||||
name to be updated, by default None
|
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:
|
profiles = get_profiles()
|
||||||
return
|
|
||||||
|
|
||||||
if account_id in profiles and display_string is not None:
|
if profiles is None:
|
||||||
if display_string not in profiles[account_id]["display_string"]:
|
return
|
||||||
profiles[account_id]["display_string"].append(display_string)
|
|
||||||
|
|
||||||
if allprofiles is not None:
|
if account_id in profiles and display_string is not None:
|
||||||
for profile in allprofiles:
|
if display_string not in profiles[account_id]["display_string"]:
|
||||||
if profile not in profiles[account_id]["profiles"]:
|
profiles[account_id]["display_string"].append(display_string)
|
||||||
profiles[account_id]["profiles"].append(profile)
|
|
||||||
|
|
||||||
if name is not None:
|
if allprofiles is not None:
|
||||||
profiles[account_id]["name"] = name
|
for profile in allprofiles:
|
||||||
|
if profile not in profiles[account_id]["profiles"]:
|
||||||
|
profiles[account_id]["profiles"].append(profile)
|
||||||
|
|
||||||
commit_profiles(profiles)
|
if name is not None:
|
||||||
|
profiles[account_id]["name"] = name
|
||||||
|
CacheData.profiles=profiles
|
||||||
|
commit_profiles()
|
||||||
|
|
||||||
|
|
||||||
def ban_player(account_id: str) -> None:
|
def ban_player(account_id: str) -> None:
|
||||||
|
|
@ -190,6 +195,7 @@ def ban_player(account_id: str) -> None:
|
||||||
profiles = get_profiles()
|
profiles = get_profiles()
|
||||||
if account_id in profiles:
|
if account_id in profiles:
|
||||||
profiles[account_id]["isBan"] = True
|
profiles[account_id]["isBan"] = True
|
||||||
|
CacheData.profiles=profiles
|
||||||
_thread.start_new_thread(commit_profiles, (profiles,))
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -204,6 +210,7 @@ def mute(account_id: str) -> None:
|
||||||
profiles = get_profiles()
|
profiles = get_profiles()
|
||||||
if account_id in profiles:
|
if account_id in profiles:
|
||||||
profiles[account_id]["isMuted"] = True
|
profiles[account_id]["isMuted"] = True
|
||||||
|
CacheData.profiles=profiles
|
||||||
_thread.start_new_thread(commit_profiles, (profiles,))
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -218,6 +225,7 @@ def unmute(account_id: str) -> None:
|
||||||
profiles = get_profiles()
|
profiles = get_profiles()
|
||||||
if account_id in profiles:
|
if account_id in profiles:
|
||||||
profiles[account_id]["isMuted"] = False
|
profiles[account_id]["isMuted"] = False
|
||||||
|
CacheData.profiles=profiles
|
||||||
_thread.start_new_thread(commit_profiles, (profiles,))
|
_thread.start_new_thread(commit_profiles, (profiles,))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -237,7 +245,7 @@ def update_spam(account_id: str, spam_count: int, last_spam: float) -> None:
|
||||||
if account_id in profiles:
|
if account_id in profiles:
|
||||||
profiles[account_id]["spamCount"] = spam_count
|
profiles[account_id]["spamCount"] = spam_count
|
||||||
profiles[account_id]["lastSpam"] = last_spam
|
profiles[account_id]["lastSpam"] = last_spam
|
||||||
|
CacheData.profiles=profiles
|
||||||
commit_profiles(profiles)
|
commit_profiles(profiles)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -252,8 +260,8 @@ def commit_roles(data: dict) -> None:
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
|
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "roles.json") as roles_file:
|
# with OpenJson(PLAYERS_DATA_PATH + "roles.json") as roles_file:
|
||||||
roles_file.format(data)
|
# roles_file.format(data)
|
||||||
|
|
||||||
|
|
||||||
def get_roles() -> dict:
|
def get_roles() -> dict:
|
||||||
|
|
@ -265,9 +273,10 @@ def get_roles() -> dict:
|
||||||
roles
|
roles
|
||||||
"""
|
"""
|
||||||
if CacheData.roles == {}:
|
if CacheData.roles == {}:
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "roles.json") as roles_file:
|
f = open(PLAYERS_DATA_PATH + "roles.json", "r")
|
||||||
roles = roles_file.load()
|
roles = json.load(f)
|
||||||
CacheData.roles = roles
|
f.close()
|
||||||
|
CacheData.roles = roles
|
||||||
return roles
|
return roles
|
||||||
return CacheData.roles
|
return CacheData.roles
|
||||||
|
|
||||||
|
|
@ -446,8 +455,10 @@ def get_custom() -> dict:
|
||||||
custom effects
|
custom effects
|
||||||
"""
|
"""
|
||||||
if CacheData.custom == {}:
|
if CacheData.custom == {}:
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "custom.json") as custom_file:
|
f=open(PLAYERS_DATA_PATH + "custom.json","r")
|
||||||
custom = custom_file.load()
|
custom = json.load(f)
|
||||||
|
f.close()
|
||||||
|
CacheData.custom=custom
|
||||||
return custom
|
return custom
|
||||||
return CacheData.custom
|
return CacheData.custom
|
||||||
|
|
||||||
|
|
@ -514,8 +525,8 @@ def remove_tag(account_id: str) -> None:
|
||||||
|
|
||||||
def commit_c():
|
def commit_c():
|
||||||
"""Commits the custom data into the custom.json."""
|
"""Commits the custom data into the custom.json."""
|
||||||
with OpenJson(PLAYERS_DATA_PATH + "custom.json") as custom_file:
|
# with OpenJson(PLAYERS_DATA_PATH + "custom.json") as custom_file:
|
||||||
custom_file.dump(CacheData.custom, indent=4)
|
# custom_file.dump(CacheData.custom, indent=4)
|
||||||
|
|
||||||
|
|
||||||
def update_toppers(topper_list: list[str]) -> None:
|
def update_toppers(topper_list: list[str]) -> None:
|
||||||
|
|
@ -539,3 +550,22 @@ def load_white_list() -> None:
|
||||||
data = whitelist_file.load()
|
data = whitelist_file.load()
|
||||||
for account_id in data:
|
for account_id in data:
|
||||||
CacheData.whitelist.append(account_id)
|
CacheData.whitelist.append(account_id)
|
||||||
|
|
||||||
|
def load_cache():
|
||||||
|
""" to be called on server boot"""
|
||||||
|
get_profiles()
|
||||||
|
get_custom()
|
||||||
|
get_roles()
|
||||||
|
|
||||||
|
def dump_cache():
|
||||||
|
if CacheData.profiles!={}:
|
||||||
|
with open(PLAYERS_DATA_PATH + "profiles.json","w") as f:
|
||||||
|
json.dump(CacheData.profiles,f,indent=4)
|
||||||
|
if CacheData.roles!={}:
|
||||||
|
with open(PLAYERS_DATA_PATH + "roles.json", "w") as f:
|
||||||
|
json.dump(CacheData.roles, f, indent=4)
|
||||||
|
if CacheData.custom!={}:
|
||||||
|
with open(PLAYERS_DATA_PATH + "custom.json", "w") as f:
|
||||||
|
json.dump(CacheData.custom, f, indent=4)
|
||||||
|
time.sleep(20)
|
||||||
|
dump_cache()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue