2021-05-14 23:56:53 +02:00
|
|
|
|
#include <sourcemod>
|
2021-05-22 13:32:23 +02:00
|
|
|
|
#include <sdktools>
|
2021-05-18 15:11:19 +02:00
|
|
|
|
#include <cstrike>
|
2021-05-14 23:56:53 +02:00
|
|
|
|
#include "socket.inc"
|
|
|
|
|
|
|
|
|
|
#define MY_SOCKET_IP "localhost"
|
2021-05-20 00:53:06 +02:00
|
|
|
|
//#define MY_SOCKET_IP "127.0.0.1"
|
2021-05-20 15:19:34 +02:00
|
|
|
|
#define MY_SOCKET_PORT 3000
|
2021-05-16 12:39:31 +02:00
|
|
|
|
#define STEAMID_LENGTH 128
|
|
|
|
|
|
2021-06-03 14:48:57 +02:00
|
|
|
|
char teamCT[10][128];
|
|
|
|
|
char teamT[10][128];
|
2021-05-14 23:56:53 +02:00
|
|
|
|
|
2021-06-03 14:48:57 +02:00
|
|
|
|
/* liste de tous les clients connectés */
|
|
|
|
|
int listClients[20];
|
|
|
|
|
int nbClients = 0;
|
2021-05-29 01:16:18 +02:00
|
|
|
|
|
2021-05-19 00:30:56 +02:00
|
|
|
|
int port = -1;
|
|
|
|
|
|
2021-05-14 23:56:53 +02:00
|
|
|
|
public Plugin myinfo =
|
|
|
|
|
{
|
|
|
|
|
name = "InfoLan 2021 Bot",
|
|
|
|
|
author = "rick@gnous.eu",
|
2021-06-03 13:30:55 +02:00
|
|
|
|
description = "Plugin communiquant avec le bot discord \
|
|
|
|
|
https://github.com/InfoLAN/cs-picker afin d’automatiser \
|
2021-05-14 23:56:53 +02:00
|
|
|
|
l’InfoLan 2021",
|
2021-06-03 14:12:46 +02:00
|
|
|
|
version = "1.5.9",
|
2021-05-14 23:56:53 +02:00
|
|
|
|
url = "https://git.gnous.eu/Rick/infolanBot"
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-18 15:43:05 +02:00
|
|
|
|
public void OnPluginStart()
|
|
|
|
|
{
|
2021-05-20 00:53:06 +02:00
|
|
|
|
HookEvent("player_activate", SetTeamPlayer);
|
2021-05-29 03:16:17 +02:00
|
|
|
|
HookEvent("cs_win_panel_round", UpdateScoreBoard);
|
2021-05-29 03:27:38 +02:00
|
|
|
|
HookEvent("cs_win_panel_match", FinCarte);
|
2021-05-18 15:43:05 +02:00
|
|
|
|
|
|
|
|
|
RegServerCmd("!team", SetTeam);
|
|
|
|
|
|
2021-06-03 14:12:46 +02:00
|
|
|
|
/* juste pour avoir un truc jolie */
|
|
|
|
|
PrintToServer("\n _____ __ _ _ _");
|
|
|
|
|
PrintToServer("|_ _| / _| | | /\\ | \\ | |");
|
|
|
|
|
PrintToServer(" | | _ __ | |_ ___ | | / \\ | \\| |");
|
|
|
|
|
PrintToServer(" | | | '_ \\| _/ _ \\| | / /\\ \\ | . ` |");
|
|
|
|
|
PrintToServer(" _| |_| | | | || (_) | |____ / ____ \\| |\\ |");
|
|
|
|
|
PrintToServer("|_____|_| |_|_| \\___/|______/_/ \\_\\_| \\_|");
|
|
|
|
|
PrintToServer("\n Le plugin a démarré avec succès !\n");
|
|
|
|
|
PrintToServer("Autheur: rick@gnous.eu");
|
|
|
|
|
PrintToServer("Depot git: https://git.gnous.eu/Rick/infolanBot");
|
|
|
|
|
PrintToServer("Depot github: https://github.com/InfoLAN/infolanCSGOplugin");
|
|
|
|
|
PrintToServer("Licence: GPL3\n");
|
2021-05-18 15:43:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Fonction trigger par l’event cs_win_panel_matchk
|
|
|
|
|
* Envoie l’information que le match est fini.
|
|
|
|
|
*/
|
2021-05-29 03:27:38 +02:00
|
|
|
|
public void FinCarte(Event event, const char[] name, bool dontBroadcast)
|
|
|
|
|
{
|
|
|
|
|
ServeurLibre();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Met le numéro de port dans la variable après le chargement de la config
|
|
|
|
|
*/
|
2021-05-19 00:30:56 +02:00
|
|
|
|
public void OnConfigsExecuted()
|
|
|
|
|
{
|
|
|
|
|
if (port < 0)
|
|
|
|
|
port = GetConVarInt(FindConVar("hostport"));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Lorsqu’une personne se connecte, vérifie s’il peut se connecter.
|
|
|
|
|
* Si oui, la met dans la bonne équipe, sinon la kick.
|
|
|
|
|
*/
|
2021-05-18 15:11:19 +02:00
|
|
|
|
//public void OnClientAuthorized(int client, const char[] auth)
|
2021-05-20 00:53:06 +02:00
|
|
|
|
public void SetTeamPlayer(Event event, const char[] name, bool dontBroadcast)
|
2021-05-16 12:39:31 +02:00
|
|
|
|
{
|
2021-06-03 14:48:57 +02:00
|
|
|
|
char clientId[128];
|
2021-05-18 15:11:19 +02:00
|
|
|
|
int client = GetClientOfUserId(event.GetInt("userid"));
|
2021-05-18 20:04:27 +02:00
|
|
|
|
GetClientAuthId(client, AuthId_Steam2, clientId, sizeof(clientId));
|
|
|
|
|
if (!strcmp(clientId, "BOT", true)) { return; }
|
|
|
|
|
if (FindAdminByIdentity("steam", clientId) != INVALID_ADMIN_ID)
|
2021-05-16 12:39:31 +02:00
|
|
|
|
{
|
2021-05-18 20:04:27 +02:00
|
|
|
|
ChangeClientTeam(client, CS_TEAM_SPECTATOR);
|
2021-06-03 14:48:57 +02:00
|
|
|
|
listClients[nbClients] = client;
|
|
|
|
|
nbClients++;
|
2021-05-18 20:04:27 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool allGood = GetClientAuthId(client, AuthId_SteamID64, clientId, sizeof(clientId));
|
|
|
|
|
if (allGood)
|
2021-05-16 12:39:31 +02:00
|
|
|
|
{
|
2021-05-18 20:04:27 +02:00
|
|
|
|
allGood = false;
|
2021-05-20 00:53:06 +02:00
|
|
|
|
int i = 0;
|
2021-05-18 20:04:27 +02:00
|
|
|
|
while (!allGood && i < sizeof(teamCT))
|
2021-05-18 15:11:19 +02:00
|
|
|
|
{
|
2021-05-18 20:04:27 +02:00
|
|
|
|
if (!strcmp(teamCT[i], clientId, false))
|
|
|
|
|
{
|
|
|
|
|
ChangeClientTeam(client, CS_TEAM_CT);
|
|
|
|
|
allGood = true;
|
2021-06-03 14:48:57 +02:00
|
|
|
|
listClients[nbClients] = client;
|
|
|
|
|
nbClients++;
|
2021-05-18 20:04:27 +02:00
|
|
|
|
}
|
|
|
|
|
else if (!strcmp(teamT[i], clientId, false))
|
2021-05-18 15:11:19 +02:00
|
|
|
|
{
|
2021-05-18 20:04:27 +02:00
|
|
|
|
ChangeClientTeam(client, CS_TEAM_T);
|
2021-05-18 15:11:19 +02:00
|
|
|
|
allGood = true;
|
2021-06-03 14:48:57 +02:00
|
|
|
|
listClients[nbClients] = client;
|
|
|
|
|
nbClients++;
|
2021-05-18 15:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
if (!allGood)
|
|
|
|
|
{
|
|
|
|
|
KickClient(client, "Vous n’êtes pas autorisé à suivre / jouer \
|
|
|
|
|
sur ce serveur");
|
|
|
|
|
}
|
2021-05-22 15:04:18 +02:00
|
|
|
|
else
|
|
|
|
|
CS_RespawnPlayer(client);
|
2021-05-16 12:39:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Commande pour enregistrer les équipes:
|
|
|
|
|
* !team ct NomEquipe id1 id2 id3 id4 id5
|
2021-05-16 12:39:31 +02:00
|
|
|
|
*/
|
|
|
|
|
public Action SetTeam(int args)
|
2021-05-14 23:56:53 +02:00
|
|
|
|
{
|
2021-06-03 14:56:12 +02:00
|
|
|
|
char tmp[2058];
|
|
|
|
|
GetCmdArgString(tmp, 2058);
|
|
|
|
|
LogMessage("Commande !team reçu: %s", tmp);
|
|
|
|
|
if (args != 7)
|
|
|
|
|
LogMessage("!team: pas assez ou trop d’arguments données.");
|
|
|
|
|
|
2021-05-14 23:56:53 +02:00
|
|
|
|
char arg[128];
|
2021-05-16 12:39:31 +02:00
|
|
|
|
GetCmdArg(1, arg, sizeof(arg));
|
|
|
|
|
int team = (!strcmp(arg, "ct", false) ? 1 : 2);
|
2021-05-18 15:11:19 +02:00
|
|
|
|
GetCmdArg(2, arg, sizeof(arg));
|
|
|
|
|
if (team == 1)
|
|
|
|
|
ServerCommand("mp_teamname_1 %s", arg)
|
|
|
|
|
else
|
|
|
|
|
ServerCommand("mp_teamname_2 %s", arg)
|
2021-05-20 00:53:06 +02:00
|
|
|
|
|
2021-06-03 14:56:12 +02:00
|
|
|
|
/* on garde 7 pour éviter de mettre + de 5 personnes / équipe */
|
|
|
|
|
for(int i = 3; i <= 7 && i <= args; i++)
|
2021-05-14 23:56:53 +02:00
|
|
|
|
{
|
2021-06-03 14:56:12 +02:00
|
|
|
|
GetCmdArg(i, arg, sizeof(arg));
|
|
|
|
|
if (team == 1)
|
|
|
|
|
teamCT[i - 3] = arg;
|
|
|
|
|
else
|
|
|
|
|
teamT[i - 3] = arg;
|
2021-05-14 23:56:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Plugin_Handled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Envoie une requete POST au bot pour lui indiquer que la partie est finie
|
|
|
|
|
* et que le serveur est libre.
|
|
|
|
|
*/
|
2021-05-20 01:26:13 +02:00
|
|
|
|
public void ServeurLibre()
|
2021-05-18 15:43:05 +02:00
|
|
|
|
{
|
|
|
|
|
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
|
2021-05-19 00:30:56 +02:00
|
|
|
|
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
|
2021-05-18 15:43:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Envoie une requete POST au bot avec les nouveaux scores
|
|
|
|
|
*/
|
2021-05-20 00:53:06 +02:00
|
|
|
|
public void UpdateScoreBoard(Event event, const char[] name, bool dontBroadcast)
|
2021-05-19 00:30:56 +02:00
|
|
|
|
{
|
2021-05-20 00:53:06 +02:00
|
|
|
|
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
|
|
|
|
|
SocketConnect(socket, SocketUpdateScore, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Socket envoyant le score à la fin du round
|
|
|
|
|
*/
|
2021-05-20 00:53:06 +02:00
|
|
|
|
public void SocketUpdateScore(Handle socket, any arg)
|
|
|
|
|
{
|
2021-05-20 01:08:11 +02:00
|
|
|
|
char requestStr[1024];
|
|
|
|
|
char json[64];
|
2021-05-29 03:15:03 +02:00
|
|
|
|
FormatEx(json, sizeof(json), "{\"port\": %d, \"ct\": %d, \"t\": %d}",
|
|
|
|
|
port, CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T));
|
2021-05-19 00:30:56 +02:00
|
|
|
|
FormatEx(
|
|
|
|
|
requestStr,
|
|
|
|
|
sizeof(requestStr),
|
2021-05-20 00:53:06 +02:00
|
|
|
|
"POST /api/csgo/updatescore HTTP/1.1\r\n\
|
|
|
|
|
Content-Type: application/json\r\n\
|
2021-05-20 01:08:11 +02:00
|
|
|
|
Content-Length: %d\r\n\r\n%s", strlen(json), json
|
2021-05-19 00:30:56 +02:00
|
|
|
|
);
|
2021-05-20 01:08:11 +02:00
|
|
|
|
SocketSend(socket, requestStr, sizeof(requestStr));
|
2021-05-20 00:53:06 +02:00
|
|
|
|
SocketDisconnect(socket);
|
|
|
|
|
CloseHandle(socket);
|
2021-05-19 00:30:56 +02:00
|
|
|
|
}
|
2021-05-18 15:43:05 +02:00
|
|
|
|
|
2021-06-01 00:31:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Socket envoyant la requete indiquant la fin du match
|
|
|
|
|
*/
|
2021-05-19 00:30:56 +02:00
|
|
|
|
public void SocketEnvoieVictoire(Handle socket, any arg)
|
2021-05-18 15:43:05 +02:00
|
|
|
|
{
|
2021-05-20 01:08:11 +02:00
|
|
|
|
char requestStr[1024];
|
|
|
|
|
char json[64];
|
|
|
|
|
FormatEx(json, sizeof(json), "{\"serverport\": %d, \"available\": true}", port);
|
2021-05-18 15:43:05 +02:00
|
|
|
|
FormatEx(
|
|
|
|
|
requestStr,
|
|
|
|
|
sizeof(requestStr),
|
2021-05-20 01:08:11 +02:00
|
|
|
|
"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-19 00:30:56 +02:00
|
|
|
|
);
|
2021-05-20 01:08:11 +02:00
|
|
|
|
SocketSend(socket, requestStr, sizeof(requestStr));
|
2021-05-20 00:53:06 +02:00
|
|
|
|
SocketDisconnect(socket);
|
|
|
|
|
CloseHandle(socket);
|
2021-05-18 15:43:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 12:29:37 +02:00
|
|
|
|
/* Méthodes sockets */
|
2021-06-01 00:31:17 +02:00
|
|
|
|
|
2021-05-20 01:35:58 +02:00
|
|
|
|
public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg) {}
|
2021-05-14 23:56:53 +02:00
|
|
|
|
|
2021-06-03 12:29:37 +02:00
|
|
|
|
public void OnSocketDisconnected(Handle socket, any arg)
|
2021-05-14 23:56:53 +02:00
|
|
|
|
{
|
|
|
|
|
CloseHandle(socket);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 00:53:06 +02:00
|
|
|
|
public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) {
|
|
|
|
|
PrintToServer("socket error %d (errno %d)", errorType, errorNum);
|
|
|
|
|
CloseHandle(socket);
|
|
|
|
|
}
|