From 5ea26605d3cf510cbbc49329bd7ed42e28f156be Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Sun, 9 Jun 2019 01:03:58 +0200 Subject: [PATCH] implement buttons --- open-leaderboard-server.py | 27 +++++++++++++++------- static/buttons.js | 46 ++++++++++++++++++++++++++++++++++++++ static/site.css | 3 ++- templates/base.html | 8 ++++--- 4 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 static/buttons.js diff --git a/open-leaderboard-server.py b/open-leaderboard-server.py index d683db1..a00667d 100755 --- a/open-leaderboard-server.py +++ b/open-leaderboard-server.py @@ -53,6 +53,18 @@ class Player: # mark returned string as preformated html # return flask.Markup(string) +def requestRange(start, end): + '''Request a range from the rating server''' + + # request information from rating server # + requestURL = BASE_URL.format(server=SERVER, \ + path=LOCATION, \ + paramStart=PARAM_START, \ + paramEnd=PARAM_END, \ + start=start, \ + end=end) + + return str(requests.get(requestURL).content, "utf-8") @app.route('/leaderboard') def leaderboard(): @@ -71,15 +83,14 @@ def leaderboard(): end = start + SEGMENT - # request information from rating server # - requestURL = BASE_URL.format(server=SERVER, \ - path=LOCATION, \ - paramStart=PARAM_START, \ - paramEnd=PARAM_END, \ - start=start, \ - end=end) - responseString = str(requests.get(requestURL).content, "utf-8") + # request and check if we are within range # + responseString = requestRange(start, end) + if "MAXENTRY:" in responseString: + maxentry = int(responseString.split(":")[1]) + start = maxentry - SEGMENT - 1 + end = maxentry - 1 + responseString = requestRange(start, end) # create relevant html-lines from player players = [Player(line) for line in responseString.split("\n")] diff --git a/static/buttons.js b/static/buttons.js new file mode 100644 index 0000000..5ba62f8 --- /dev/null +++ b/static/buttons.js @@ -0,0 +1,46 @@ +function forward(){ + + var url = new URL(window.location.href) + var start = url.searchParams.get("start") + var page = url.searchParams.get("page") + + if(page){ + page = parseInt(page) + 1 + }else if(start){ + page = Math.trunc(parseInt(start)/100) + 1 + }else{ + page = 1 + } + + url.searchParams.set("page", page) + window.location.href = url.href + +} +function backward(){ + + var url = new URL(window.location.href) + var start = url.searchParams.get("start") + var page = url.searchParams.get("page") + + if(page){ + page = parseInt(page) - 1 + if(page < 0){ + page = 0 + } + }else if(start){ + page = Math.trunc(parseInt(start)/100) - 1 + }else{ + page = 0 + } + + url.searchParams.set("page", page) + window.location.href = url.href +} + +function firstPage(){ + var href = window.location.href + var parameterSeperator = "?" + if(href.includes(parameterSeperator)){ + window.location.href = href.split(parameterSeperator)[0] + } +} diff --git a/static/site.css b/static/site.css index aafee55..e6a0742 100644 --- a/static/site.css +++ b/static/site.css @@ -11,9 +11,9 @@ body{ .top-bar{ position: fixed; background-color: orange; - height: 20px; padding-bottom: 10px; padding-top: 10px; + padding-left: 10px; left: 0; top: 0; @@ -23,6 +23,7 @@ body{ .top-button{ height: 80%; float: left; + margin-right: 10px; background-color: red; diff --git a/templates/base.html b/templates/base.html index 9b9b024..d0fbeeb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,12 +1,14 @@ +
-
Forward
-
Backward
-
Top 100
+ + + +