diff --git a/.gitignore b/.gitignore index 752d977..b2bf227 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ static/moment.js *.sqlite *.png blacklist.json +servers.json diff --git a/README.md b/README.md index 93e3aab..7410c8a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,22 @@ Players can be blacklisted by name via a *blacklist.json* file in the project ro "blacklist" : ["name", "name_2"] } +# Adding servers for player count live info +Source-Servers can be added via the *servers.json*-file: + + [ + { + "name" : "server_1", + "host" : "example.com", + "port" : 27015 + }, + { + ... + } + ] + +*Python-valve* is required if this file exists. + # Preview  diff --git a/server.py b/server.py index 31954cc..d8f54cd 100755 --- a/server.py +++ b/server.py @@ -20,6 +20,7 @@ cache = fcache.Cache(app, config={'CACHE_TYPE': 'simple'}) cache.init_app(app) SEGMENT=100 +SERVERS=list() @@ -29,6 +30,27 @@ def prettifyMinMaxY(computedMin, computedMax): else: return (computedMin - 100, 4000) +@app.route("/players-online") +def playersOnline(): + '''Calc and return the online players''' + + playerTotal = 0 + error = "" + + for s in SERVERS: + try: + with valve.source.a2s.ServerQuerier((args.host, args.port)) as server: + playerTotal += int(server.info()["player_count"]) + except NoResponseError: + error = "Server Unreachable" + except Exception as e: + error = str(e) + + retDict = { "player_total" : playerTotal, "error" : error } + return flask.Response(json.dumps(retDict), 200, mimetype='application/json') + + + @app.route("/round-info") def singleRound(): '''Display info about a single round itdentified by it's timestamp''' @@ -252,7 +274,12 @@ def send_js(path): @app.before_first_request def init(): - pass + SERVERS_FILE = "servers.json" + if os.path.isfile(SERVERS_FILE): + import valve.source.a2s + from valve.source import NoResponseError + with open(SERVERS_FILE) as f: + SERVERS = json.load(f) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Start open-leaderboard', \ diff --git a/templates/base.html b/templates/base.html index 7cf34e8..8a33bab 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,6 +10,21 @@ {% include 'navbar_leaderboard.html' %}