-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoreManager.cs
More file actions
122 lines (105 loc) · 3.4 KB
/
scoreManager.cs
File metadata and controls
122 lines (105 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
public static class ScoreManager
{
private const string SERVER_URL = "localhost:80/new_sql";
private const string SAVE_SCORE_URL = "update_score.php";
private const int REQUEST_TIMEOUT = 100;
public static async Task<(bool success, string message, ScoreResultData data)> SaveScore(float completionTime, string difficulty)
{
// Check if user is logged in
if (!SQLManager.IsLoggedIn)
{
return (false, "User not logged in", null);
}
// Create the data object to send
var scoreData = new ScoreData
{
username = SQLManager.CurrentUsername,
completionTime = completionTime,
difficulty = difficulty
};
try
{
var result = await SendJsonRequest($"{SERVER_URL}/{SAVE_SCORE_URL}", scoreData);
return result;
}
catch (Exception ex)
{
return (false, $"Error: {ex.Message}", null);
}
}
private static async Task<(bool success, string message, ScoreResultData data)> SendJsonRequest(string url, object data)
{
// Serialize the data object to JSON
string jsonData = JsonUtility.ToJson(data);
using (UnityWebRequest req = new UnityWebRequest(url, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
req.uploadHandler = new UploadHandlerRaw(bodyRaw);
req.downloadHandler = new DownloadHandlerBuffer();
// Set content type header for JSON
req.SetRequestHeader("Content-Type", "application/json");
req.SendWebRequest();
while (!req.isDone) await Task.Delay(REQUEST_TIMEOUT);
if (req.result != UnityWebRequest.Result.Success)
{
return (false, req.error, null);
}
// Parse JSON response
try
{
ScoreResponse response = JsonUtility.FromJson<ScoreResponse>(req.downloadHandler.text);
if (response != null)
{
if (response.success)
{
return (true, response.message ?? "Score processed successfully", response.data);
}
else
{
return (false, response.message ?? $"Error code: {response.error}", null);
}
}
else
{
return (false, "Invalid server response format", null);
}
}
catch
{
return (false, $"Error parsing response: {req.downloadHandler.text}", null);
}
}
}
}
// Data structures for score submission
[System.Serializable]
public class ScoreData
{
public string username;
public float completionTime;
public string difficulty;
}
[System.Serializable]
public class ScoreResponse
{
public bool success;
public int error;
public string message;
public ScoreResultData data;
}
[System.Serializable]
public class ScoreResultData
{
public string username;
public float completionTime;
public float previousBest;
public string difficulty;
public bool improved;
public float improvement;
public bool firstTime;
}