205 lines
5.7 KiB
SourcePawn
205 lines
5.7 KiB
SourcePawn
#include <sourcemod>
|
||
#include <cstrike>
|
||
#include "socket.inc"
|
||
|
||
#define MY_SOCKET_IP "localhost"
|
||
//#define MY_SOCKET_IP "127.0.0.1"
|
||
#define MY_SOCKET_PORT 300
|
||
#define STEAMID_LENGTH 128
|
||
|
||
char teamCT[6][150];
|
||
char teamT[6][150];
|
||
char clientId[128];
|
||
|
||
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 d’automatiser \
|
||
l’InfoLan 2021",
|
||
version = "1.3",
|
||
url = "https://git.gnous.eu/Rick/infolanBot"
|
||
};
|
||
|
||
public void OnPluginStart()
|
||
{
|
||
//HookEvent("cs_win_panel_match", ServeurLibre);
|
||
HookEvent("player_activate", SetTeamPlayer);
|
||
HookEvent("cs_win_panel_round", UpdateScoreBoard);
|
||
|
||
RegServerCmd("!team", SetTeam);
|
||
RegServerCmd("!tests", TestSocket);
|
||
RegServerCmd("!testsA", TestSocketA);
|
||
|
||
PrintToServer("Hello world!");
|
||
}
|
||
|
||
public void OnConfigsExecuted()
|
||
{
|
||
if (port < 0)
|
||
port = GetConVarInt(FindConVar("hostport"));
|
||
}
|
||
|
||
//public void OnClientAuthorized(int client, const char[] auth)
|
||
public void SetTeamPlayer(Event event, const char[] name, bool dontBroadcast)
|
||
{
|
||
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)
|
||
{
|
||
ChangeClientTeam(client, CS_TEAM_SPECTATOR);
|
||
}
|
||
else
|
||
{
|
||
bool allGood = GetClientAuthId(client, AuthId_SteamID64, clientId, sizeof(clientId));
|
||
if (allGood)
|
||
{
|
||
allGood = false;
|
||
int i = 0;
|
||
while (!allGood && i < sizeof(teamCT))
|
||
{
|
||
if (!strcmp(teamCT[i], clientId, false))
|
||
{
|
||
ChangeClientTeam(client, CS_TEAM_CT);
|
||
allGood = true;
|
||
}
|
||
else if (!strcmp(teamT[i], clientId, false))
|
||
{
|
||
ChangeClientTeam(client, CS_TEAM_T);
|
||
allGood = true;
|
||
}
|
||
i++;
|
||
}
|
||
if (!allGood)
|
||
{
|
||
KickClient(client, "Vous n’êtes pas autorisé à suivre / jouer \
|
||
sur ce serveur");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
* !team
|
||
*
|
||
*
|
||
*/
|
||
public Action SetTeam(int args)
|
||
{
|
||
char arg[128];
|
||
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)
|
||
|
||
if (args == 7)
|
||
{
|
||
for(int i = 3; i <= args; i++)
|
||
{
|
||
GetCmdArg(i, arg, sizeof(arg));
|
||
if (team == 1)
|
||
teamCT[i - 3] = arg;
|
||
else
|
||
teamT[i - 3] = arg;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
PrintToServer("Erreur, pas assez d’args");
|
||
}
|
||
|
||
return Plugin_Handled;
|
||
}
|
||
|
||
/* envoie information plugin */
|
||
|
||
public void OnMapEnd()
|
||
{
|
||
// TODO kick tout le monde
|
||
PrintToServer("======= FIN ======");
|
||
ServeurLibre();
|
||
/* kick tout le monde */
|
||
}
|
||
|
||
public void ServeurLibre()
|
||
{
|
||
// requete post pour match fini et serveur libre
|
||
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
|
||
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
|
||
}
|
||
|
||
public void UpdateScoreBoard(Event event, const char[] name, bool dontBroadcast)
|
||
{
|
||
// 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];
|
||
FormatEx(json, sizeof(json), "{\"port\": %d, \"ct\": %d, \"t\": %d}",
|
||
port, CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T));
|
||
FormatEx(
|
||
requestStr,
|
||
sizeof(requestStr),
|
||
"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
|
||
);
|
||
SocketSend(socket, requestStr, sizeof(requestStr));
|
||
SocketDisconnect(socket);
|
||
CloseHandle(socket);
|
||
}
|
||
|
||
public void SocketEnvoieVictoire(Handle socket, any arg)
|
||
{
|
||
char requestStr[1024];
|
||
char json[64];
|
||
FormatEx(json, sizeof(json), "{\"serverport\": %d, \"available\": true}", port);
|
||
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
|
||
);
|
||
SocketSend(socket, requestStr, sizeof(requestStr));
|
||
SocketDisconnect(socket);
|
||
CloseHandle(socket);
|
||
}
|
||
|
||
public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg) {}
|
||
|
||
public OnSocketDisconnected(Handle socket, any arg)
|
||
{
|
||
CloseHandle(socket);
|
||
}
|
||
|
||
public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) {
|
||
PrintToServer("socket error %d (errno %d)", errorType, errorNum);
|
||
CloseHandle(socket);
|
||
}
|