Skip to content

Commit

Permalink
Merge branch 'leaderboard_site_update' into final-temp-master
Browse files Browse the repository at this point in the history
  • Loading branch information
HakierGrzonzo committed Apr 21, 2020
2 parents 7d37930 + 286aa9b commit 3c4e1eb
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 67 deletions.
7 changes: 0 additions & 7 deletions Assets/Packages/RestSharp.106.10.1.meta~HEAD.meta

This file was deleted.

57 changes: 36 additions & 21 deletions Assets/Scripts/LeaderBoardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ i zwraca miejsce w rankingu (UploadTime_and_get_place(username))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Net;
using RestSharp;
using System.Text;

public class LeaderBoardHandler : MonoBehaviour
{
Expand All @@ -25,13 +26,22 @@ public class LeaderBoardHandler : MonoBehaviour
public string LevelName;
private System.DateTime startTime;
private System.DateTime stopTime;
private RestClient client = new RestClient("http://grzegorzkoperwas.site:5000");
[SerializeField]
private bool debug_server;
private string url;




public void Awake()
{
if (debug_server) {
url = "http://localhost:5000";
Debug.LogWarning("Running leaderboard in debug mode");
}
else {
url = "http://grzegorzkoperwas.site:5000";
}
StartTimer();
}

Expand Down Expand Up @@ -62,29 +72,34 @@ public string GetCurrentTime() {

// Uploads time to server under (username) and returns place in ranking
public int UploadTime_and_get_place(string username = "testUsername") {
var request = new RestRequest("/api/postTime", Method.POST);
request.AddParameter("nick", username);
request.AddParameter("time", GetFinalTimeISO());
request.AddParameter("level", LevelName);
try
var args = new List<IMultipartFormSection>();
args.Add(new MultipartFormDataSection("nick", Encoding.UTF8.GetBytes(username)));
args.Add(new MultipartFormDataSection("time", Encoding.UTF8.GetBytes(GetFinalTimeISO())));
args.Add(new MultipartFormDataSection("level", Encoding.UTF8.GetBytes(LevelName)));
var addres = url + "/api/postTime";
Debug.Log("Connecting…");
var response = UnityWebRequest.Post(addres, args);
response.SendWebRequest();
while (!response.isDone)
{
var response = client.Execute<PostResponse>(request);
if (response.StatusCode == HttpStatusCode.OK && response.Data.success) {
return response.Data.place;

}
if (response.isNetworkError || response.isHttpError) {
Debug.LogWarning("http or network error");
return -1;
}
else {
try
{
var val = response.downloadHandler.text;
return int.Parse(val);
}
else {
catch (System.Exception e)
{
Debug.Log("Failed decoding response");
Debug.Log(e);
return -1;
}
}
catch (System.Exception)
{
throw;
}
}

// class for RestRequest returns
public class PostResponse {
public bool success { get; set; }
public int place { get; set; }
}
}
26 changes: 19 additions & 7 deletions Leaderboard/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ def GenerateMapList(data):
maps.append(x['level'])
return maps

debug = True
def timeFromIso(string):
try:
return datetime.time.fromisoformat(string)
except AttributeError:
h, m, s = [int(x) for x in str(string).split(':')]
return datetime.time(h, m, s)


debug = False
if debug:
database = TinyDB('test.json')
else:
print('Running in production')
database = TinyDB('leaderBoard.json')

maps = GenerateMapList(database)
Expand All @@ -24,11 +33,15 @@ def index():
mapsDict = list([{'url': '/board/' + x, 'name': x} for x in maps])
return render_template('index.html', maps = mapsDict)

@app.route('/Downloads')
def Downloads():
return render_template('downloads.html')

@app.route('/board/<level>')
def board(level):
entry = Query()
entries = database.search(entry.level == level)
entries.sort(key = lambda item: datetime.time.fromisoformat(item['time']))
entries.sort(key = lambda item: timeFromIso(item['time']))
return render_template('board.html', players = entries, level = level)

@app.route('/api/postTime', methods=['POST'])
Expand All @@ -37,11 +50,10 @@ def addTime():
try:
nick = request.form['nick']
level = str(request.form['level'])
iso_time = datetime.time.fromisoformat(request.form['time'])
iso_time = timeFromIso(request.form['time'])
time = request.form['time']
if len(nick) > 64:
nick = nick[:63]
print(nick, level, time)
if level not in maps:
maps.append(level)
except KeyError:
Expand All @@ -50,10 +62,10 @@ def addTime():
return {'success': False, 'reason': 'TimeNotISO'}
# get Place in leaderboard
entry = Query()
is_greater = lambda item: datetime.time.fromisoformat(item) < iso_time
is_greater = lambda item: timeFromIso(item) < iso_time
res = database.count((entry.time.test(is_greater)) & (entry.level == level))
database.insert({'name': nick, 'time': time, 'level': level})
return {'place': res + 1, 'success': True}
return str(res + 1)

@app.route('/api/getTimes/<level>')
@app.route('/api/getTimes/<level>/<name>')
Expand All @@ -63,7 +75,7 @@ def getTimes(level, name = None):
times = database.search(entry.level == level)
else:
times = database.search((entry.name == name) & (entry.level == level))
times.sort(key = lambda item: datetime.time.fromisoformat(item['time']))
times.sort(key = lambda item: timeFromIso(item['time']))
if times == None:
return {'success': True, 'times': list()}
if len(times) == 0:
Expand Down
14 changes: 14 additions & 0 deletions Leaderboard/app/static/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<footer>
<hr>
<table>
<tr>
<td class="footer">Source code available on <a href="https://github.com/Eroar/Ludum-Dare-46">Github.</a></td>
<td class="footer">LudumDare link <a href="https://ldjam.com/events/ludum-dare/46/baby-dont-hurt-me">TODO.</a></td>
</tr>
<tr>
<td class="footer">Site made by <em>Grzegorz Koperwas</em></td>
<td class="footer" style="max-width: 30ex;">We were too poor for cookies, so there are no cookies here<span class="smoll">Pls hire me!</span></td>
</tr>
</table>
<p></p>
</footer>
Binary file added Leaderboard/app/static/itWorks.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 68 additions & 4 deletions Leaderboard/app/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ body {
h1, h2, h3, h4 {
margin: 5px;
}

h1 {
margin-top: 1em;
margin-bottom: 1em;
}

div.interface {
max-width: 15cm;
Expand All @@ -36,9 +41,7 @@ body {
visibility: hidden;
}
table.calendar {
margin-left: 1cm;
min-width: 60%;
margin: 20px;
margin-top: 2em;
}
th.day-header {
background-color: #848484;
Expand Down Expand Up @@ -98,14 +101,75 @@ body {
bottom: 0;
left: 0;
width: 100%;
text-align: center;
background-color: #848484;
}

td.footer {
padding-left: 2ex;
padding-right: 2ex;
}
footer > table {
margin-left: auto;
margin-right: auto;
}

.listContainer {
max-width: 18cm;
margin-top: 1cm;
margin-bottom: 2cm;
margin-left: auto;
margin-right: auto;
}
.smoll {
font-size: 5pt;
}
img {
max-width: 14cm;
max-width: 100%;
margin-left: auto;
margin-right: auto;
}
.forceDown {
position: fixed;
bottom: 0;
}
body {
max-width: 100%;
}
.downloadButton {
margin-right: auto;
margin-left: auto;
border: seagreen;
border-style: solid;
text-align: center;
padding: 1em 1em 1em 1em;
text-shadow: seagreen;
background-color: chartreuse;
max-width: 40%;
}
.normalButton, .downloadButton > span {
color: black;
text-decoration: none;
}

.normalButton {
margin-right: auto;
margin-left: auto;
border: black;
border-style: solid;
text-align: center;
padding: 1em 1em 1em 1em;
background-color: gray;
max-width: 40%;
}

table {
margin-top: 0;
margin-left: auto;
margin-right: auto;
}
li {
margin-top: 1em;
margin-bottom: 1em;
}

Binary file added Leaderboard/app/static/mergeMeme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 56 additions & 21 deletions Leaderboard/app/templates/board.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,61 @@
<head>
<title>LD46 Leaderboard</title>
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<h1>{{ level }} Leaderboard:</h1>
<p><a href="/index">Go back</a></p>
<div class="listContainer">
<table class="calendar">
<tr>
<th class="day-header">Player</th>
<th class="day-header">Time</th>
</tr>
{% for player in players %}
<tr>
<td class="event-time">{{player.name}}</td>
<td class="calendar">{{player.time}}</td>
</tr>
{% endfor %}
</table>
</div>
<footer>
<hr>
<p>Source code available on <a href="https://github.com/Eroar/Ludum-Dare-46">Github.</a><br>
LudumDare link <a href="xd.com">TODO.</a></p>
</footer>
<body>
<script>
function includeHTML() {
var z, i, elmnt, file, xhttp;
/* Loop through a collection of all HTML elements: */
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/* Make an HTTP request using the attribute value as the file name: */
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/* Remove the attribute, and call this function once more: */
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/* Exit the function: */
return;
}
}
}
</script>
<div class="listContainer">
<h1>{{ level }} Leaderboard:</h1>
<a href="/index">
<div class="normalButton">
<span>Go back</span>
</div>
</a>
<table class="calendar">
<tr>
<th class="day-header">Player</th>
<th class="day-header">Time</th>
</tr>
{% for player in players %}
<tr>
<td class="event-time">{{player.name}}</td>
<td class="calendar">{{player.time}}</td>
</tr>
{% endfor %}
</table>
</div>
<div w3-include-html="{{ url_for('static', filename='footer.html') }}"></div>
<script>
includeHTML();
</script>
</body>
</html>
Loading

0 comments on commit 3c4e1eb

Please sign in to comment.