33using BHD_ServerManager . Classes . InstanceManagers ;
44using BHD_ServerManager . Classes . Instances ;
55using BHD_ServerManager . Classes . ObjectClasses ;
6+ using BHD_ServerManager . Classes . Services . NetLimiter ;
7+ using BHD_ServerManager . Classes . SupportClasses ;
68using System . Diagnostics ;
79using System . Net ;
810using System . Text ;
@@ -18,6 +20,7 @@ public partial class PlayerCard : UserControl
1820
1921 private static theInstance ThisInstance = CommonCore . theInstance ! ;
2022 private static chatInstance ChatInstance = CommonCore . instanceChat ! ;
23+ private static banInstance BanInstance = CommonCore . instanceBans ! ;
2124 private playerObject Player { get ; set ; } = new playerObject ( ) ;
2225 private int SlotNumber = 0 ;
2326 private ContextMenuStrip ContextMenu ;
@@ -162,25 +165,163 @@ private ToolStripMenuItem CreateBanMenuItem()
162165 command . DropDownItems . Add ( banByIP ) ;
163166 command . DropDownItems . Add ( banByNameAndIP ) ;
164167
165- banByName . Click += ( sender , e ) =>
168+ banByName . Click += async ( sender , e ) =>
166169 {
167- // banInstanceManager.AddBannedPlayer(Player.PlayerNameBase64!);
168- MessageBox . Show ( $ "Player { Player . PlayerName } has been banned by name.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
169- Debug . WriteLine ( $ "Ban by name command clicked for player { Player . PlayerName } .") ;
170+ try
171+ {
172+ // Create name ban record
173+ var nameRecord = new banInstancePlayerName
174+ {
175+ RecordID = 0 ,
176+ MatchID = 0 ,
177+ PlayerName = Player . PlayerNameBase64 ! ,
178+ Date = DateTime . Now ,
179+ ExpireDate = null ,
180+ AssociatedIP = 0 ,
181+ RecordType = banInstanceRecordType . Permanent ,
182+ RecordCategory = ( int ) RecordCategory . Ban ,
183+ Notes = $ "Banned from PlayerCard context menu"
184+ } ;
185+
186+ // Add to database
187+ int recordID = DatabaseManager . AddPlayerNameRecord ( nameRecord ) ;
188+ nameRecord . RecordID = recordID ;
189+
190+ // Add to in-memory list
191+ BanInstance . BannedPlayerNames . Add ( nameRecord ) ;
192+
193+ // Kick the player
194+ ServerMemory . WriteMemorySendConsoleCommand ( "punt " + Player . PlayerSlot ) ;
195+
196+ MessageBox . Show ( $ "Player { Player . PlayerName } has been banned by name and kicked from the server.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
197+ Debug . WriteLine ( $ "Ban by name command executed for player { Player . PlayerName } (RecordID: { recordID } )") ;
198+ }
199+ catch ( Exception ex )
200+ {
201+ MessageBox . Show ( $ "Error banning player by name: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
202+ AppDebug . Log ( "PlayerCard" , $ "Error banning player by name: { ex } ") ;
203+ }
170204 } ;
171- banByIP . Click += ( sender , e ) =>
205+
206+ banByIP . Click += async ( sender , e ) =>
172207 {
173- var ipAddress = IPAddress . Parse ( Player . PlayerIPAddress ! ) ;
174- // banInstanceManager.AddBannedPlayer(null!, ipAddress, 32);
175- MessageBox . Show ( $ "Player { Player . PlayerName } has been banned by IP.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
176- Debug . WriteLine ( $ "Ban by IP command clicked for player { Player . PlayerName } .") ;
208+ try
209+ {
210+ var ipAddress = IPAddress . Parse ( Player . PlayerIPAddress ! ) ;
211+
212+ // Create IP ban record
213+ var ipRecord = new banInstancePlayerIP
214+ {
215+ RecordID = 0 ,
216+ MatchID = 0 ,
217+ PlayerIP = ipAddress ,
218+ SubnetMask = 32 ,
219+ Date = DateTime . Now ,
220+ ExpireDate = null ,
221+ AssociatedName = 0 ,
222+ RecordType = banInstanceRecordType . Permanent ,
223+ RecordCategory = ( int ) RecordCategory . Ban ,
224+ Notes = $ "Banned from PlayerCard context menu"
225+ } ;
226+
227+ // Add to database
228+ int recordID = DatabaseManager . AddPlayerIPRecord ( ipRecord ) ;
229+ ipRecord . RecordID = recordID ;
230+
231+ // Add to in-memory list
232+ BanInstance . BannedPlayerIPs . Add ( ipRecord ) ;
233+
234+ // Add to NetLimiter filter if enabled
235+ if ( ThisInstance . netLimiterEnabled && ! string . IsNullOrEmpty ( ThisInstance . netLimiterFilterName ) )
236+ {
237+ await NetLimiterClient . AddIpToFilterAsync ( ThisInstance . netLimiterFilterName , ipAddress . ToString ( ) , 32 ) ;
238+ Debug . WriteLine ( $ "Added IP { ipAddress } to NetLimiter filter '{ ThisInstance . netLimiterFilterName } '") ;
239+ }
240+
241+ // Kick the player
242+ ServerMemory . WriteMemorySendConsoleCommand ( "punt " + Player . PlayerSlot ) ;
243+
244+ MessageBox . Show ( $ "Player { Player . PlayerName } has been banned by IP and kicked from the server.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
245+ Debug . WriteLine ( $ "Ban by IP command executed for player { Player . PlayerName } (RecordID: { recordID } )") ;
246+ }
247+ catch ( Exception ex )
248+ {
249+ MessageBox . Show ( $ "Error banning player by IP: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
250+ AppDebug . Log ( "PlayerCard" , $ "Error banning player by IP: { ex } ") ;
251+ }
177252 } ;
178- banByNameAndIP . Click += ( sender , e ) =>
253+
254+ banByNameAndIP . Click += async ( sender , e ) =>
179255 {
180- var playerAddress = IPAddress . Parse ( Player . PlayerIPAddress ! ) ;
181- // banInstanceManager.AddBannedPlayer(Player.PlayerNameBase64!, playerAddress, 32);
182- MessageBox . Show ( $ "Player { Player . PlayerName } has been banned.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
183- Debug . WriteLine ( $ "Ban command clicked for player { Player . PlayerName } .") ;
256+ try
257+ {
258+ var ipAddress = IPAddress . Parse ( Player . PlayerIPAddress ! ) ;
259+ int nameRecordID = 0 ;
260+ int ipRecordID = 0 ;
261+
262+ // Create name ban record
263+ var nameRecord = new banInstancePlayerName
264+ {
265+ RecordID = 0 ,
266+ MatchID = 0 ,
267+ PlayerName = Player . PlayerNameBase64 ! ,
268+ Date = DateTime . Now ,
269+ ExpireDate = null ,
270+ AssociatedIP = 0 ,
271+ RecordType = banInstanceRecordType . Permanent ,
272+ RecordCategory = ( int ) RecordCategory . Ban ,
273+ Notes = $ "Banned from PlayerCard context menu"
274+ } ;
275+
276+ // Add name to database
277+ nameRecordID = DatabaseManager . AddPlayerNameRecord ( nameRecord ) ;
278+ nameRecord . RecordID = nameRecordID ;
279+
280+ // Create IP ban record
281+ var ipRecord = new banInstancePlayerIP
282+ {
283+ RecordID = 0 ,
284+ MatchID = 0 ,
285+ PlayerIP = ipAddress ,
286+ SubnetMask = 32 ,
287+ Date = DateTime . Now ,
288+ ExpireDate = null ,
289+ AssociatedName = nameRecordID ,
290+ RecordType = banInstanceRecordType . Permanent ,
291+ RecordCategory = ( int ) RecordCategory . Ban ,
292+ Notes = $ "Banned from PlayerCard context menu"
293+ } ;
294+
295+ // Add IP to database
296+ ipRecordID = DatabaseManager . AddPlayerIPRecord ( ipRecord ) ;
297+ ipRecord . RecordID = ipRecordID ;
298+
299+ // Update name record with associated IP
300+ nameRecord . AssociatedIP = ipRecordID ;
301+ DatabaseManager . UpdatePlayerNameRecord ( nameRecord ) ;
302+
303+ // Add to in-memory lists
304+ BanInstance . BannedPlayerNames . Add ( nameRecord ) ;
305+ BanInstance . BannedPlayerIPs . Add ( ipRecord ) ;
306+
307+ // Add to NetLimiter filter if enabled
308+ if ( ThisInstance . netLimiterEnabled && ! string . IsNullOrEmpty ( ThisInstance . netLimiterFilterName ) )
309+ {
310+ await NetLimiterClient . AddIpToFilterAsync ( ThisInstance . netLimiterFilterName , ipAddress . ToString ( ) , 32 ) ;
311+ Debug . WriteLine ( $ "Added IP { ipAddress } to NetLimiter filter '{ ThisInstance . netLimiterFilterName } '") ;
312+ }
313+
314+ // Kick the player
315+ ServerMemory . WriteMemorySendConsoleCommand ( "punt " + Player . PlayerSlot ) ;
316+
317+ MessageBox . Show ( $ "Player { Player . PlayerName } has been banned by name and IP, then kicked from the server.", "Player Action" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
318+ Debug . WriteLine ( $ "Ban by name and IP command executed for player { Player . PlayerName } (Name RecordID: { nameRecordID } , IP RecordID: { ipRecordID } )") ;
319+ }
320+ catch ( Exception ex )
321+ {
322+ MessageBox . Show ( $ "Error banning player: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
323+ AppDebug . Log ( "PlayerCard" , $ "Error banning player by name and IP: { ex } ") ;
324+ }
184325 } ;
185326
186327 return command ;
0 commit comments