mirror of
https://github.com/FAUSheppy/open-web-leaderboard.git
synced 2026-01-22 02:47:39 +01:00
Merge branch 'master' of https://gitlab.com/Sheppy_/open-web-leaderboard
This commit is contained in:
@@ -3,7 +3,7 @@ The Open Web Leaderboard is a leaderboard that can easily be used with any backe
|
|||||||
|
|
||||||
- getRankRange(start, end) -> return a list of players from start rank to end rank
|
- getRankRange(start, end) -> return a list of players from start rank to end rank
|
||||||
- getMaxEntries() -> return the total number of entries in the leaderboard
|
- getMaxEntries() -> return the total number of entries in the leaderboard
|
||||||
- findPlayer() -> find a player by name
|
- findPlayer() -> find a player by name and return a (player, rank)-tupel
|
||||||
|
|
||||||
The system was developed to be used with the [skillbird-framwork](https://github.com/FAUSheppy/skillbird). If you use this framework, the program should be working without any arguments. If you need more conductibility feel free to open a pull-request or send me a message.
|
The system was developed to be used with the [skillbird-framwork](https://github.com/FAUSheppy/skillbird). If you use this framework, the program should be working without any arguments. If you need more conductibility feel free to open a pull-request or send me a message.
|
||||||
|
|
||||||
|
|||||||
18
database.py
18
database.py
@@ -8,7 +8,6 @@ DB_BASE = "file:{}?mode=ro"
|
|||||||
def getTotalPlayers(database):
|
def getTotalPlayers(database):
|
||||||
'''Get the total number of players in the database'''
|
'''Get the total number of players in the database'''
|
||||||
|
|
||||||
print(DB_BASE.format(database))
|
|
||||||
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
|
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("SELECT Count(*) FROM players")
|
cursor.execute("SELECT Count(*) FROM players")
|
||||||
@@ -32,22 +31,27 @@ def getRankRange(database, start, end):
|
|||||||
|
|
||||||
return playerList
|
return playerList
|
||||||
|
|
||||||
def findPlayerByName(playerName):
|
def findPlayerByName(database, playerName):
|
||||||
'''Find a player by his name (prefer fullmatch)'''
|
'''Find a player by his name (prefer fullmatch)'''
|
||||||
|
|
||||||
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
|
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
playerNamePrepared = "%{}%".format(playerName.replace("%", "%%"))
|
playerNamePrepared = "%{}%".format(playerName.replace("%", "%%"))
|
||||||
cursor.execute("SELECT * FROM players WHERE name == ?", (playerName,))
|
cursor.execute("SELECT * FROM players WHERE name == ?", (playerName,))
|
||||||
row = cursor.fetone()
|
row = cursor.fetchone()
|
||||||
|
|
||||||
playerRow = None
|
playerRow = None
|
||||||
if row:
|
if row:
|
||||||
cursor.execute("SELECT * FROM players WHERE name LIKE ?", (playerNamePrepared,))
|
cursor.execute("SELECT * FROM players WHERE name LIKE ?", (playerNamePrepared,))
|
||||||
row = cursor.fetchone()[0]
|
playerRow = cursor.fetchone()
|
||||||
if not row:
|
if not playerRow:
|
||||||
conn.close()
|
conn.close()
|
||||||
return None
|
return (None, None)
|
||||||
|
|
||||||
|
playerInLeaderboard = player.PlayerInLeaderboard(playerRow)
|
||||||
|
# compte rank
|
||||||
|
cursor.execute("SELECT COUNT(*) from players where mu < ( SELECT mu from players where playerId == ? );",
|
||||||
|
(playerInLeaderboard.playerId,))
|
||||||
|
rank = cursor.fetchone()[0]
|
||||||
conn.close()
|
conn.close()
|
||||||
return players.Leaderboard(playerRow)
|
return (playerInLeaderboard, rank)
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ class PlayerInLeaderboard:
|
|||||||
def __init__(self, dbRow):
|
def __init__(self, dbRow):
|
||||||
'''Initialize a player object later to be serialized to HTML'''
|
'''Initialize a player object later to be serialized to HTML'''
|
||||||
|
|
||||||
playerID, name, lastGame, wins, mu, sigma, games = dbRow
|
playerId, name, lastGame, wins, mu, sigma, games = dbRow
|
||||||
|
|
||||||
# set relevant values #
|
# set relevant values #
|
||||||
self.name = name
|
self.name = name
|
||||||
self.playerID = playerID
|
self.playerId = playerId
|
||||||
self.mu = mu
|
self.mu = mu
|
||||||
self.sigma = sigma
|
self.sigma = sigma
|
||||||
self.rating = int(self.mu) - int(self.sigma)
|
self.rating = int(self.mu) - int(self.sigma)
|
||||||
|
|||||||
42
server.py
42
server.py
@@ -18,39 +18,6 @@ cache.init_app(app)
|
|||||||
|
|
||||||
SEGMENT=100
|
SEGMENT=100
|
||||||
|
|
||||||
class PlayerInLeaderboard:
|
|
||||||
def __init__(self, dbRow):
|
|
||||||
'''Initialize a player object later to be serialized to HTML'''
|
|
||||||
|
|
||||||
name, playerID, rating, games, wins = dbRow
|
|
||||||
|
|
||||||
# set relevant values #
|
|
||||||
self.name = name
|
|
||||||
self.playerID = playerID
|
|
||||||
self.rating = int(float(rating))
|
|
||||||
self.games = int(games)
|
|
||||||
self.wins = int(wins)
|
|
||||||
self.loses = self.games - self.wins
|
|
||||||
|
|
||||||
# determine winratio #
|
|
||||||
if self.games == 0:
|
|
||||||
self.winratio = "N/A"
|
|
||||||
else:
|
|
||||||
self.winratio = str(int(self.wins/self.games * 100))
|
|
||||||
|
|
||||||
def getLineHTML(self, rank):
|
|
||||||
'''Build a single line for a specific player in the leaderboard'''
|
|
||||||
|
|
||||||
string = flask.render_template("playerLine.html", \
|
|
||||||
playerRank = rank, \
|
|
||||||
playerName = self.name, \
|
|
||||||
playerRating = self.rating, \
|
|
||||||
playerGames = self.games, \
|
|
||||||
playerWinratio = self.winratio)
|
|
||||||
|
|
||||||
# mark returned string as preformated html #
|
|
||||||
return flask.Markup(string)
|
|
||||||
|
|
||||||
@app.route('/leaderboard')
|
@app.route('/leaderboard')
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@cache.cached(timeout=600, query_string=True)
|
@cache.cached(timeout=600, query_string=True)
|
||||||
@@ -72,15 +39,12 @@ def leaderboard():
|
|||||||
searchName = ""
|
searchName = ""
|
||||||
|
|
||||||
if playerName:
|
if playerName:
|
||||||
playersWithRankUrl = FIND_PLAYER.format(server=SERVER, pname=playerName)
|
playerInLeaderboard, rank = db.findPlayerByName(app.config["DB_PATH"], playerName)
|
||||||
playersWithRank = str(requests.get(playersWithRankUrl).content, "utf-8")
|
if not playerInLeaderboard:
|
||||||
print(playersWithRank)
|
|
||||||
if playersWithRank == "[]":
|
|
||||||
cannotFindPlayer = flask.Markup("<div class=noPlayerFound>No player of that name</div>")
|
cannotFindPlayer = flask.Markup("<div class=noPlayerFound>No player of that name</div>")
|
||||||
start = 0
|
start = 0
|
||||||
else:
|
else:
|
||||||
searchName = playersWithRank.split(",")[1].strip(" '")
|
searchName = playerInLeaderboard.name
|
||||||
rank = int(playersWithRank.split(",")[4].strip(" ')]["))
|
|
||||||
start = rank - (rank % SEGMENT)
|
start = rank - (rank % SEGMENT)
|
||||||
|
|
||||||
end = start + SEGMENT
|
end = start + SEGMENT
|
||||||
|
|||||||
Reference in New Issue
Block a user