Skip to content

Commit

Permalink
fuck restSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
HakierGrzonzo committed Apr 21, 2020
1 parent 2fc0c2d commit 286aa9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
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; }
}
}
2 changes: 1 addition & 1 deletion Leaderboard/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def addTime():
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 Down

0 comments on commit 286aa9b

Please sign in to comment.