This commit is contained in:
2020-06-17 16:30:25 +02:00
4 changed files with 17 additions and 49 deletions

View File

@@ -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
- 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.

View File

@@ -8,7 +8,6 @@ DB_BASE = "file:{}?mode=ro"
def getTotalPlayers(database):
'''Get the total number of players in the database'''
print(DB_BASE.format(database))
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
cursor = conn.cursor()
cursor.execute("SELECT Count(*) FROM players")
@@ -32,22 +31,27 @@ def getRankRange(database, start, end):
return playerList
def findPlayerByName(playerName):
def findPlayerByName(database, playerName):
'''Find a player by his name (prefer fullmatch)'''
conn = sqlite3.connect(DB_BASE.format(database), uri=True)
cursor = conn.cursor()
playerNamePrepared = "%{}%".format(playerName.replace("%", "%%"))
cursor.execute("SELECT * FROM players WHERE name == ?", (playerName,))
row = cursor.fetone()
row = cursor.fetchone()
playerRow = None
if row:
cursor.execute("SELECT * FROM players WHERE name LIKE ?", (playerNamePrepared,))
row = cursor.fetchone()[0]
if not row:
playerRow = cursor.fetchone()
if not playerRow:
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()
return players.Leaderboard(playerRow)
return (playerInLeaderboard, rank)

View File

@@ -5,11 +5,11 @@ class PlayerInLeaderboard:
def __init__(self, dbRow):
'''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 #
self.name = name
self.playerID = playerID
self.playerId = playerId
self.mu = mu
self.sigma = sigma
self.rating = int(self.mu) - int(self.sigma)

View File

@@ -18,39 +18,6 @@ cache.init_app(app)
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('/')
@cache.cached(timeout=600, query_string=True)
@@ -72,15 +39,12 @@ def leaderboard():
searchName = ""
if playerName:
playersWithRankUrl = FIND_PLAYER.format(server=SERVER, pname=playerName)
playersWithRank = str(requests.get(playersWithRankUrl).content, "utf-8")
print(playersWithRank)
if playersWithRank == "[]":
playerInLeaderboard, rank = db.findPlayerByName(app.config["DB_PATH"], playerName)
if not playerInLeaderboard:
cannotFindPlayer = flask.Markup("<div class=noPlayerFound>No player of that name</div>")
start = 0
else:
searchName = playersWithRank.split(",")[1].strip(" '")
rank = int(playersWithRank.split(",")[4].strip(" ')]["))
searchName = playerInLeaderboard.name
start = rank - (rank % SEGMENT)
end = start + SEGMENT