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 html
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import glob
|
import glob
|
||||||
|
import json
|
||||||
|
|
||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
@ -132,6 +133,15 @@ class ServerInstance:
|
|||||||
self.server_object = HelperServers.get_server_obj(self.server_id)
|
self.server_object = HelperServers.get_server_obj(self.server_id)
|
||||||
self.stats_helper = HelperServerStats(self.server_id)
|
self.stats_helper = HelperServerStats(self.server_id)
|
||||||
self.last_backup_failed = False
|
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:
|
try:
|
||||||
self.tz = get_localzone()
|
self.tz = get_localzone()
|
||||||
except ZoneInfoNotFoundError as e:
|
except ZoneInfoNotFoundError as e:
|
||||||
@ -770,6 +780,7 @@ class ServerInstance:
|
|||||||
)
|
)
|
||||||
if self.settings["stop_command"]:
|
if self.settings["stop_command"]:
|
||||||
self.send_command(self.settings["stop_command"])
|
self.send_command(self.settings["stop_command"])
|
||||||
|
self.write_player_cache()
|
||||||
else:
|
else:
|
||||||
# windows will need to be handled separately for Ctrl+C
|
# windows will need to be handled separately for Ctrl+C
|
||||||
self.process.terminate()
|
self.process.terminate()
|
||||||
@ -1218,6 +1229,40 @@ class ServerInstance:
|
|||||||
)
|
)
|
||||||
update_thread.start()
|
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):
|
def check_update(self):
|
||||||
return self.stats_helper.get_server_stats()["updating"]
|
return self.stats_helper.get_server_stats()["updating"]
|
||||||
|
|
||||||
@ -1745,6 +1790,7 @@ class ServerInstance:
|
|||||||
|
|
||||||
def record_server_stats(self):
|
def record_server_stats(self):
|
||||||
server_stats = self.get_servers_stats()
|
server_stats = self.get_servers_stats()
|
||||||
|
self.cache_players()
|
||||||
self.stats_helper.insert_server_stats(server_stats)
|
self.stats_helper.insert_server_stats(server_stats)
|
||||||
|
|
||||||
# delete old data
|
# delete old data
|
||||||
|
@ -781,24 +781,11 @@ class PanelHandler(BaseHandler):
|
|||||||
page_data[
|
page_data[
|
||||||
"banned_players"
|
"banned_players"
|
||||||
] = self.controller.servers.get_banned_players(server_id)
|
] = self.controller.servers.get_banned_players(server_id)
|
||||||
page_data[
|
server_instance = self.controller.servers.get_server_instance_by_id(
|
||||||
"cached_players"
|
server_id
|
||||||
] = 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"
|
|
||||||
)
|
)
|
||||||
player["last_seen"] = (
|
page_data["cached_players"] = server_instance.player_cache
|
||||||
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"]:
|
for player in page_data["banned_players"]:
|
||||||
player["banned"] = True
|
player["banned"] = True
|
||||||
temp_date = datetime.datetime.strptime(
|
temp_date = datetime.datetime.strptime(
|
||||||
|
@ -9,14 +9,14 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for player in data['all_players'] %}
|
{% for player in data['cached_players'] %}
|
||||||
<tr id="playerItem-{{ player['name'] }}" class="playerItem--">
|
<tr id="playerItem-{{ player['name'] }}" class="playerItem--">
|
||||||
<td>
|
<td>
|
||||||
<strong> {{ player['name'] }}</strong>
|
<strong> {{ player['name'] }}</strong>
|
||||||
</td>
|
</td>
|
||||||
{% if player['status'] == 'online' %}
|
{% if player['status'] == 'Online' %}
|
||||||
<td><span class="text-success"><i class="fas fa-signal"></i> {{ player['status'] }}</span></td>
|
<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>
|
<td><span class="text-warning"><i class="fa-solid fa-signal-slash"></i> Last connection : {{ player['last_seen'] }}</span></td>
|
||||||
{% end %}
|
{% end %}
|
||||||
<td class="buttons">
|
<td class="buttons">
|
||||||
|
Loading…
Reference in New Issue
Block a user