mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Use a cache for players instead of reading file
This commit is contained in:
parent
91a81095b4
commit
a04771df34
@ -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:%S")
|
||||
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:%S"),
|
||||
}
|
||||
)
|
||||
|
||||
def check_update(self):
|
||||
return self.stats_helper.get_server_stats()["updating"]
|
||||
|
||||
@ -1745,6 +1790,7 @@ class ServerInstance:
|
||||
|
||||
def record_server_stats(self):
|
||||
server_stats = self.get_servers_stats()
|
||||
self.cache_players()
|
||||
self.stats_helper.insert_server_stats(server_stats)
|
||||
|
||||
# delete old data
|
||||
|
@ -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)
|
||||
|
||||
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"
|
||||
server_instance = self.controller.servers.get_server_instance_by_id(
|
||||
server_id
|
||||
)
|
||||
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)
|
||||
page_data["cached_players"] = server_instance.player_cache
|
||||
|
||||
for player in page_data["banned_players"]:
|
||||
player["banned"] = True
|
||||
temp_date = datetime.datetime.strptime(
|
||||
|
@ -9,14 +9,14 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for player in data['all_players'] %}
|
||||
{% for player in data['cached_players'] %}
|
||||
<tr id="playerItem-{{ player['name'] }}" class="playerItem--">
|
||||
<td>
|
||||
<strong> {{ player['name'] }}</strong>
|
||||
</td>
|
||||
{% if player['status'] == 'online' %}
|
||||
{% if player['status'] == 'Online' %}
|
||||
<td><span class="text-success"><i class="fas fa-signal"></i> {{ player['status'] }}</span></td>
|
||||
{% elif player['status'] == 'offline' %}
|
||||
{% elif player['status'] == 'Offline' %}
|
||||
<td><span class="text-warning"><i class="fa-solid fa-signal-slash"></i> Last connection : {{ player['last_seen'] }}</span></td>
|
||||
{% end %}
|
||||
<td class="buttons">
|
||||
|
Loading…
Reference in New Issue
Block a user