plane/infolan.sp

261 lines
7 KiB
SourcePawn
Raw Normal View History

#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include "socket.inc"
#define MY_SOCKET_IP "localhost"
2021-05-19 22:53:06 +00:00
//#define MY_SOCKET_IP "127.0.0.1"
2021-05-20 13:19:34 +00:00
#define MY_SOCKET_PORT 3000
2021-05-16 10:39:31 +00:00
#define STEAMID_LENGTH 128
char teamCT[5][150];
char teamT[5][150];
char clientId[128];
2021-05-28 23:16:18 +00:00
int allId[20];
int nbAllId = 0;
2021-05-18 22:30:56 +00:00
int port = -1;
public Plugin myinfo =
{
name = "InfoLan 2021 Bot",
author = "rick@gnous.eu",
description = "Bot communiquant avec le bot discord \
https://github.com/Lunki51/cs-picker afin dautomatiser \
lInfoLan 2021",
2021-05-29 02:07:37 +00:00
version = "1.5.7",
url = "https://git.gnous.eu/Rick/infolanBot"
};
2021-05-18 13:43:05 +00:00
public void OnPluginStart()
{
2021-05-19 22:53:06 +00:00
HookEvent("player_activate", SetTeamPlayer);
HookEvent("cs_win_panel_round", UpdateScoreBoard);
2021-05-29 01:27:38 +00:00
HookEvent("cs_win_panel_match", FinCarte);
//HookEvent("cs_match_end_restart", TestRestart, EventHookMode_Pre);
2021-05-18 13:43:05 +00:00
RegServerCmd("!team", SetTeam);
2021-05-20 13:19:34 +00:00
/* pour le debug
2021-05-19 22:53:06 +00:00
RegServerCmd("!tests", TestSocket);
RegServerCmd("!testsA", TestSocketA);
2021-05-20 13:19:34 +00:00
*/
2021-05-18 13:43:05 +00:00
PrintToServer("Hello world!");
}
2021-05-29 01:27:38 +00:00
public void FinCarte(Event event, const char[] name, bool dontBroadcast)
{
ServeurLibre();
}
2021-05-28 23:54:09 +00:00
public void TestRestart(Event event, const char[] name, bool dontBroadcast)
{
// TODO kick tout le monde
PrintToServer("======= FIN ======");
for (int i = 0; i < nbAllId; i++)
{
PrintToServer("On kick %d.", allId[i]);
if (IsClientInGame(allId[i])) {
PrintToServer("NUUUUUUUL");
KickClient(allId[i], "Le match est fini.");
}
allId[i] = 0;
}
ServeurLibre();
nbAllId = 0;
/* kick tout le monde */
}
2021-05-18 22:30:56 +00:00
public void OnConfigsExecuted()
{
if (port < 0)
port = GetConVarInt(FindConVar("hostport"));
}
//public void OnClientAuthorized(int client, const char[] auth)
2021-05-19 22:53:06 +00:00
public void SetTeamPlayer(Event event, const char[] name, bool dontBroadcast)
2021-05-16 10:39:31 +00:00
{
int client = GetClientOfUserId(event.GetInt("userid"));
GetClientAuthId(client, AuthId_Steam2, clientId, sizeof(clientId));
if (!strcmp(clientId, "BOT", true)) { return; }
if (FindAdminByIdentity("steam", clientId) != INVALID_ADMIN_ID)
2021-05-16 10:39:31 +00:00
{
ChangeClientTeam(client, CS_TEAM_SPECTATOR);
2021-05-28 23:16:18 +00:00
allId[nbAllId] = client;
nbAllId++;
}
else
{
bool allGood = GetClientAuthId(client, AuthId_SteamID64, clientId, sizeof(clientId));
if (allGood)
2021-05-16 10:39:31 +00:00
{
allGood = false;
2021-05-19 22:53:06 +00:00
int i = 0;
while (!allGood && i < sizeof(teamCT))
{
if (!strcmp(teamCT[i], clientId, false))
{
ChangeClientTeam(client, CS_TEAM_CT);
allGood = true;
2021-05-28 23:16:18 +00:00
allId[nbAllId] = client;
nbAllId++;
}
else if (!strcmp(teamT[i], clientId, false))
{
ChangeClientTeam(client, CS_TEAM_T);
allGood = true;
2021-05-28 23:16:18 +00:00
allId[nbAllId] = client;
nbAllId++;
}
i++;
}
if (!allGood)
{
KickClient(client, "Vous nêtes pas autorisé à suivre / jouer \
sur ce serveur");
}
else
CS_RespawnPlayer(client);
2021-05-16 10:39:31 +00:00
}
}
}
/*
* !team
*
*
*/
public Action SetTeam(int args)
{
char arg[128];
2021-05-16 10:39:31 +00:00
GetCmdArg(1, arg, sizeof(arg));
int team = (!strcmp(arg, "ct", false) ? 1 : 2);
GetCmdArg(2, arg, sizeof(arg));
if (team == 1)
ServerCommand("mp_teamname_1 %s", arg)
else
ServerCommand("mp_teamname_2 %s", arg)
2021-05-19 22:53:06 +00:00
2021-05-16 10:39:31 +00:00
if (args == 7)
{
for(int i = 3; i <= args; i++)
2021-05-16 10:39:31 +00:00
{
GetCmdArg(i, arg, sizeof(arg));
if (team == 1)
teamCT[i - 3] = arg;
2021-05-16 10:39:31 +00:00
else
teamT[i - 3] = arg;
2021-05-16 10:39:31 +00:00
}
}
else
{
2021-05-16 10:39:31 +00:00
PrintToServer("Erreur, pas assez dargs");
}
return Plugin_Handled;
}
/* envoie information plugin */
2021-05-28 23:44:47 +00:00
public void OnMapVoteStarted()
{
// TODO kick tout le monde
PrintToServer("======= FIN ======");
2021-05-28 23:16:18 +00:00
for (int i = 0; i < nbAllId; i++)
{
2021-05-28 23:32:48 +00:00
PrintToServer("On kick %d.", allId[i]);
2021-05-28 23:44:47 +00:00
if (IsClientInGame(allId[i])) {
PrintToServer("NUUUUUUUL");
2021-05-28 23:16:18 +00:00
KickClient(allId[i], "Le match est fini.");
2021-05-28 23:44:47 +00:00
}
2021-05-28 23:32:48 +00:00
allId[i] = 0;
2021-05-28 23:16:18 +00:00
}
2021-05-28 23:32:48 +00:00
ServeurLibre();
nbAllId = 0;
/* kick tout le monde */
}
public void ServeurLibre()
2021-05-18 13:43:05 +00:00
{
2021-05-28 23:32:48 +00:00
/*
for (int i = 0; i < sizeof(teamCT); i++)
{
teamCT[i] = "";
teamT[i] = "";
}
2021-05-28 23:32:48 +00:00
*/
2021-05-18 13:43:05 +00:00
// requete post pour match fini et serveur libre
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
2021-05-18 22:30:56 +00:00
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
2021-05-18 13:43:05 +00:00
}
2021-05-19 22:53:06 +00:00
public void UpdateScoreBoard(Event event, const char[] name, bool dontBroadcast)
2021-05-18 22:30:56 +00:00
{
2021-05-19 22:53:06 +00:00
// request post avec les scores des 2 équipes
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
SocketConnect(socket, SocketUpdateScore, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
}
public Action TestSocket(int args)
{
PrintToServer("envoie socket");
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
SocketConnect(socket, SocketUpdateScore, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
return Plugin_Handled;
}
public Action TestSocketA(int args)
{
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
return Plugin_Handled;
}
public void SocketUpdateScore(Handle socket, any arg)
{
char requestStr[1024];
char json[64];
2021-05-29 01:15:03 +00:00
FormatEx(json, sizeof(json), "{\"port\": %d, \"ct\": %d, \"t\": %d}",
port, CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T));
2021-05-18 22:30:56 +00:00
FormatEx(
requestStr,
sizeof(requestStr),
2021-05-19 22:53:06 +00:00
"POST /api/csgo/updatescore HTTP/1.1\r\n\
Content-Type: application/json\r\n\
Content-Length: %d\r\n\r\n%s", strlen(json), json
2021-05-18 22:30:56 +00:00
);
SocketSend(socket, requestStr, sizeof(requestStr));
2021-05-19 22:53:06 +00:00
SocketDisconnect(socket);
CloseHandle(socket);
2021-05-18 22:30:56 +00:00
}
2021-05-18 13:43:05 +00:00
2021-05-18 22:30:56 +00:00
public void SocketEnvoieVictoire(Handle socket, any arg)
2021-05-18 13:43:05 +00:00
{
char requestStr[1024];
char json[64];
FormatEx(json, sizeof(json), "{\"serverport\": %d, \"available\": true}", port);
2021-05-18 13:43:05 +00:00
FormatEx(
requestStr,
sizeof(requestStr),
"POST /api/csgo/matchend HTTP/1.1\r\n\
Content-Type: application/json\r\n\
Content-Length: %d\r\n\r\n%s", strlen(json), json
2021-05-18 22:30:56 +00:00
);
SocketSend(socket, requestStr, sizeof(requestStr));
2021-05-19 22:53:06 +00:00
SocketDisconnect(socket);
CloseHandle(socket);
2021-05-18 13:43:05 +00:00
}
2021-05-19 23:35:58 +00:00
public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg) {}
2021-05-19 22:53:06 +00:00
public OnSocketDisconnected(Handle socket, any arg)
{
CloseHandle(socket);
}
2021-05-19 22:53:06 +00:00
public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) {
PrintToServer("socket error %d (errno %d)", errorType, errorNum);
CloseHandle(socket);
}