implement player count display

This commit is contained in:
2021-03-25 13:54:56 +01:00
parent 4f2b51eaad
commit fdc269cbfd
4 changed files with 60 additions and 1 deletions

View File

@@ -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', \