mirror of
https://github.com/hypervortex/VH-Bombsquad-Modded-Server-Files
synced 2026-08-15 13:04:50 +00:00
Update autoadmin.py
This commit is contained in:
parent
c8b2c3cbec
commit
2ae713cc39
1 changed files with 54 additions and 36 deletions
90
dist/ba_root/mods/tools/autoadmin.py
vendored
90
dist/ba_root/mods/tools/autoadmin.py
vendored
|
|
@ -4,38 +4,21 @@
|
|||
from playersData import pdata
|
||||
from stats import mystats
|
||||
import setting
|
||||
import threading
|
||||
|
||||
stats = mystats.get_all_stats()
|
||||
settings = setting.get_settings_data()
|
||||
|
||||
top_player_count = settings['autoadmin']['top_rank_count']
|
||||
admin_rank = settings['autoadmin']['admin_rank']
|
||||
admin_rank = settings['autoadmin'].get('admin_rank', [])
|
||||
admin_score = settings['autoadmin']['score_for_admin']
|
||||
vip_rank = settings['autoadmin']['vip_rank']
|
||||
vip_rank = settings['autoadmin'].get('vip_rank', [])
|
||||
vip_score = settings['autoadmin']['score_for_vip']
|
||||
BLUE = '\033[34m'
|
||||
RESET = '\033[0m'
|
||||
BLACK = '\033[30m'
|
||||
##
|
||||
## Check if autoadmin is enabled
|
||||
autoadmin_enabled = settings.get('autoadmin', {}).get('enable', False)
|
||||
if autoadmin_enabled:
|
||||
print(f"{BLACK}AutoAdmin is Enabled...{RESET}")
|
||||
##
|
||||
## Validate and handle blank or empty admin_rank setting
|
||||
admin_ranks = settings['autoadmin'].get('admin_rank', [])
|
||||
if not admin_ranks:
|
||||
print(f"{BLUE}No admin ranks specified. Skipping admin role assignment.{RESET}")
|
||||
else:
|
||||
pass
|
||||
##
|
||||
## Validate and handle blank or empty vip_rank setting
|
||||
vip_ranks = settings['autoadmin'].get('vip_rank', [])
|
||||
if not vip_ranks:
|
||||
print(f"{BLUE}No VIP ranks specified. Skipping VIP role assignment.{RESET}")
|
||||
else:
|
||||
pass
|
||||
##
|
||||
##
|
||||
|
||||
|
||||
def get_top_players(data, count=top_player_count):
|
||||
sorted_players = sorted(data.items(), key=lambda x: x[1].get('rank', float('inf')))
|
||||
top_players = sorted_players[:count]
|
||||
|
|
@ -51,11 +34,12 @@ def remove_all_ids_from_role(role_name, player_ids):
|
|||
for player_id in player_ids:
|
||||
if player_id in roles:
|
||||
roles.remove(player_id)
|
||||
pdata.updates_roles()
|
||||
print(f"Removed {player_id} from the '{role_name}' role.")
|
||||
else:
|
||||
print(f"{player_id} not found in '{role_name}' role.")
|
||||
|
||||
|
||||
# Update roles data immediately after removing player IDs
|
||||
pdata.updates_roles()
|
||||
else:
|
||||
print(f"Role '{role_name}' not found in roles.json.")
|
||||
|
||||
|
|
@ -69,13 +53,15 @@ def add_ids_to_role(role_name, player_ids):
|
|||
for player_id in player_ids:
|
||||
if player_id not in role_ids:
|
||||
role_ids.append(player_id)
|
||||
pdata.updates_roles()
|
||||
print(f"Added player ID {player_id} to the '{role_name}' role.")
|
||||
else:
|
||||
print(f"Player ID {player_id} is already in the '{role_name}' role.")
|
||||
print(f"Player ID {player_id} is already in the '{role_name}' role.")
|
||||
|
||||
# Update roles data immediately after adding player IDs
|
||||
pdata.updates_roles()
|
||||
else:
|
||||
print(f"Role '{role_name}' not found in roles.json.")
|
||||
|
||||
|
||||
|
||||
def get_player_rank_and_score(player_id):
|
||||
"""Get the rank and score of a player."""
|
||||
|
|
@ -83,44 +69,76 @@ def get_player_rank_and_score(player_id):
|
|||
return player_data.get('rank', 0), player_data.get('scores', 0)
|
||||
|
||||
|
||||
# Function to print messages when admin or VIP roles are updated or removed
|
||||
def print_update_message(role_name, action, player_id):
|
||||
print(f"{role_name.capitalize()} {action}: {player_id}")
|
||||
|
||||
|
||||
# Function to remove outdated admins
|
||||
def remove_outdated_admins():
|
||||
"""Remove outdated admins based on specified conditions."""
|
||||
if not admin_rank:
|
||||
print("No admin ranks specified. Skipping admin role assignment.")
|
||||
return
|
||||
|
||||
roles_data = pdata.get_roles()
|
||||
current_admin_ids = roles_data.get('admin', {}).get('ids', [])
|
||||
|
||||
for player_id in current_admin_ids:
|
||||
rank, score = get_player_rank_and_score(player_id)
|
||||
if rank not in admin_rank and score < admin_score:
|
||||
print(f"Removing admin ID: {player_id}")
|
||||
print_update_message('admin', 'removed', player_id)
|
||||
remove_all_ids_from_role('admin', [player_id])
|
||||
threading.Timer(500, remove_outdated_admins).start() # Start timer for removing outdated admins
|
||||
|
||||
# Function to remove outdated VIPs
|
||||
def remove_outdated_vips():
|
||||
"""Remove outdated vips based on specified conditions."""
|
||||
if not vip_rank:
|
||||
print("No VIP ranks specified. Skipping VIP role assignment.")
|
||||
return
|
||||
|
||||
roles_data = pdata.get_roles()
|
||||
current_vip_ids = roles_data.get('vip', {}).get('ids', [])
|
||||
|
||||
for player_id in current_vip_ids:
|
||||
rank, score = get_player_rank_and_score(player_id)
|
||||
if rank not in vip_rank and score < vip_score:
|
||||
print(f"Removing VIP ID: {player_id}")
|
||||
print_update_message('vip', 'removed', player_id)
|
||||
remove_all_ids_from_role('vip', [player_id])
|
||||
threading.Timer(500, remove_outdated_vips).start() # Start timer for removing outdated VIPs
|
||||
|
||||
|
||||
# Function to update admins and VIPs
|
||||
def update_admins_and_vips():
|
||||
top_player = get_top_players(stats)
|
||||
top_players = get_top_players(stats)
|
||||
roles_data = pdata.get_roles()
|
||||
current_admin_ids = roles_data.get('admin', {}).get('ids', [])
|
||||
current_vip_ids = roles_data.get('vip', {}).get('ids', [])
|
||||
|
||||
for rank, (player_id, player_data) in enumerate(top_player, start=1):
|
||||
for rank, (player_id, player_data) in enumerate(top_players, start=1):
|
||||
score = player_data.get('scores', 0)
|
||||
if player_id not in roles_data:
|
||||
if rank in admin_rank and score >= admin_score and player_id not in current_admin_ids:
|
||||
print_update_message('admin', 'added', player_id)
|
||||
add_ids_to_role('admin', [player_id])
|
||||
elif rank in vip_rank and score >= vip_score and player_id not in current_vip_ids:
|
||||
add_ids_to_role('vip', [player_id])
|
||||
print_update_message('vip', 'added', player_id)
|
||||
add_ids_to_role('vip', [player_id])
|
||||
else:
|
||||
if rank not in admin_rank and score <= admin_score and player_id in current_admin_ids:
|
||||
print_update_message('admin', 'removed', player_id)
|
||||
remove_all_ids_from_role('admin', [player_id])
|
||||
elif rank not in vip_rank and score <= vip_score and player_id in current_vip_ids:
|
||||
remove_all_ids_from_role('vip', [player_id])
|
||||
print_update_message('vip', 'removed', player_id)
|
||||
remove_all_ids_from_role('vip', [player_id])
|
||||
threading.Timer(500, update_admins_and_vips).start()
|
||||
|
||||
|
||||
# Start autoadmin functions if enabled
|
||||
autoadmin_enabled = settings.get('autoadmin', {}).get('enable', False)
|
||||
if autoadmin_enabled:
|
||||
print(f"{BLACK}AutoAdmin is Enabled...{RESET}")
|
||||
update_admins_and_vips()
|
||||
remove_outdated_admins()
|
||||
remove_outdated_vips()
|
||||
else:
|
||||
print(f"{BLACK}AutoAdmin is Disabled. {RESET}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue