-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseConn.cs
266 lines (239 loc) · 10.2 KB
/
DatabaseConn.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DiceGame {
public class DatabaseConn {
static string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
string uname, upass;
public DatabaseConn(string _uname, string _upass) {
uname = _uname;
upass = SHA256Hash(_upass);
if (_upass != "") {
setLoginString(upass);
}
}
public static void setUsername(string _username) {
Properties.Settings.Default.Username = _username;
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"UPDATE player_data SET username = '" + _username + "' WHERE Id = 0";
// using (var cmd = new SqlCommand(sql, con)) {
// cmd.Prepare();
// cmd.ExecuteNonQuery();
// }
//}
Properties.Settings.Default.Save();
}
public static string getUsername() {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"SELECT * FROM player_data";
// using (var cmd = new SqlCommand(sql, con)) {
// using (var rdr = cmd.ExecuteReader()) {
// while (rdr.Read()) {
// try {
// return Convert.ToString(rdr["username"]);
// } catch (Exception x) { }
// }
// return null;
// }
// }
//}
return Properties.Settings.Default.Username;
}
public static void setMoney(int _money) {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"UPDATE player_data SET money = " + _money + " WHERE Id = 0";
// using (var cmd = new SqlCommand(sql, con)) {
// cmd.Prepare();
// cmd.ExecuteNonQuery();
// }
//}
Properties.Settings.Default.Money = _money;
Properties.Settings.Default.Save();
}
public static int getMoney() {
return Properties.Settings.Default.Money;
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"SELECT * FROM player_data";
// using (var cmd = new SqlCommand(sql, con)) {
// using (var rdr = cmd.ExecuteReader()) {
// while (rdr.Read()) {
// try {
// return Convert.ToInt32(rdr["money"]);
// } catch (Exception x) { }
// }
// return 0;
// }
// }
//}
}
public static void setImage(string _image) {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"UPDATE player_data SET image = '" + _image + "' WHERE Id = 0";
// using (var cmd = new SqlCommand(sql, con)) {
// cmd.Prepare();
// cmd.ExecuteNonQuery();
// }
//}
Properties.Settings.Default.Pfp = _image;
Properties.Settings.Default.Save();
}
public static string getImage() {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"SELECT * FROM player_data";
// using (var cmd = new SqlCommand(sql, con)) {
// using (var rdr = cmd.ExecuteReader()) {
// while (rdr.Read()) {
// try {
// var x = Convert.ToString(rdr["image"]);
// if (x == "null") {
// x = null;
// }
// return x;
// } catch (Exception x) { }
// }
// return null;
// }
// }
//}
return Properties.Settings.Default.Pfp;
}
public async void DownloadAndSet() {
LoginData data = new LoginData();
uname = getUsername();
data.username = uname;
var getData = await Data("download", getLoginString());
data.money = getData.money;
getData = await Data("getImage", getLoginString());
data.image = getData.image;
setMoney(data.money);
setImage(data.image);
}
public async Task<LoginData> Download() {
LoginData data = new LoginData();
data.username = uname;
//Console.WriteLine("before old get data");
var getData = await Data("download", upass);
//Console.WriteLine("after old get data");
data.money = getData.money;
//Console.WriteLine("before new get data");
getData = await Data("getImage", upass);
//Console.WriteLine("after new get data");
data.image = getData.image;
Console.Write(data.image);
return data;
}
public async void Upload() {
try {
uname = getUsername();
var d = new data();
d.money = getMoney();
d.image = "";
await Data("upload", getLoginString(), JsonConvert.SerializeObject(d));
} catch (Exception) { }
}
private string getLoginString() {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"SELECT * FROM player_data";
// using (var cmd = new SqlCommand(sql, con)) {
// using (var rdr = cmd.ExecuteReader()) {
// while (rdr.Read()) {
// try {
// var x = Convert.ToString(rdr["login_string"]);
// return x;
// } catch (Exception x) { }
// }
// return null;
// }
// }
//}
return Properties.Settings.Default.login_string;
}
private void setLoginString(string _login_string) {
//using (SqlConnection con = new SqlConnection(connectionString)) {
// con.Open();
// var sql = @"UPDATE player_data SET login_string = '" + _login_string + "' WHERE Id = 0";
// using (var cmd = new SqlCommand(sql, con)) {
// cmd.Prepare();
// cmd.ExecuteNonQuery();
// }
//}
//Console.WriteLine("Login string: " + getLoginString());
Properties.Settings.Default.login_string = _login_string;
Properties.Settings.Default.Save();
}
//public void Upload(int _money) {
// data myData = new data();
// myData.money = _money;
// Data("upload", upass, JsonConvert.SerializeObject(myData));
//}
public async Task<data> Data(string operation, string login_string, string data = "null", string server = "https://concretegames.net", string link = "/games/highrollers.php?", string return_value = "highrollers") {
var req = server + link + "username=" + uname + "&login_string=" + login_string + "&operation=" + operation + "&data=" + data + "&return=" + return_value;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string output = new StreamReader(response.GetResponseStream()).ReadToEnd();
string message = "";
if (output.Contains("error")) {
if (output.Contains("message")) {
message = output.Split('m')[1].Split(':')[1].Replace("'", "").Replace("}", "");
Console.WriteLine(message);
} else {
message = output.Split('u')[1].Split(':')[1].Replace("'", "").Replace("}", "");
Console.WriteLine(message);
}
return new data();
} else if (output == "null" || output == "" || output == null) {
return new data();
} else if (output[0] == 'h') {
var d = new data();
d.image = output;
return d;
} else {
//Console.WriteLine(output);
return JsonConvert.DeserializeObject<data>(output);
//return null;
}
}
public static string SHA256Hash(string text) {
SHA256 sha256 = new SHA256CryptoServiceProvider();
sha256.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
byte[] result = sha256.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++) {
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
[Serializable]
public class data {
public int money = 10000;
public string image = "https://concretegames.net/uploads/DefaultUser.png";
}
[Serializable]
public class Message {
public string isError;
public string message;
public string error;
//public string error;
}
[Serializable]
public class Error {
public string isError;
public string error;
//public string error;
}
}
}