-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLManager.cs
More file actions
168 lines (129 loc) · 4.42 KB
/
SQLManager.cs
File metadata and controls
168 lines (129 loc) · 4.42 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
public static class SQLManager
{
private const string SERVER_URL = "localhost:80/new_sql";
private const int REQUEST_TIMEOUT = 100;
// Store the current username
private static string _currentUsername = "";
// to access the current user
public static string CurrentUsername
{
get { return _currentUsername; }
private set { _currentUsername = value; }
}
public static bool IsLoggedIn
{
get { return !string.IsNullOrEmpty(_currentUsername); }
}
public static async Task<(bool success, int errorCode, string message)> RegisterUser(string username, string password)
{
string REGISTER_USER_URL = $"{SERVER_URL}/RegisterUser.php";
// Create JSON data
var userData = new UserData
{
username = username,
password = password
};
var result = await SendJsonRequest(REGISTER_USER_URL, userData);
return (result.success, result.errorCode, result.returnMessage);
}
public static async Task<(bool success, int errorCode, string username)> LoginUser(string username, string password)
{
string LOGIN_USER_URL = $"{SERVER_URL}/login.php";
// Create JSON data
var userData = new UserData
{
username = username,
password = password
};
var result = await SendJsonRequest(LOGIN_USER_URL, userData);
// Store the username if login was successful
if (result.success)
{
CurrentUsername = username;
// Save username to PlayerPrefs for persistence across app restarts
PlayerPrefs.SetString("Username", username);
PlayerPrefs.Save();
}
return (result.success, result.errorCode, result.returnMessage);
}
public static void LoadSavedSession()
{
// Try to load a saved username
if (PlayerPrefs.HasKey("Username"))
{
CurrentUsername = PlayerPrefs.GetString("Username");
}
}
public static void Logout()
{
// Clear the session
CurrentUsername = "";
// Remove saved username
PlayerPrefs.DeleteKey("Username");
PlayerPrefs.Save();
}
private static async Task<(bool success,int errorCode, string returnMessage)> 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");
// Send the request
req.SendWebRequest();
// Wait for the request
while (!req.isDone) await Task.Delay(REQUEST_TIMEOUT);
if (req.result != UnityWebRequest.Result.Success)
{
return (false, -1, req.error);
}
// Parse JSON response
try
{
ServerResponse response = JsonUtility.FromJson<ServerResponse>(req.downloadHandler.text);
if (response != null)
{
if (response.success)
{
return (true, 0, response.message ?? "Success");
}
else
{
return (false, response.error, response.message ?? $"Error code: {response.error}");
}
}
else
{
return (false, -1, "Invalid server response format");
}
}
catch (System.Exception e)
{
return (false, -1, $"Error parsing response: {req.downloadHandler.text}");
}
}
}
}
// Need to be serializable for JsonUtility
[System.Serializable]
public class UserData
{
public string username;
public string password;
}
[System.Serializable]
public class ServerResponse
{
public bool success;
public int error;
public string message;
public string username;
}