Commit d9f293a3 authored by Neoony's avatar Neoony
Browse files

v0.1 WIP4

Reworked automated joining to populated servers
parent 6382061a
Pipeline #3590 passed with stages
in 2 minutes and 8 seconds
......@@ -20,7 +20,7 @@
#include <sdkhooks>
//#include <emputils>
#define PluginVer "v0.1 WIP3"
#define PluginVer "v0.1 WIP4"
public Plugin myinfo =
{
......@@ -54,11 +54,13 @@ int rowCount;
Handle PlayerCountCheck;
Handle StartVoteTimer;
Handle ConnectDialogTimer;
Handle ConnectDialogToFullTimer;
Handle RestrictVote;
Handle VoteHandle = INVALID_HANDLE;
Handle Timer1 = INVALID_HANDLE;
Handle Timer2 = INVALID_HANDLE;
Handle Timer1[MAXPLAYERS+1] = INVALID_HANDLE;
Handle Timer2[MAXPLAYERS+1] = INVALID_HANDLE;
Handle PopulatedRedirectTimer = INVALID_HANDLE;
bool PopulatedServerAlive = false;
Database hDatabase = null;
......@@ -69,6 +71,7 @@ char maxplayersx[10][256];
char ServerVoteItem[10][256];
char redirectAddress[128];
float Thresholdflt;
......@@ -120,92 +123,155 @@ public void OnLibraryAdded(const char[] name)
public void OnClientPutInServer(int Client)
{
//Clear timer
if (Timer1 != INVALID_HANDLE)
if (Timer1[Client] != INVALID_HANDLE)
{
KillTimer(Timer1);
Timer1 = INVALID_HANDLE;
KillTimer(Timer1[Client]);
Timer1[Client] = INVALID_HANDLE;
}
//Clear timer
if (Timer2 != INVALID_HANDLE)
if (Timer2[Client] != INVALID_HANDLE)
{
KillTimer(Timer2);
Timer2 = INVALID_HANDLE;
KillTimer(Timer2[Client]);
Timer2[Client] = INVALID_HANDLE;
}
PrintToChat(Client, "\x04[L] \x01This server is running\x04 [Lobby]\x01 %s by\x07ff6600 Neoony", PluginVer);
Timer1 = CreateTimer(5.0, Tmr1, Client, TIMER_REPEAT);
Timer1[Client] = CreateTimer(5.0, Tmr1, Client, TIMER_REPEAT);
}
RedirectToPopulatedServer();
public bool OnClientConnect(int Client)
{
PrintToServer("Client %i connecting", Client);
CheckPopulatedServersSinglePlayer(Client);
return true;
}
public void CheckPopulatedServersSinglePlayer(int Client)
{
// Query for servers with more or equal than l_minplayers players.
char populatedServerQuery[256];
//int minplayers = GetConVarInt(l_minplayers);
Format(populatedServerQuery, sizeof(populatedServerQuery), "SELECT addr FROM `LobbyDB`.`servers` WHERE players >= %i AND password=0 AND isenabled=1 AND name NOT LIKE '%%LOBBY%%' ORDER BY players DESC LIMIT 1;", GetConVarInt(l_minplayers));
SQL_TQuery(hDatabase, RedirectToPopulatedServerSinglePlayerCallback, populatedServerQuery, Client);
}
public void RedirectToPopulatedServerSinglePlayerCallback(Handle hDriver, Handle hResult, const char[] sError, int Client)
{
if (hResult == INVALID_HANDLE)
{
// The result is invalid -> an error occured
LogError("[Database] SQL-Query failed! Error: %s", sError);
PopulatedServerAlive = false;
return;
}
int fullServerCount = SQL_GetRowCount(hResult);
if (fullServerCount == 0)
{
PopulatedServerAlive = false;
return; // No full server found.
}
char redirectAddressSP[128];
SQL_FetchRow(hResult);
SQL_FetchString(hResult, 0, redirectAddressSP, sizeof(redirectAddressSP));
PopulatedServerAlive = true;
if(IsVoteInProgress() == false)
{
PrintToServer("[L] Redirecting single client %i", Client);
ClientCommand(Client, "lobby_redirect %s", redirectAddressSP);
}
}
public void RedirectToPopulatedServer()
public void CheckPopulatedServers()
{
// Query for servers with more or equal than l_minplayers players.
char populatedServerQuery[256];
int minplayers = GetConVarInt(l_minplayers);
Format(populatedServerQuery, sizeof(populatedServerQuery), "SELECT addr FROM `LobbyDB`.`servers` WHERE players >= %i AND password=0 AND isenabled=1 AND name NOT LIKE '%%LOBBY%%' ORDER BY players DESC LIMIT 1;", minplayers);
//int minplayers = GetConVarInt(l_minplayers);
Format(populatedServerQuery, sizeof(populatedServerQuery), "SELECT addr FROM `LobbyDB`.`servers` WHERE players >= %i AND password=0 AND isenabled=1 AND name NOT LIKE '%%LOBBY%%' ORDER BY players DESC LIMIT 1;", GetConVarInt(l_minplayers));
SQL_TQuery(hDatabase, RedirectToPopulatedServerCallback, populatedServerQuery);
}
public void RedirectToPopulatedServerCallback(Handle hDriver, Handle hResult, const char[] sError, int iData)
{
if (hResult == INVALID_HANDLE) {
if (hResult == INVALID_HANDLE)
{
// The result is invalid -> an error occured
LogError("[Database] SQL-Query failed! Error: %s", sError);
PopulatedServerAlive = false;
PlayersNumberCheck();
return;
}
int fullServerCount = SQL_GetRowCount(hResult);
if (fullServerCount == 0)
{
PopulatedServerAlive = false;
PlayersNumberCheck();
return; // No full server found.
}
char redirectAddress[128];
SQL_FetchRow(hResult);
SQL_FetchString(hResult, 0, redirectAddress, sizeof(redirectAddress));
PopulatedServerAlive = true;
if(IsVoteInProgress() == false && PopulatedRedirectTimer == INVALID_HANDLE)
{
PrintToChatAll("\x04[L] \x01Server with enough players has been\x07008000 chosen\x01, players will be\x07008000 redirected\x01 in\x073399ff 5\x01 seconds");
PopulatedRedirectTimer = CreateTimer(5.0, PopulatedRedirectTmr);
}
}
// Found populated server, redirect everyone
for (int client = 1; client <= MaxClients; client++) {
if (!IsClientInGame(client))
continue;
//PrintToChatAll("Redirecting full server");
//PrintToServer("Redirecting full server");
//ClientCommand(client, "lobby_redirect %s", redirectAddress);
RestrictVote = CreateTimer(GetConVarFloat(l_restrictvote), RestrictVt, _, TIMER_REPEAT);
ConnectDialogToFullTimer = CreateTimer(5.0, ConnectDialogToFullTmr, redirectAddress[127], TIMER_REPEAT);
PrintToChatAll("\x04[L] \x01Server with enough players has been\x07008000 chosen\x01, players will be redirected in\x073399ff 5\x01 seconds");
//PrintToChatAll("Redirecting full server %s", redirectAddress);
public Action PopulatedRedirectTmr(Handle timer)
{
//Clear timer
if (PopulatedRedirectTimer != INVALID_HANDLE)
{
KillTimer(PopulatedRedirectTimer);
PopulatedRedirectTimer = INVALID_HANDLE;
}
PrintToChatAll("\x04[L] \x01\x07008000Redirecting\x01 players...");
for (int client = 1; client <= MaxClients; client++)
{
if (IsClientConnected(client))
{
ClientCommand(client, "lobby_redirect %s", redirectAddress);
PrintToServer("[L] Redirecting client %i", client);
}
}
}
public Action Tmr1(Handle timer, int Client)
{
//Clear timer
if (Timer2 != INVALID_HANDLE)
if (Timer2[Client] != INVALID_HANDLE)
{
KillTimer(Timer2);
Timer2 = INVALID_HANDLE;
KillTimer(Timer2[Client]);
Timer2[Client] = INVALID_HANDLE;
}
PrintToChat(Client, "\x04[L] \x01\x07008000Welcome\x01 in the Empires\x04 Lobby\x01");
Timer2 = CreateTimer(5.0, Tmr2, Client, TIMER_REPEAT);
Timer2[Client] = CreateTimer(5.0, Tmr2, Client, TIMER_REPEAT);
//Clear timer
if (Timer1 != INVALID_HANDLE)
if (Timer1[Client] != INVALID_HANDLE)
{
KillTimer(Timer1);
Timer1 = INVALID_HANDLE;
KillTimer(Timer1[Client]);
Timer1[Client] = INVALID_HANDLE;
}
}
public Action Tmr2(Handle timer, int Client)
{
//Clear timer
if (Timer1 != INVALID_HANDLE)
if (Timer1[Client] != INVALID_HANDLE)
{
KillTimer(Timer1);
Timer1 = INVALID_HANDLE;
KillTimer(Timer1[Client]);
Timer1[Client] = INVALID_HANDLE;
}
PrintToChat(Client, "\x04[L] \x01A vote about joining a server will be created when this lobby reaches\x07ff6600 %d\x01 players in a team", GetConVarInt(l_minplayers));
//Clear timer
if (Timer2 != INVALID_HANDLE)
if (Timer2[Client] != INVALID_HANDLE)
{
KillTimer(Timer2);
Timer2 = INVALID_HANDLE;
KillTimer(Timer2[Client]);
Timer2[Client] = INVALID_HANDLE;
}
}
......@@ -243,17 +309,11 @@ public void OnMapEnd()
KillTimer(ConnectDialogTimer);
ConnectDialogTimer = INVALID_HANDLE;
}
if (Timer1 != INVALID_HANDLE)
{
KillTimer(Timer1);
Timer1 = INVALID_HANDLE;
}
if (Timer2 != INVALID_HANDLE)
//Clear timer
if (PopulatedRedirectTimer != INVALID_HANDLE)
{
KillTimer(Timer2);
Timer2 = INVALID_HANDLE;
KillTimer(PopulatedRedirectTimer);
PopulatedRedirectTimer = INVALID_HANDLE;
}
}
......@@ -275,21 +335,24 @@ public void OnConfigsExecuted()
KillTimer(ConnectDialogTimer);
ConnectDialogTimer = INVALID_HANDLE;
}
if (Timer1 != INVALID_HANDLE)
}
public void OnClientDisconnect(int Client)
{
//Clear timer
if (Timer1[Client] != INVALID_HANDLE)
{
KillTimer(Timer1);
Timer1 = INVALID_HANDLE;
KillTimer(Timer1[Client]);
Timer1[Client] = INVALID_HANDLE;
}
if (Timer2 != INVALID_HANDLE)
//Clear timer
if (Timer2[Client] != INVALID_HANDLE)
{
KillTimer(Timer2);
Timer2 = INVALID_HANDLE;
KillTimer(Timer2[Client]);
Timer2[Client] = INVALID_HANDLE;
}
}
//SQL
void StartSQL()
{
......@@ -427,25 +490,35 @@ public void InfoOfServer(Handle hDriver, Handle hResult, const char[] sError, in
}
public Action PlayerCountChk(Handle timer)
{
CheckPopulatedServers();
}
public void PlayersNumberCheck()
{
PlayersNumber = GetTeamClientCount(2) + GetTeamClientCount(3);
//if (PlayersNumber < GetConVarInt(l_minplayers))
//{
// PrintToChatAll("\x04[L] \x01Not enough players");
//}
if (PlayersNumber >= GetConVarInt(l_minplayers) && votestarted == 0 && IsVoteInProgress() == false)
if (PlayersNumber >= GetConVarInt(l_minplayers) && votestarted == 0 && IsVoteInProgress() == false && PopulatedServerAlive == false)
{
PlayersNumberReached();
}
}
public void PlayersNumberReached()
{
//Clear timer
if (PlayerCountCheck != INVALID_HANDLE)
{
KillTimer(PlayerCountCheck);
PlayerCountCheck = INVALID_HANDLE;
}
if (PopulatedServerAlive == false)
{
PrintToChatAll("\x04[L] \x01Player amount required has been reached");
PrintToChatAll("\x04[L] \x01Creating a vote to join a server in\x073399ff 5\x01 seconds");
StartVoteTimer = CreateTimer(5.0, StartVoteTmr, _, TIMER_REPEAT);
QueryNumberOfServers();
votestarted = 1;
//Clear timer
if (PlayerCountCheck != INVALID_HANDLE)
{
KillTimer(PlayerCountCheck);
PlayerCountCheck = INVALID_HANDLE;
}
}
}
......@@ -466,11 +539,12 @@ public Action StartVote()
int PlayersCount;
for (int i = 1; i <= MaxClients; i++)
if (IsClientInGame(i) && !IsFakeClient(i))
{
Players[PlayersCount++] = i;
if (IsClientInGame(i) && !IsFakeClient(i))
{
Players[PlayersCount++] = i;
}
}
VoteInfo[VOTE_INFO_NUMCLIENTS] = PlayersCount;
......@@ -551,34 +625,14 @@ public Action VoteSuccessful(int param1)
RestrictVote = CreateTimer(GetConVarFloat(l_restrictvote), RestrictVt, _, TIMER_REPEAT);
}
public Action StartConnectDialogToFull(char redirectAddress)
{
for (int client = 1; client <= MaxClients; client++)
if (IsClientInGame(client))
{
ClientCommand(client, "lobby_redirect %s", redirectAddress);
}
}
public Action ConnectDialogToFullTmr(Handle timer, char redirectAddress)
{
//int value1 = 1;
StartConnectDialogToFull(redirectAddress);
//Clear timer
if (ConnectDialogToFullTimer != INVALID_HANDLE)
{
KillTimer(ConnectDialogToFullTimer);
ConnectDialogToFullTimer = INVALID_HANDLE;
}
PrintToChatAll("\x04[L] \x01Redirecting\x07008000 players...");
}
public Action StartConnectDialog(int param1)
{
for (int client = 1; client <= MaxClients; client++)
if (IsClientInGame(client))
{
ClientCommand(client, "lobby_redirect %s", addrx[param1]);
if (IsClientInGame(client))
{
ClientCommand(client, "lobby_redirect %s", addrx[param1]);
}
}
}
......@@ -604,7 +658,7 @@ public Action ConnectDialogTmr(Handle timer, int param1)
KillTimer(ConnectDialogTimer);
ConnectDialogTimer = INVALID_HANDLE;
}
PrintToChatAll("\x04[L] \x01Redirecting\x07008000 players...");
PrintToChatAll("\x04[L] \x01\x07008000Redirecting\x01 players...");
}
public Action SCommand_StartVote(int args)
......
......@@ -4,7 +4,7 @@
{
"Version"
{
"Latest" "v0.1 WIP3"
"Latest" "v0.1 WIP4"
}
"Notes" "First release"
......@@ -13,6 +13,7 @@
"Notes" "WIP2: Fixed fail to start the vote first time"
"Notes" "WIP2: Changed lobby redirect command to client command"
"Notes" "WIP3: Added auto redirect when some of the candidate servers meet l_minplayers"
"Notes" "WIP4: Reworked automated joining to populated servers"
}
"Files"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment