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

1
.gitignore vendored
View File

@@ -17,3 +17,4 @@ static/moment.js
*.sqlite *.sqlite
*.png *.png
blacklist.json blacklist.json
servers.json

View File

@@ -32,6 +32,22 @@ Players can be blacklisted by name via a *blacklist.json* file in the project ro
"blacklist" : ["name", "name_2"] "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 # Preview
![open-web-leaderboard](https://media.atlantishq.de/leaderboard-github-picture.png) ![open-web-leaderboard](https://media.atlantishq.de/leaderboard-github-picture.png)

View File

@@ -20,6 +20,7 @@ cache = fcache.Cache(app, config={'CACHE_TYPE': 'simple'})
cache.init_app(app) cache.init_app(app)
SEGMENT=100 SEGMENT=100
SERVERS=list()
@@ -29,6 +30,27 @@ def prettifyMinMaxY(computedMin, computedMax):
else: else:
return (computedMin - 100, 4000) 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") @app.route("/round-info")
def singleRound(): def singleRound():
'''Display info about a single round itdentified by it's timestamp''' '''Display info about a single round itdentified by it's timestamp'''
@@ -252,7 +274,12 @@ def send_js(path):
@app.before_first_request @app.before_first_request
def init(): 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__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Start open-leaderboard', \ parser = argparse.ArgumentParser(description='Start open-leaderboard', \

View File

@@ -10,6 +10,21 @@
{% include 'navbar_leaderboard.html' %} {% include 'navbar_leaderboard.html' %}
<div class="container mt-3 mb-3" role="main"> <div class="container mt-3 mb-3" role="main">
<div id="playerDisplay" class="playerDisplay mb-3 mt-2">
<script>
fetch("/players-online").then(
response => response.json()
).then(
data => {
if(data["error"] == ""){
document.getElementById("playerDisplay").innerHTML = "Players Online: " + data["player_total"]
}else{
document.getElementById("playerDisplay").innerHTML = "Players Online: (error)" + data["error"]
}
}
)
</script>
</div>
<table id="tableMain" class="table table-striped table-bordered table-sm" <table id="tableMain" class="table table-striped table-bordered table-sm"
cellspacing="0"> cellspacing="0">
<thead> <thead>