-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
215 lines (193 loc) · 7.61 KB
/
Copy pathDatabase.cs
File metadata and controls
215 lines (193 loc) · 7.61 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
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
using MySqlConnector;
namespace SharpyMapList;
public class Database
{
private readonly string _connectionString;
private readonly string _recordsTable;
private readonly string _statsTable;
public Database(PluginConfig config)
{
_recordsTable = config.PlayerRecordsTable;
_statsTable = config.PlayerStatsTable;
_connectionString = new MySqlConnectionStringBuilder
{
Server = config.DatabaseHost,
Port = (uint)config.DatabasePort,
Database = config.DatabaseName,
UserID = config.DatabaseUser,
Password = config.DatabasePassword,
SslMode = MySqlSslMode.Preferred,
ConnectionTimeout = 30
}.ConnectionString;
}
private async Task<MySqlConnection> OpenAsync()
{
var conn = new MySqlConnection(_connectionString);
await conn.OpenAsync();
return conn;
}
public async Task<bool> TestConnectionAsync()
{
try
{
using var conn = await OpenAsync();
return true;
}
catch { return false; }
}
public async Task<List<(string MapName, int TimerTicks)>> GetPlayerMapsDoneAsync(string steamId, int style)
{
using var conn = await OpenAsync();
using var cmd = new MySqlCommand($@"
SELECT `MapName`, MIN(`TimerTicks`) AS BestTime
FROM `{_recordsTable}`
WHERE `SteamID` = @SteamID AND `Style` = @Style
AND `MapName` NOT LIKE '%\_bonus%'
AND `MapName` REGEXP '^[a-zA-Z]'
GROUP BY `MapName`
ORDER BY `MapName` ASC;", conn);
cmd.Parameters.AddWithValue("@SteamID", steamId);
cmd.Parameters.AddWithValue("@Style", style);
var results = new List<(string, int)>();
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
results.Add((reader.GetString(0), reader.GetInt32(1)));
return results;
}
public async Task<List<string>> GetPlayerMapsLeftAsync(string steamId, int style)
{
using var conn = await OpenAsync();
// Try MapPool first
bool mapPoolExists = false;
try
{
using var check = new MySqlCommand("SELECT 1 FROM `MapPool` LIMIT 1;", conn);
await check.ExecuteScalarAsync();
mapPoolExists = true;
}
catch { }
MySqlCommand cmd;
if (mapPoolExists)
{
cmd = new MySqlCommand($@"
SELECT `MapName` FROM `MapPool`
WHERE `MapName` NOT IN (
SELECT DISTINCT `MapName` FROM `{_recordsTable}`
WHERE `SteamID` = @SteamID AND `Style` = @Style
AND `MapName` NOT LIKE '%\_bonus%'
)
ORDER BY `MapName` ASC;", conn);
}
else
{
cmd = new MySqlCommand($@"
SELECT DISTINCT `MapName`
FROM `{_recordsTable}`
WHERE `MapName` NOT LIKE '%\_bonus%'
AND `MapName` REGEXP '^[a-zA-Z]'
AND `Style` = @Style
AND `MapName` NOT IN (
SELECT DISTINCT `MapName` FROM `{_recordsTable}`
WHERE `SteamID` = @SteamID AND `Style` = @Style
)
ORDER BY `MapName` ASC;", conn);
}
cmd.Parameters.AddWithValue("@SteamID", steamId);
cmd.Parameters.AddWithValue("@Style", style);
var results = new List<string>();
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
results.Add(reader.GetString(0));
return results;
}
public async Task<int> GetTotalValidMapsAsync(int style)
{
using var conn = await OpenAsync();
bool mapPoolExists = false;
try
{
using var check = new MySqlCommand("SELECT 1 FROM `MapPool` LIMIT 1;", conn);
await check.ExecuteScalarAsync();
mapPoolExists = true;
}
catch { }
MySqlCommand cmd;
if (mapPoolExists)
{
cmd = new MySqlCommand("SELECT COUNT(*) FROM `MapPool`;", conn);
}
else
{
cmd = new MySqlCommand($@"
SELECT COUNT(DISTINCT `MapName`)
FROM `{_recordsTable}`
WHERE `MapName` NOT LIKE '%\_bonus%'
AND `MapName` REGEXP '^[a-zA-Z]'
AND `Style` = @Style;", conn);
}
cmd.Parameters.AddWithValue("@Style", style);
var result = await cmd.ExecuteScalarAsync();
return result != null && result != DBNull.Value ? Convert.ToInt32(result) : 0;
}
public async Task<(int TimesConnected, int GlobalPoints, int TotalPlaytime, int MapsDone, int TotalMaps, int ServerRank, int TotalRanked)> GetPlayerProfileAsync(string steamId, int style)
{
using var conn = await OpenAsync();
int timesConnected = 0, globalPoints = 0, totalPlaytime = 0;
using (var statsCmd = new MySqlCommand($@"
SELECT `TimesConnected`, COALESCE(`GlobalPoints`, 0), COALESCE(`TotalPlaytime`, 0)
FROM `{_statsTable}`
WHERE `SteamID` = @SteamID;", conn))
{
statsCmd.Parameters.AddWithValue("@SteamID", steamId);
using var reader = await statsCmd.ExecuteReaderAsync();
if (await reader.ReadAsync())
{
timesConnected = reader.GetInt32(0);
globalPoints = reader.GetInt32(1);
totalPlaytime = reader.GetInt32(2);
}
}
using (var doneCmd = new MySqlCommand($@"
SELECT COUNT(DISTINCT `MapName`)
FROM `{_recordsTable}`
WHERE `SteamID` = @SteamID AND `Style` = @Style
AND `MapName` NOT LIKE '%\_bonus%'
AND `MapName` REGEXP '^[a-zA-Z]';", conn))
{
doneCmd.Parameters.AddWithValue("@SteamID", steamId);
doneCmd.Parameters.AddWithValue("@Style", style);
var r = await doneCmd.ExecuteScalarAsync();
var mapsDone = r != null ? Convert.ToInt32(r) : 0;
int totalMaps;
using (var totalCmd = new MySqlCommand($@"
SELECT COUNT(DISTINCT `MapName`)
FROM `{_recordsTable}`
WHERE `MapName` NOT LIKE '%\_bonus%'
AND `MapName` REGEXP '^[a-zA-Z]'
AND `Style` = @Style;", conn))
{
totalCmd.Parameters.AddWithValue("@Style", style);
var t = await totalCmd.ExecuteScalarAsync();
totalMaps = t != null ? Convert.ToInt32(t) : 0;
}
int serverRank = 0, totalRanked = 0;
if (globalPoints > 0)
{
using (var rankCmd = new MySqlCommand($@"
SELECT COUNT(*) FROM `{_statsTable}`
WHERE `GlobalPoints` > @Points;", conn))
{
rankCmd.Parameters.AddWithValue("@Points", globalPoints);
serverRank = Convert.ToInt32(await rankCmd.ExecuteScalarAsync() ?? 0) + 1;
}
using (var totalRankedCmd = new MySqlCommand($@"
SELECT COUNT(*) FROM `{_statsTable}`
WHERE `GlobalPoints` > 0;", conn))
{
totalRanked = Convert.ToInt32(await totalRankedCmd.ExecuteScalarAsync() ?? 0);
}
}
return (timesConnected, globalPoints, totalPlaytime, mapsDone, totalMaps, serverRank, totalRanked);
}
}
}