plane/infolan.sp

267 lines
7.7 KiB
SourcePawn
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include "socket.inc"
#define MY_SOCKET_IP "localhost"
//#define MY_SOCKET_IP "127.0.0.1"
#define MY_SOCKET_PORT 3000
#define STEAMID_LENGTH 128
char teamCT[10][128];
char teamT[10][128];
int nbJoueursCT = 0;
int nbJoueursT = 0;
/* liste de tous les clients connectés */
int listClients[20];
int nbClients = 0;
int port = -1;
public Plugin myinfo =
{
name = "InfoLan 2021 Bot",
author = "rick@gnous.eu",
description = "Plugin communiquant avec le bot discord \
https://github.com/InfoLAN/cs-picker afin dautomatiser \
lInfoLan 2021",
version = "1.6.2",
url = "https://git.gnous.eu/Rick/infolanBot"
};
public void OnPluginStart()
{
HookEvent("player_connect_full", SetTeamPlayer);
HookEvent("cs_win_panel_round", UpdateScoreBoard);
HookEvent("cs_win_panel_match", FinCarte);
RegServerCmd("!team", SetTeam);
RegServerCmd("!addTeam", AjoutJoueur);
/* 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");
LogMessage("Le plugin a démarré avec succès !");
}
/**
* Fonction trigger par levent cs_win_panel_matchk
* Envoie linformation que le match est fini.
*/
public void FinCarte(Event event, const char[] name, bool dontBroadcast)
{
ServeurLibre();
}
/**
* Met le numéro de port dans la variable après le chargement de la config
*/
public void OnConfigsExecuted()
{
if (port < 0)
port = GetConVarInt(FindConVar("hostport"));
LogMessage("Port configuré :%d", port);
}
/**
* Lorsquune personne se connecte, vérifie sil peut se connecter.
* Si oui, la met dans la bonne équipe, sinon la kick.
*/
//public void OnClientAuthorized(int client, const char[] auth)
public void SetTeamPlayer(Event event, const char[] name, bool dontBroadcast)
{
char clientId[128];
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);
listClients[nbClients] = client;
nbClients++;
}
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;
listClients[nbClients] = client;
nbClients++;
}
else if (!strcmp(teamT[i], clientId, false))
{
ChangeClientTeam(client, CS_TEAM_T);
allGood = true;
listClients[nbClients] = client;
nbClients++;
}
i++;
}
if (!allGood)
{
KickClient(client, "Vous nêtes pas autorisé à suivre / jouer \
sur ce serveur");
}
}
}
}
/**
* Commande pour enregistrer les équipes:
* !team ct NomEquipe id1 id2 id3 id4 id5
*/
public Action SetTeam(int args)
{
char tmp[2058];
GetCmdArgString(tmp, 2058);
LogMessage("Commande !team reçu: %s", tmp);
if (args != 7)
LogMessage("!team: pas assez ou trop darguments données.");
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)
/* on garde 7 pour éviter de mettre + de 5 personnes / équipe */
for(int i = 3; i <= 7 && i <= args; i++)
{
GetCmdArg(i, arg, sizeof(arg));
if (team == 1)
{
teamCT[nbJoueursCT] = arg;
nbJoueursCT++;
}
else
{
teamT[nbJoueursT] = arg;
nbJoueursT++;
}
}
return Plugin_Handled;
}
/**
* Commande pour ajouter un joueur dans une équipe
* !addTeam ct id1
*/
public Action AjoutJoueur(int args)
{
char tmp[2058];
GetCmdArgString(tmp, 2058);
LogMessage("Commande !team reçu: %s", tmp);
char arg[128];
GetCmdArg(1, arg, sizeof(arg));
int team = (!strcmp(arg, "ct", false) ? 1 : 2);
GetCmdArg(2, arg, sizeof(arg));
if (team == 1)
{
teamCT[nbJoueursCT] = arg;
nbJoueursCT++;
}
else
{
teamT[nbJoueursT] = arg;
nbJoueursT++;
}
}
/**
* Envoie une requete POST au bot pour lui indiquer que la partie est finie
* et que le serveur est libre.
*/
public void ServeurLibre()
{
LogMessage("Envoi socket pour serveur libre.");
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
}
/**
* Envoie une requete POST au bot avec les nouveaux scores
*/
public void UpdateScoreBoard(Event event, const char[] name, bool dontBroadcast)
{
LogMessage("Envoi socket pour maj les scores.");
Handle socket = SocketCreate(SOCKET_TCP, OnSocketError);
SocketConnect(socket, SocketUpdateScore, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
}
/**
* Socket envoyant le score à la fin du round
*/
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);
}
/**
* Socket envoyant la requete indiquant la fin du match
*/
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);
}
/* Méthodes sockets */
public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg) {}
public void OnSocketDisconnected(Handle socket, any arg)
{
CloseHandle(socket);
}
public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile)
{
LogMessage("Socket error %d (errno %d)", errorType, errorNum);
CloseHandle(socket);
}