From 7cd3a49f6d3a7bf73fafff72445fe7a4e64a177d Mon Sep 17 00:00:00 2001 From: sqozz Date: Wed, 24 Mar 2021 16:40:17 +0100 Subject: [PATCH] Add realtime data --- homepage/main.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/homepage/main.py b/homepage/main.py index d539434..41cd8c1 100644 --- a/homepage/main.py +++ b/homepage/main.py @@ -3,7 +3,9 @@ from bottle import route, run, static_file, template from os import listdir from os.path import join as pathjoin from random import random, choice +from mcrcon import MCRcon import configparser +import re # echo -n "["; ls | xargs -I{} echo -n \"{}\",; echo "]" BG_IMAGES = listdir("./static/img/background") @@ -19,11 +21,8 @@ news = """ @route("/") def index(): - return template("html/index.html", **{"player_count": int(random()*20), "max_players": 20, "news": news}) - -@route("/request") -def request_whitelist(): - return "ok" + players = currentPlayerData() + return template("html/index.html", **{"player_count": players["count"]["current"] , "max_players": players["count"]["max"], "news": news}) @route("/img/bg.png") def random_bg_image(): @@ -42,6 +41,28 @@ def random_bg_image(): def callback(path): return static_file(path, root="static") +def currentPlayerData(): + player = {} + with MCRcon(CONFIG["mcrcon"]["host"], CONFIG["mcrcon"]["password"], port=int(CONFIG["mcrcon"]["port"])) as mcr: + resp = mcr.command("list uuids") + resp = resp.split(":") + player_list = resp[1].strip().split(",") + player_list = list(map(lambda x: x.strip(), player_list)) + count_regex = re.match("There are ([0-9]+) of a max of ([0-9]+) players online", resp[0]) + num_players = count_regex.group(1) + max_players = count_regex.group(2) + + player["count"] = {} + player["count"]["current"] = num_players + player["count"]["max"] = max_players + player["players"] = [] + for p in player_list: + data = p.split(" ") + name = data[0].strip() + uuid = data[1].strip().replace("(", "").replace(")", "") + player["players"].append({"name": name, "uuid": uuid}) + return player + def parseConfig(): config = configparser.ConfigParser() config.read("config.ini")