mirror of
https://github.com/FAUSheppy/open-web-leaderboard.git
synced 2025-12-06 07:01:36 +01:00
implement simple medals
This commit is contained in:
93
medals.py
Normal file
93
medals.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import flask
|
||||||
|
|
||||||
|
medalDict = { "games-played-1" : { "name" : "Tourist",
|
||||||
|
"text" : "Played {} games on this server",
|
||||||
|
"color" : "white",
|
||||||
|
"text-color" : "black" },
|
||||||
|
|
||||||
|
"games-played-2" : { "name" : "Enlisted",
|
||||||
|
"text" : "Played {} games on this server",
|
||||||
|
"color": "green",
|
||||||
|
"text-color" : "white" },
|
||||||
|
|
||||||
|
"games-played-3" : { "name" : "Veteran",
|
||||||
|
"text" : "Played {} games on this server",
|
||||||
|
"color" : "yellow",
|
||||||
|
"text-color" : "black" },
|
||||||
|
|
||||||
|
"rating-cur-1" : { "name" : "Slightly skilled",
|
||||||
|
"text" : "Rated above 1500",
|
||||||
|
"color" : "beige",
|
||||||
|
"text-color" : "black" },
|
||||||
|
|
||||||
|
"rating-2k-1" : { "name" : "Contender",
|
||||||
|
"text" : "Played {} games above 2000 rating",
|
||||||
|
"color" : "coral",
|
||||||
|
"text-color" : "black" },
|
||||||
|
"rating-2k-2" : { "name" : "Known Contender",
|
||||||
|
"text" : "Played {} games above 2000 rating",
|
||||||
|
"color" : "lightgreen",
|
||||||
|
"text-color" : "black" },
|
||||||
|
|
||||||
|
"rating-3k-1" : { "name" : "Epic",
|
||||||
|
"text" : "Played {} games above 3000 rating",
|
||||||
|
"color" : "lightblue",
|
||||||
|
"text-color" : "black" },
|
||||||
|
"rating-3k-2" : { "name" : "Legend",
|
||||||
|
"text" : "Played {} games above 3000 rating",
|
||||||
|
"color" : "orange",
|
||||||
|
"text-color" : "black" },
|
||||||
|
"rating-3k-3" : { "name" : "All Along The Watchtower",
|
||||||
|
"text" : "Played {} games above 3000 rating",
|
||||||
|
"color" : "red",
|
||||||
|
"text-color" : "black" },
|
||||||
|
}
|
||||||
|
|
||||||
|
def medalGen(medal, formatInsert=None):
|
||||||
|
'''Gen HTML for metal'''
|
||||||
|
html = '\
|
||||||
|
<div style="background-color: {bg} !important; color: {color} !important;"\
|
||||||
|
class="btn btn-secondary"\
|
||||||
|
data-toggle="tooltip" data-placement="top" title="{tooltip}">\
|
||||||
|
{text}\
|
||||||
|
</div>\
|
||||||
|
'
|
||||||
|
tmp = html.format(bg=medal["color"], tooltip=medal["text"], text=medal["name"],
|
||||||
|
color=medal["text-color"])
|
||||||
|
if formatInsert:
|
||||||
|
tmp = tmp.format(formatInsert)
|
||||||
|
return flask.Markup(tmp)
|
||||||
|
|
||||||
|
def getMedals(ratingList, gamesPlayed, currentRating):
|
||||||
|
'''Get Medals this player should have'''
|
||||||
|
medals = []
|
||||||
|
|
||||||
|
if gamesPlayed > 500:
|
||||||
|
medals += [medalGen(medalDict["games-played-3"], gamesPlayed)]
|
||||||
|
elif gamesPlayed > 100:
|
||||||
|
medals += [medalGen(medalDict["games-played-2"], gamesPlayed)]
|
||||||
|
elif gamesPlayed > 50:
|
||||||
|
medals += [medalGen(medalDict["games-played-1"], gamesPlayed)]
|
||||||
|
|
||||||
|
games2k = len(list(filter(lambda x: x > 2000, ratingList)))
|
||||||
|
games3k = len(list(filter(lambda x: x > 3000, ratingList)))
|
||||||
|
|
||||||
|
if games2k > 100:
|
||||||
|
medals += [medalGen(medalDict["rating-2k-2"], games2k)]
|
||||||
|
elif games2k > 0:
|
||||||
|
medals += [medalGen(medalDict["rating-2k-1"], games2k)]
|
||||||
|
|
||||||
|
if games3k > 200:
|
||||||
|
medals += [medalGen(medalDict["rating-3k-3"], game3k)]
|
||||||
|
|
||||||
|
if games3k > 50:
|
||||||
|
medals += [medalGen(medalDict["rating-3k-2"], games3k)]
|
||||||
|
elif games3k > 5:
|
||||||
|
medals += [medalGen(medalDict["rating-3k-1"], games3k)]
|
||||||
|
|
||||||
|
if currentRating > 1500:
|
||||||
|
medals += [medalGen(medalDict["rating-cur-1"])]
|
||||||
|
|
||||||
|
return medals
|
||||||
|
|
||||||
|
|
||||||
20
server.py
20
server.py
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
import medals
|
||||||
import flask
|
import flask
|
||||||
import requests
|
import requests
|
||||||
import argparse
|
import argparse
|
||||||
@@ -159,16 +160,29 @@ def player():
|
|||||||
minRating = 3000
|
minRating = 3000
|
||||||
maxRating = 0
|
maxRating = 0
|
||||||
|
|
||||||
|
# data for medals #
|
||||||
|
medalsRatingList = []
|
||||||
|
|
||||||
if histData:
|
if histData:
|
||||||
datapoints = histData[playerId]
|
datapoints = histData[playerId]
|
||||||
if datapoints:
|
if datapoints:
|
||||||
|
|
||||||
tickCounter = 10
|
tickCounter = 10
|
||||||
for dpk in datapoints.keys():
|
for dpk in datapoints.keys():
|
||||||
|
|
||||||
|
# timestamp #
|
||||||
t = datetime.datetime.fromtimestamp(int(float(dpk)))
|
t = datetime.datetime.fromtimestamp(int(float(dpk)))
|
||||||
tsMs = str(int(t.timestamp() * 1000))
|
tsMs = str(int(t.timestamp() * 1000))
|
||||||
ratingString = str(int(datapoints[dpk]["mu"]) - 2*int(datapoints[dpk]["sigma"]))
|
|
||||||
|
computedRating = int(datapoints[dpk]["mu"]) - 2*int(datapoints[dpk]["sigma"])
|
||||||
|
|
||||||
|
# for medals #
|
||||||
|
medalsRatingList += [computedRating]
|
||||||
|
|
||||||
|
# for moment js #
|
||||||
|
ratingString = str(computedRating)
|
||||||
ratingAmored = '{ x : ' + tsMs + ', y : ' + ratingString + '}'
|
ratingAmored = '{ x : ' + tsMs + ', y : ' + ratingString + '}'
|
||||||
|
|
||||||
csv_timestamps += [str(tsMs)]
|
csv_timestamps += [str(tsMs)]
|
||||||
csv_ratings += [ratingAmored]
|
csv_ratings += [ratingAmored]
|
||||||
|
|
||||||
@@ -181,6 +195,8 @@ def player():
|
|||||||
maxRating = max(maxRating, int(ratingString))
|
maxRating = max(maxRating, int(ratingString))
|
||||||
|
|
||||||
yMin, yMax = prettifyMinMaxY(minRating, maxRating)
|
yMin, yMax = prettifyMinMaxY(minRating, maxRating)
|
||||||
|
|
||||||
|
medalsList = medals.getMedals(medalsRatingList, player.games, player.rating)
|
||||||
|
|
||||||
# change displayed rank to start from 1 :)
|
# change displayed rank to start from 1 :)
|
||||||
player.rank += 1
|
player.rank += 1
|
||||||
@@ -188,7 +204,7 @@ def player():
|
|||||||
return flask.render_template("player.html", player=player, CSV_RATINGS=",".join(csv_ratings),
|
return flask.render_template("player.html", player=player, CSV_RATINGS=",".join(csv_ratings),
|
||||||
CSV_MONTH_YEAR_OF_RATINGS=",".join(csv_month_year),
|
CSV_MONTH_YEAR_OF_RATINGS=",".join(csv_month_year),
|
||||||
CSV_TIMESTAMPS=csv_timestamps,
|
CSV_TIMESTAMPS=csv_timestamps,
|
||||||
Y_MIN=yMin, Y_MAX=yMax)
|
Y_MIN=yMin, Y_MAX=yMax, medals=medalsList)
|
||||||
|
|
||||||
@app.route('/leaderboard')
|
@app.route('/leaderboard')
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
|
|||||||
@@ -27,6 +27,11 @@
|
|||||||
<canvas id="lineChart">
|
<canvas id="lineChart">
|
||||||
</canvas>
|
</canvas>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mt-3 medal-container">
|
||||||
|
{% for m in medals %}
|
||||||
|
{{ m }}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
<p class="mt-5 mb-3">
|
<p class="mt-5 mb-3">
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user