-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS2DatabaseCleanup.cs
More file actions
160 lines (136 loc) · 4.71 KB
/
CS2DatabaseCleanup.cs
File metadata and controls
160 lines (136 loc) · 4.71 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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Events;
using Newtonsoft.Json;
namespace CS2DatabaseCleanup;
public class CS2DatabaseCleanup : BasePlugin
{
public override string ModuleName => "CS2 Database Cleanup";
public override string ModuleVersion => "1.0.0";
private DatabaseService? _databaseService;
private PluginConfig? _config;
private readonly string _configPath;
public CS2DatabaseCleanup()
{
_configPath = Path.Combine(ModuleDirectory, "config.json");
}
public override void Load(bool hotReload)
{
LoadConfig();
if (_config?.Database != null)
{
_databaseService = new DatabaseService(_config.Database, _config.Rules);
Task.Run(async () => await _databaseService.InitializeDatabaseAsync());
}
else
{
Server.PrintToConsole("[CS2DatabaseCleanup] Failed to load configuration");
}
RegisterEventHandler<EventPlayerConnect>(OnPlayerConnect);
RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);
RegisterEventHandler<EventRoundStart>(OnRoundStart);
Server.PrintToConsole("[CS2DatabaseCleanup] Plugin loaded successfully");
}
private void LoadConfig()
{
try
{
if (!File.Exists(_configPath))
{
CreateDefaultConfig();
}
var configJson = File.ReadAllText(_configPath);
_config = JsonConvert.DeserializeObject<PluginConfig>(configJson);
}
catch (Exception ex)
{
Server.PrintToConsole($"[CS2DatabaseCleanup] Failed to load config: {ex.Message}");
}
}
private void CreateDefaultConfig()
{
var defaultConfig = new PluginConfig
{
Database = new DatabaseConfig
{
Host = "your_database_host",
Port = 3306,
Username = "your_username",
Password = "your_password",
DatabaseName = "your_database_name"
},
Rules = new List<CleanupRule>
{
new()
{
Rule = "@lastLogin>30d",
Queries = new List<string>
{
"DELETE FROM wp_player_skins WHERE steamid = @steamid;",
"DELETE FROM wp_player_stats WHERE steamid = @steamid;"
}
},
new()
{
Rule = "@firstLogin>60d",
Queries = new List<string>
{
"DELETE FROM players WHERE steamid = @steamid;"
}
}
}
};
var configJson = JsonConvert.SerializeObject(defaultConfig, Formatting.Indented);
File.WriteAllText(_configPath, configJson);
Server.PrintToConsole($"[CS2DatabaseCleanup] Created default config at: {_configPath}");
}
[GameEventHandler]
public HookResult OnPlayerConnect(EventPlayerConnect @event, GameEventInfo info)
{
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
var steamId = player.SteamID;
var playerName = player.PlayerName;
Task.Run(async () =>
{
if (_databaseService != null)
{
await _databaseService.UpdatePlayerConnectAsync(steamId, playerName);
}
});
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot) return HookResult.Continue;
var steamId = player.SteamID;
Task.Run(async () =>
{
if (_databaseService != null)
{
await _databaseService.UpdatePlayerDisconnectAsync(steamId);
}
});
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
// Execute cleanup rules on round start (you can change this to map change if preferred)
Task.Run(async () =>
{
if (_databaseService != null)
{
await _databaseService.ExecuteCleanupRulesAsync();
}
});
return HookResult.Continue;
}
public override void Unload(bool hotReload)
{
Server.PrintToConsole("[CS2DatabaseCleanup] Plugin unloaded");
}
}