From 1be41b85345c2bae4b8ba1dc07642e9762590c08 Mon Sep 17 00:00:00 2001 From: luukas Date: Fri, 5 Mar 2021 10:58:38 +0200 Subject: [PATCH] Player controls --- app/classes/minecraft/stats.py | 27 +++ app/classes/shared/helpers.py | 16 ++ app/classes/web/panel_handler.py | 26 ++- .../panel/server_admin_controls.html | 184 ++++++++++++++++++ .../templates/panel/server_config.html | 4 + .../templates/panel/server_files.html | 4 + app/frontend/templates/panel/server_logs.html | 4 + app/frontend/templates/panel/server_term.html | 4 + 8 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 app/frontend/templates/panel/server_admin_controls.html diff --git a/app/classes/minecraft/stats.py b/app/classes/minecraft/stats.py index 619c591e..2d4a4209 100644 --- a/app/classes/minecraft/stats.py +++ b/app/classes/minecraft/stats.py @@ -9,6 +9,7 @@ import datetime from app.classes.shared.helpers import helper from app.classes.minecraft.mc_ping import ping from app.classes.shared.controller import controller +from app.classes.shared.models import db_helper from app.classes.shared.models import Host_Stats, Server_Stats logger = logging.getLogger(__name__) @@ -151,6 +152,32 @@ class Stats: } return ping_data + + def get_server_players(self, server_id): + + server = db_helper.get_server_data_by_id(server_id) + + logger.info("Getting players for server {}".format(server)) + + # get our settings and data dictionaries + server_settings = server.get('server_settings', {}) + server_data = server.get('server_data_obj', {}) + + + # TODO: search server properties file for possible override of 127.0.0.1 + internal_ip = server_data.get('server_ip', "127.0.0.1") + server_port = server_settings.get('server_port', "25565") + + logger.debug("Pinging {} on port {}".format(internal_ip, server_port)) + int_mc_ping = ping(internal_ip, int(server_port)) + + ping_data = {} + + # if we got a good ping return, let's parse it + if int_mc_ping: + ping_data = self.parse_server_ping(int_mc_ping) + return ping_data['players'] + return [] def get_servers_stats(self): diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index bc05420c..e010df81 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -516,6 +516,22 @@ class Helpers: @staticmethod def in_path(x, y): return os.path.abspath(y).__contains__(os.path.abspath(x)) + + @staticmethod + def get_banned_players(server_id, db_helper): + stats = db_helper.get_server_stats_by_id(server_id) + server_path = stats[0]['server_id']['path'] + path = os.path.join(server_path, 'banned-players.json') + + try: + with open(path) as file: + content = file.read() + file.close() + except Exception as ex: + print (ex) + return None + + return json.loads(content) helper = Helpers() diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 616074dd..5a434439 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -12,6 +12,7 @@ from app.classes.web.base_handler import BaseHandler from app.classes.shared.controller import controller from app.classes.shared.models import db_helper, Servers from app.classes.shared.helpers import helper +from app.classes.minecraft.stats import stats logger = logging.getLogger(__name__) @@ -109,7 +110,7 @@ class PanelHandler(BaseHandler): self.redirect("/panel/error?error=Invalid Server ID") return False - valid_subpages = ['term', 'logs', 'config', 'files'] + valid_subpages = ['term', 'logs', 'config', 'files', 'admin_controls'] if subpage not in valid_subpages: logger.debug('not a valid subpage') @@ -119,6 +120,29 @@ class PanelHandler(BaseHandler): # server_data isn't needed since the server_stats also pulls server data # page_data['server_data'] = db_helper.get_server_data_by_id(server_id) page_data['server_stats'] = db_helper.get_server_stats_by_id(server_id) + page_data['get_players'] = lambda: stats.get_server_players(server_id) + + def get_banned_players_html(): + banned_players = helper.get_banned_players(server_id, db_helper) + if banned_players is None: + return """ +
  • +

    Error while reading banned-players.json

    +
  • + """ + html = "" + for player in banned_players: + html += """ +
  • +

    {}

    + Banned by {} for reason: {} + +
  • + """.format(player['name'], player['source'], player['reason'], player['name']) + + return html + if subpage == "admin_controls": + page_data['banned_players'] = get_banned_players_html() # template = "panel/server_details.html" template = "panel/server_{subpage}.html".format(subpage=subpage) diff --git a/app/frontend/templates/panel/server_admin_controls.html b/app/frontend/templates/panel/server_admin_controls.html new file mode 100644 index 00000000..1cc8348d --- /dev/null +++ b/app/frontend/templates/panel/server_admin_controls.html @@ -0,0 +1,184 @@ +{% extends ../base.html %} + +{% block meta %} + +{% end %} + +{% block title %}Crafty Controller - Server Details{% end %} + +{% block content %} + +
    + + +
    +
    + +
    + +
    + + + {% include "parts/details_stats.html %} + +
    + +
    +
    +
    + + +
    +
    + +

    Players:

    +
      + {% for player in data['get_players']() %} +
    • +

      {{ player }}

      +
      + + + + +
      +
    • + {% end %} +
    +
    +
    +

    Banned Players:

    +
      +
    • +

      Loading Banned Players

      +
    • + +
    +
    +
    + +
    +
    +
    +
    + + + +
    + + +{% end %} + +{% block js %} + + +{% end %} \ No newline at end of file diff --git a/app/frontend/templates/panel/server_config.html b/app/frontend/templates/panel/server_config.html index 5d7450d2..d7c68c1e 100644 --- a/app/frontend/templates/panel/server_config.html +++ b/app/frontend/templates/panel/server_config.html @@ -57,6 +57,10 @@ Config + diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index a4b15940..72b9263a 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -57,6 +57,10 @@ Config + diff --git a/app/frontend/templates/panel/server_logs.html b/app/frontend/templates/panel/server_logs.html index 57bab289..fdbcd319 100644 --- a/app/frontend/templates/panel/server_logs.html +++ b/app/frontend/templates/panel/server_logs.html @@ -58,6 +58,10 @@ Config + diff --git a/app/frontend/templates/panel/server_term.html b/app/frontend/templates/panel/server_term.html index 5c7a2390..96d5796c 100644 --- a/app/frontend/templates/panel/server_term.html +++ b/app/frontend/templates/panel/server_term.html @@ -58,6 +58,10 @@ Config +