diff --git a/CHANGELOG.md b/CHANGELOG.md index 03f71e17..975fa01e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ TBD - Send empty json for no banned/cached players ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/589)) - Bump Tornado from 6.0 to 6.3.2 in response to CVE-2023-28370 ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/591)) - Fix bug where commands would show "command_server" when initially created ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/592)) +### Refactor +- Optimize player management page ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/593)) ### Tweaks TBD ### Lang diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index fa68eb62..c1a11158 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -11,6 +11,7 @@ import subprocess import html import urllib.request import glob +import json from zoneinfo import ZoneInfo @@ -132,6 +133,15 @@ class ServerInstance: self.server_object = HelperServers.get_server_obj(self.server_id) self.stats_helper = HelperServerStats(self.server_id) self.last_backup_failed = False + try: + with open( + os.path.join(self.server_object.path, "db_stats", "players_cache.json"), + "r", + encoding="utf-8", + ) as f: + self.player_cache = list(json.load(f).values()) + except: + self.player_cache = [] try: self.tz = get_localzone() except ZoneInfoNotFoundError as e: @@ -770,6 +780,7 @@ class ServerInstance: ) if self.settings["stop_command"]: self.send_command(self.settings["stop_command"]) + self.write_player_cache() else: # windows will need to be handled separately for Ctrl+C self.process.terminate() @@ -1218,6 +1229,40 @@ class ServerInstance: ) update_thread.start() + def write_player_cache(self): + write_json = {} + for item in self.player_cache: + write_json[item["name"]] = item + with open( + os.path.join(self.server_path, "db_stats", "players_cache.json"), + "w", + encoding="utf-8", + ) as f: + f.write(json.dumps(write_json, indent=4)) + logger.info("Cache file refreshed") + + def cache_players(self): + server_players = self.get_server_players() + for p in self.player_cache[:]: + if p["status"] == "Online" and p["name"] not in server_players: + p["status"] = "Offline" + p["last_seen"] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M") + elif p["name"] in server_players: + self.player_cache.remove(p) + for player in server_players: + if player == "Anonymous Player": + # Skip Anonymous Player + continue + if player in self.player_cache: + self.player_cache.remove(player) + self.player_cache.append( + { + "name": player, + "status": "Online", + "last_seen": datetime.datetime.now().strftime("%d/%m/%Y %H:%M"), + } + ) + def check_update(self): return self.stats_helper.get_server_stats()["updating"] @@ -1404,6 +1449,12 @@ class ServerInstance: minutes=self.helper.get_setting("dir_size_poll_freq_minutes"), id=str(self.server_id) + "_dir_poll", ) + self.dir_scheduler.add_job( + self.cache_players, + "interval", + seconds=5, + id=str(self.server_id) + "_players_poll", + ) def calc_dir_size(self): server_dt = HelperServers.get_server_data_by_id(self.server_id) @@ -1473,6 +1524,7 @@ class ServerInstance: "created": datetime.datetime.now().strftime( "%Y/%m/%d, %H:%M:%S" ), + "players_cache": self.player_cache, }, ) total_players += int(raw_ping_result.get("online")) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index df58263d..48945fac 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -781,24 +781,11 @@ class PanelHandler(BaseHandler): page_data[ "banned_players" ] = self.controller.servers.get_banned_players(server_id) - page_data[ - "cached_players" - ] = self.controller.servers.get_cached_players(server_id) + server_instance = self.controller.servers.get_server_instance_by_id( + server_id + ) + page_data["cached_players"] = server_instance.player_cache - page_data["all_players"] = [] - for player in page_data["cached_players"]: - if player["name"] in page_data["get_players"]: - player["status"] = "online" - else: - player["status"] = "offline" - temp_date = datetime.datetime.strptime( - player["expiresOn"], "%Y-%m-%d %H:%M:%S %z" - ) - player["last_seen"] = ( - temp_date - datetime.timedelta(30, 0, 0, 0, 0, 0, 0) - ).strftime("%Y/%m/%d %H:%M:%S") - player["avatar"] = Helpers.get_player_avatar(player["uuid"]) - page_data["all_players"].append(player) for player in page_data["banned_players"]: player["banned"] = True temp_date = datetime.datetime.strptime( diff --git a/app/frontend/templates/panel/parts/details_stats.html b/app/frontend/templates/panel/parts/details_stats.html index 38007544..47fa501d 100644 --- a/app/frontend/templates/panel/parts/details_stats.html +++ b/app/frontend/templates/panel/parts/details_stats.html @@ -228,6 +228,24 @@ } initParser('input_motd', 'input_motd'); + let text = "" + let players = server.players_cache; + for(let i=0; i < players.length; i++){ + text += ``; + text += `${players[i]["name"]}`; + if(players[i]["status"] === "Online"){ + text += ` ${ players[i]['status'] }` + }else{ + text += ` ${ players[i]['status'] } Last connection :
${ players[i]['last_seen'] }
` + } + if(server["running"]){ + text += `


` + }else{ + text += ` Unavailable
(Server Offline)
` + } + + } + $("#player-body").html(text); } diff --git a/app/frontend/templates/panel/parts/server_players.html b/app/frontend/templates/panel/parts/server_players.html index fd014018..df059e95 100644 --- a/app/frontend/templates/panel/parts/server_players.html +++ b/app/frontend/templates/panel/parts/server_players.html @@ -1,4 +1,4 @@ -
+

{{ translate('serverPlayerManagement', 'players', data['lang']) }}:

@@ -8,25 +8,28 @@ - - {% for player in data['all_players'] %} - + + {% for player in data['cached_players'] %} + - {% if player['status'] == 'online' %} - - {% elif player['status'] == 'offline' %} - + {% if player['status'] == 'Online' %} + + {% elif player['status'] == 'Offline' %} + {% end %} - @@ -34,9 +37,23 @@
Actions
{{ player['name'] }} {{ player['status'] }} Last connection : {{ player['last_seen'] }} {{ player['status'] }} {{ player['status'] }} Last connection :
{{ player['last_seen'] }}
+ {% if data['server_stats']['running'] %} - - - - + +
+ +
+ +
+ {% else %} - Unavailable (Server Offline) + Unavailable
(Server Offline)
{% end %}
-
+ +

{{ translate('serverPlayerManagement', 'bannedPlayers', data['lang']) }}:

- +
@@ -58,4 +75,22 @@ {% end %}
Player
+ + + + + + + + + {% for player in data['banned_players'] %} + + + + + {% end %} + +
PlayerActions
{{ player['name'] }} + +
\ No newline at end of file