-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservedSlotsSystem.cpp
279 lines (235 loc) · 8.71 KB
/
ReservedSlotsSystem.cpp
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
/* ======== Basic Admin tool ========
* Copyright (C) 2004-2007 Erling K. Sæterdal
* No warranties of any kind
*
* License: zlib/libpng
*
* Author(s): Erling K. Sæterdal ( EKS )
* Credits:
* Menu code based on code from CSDM ( http://www.tcwonline.org/~dvander/cssdm ) Created by BAILOPAN
* Helping on misc errors/functions: BAILOPAN,karma,LDuke,sslice,devicenull,PMOnoTo,cybermind ( most who idle in #sourcemod on GameSurge realy )
* ============================ */
#include "BATCore.h"
#include "ReservedSlotsSystem.h"
#include "const.h"
int g_ReservedSlotsUsers=0;
int g_PlayerCount=0;
int g_ReservedSlotsCount = 0;
int g_ReservedSlotsSystem = 0;
bool g_ReservedSlotsUseRedirect=false;
char g_ReservedSlotsIPAddress[128];
bool g_BlockIDCheck;
ConVar *g_pBotQuota;
ConVar *g_pSourceTVName;
#define DEBUG_RESEREVEDSLOTS 0
ReservedSlotsSystem::ReservedSlotsSystem()
{
g_VisibleMaxPlayers = g_BATCore.GetBATVar().GetCvarPointer("sv_visiblemaxplayers");
g_pSourceTVName = g_BATCore.GetBATVar().GetCvarPointer("tv_name");
}
bool ReservedSlotsSystem::HandleReservedSlotsRequest(int id)
{
if(g_ReservedSlotsSystem == 0 || g_ReservedSlotsSystem >= 4)
return true;
if(!AllowServerJoin(id))
{
KickOrRedirectPlayer(id);
return false;
}
#if DEBUG_RESEREVEDSLOTS == 1
else
g_BATCore.AddLogEntry("Reserved slots debug: %s was allowed to connect: Reserved slots access: %d",g_BATCore.GetPlayerName(id),g_BATCore.HasAccess(id,ADMIN_RESERVEDSLOTS));
#endif
if(g_ReservedSlotsSystem == 2)
UpdateVisibleSlots();
return true;
}
bool ReservedSlotsSystem::AllowServerJoin(int id)
{
int MaxPublicFreeSlots = GetFreeNoneAdminSlots();
if(MaxPublicFreeSlots == -1)
{
g_BATCore.AddLogEntry("Your reserved slots system has a incorrect cvar value");
return true;
}
bool HasResevedSlotsAccess = g_BATCore.HasAccess(id,ADMIN_RESERVEDSLOTS);
switch(g_ReservedSlotsSystem)
{
case 1:
if(HasResevedSlotsAccess)
return true;
if(MaxPublicFreeSlots <= 0)
return false;
return true;
case 2:
if(HasResevedSlotsAccess)
return true;
if(MaxPublicFreeSlots <= 0)
return false;
return true;
case 3:
if(!HasResevedSlotsAccess) // Its not a admin, we check if there are any free slots for him
{
if(MaxPublicFreeSlots <= 0)
return false;
return true;
}
// Its a admin, we now need to check if a none admin needs to be kicked.
if(MaxPublicFreeSlots >= 1)
{
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: admin named %s connected, no need to kick random none admin. %d",g_BATCore.GetPlayerName(id),MaxPublicFreeSlots);
#endif
return true;
}
int PlayerToKick = GetPlayerToKick();
if(PlayerToKick <= 0)
{
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Tried to find player to kick, but failed Players %d/%d",g_PlayerCount,MaxPublicFreeSlots);
#endif
return true;
}
// We now either kick or redirect the player that needs to leave.
KickOrRedirectPlayer(PlayerToKick);
return true;
}
return true;
}
void ReservedSlotsSystem::KickOrRedirectPlayer(int id)
{
g_BlockIDCheck = true;
if(g_UserInfo[id].IsBot && g_ModSettings.GameMod == MOD_CSTRIKE)
{
g_BATCore.ConsolePrint(0,"[BAT] A Bot was kicked due to slot reservation");
if(!g_pBotQuota) g_pBotQuota = g_BATCore.GetBATVar().GetCvarPointer("bot_quota");
int OldValue = g_pBotQuota->GetInt() - 1;
g_pBotQuota->SetValue(OldValue);
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Lowing bot_quota to %d",OldValue);
#endif
return;
}
if(g_ReservedSlotsUseRedirect)
{
g_MenuMngr.ShowClientReConnectDialog(id);
g_BATCore.GetTaskSystem()->CreateTask(ReservedSlotsSystem::cbTaskKickPlayer,MAX_REDIRECTTIME + 1,id);
g_BATCore.MessagePlayer(id,"[BAT] Due to slot reservation you will be kicked in %.0f seconds, or you can redirect to the other server (Default F3)",MAX_REDIRECTTIME);
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Offered %s to redirect will be kicked in %.0f seconds",g_BATCore.GetPlayerName(id),MAX_REDIRECTTIME);
#endif
}
else
{
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Kicked %s to make room for a admin",g_BATCore.GetPlayerName(id));
#endif
g_BATCore.ConsolePrint(0,"[BAT] %s was kicked due to slot reservation",g_BATCore.GetPlayerName(id));
g_BATCore.ServerCommand("kickid %d Kicked due to slot reservation",g_UserInfo[id].Userid);
}
}
int ReservedSlotsSystem::GetFreeNoneAdminSlots()
{
int FreeSlots = 0;
switch(g_ReservedSlotsSystem)
{
case 1:
FreeSlots = g_MaxClients - (g_ReservedSlotsCount + g_PlayerCount);
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: FreeSlots %d",FreeSlots);
#endif
return FreeSlots;
case 2:
FreeSlots = g_MaxClients - g_ReservedSlotsCount + g_ReservedSlotsUsers;
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: FreeSlots %d",FreeSlots);
#endif
return FreeSlots;
case 3:
FreeSlots = g_MaxClients - g_PlayerCount;
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: FreeSlots %d - %d/%d",FreeSlots,g_PlayerCount,g_MaxClients);
#endif
return FreeSlots;
}
return -100;
}
int ReservedSlotsSystem::GetPlayerToKick()
{
float ShortestConnectionTime = GetGlobals()->curtime + 5;
int ShortestConnectionInt=-1;
for(int i=1;i<g_MaxClients;i++)
{
if(g_IsConnected[i] && !g_BATCore.HasAccess(i,ADMIN_RESERVEDSLOTS) && g_UserInfo[i].ConnectTime < ShortestConnectionTime || g_IsConnected[i] && g_UserInfo[i].IsBot )
{
if(g_UserInfo[i].IsBot && strcmp(g_BATCore.GetPlayerName(i),g_pSourceTVName->GetString()) == 0)
continue;
ShortestConnectionTime = g_UserInfo[i].ConnectTime;
ShortestConnectionInt = i;
if(g_UserInfo[i].IsBot)
return i; // We found a BOT we kick that instead of any player
}
}
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Planning to kick %s (IsBot %d) connection time %.0f",g_BATCore.GetPlayerName(ShortestConnectionInt),g_UserInfo[ShortestConnectionInt].IsBot,ShortestConnectionTime);
#endif
return ShortestConnectionInt;
}
void ReservedSlotsSystem::UpdateVisibleSlots()
{
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Updated sv_VisibleMaxPlayers settings");
#endif
if(g_ReservedSlotsSystem == 0)
g_VisibleMaxPlayers->SetValue(-1);
else if(g_ReservedSlotsSystem == 1)
g_VisibleMaxPlayers->SetValue(g_MaxClients-g_ReservedSlotsCount);
else if(g_ReservedSlotsSystem == 2)
{
int ReservedSlots = g_ReservedSlotsCount - g_ReservedSlotsUsers;
if(ReservedSlots <= 0 )
g_VisibleMaxPlayers->SetValue(g_MaxClients);
else
g_VisibleMaxPlayers->SetValue(g_MaxClients-ReservedSlots);
}
else if(g_ReservedSlotsSystem == 3)
{
g_VisibleMaxPlayers->SetValue(g_MaxClients-1);
}
}
void ReservedSlotsSystem::cbTaskKickPlayer(int id)
{
g_BATCore.MessagePlayer(0,"[BAT] %s was kicked due to slot reservation",g_BATCore.GetPlayerName(id));
g_BATCore.ServerCommand("kickid %d Kicked due to slot reservation",g_UserInfo[id].Userid);
g_BlockIDCheck = true;
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Kicked %s as he failed to choose to redirect",g_BATCore.GetPlayerName(id));
#endif
}
void ReservedSlotsSystem::cbCvarUpdate()
{
g_ReservedSlotsSystem = g_BATVars.GetReservedSlots();
g_ReservedSlotsCount = g_BATVars.GetReservedSlotCount();
g_ReservedSlotsUseRedirect = g_BATVars.GetReservedSlotsDoRedirect();
_snprintf(g_ReservedSlotsIPAddress,127,"%s",g_BATVars.GetReservedSlotsRedirectAddress());
if(g_ReservedSlotsSystem < 0 && g_ReservedSlotsSystem > 3)
{
g_BATCore.AddLogEntry("ERROR! bat_reservedslots can only have values between 0 and 3");
g_BATCore.ServerCommand("bat_reservedslots 0");
return;
}
if(g_ReservedSlotsSystem != 0 && g_ReservedSlotsSystem != 3 && g_ReservedSlotsCount < 0 && g_ReservedSlotsCount > g_MaxClients )
{
g_BATCore.AddLogEntry("ERROR! bat_reservedslotscount can only have values between 0 and max players");
g_BATCore.ServerCommand("bat_reservedslots 0");
}
if(g_ReservedSlotsSystem != 0 && g_ReservedSlotsUseRedirect && ( strlen(g_ReservedSlotsIPAddress) == 0 || stricmp(g_ReservedSlotsIPAddress,"none") == 0 ))
{
g_BATCore.AddLogEntry("ERROR! bat_reservedslotsredirectaddress needs to be set if you want to redirect players");
g_BATCore.ServerCommand("bat_reservedslotsredirect 0");
}
g_BATCore.GetReservedSlotsSystem()->UpdateVisibleSlots();
#if DEBUG_RESEREVEDSLOTS == 1
g_BATCore.AddLogEntry("Reserved slots debug: Updated cvar settings");
#endif
}