mirror of
https://github.com/FAUSheppy/open-web-leaderboard.git
synced 2025-12-06 07:01:36 +01:00
fix findPlayer query
This commit is contained in:
18
database.py
18
database.py
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
43
server.py
43
server.py
@@ -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
|
||||
@@ -112,7 +76,6 @@ def leaderboard():
|
||||
if maxEntry <= 100:
|
||||
start = max(start, 0)
|
||||
|
||||
print(playerList)
|
||||
finalResponse = flask.render_template("base.html", playerList=playerList, \
|
||||
columNames=columContent, \
|
||||
start=start, \
|
||||
|
||||
Reference in New Issue
Block a user