-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpyMapListPlugin.cs
More file actions
345 lines (291 loc) · 15.1 KB
/
SharpyMapListPlugin.cs
File metadata and controls
345 lines (291 loc) · 15.1 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
namespace SharpyMapList;
public class SharpyMapListPlugin : BasePlugin, IPluginConfig<PluginConfig>
{
public override string ModuleName => "Sharpy-MapList";
public override string ModuleAuthor => "Sharpyku";
public override string ModuleVersion => "1.1.0";
public override string ModuleDescription => "Maps Done / Maps Left / Map Search / Profile commands for SharpTimer";
public PluginConfig Config { get; set; } = new();
private Database? _db;
// Style names matching SharpTimer
private static readonly Dictionary<int, string> StyleNames = new()
{
{ 0, "Normal" }, { 1, "Sideways" }, { 2, "Half-Sideways" },
{ 3, "Backwards" }, { 4, "Low Gravity" }, { 5, "Slow Motion" },
{ 6, "Fast Forward" }, { 7, "No Strafe Limit" }, { 8, "Turbo" },
{ 10, "Auto-Strafe" }, { 11, "250 Vel Max" }, { 12, "400 Vel Max" }
};
public void OnConfigParsed(PluginConfig config)
{
Config = config;
}
public override void Load(bool hotReload)
{
_db = new Database(Config);
_ = Task.Run(async () =>
{
bool ok = await _db.TestConnectionAsync();
Server.NextFrame(() =>
{
if (ok)
Logger.LogInformation("[Sharpy-MapList] Database connected successfully.");
else
Logger.LogError("[Sharpy-MapList] Database connection FAILED! Check config.");
});
});
}
private string GetPrefix()
{
return Config.ChatPrefix
.Replace("{GREEN}", $"{ChatColors.Green}")
.Replace("{DEFAULT}", $"{ChatColors.Default}")
.Replace("{GOLD}", $"{ChatColors.Gold}")
.Replace("{LIME}", $"{ChatColors.Lime}")
.Replace("{RED}", $"{ChatColors.Red}")
.Replace("{GREY}", $"{ChatColors.Grey}")
.Replace("{YELLOW}", $"{ChatColors.Yellow}")
.Replace("{WHITE}", $"{ChatColors.White}")
.Replace("{ORANGE}", $"{ChatColors.Orange}")
.Replace("{BLUE}", $"{ChatColors.Blue}")
.Replace("{PURPLE}", $"{ChatColors.Purple}")
.Replace("{LIGHTRED}", $"{ChatColors.LightRed}")
.Replace("{LIGHTBLUE}", $"{ChatColors.LightBlue}");
}
private static string GetStyleName(int style) =>
StyleNames.TryGetValue(style, out var name) ? name : $"Style {style}";
private static string FormatTime(int ticks)
{
double totalSeconds = ticks / 64.0;
int minutes = (int)(totalSeconds / 60);
double seconds = totalSeconds - minutes * 60;
return minutes > 0
? $"{minutes}:{seconds:00.000}"
: $"{seconds:0.000}";
}
private int GetPlayerStyle(CCSPlayerController player)
{
// Try to read SharpTimer's style from the player's ConVar
// Fallback: 0 (Normal)
// SharpTimer stores style in playerTimers dict, we can't access that
// So we accept an optional style argument on commands: !md 1 2 = page 1, style 2
return 0;
}
// ── !mapsdone / !md ──────────────────────────────────────────────
[ConsoleCommand("css_mapsdone", "Shows maps you have completed")]
[ConsoleCommand("css_md", "alias for !mapsdone")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void MapsDoneCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || player.IsBot) return;
if (_db == null) { player.PrintToChat($"{GetPrefix()} Database not configured."); return; }
var steamId = player.SteamID.ToString();
int page = 0;
int style = 0;
// Parse args: !md [page] [style]
if (command.ArgCount > 1 && int.TryParse(command.ArgByIndex(1), out int p)) page = Math.Max(0, p - 1);
if (command.ArgCount > 2 && int.TryParse(command.ArgByIndex(2), out int s)) style = s;
var db = _db;
var prefix = GetPrefix();
int perPage = Config.ItemsPerPage;
_ = Task.Run(async () =>
{
try
{
var mapsDone = await db.GetPlayerMapsDoneAsync(steamId, style);
int totalMaps = await db.GetTotalValidMapsAsync(style);
if (mapsDone.Count == 0)
{
Server.NextFrame(() => player?.PrintToChat($"{prefix} {ChatColors.Grey}You haven't completed any maps yet!"));
return;
}
int totalPages = (int)Math.Ceiling(mapsDone.Count / (double)perPage);
page = Math.Min(page, totalPages - 1);
var pageItems = mapsDone.Skip(page * perPage).Take(perPage).ToList();
string styleName = GetStyleName(style);
string pct = totalMaps > 0 ? (mapsDone.Count * 100.0 / totalMaps).ToString("F1") : "0";
var lines = new List<string>
{
$"{prefix} {ChatColors.Green}Maps Done {ChatColors.Grey}({styleName}){ChatColors.Default}: {ChatColors.Lime}{mapsDone.Count}{ChatColors.Default}/{ChatColors.Lime}{totalMaps} {ChatColors.Grey}({pct}%) {ChatColors.Default}[Page {page + 1}/{totalPages}]"
};
foreach (var (mapName, ticks) in pageItems)
lines.Add($" {ChatColors.Grey}• {ChatColors.Lime}{mapName} {ChatColors.Default}- {ChatColors.Green}{FormatTime(ticks)}");
if (totalPages > 1)
lines.Add($" {ChatColors.Grey}Use {ChatColors.Lime}!mapsdone {page + 2} {ChatColors.Grey}for next page");
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
foreach (var line in lines) player.PrintToChat(line);
});
}
catch (Exception ex)
{
Logger.LogError($"[Sharpy-MapList] MapsDone error: {ex.Message}");
}
});
}
// ── !mds (map done search) ─────────────────────────────────────
[ConsoleCommand("css_mds", "Search your time on a specific map")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void MapDoneSearchCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || player.IsBot) return;
if (_db == null) { player.PrintToChat($"{GetPrefix()} Database not configured."); return; }
if (command.ArgCount < 2 || string.IsNullOrWhiteSpace(command.ArgByIndex(1)))
{
player.PrintToChat($"{GetPrefix()} {ChatColors.Grey}Usage: {ChatColors.Lime}!mds <mapname> {ChatColors.Grey}(e.g. !mds bhop_alley)");
return;
}
var steamId = player.SteamID.ToString();
int style = 0;
string search = command.ArgByIndex(1).Trim().ToLower();
// Optional style arg: !mds bhop_alley 6
if (command.ArgCount > 2 && int.TryParse(command.ArgByIndex(2), out int s)) style = s;
var db = _db;
var prefix = GetPrefix();
_ = Task.Run(async () =>
{
try
{
var mapsDone = await db.GetPlayerMapsDoneAsync(steamId, style);
string styleName = GetStyleName(style);
var exact = mapsDone.Where(m => m.MapName.ToLower() == search).ToList();
var matches = exact.Count > 0 ? exact : mapsDone.Where(m => m.MapName.ToLower().Contains(search)).ToList();
if (matches.Count == 0)
{
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
player.PrintToChat($"{prefix} {ChatColors.Grey}No completed map matching {ChatColors.Lime}\"{search}\" {ChatColors.Grey}({styleName})");
});
return;
}
var lines = new List<string>();
if (matches.Count == 1)
{
var (mapName, ticks) = matches[0];
lines.Add($"{prefix} {ChatColors.Lime}{mapName} {ChatColors.Grey}({styleName}){ChatColors.Default}: {ChatColors.Green}{FormatTime(ticks)}");
}
else
{
lines.Add($"{prefix} {ChatColors.Green}Found {matches.Count} maps {ChatColors.Grey}matching \"{search}\" ({styleName}):");
foreach (var (mapName, ticks) in matches.Take(10))
lines.Add($" {ChatColors.Grey}• {ChatColors.Lime}{mapName} {ChatColors.Default}- {ChatColors.Green}{FormatTime(ticks)}");
if (matches.Count > 10)
lines.Add($" {ChatColors.Grey}...and {matches.Count - 10} more. Be more specific.");
}
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
foreach (var line in lines) player.PrintToChat(line);
});
}
catch (Exception ex)
{
Logger.LogError($"[Sharpy-MapList] MapDoneSearch error: {ex.Message}");
}
});
}
// ── !mapsleft / !ml ──────────────────────────────────────────────
[ConsoleCommand("css_mapsleft", "Shows maps you have not completed")]
[ConsoleCommand("css_ml", "alias for !mapsleft")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void MapsLeftCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || player.IsBot) return;
if (_db == null) { player.PrintToChat($"{GetPrefix()} Database not configured."); return; }
var steamId = player.SteamID.ToString();
int page = 0;
int style = 0;
if (command.ArgCount > 1 && int.TryParse(command.ArgByIndex(1), out int p)) page = Math.Max(0, p - 1);
if (command.ArgCount > 2 && int.TryParse(command.ArgByIndex(2), out int s)) style = s;
var db = _db;
var prefix = GetPrefix();
int perPage = Config.ItemsPerPage;
_ = Task.Run(async () =>
{
try
{
var mapsLeft = await db.GetPlayerMapsLeftAsync(steamId, style);
int totalMaps = await db.GetTotalValidMapsAsync(style);
if (mapsLeft.Count == 0)
{
Server.NextFrame(() => player?.PrintToChat($"{prefix} {ChatColors.Green}You've completed ALL maps! GG!"));
return;
}
int totalPages = (int)Math.Ceiling(mapsLeft.Count / (double)perPage);
page = Math.Min(page, totalPages - 1);
var pageItems = mapsLeft.Skip(page * perPage).Take(perPage).ToList();
string styleName = GetStyleName(style);
string pct = totalMaps > 0 ? (mapsLeft.Count * 100.0 / totalMaps).ToString("F1") : "0";
var lines = new List<string>
{
$"{prefix} {ChatColors.LightRed}Maps Left {ChatColors.Grey}({styleName}){ChatColors.Default}: {ChatColors.Lime}{mapsLeft.Count}{ChatColors.Default}/{ChatColors.Lime}{totalMaps} {ChatColors.Grey}({pct}% remaining) {ChatColors.Default}[Page {page + 1}/{totalPages}]"
};
foreach (var mapName in pageItems)
lines.Add($" {ChatColors.Grey}• {ChatColors.LightRed}{mapName}");
if (totalPages > 1)
lines.Add($" {ChatColors.Grey}Use {ChatColors.Lime}!mapsleft {page + 2} {ChatColors.Grey}for next page");
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
foreach (var line in lines) player.PrintToChat(line);
});
}
catch (Exception ex)
{
Logger.LogError($"[Sharpy-MapList] MapsLeft error: {ex.Message}");
}
});
}
// ── !profile / !stats / !p ───────────────────────────────────────
[ConsoleCommand("css_profile", "Shows your player profile")]
[ConsoleCommand("css_stats", "alias for !profile")]
[ConsoleCommand("css_p", "alias for !profile")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void ProfileCommand(CCSPlayerController? player, CommandInfo command)
{
if (player == null || !player.IsValid || player.IsBot) return;
if (_db == null) { player.PrintToChat($"{GetPrefix()} Database not configured."); return; }
var steamId = player.SteamID.ToString();
var playerName = player.PlayerName;
int style = 0;
if (command.ArgCount > 1 && int.TryParse(command.ArgByIndex(1), out int s)) style = s;
var db = _db;
var prefix = GetPrefix();
_ = Task.Run(async () =>
{
try
{
var (timesConnected, globalPoints, totalPlaytime, mapsDone, totalMaps, serverRank, totalRanked) =
await db.GetPlayerProfileAsync(steamId, style);
int hours = totalPlaytime / 3600;
int minutes = (totalPlaytime % 3600) / 60;
string completionPct = totalMaps > 0 ? (mapsDone * 100.0 / totalMaps).ToString("F1") : "0";
string styleName = GetStyleName(style);
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
player.PrintToChat($"{prefix} {ChatColors.Green}━━━ Player Profile ━━━");
player.PrintToChat($" {ChatColors.Grey}Player: {ChatColors.Lime}{playerName}");
player.PrintToChat($" {ChatColors.Grey}Style: {ChatColors.Lime}{styleName}");
player.PrintToChat($" {ChatColors.Grey}Maps Completed: {ChatColors.Green}{mapsDone}{ChatColors.Default}/{ChatColors.Lime}{totalMaps} {ChatColors.Grey}({completionPct}%)");
if (globalPoints > 0)
player.PrintToChat($" {ChatColors.Grey}Points: {ChatColors.Lime}{globalPoints} {ChatColors.Grey}| Rank: {ChatColors.Green}#{serverRank}{ChatColors.Default}/{ChatColors.Lime}{totalRanked}");
player.PrintToChat($" {ChatColors.Grey}Playtime: {ChatColors.Green}{hours}h {minutes}m");
player.PrintToChat($" {ChatColors.Grey}Connections: {ChatColors.Lime}{timesConnected}");
player.PrintToChat($"{prefix} {ChatColors.Green}━━━━━━━━━━━━━━━━━━━━━");
});
}
catch (Exception ex)
{
Logger.LogError($"[Sharpy-MapList] Profile error: {ex.Message}");
}
});
}
}