Requete score fonctionnelle

This commit is contained in:
rick 2021-05-20 00:53:06 +02:00
parent 5f669ede19
commit d8ce63a440
Signed by: Rick
GPG key ID: 2B593F087240EE99

87
test.sp
View file

@ -5,6 +5,7 @@
#include "socket.inc" #include "socket.inc"
#define MY_SOCKET_IP "localhost" #define MY_SOCKET_IP "localhost"
//#define MY_SOCKET_IP "127.0.0.1"
#define MY_SOCKET_PORT 3500 #define MY_SOCKET_PORT 3500
#define STEAMID_LENGTH 128 #define STEAMID_LENGTH 128
@ -28,9 +29,12 @@ public Plugin myinfo =
public void OnPluginStart() public void OnPluginStart()
{ {
HookEvent("cs_win_panel_match", ServeurLibre); HookEvent("cs_win_panel_match", ServeurLibre);
HookEvent("player_activate", SuperTest); HookEvent("player_activate", SetTeamPlayer);
HookEvent("cs_win_panel_round", UpdateScoreBoard);
RegServerCmd("!team", SetTeam); RegServerCmd("!team", SetTeam);
RegServerCmd("!tests", TestSocket);
RegServerCmd("!testsA", TestSocketA);
PrintToServer("Hello world!"); PrintToServer("Hello world!");
} }
@ -38,13 +42,11 @@ public void OnPluginStart()
public void OnConfigsExecuted() public void OnConfigsExecuted()
{ {
if (port < 0) if (port < 0)
{
port = GetConVarInt(FindConVar("hostport")); port = GetConVarInt(FindConVar("hostport"));
}
} }
//public void OnClientAuthorized(int client, const char[] auth) //public void OnClientAuthorized(int client, const char[] auth)
public void SuperTest(Event event, const char[] name, bool dontBroadcast) public void SetTeamPlayer(Event event, const char[] name, bool dontBroadcast)
{ {
int client = GetClientOfUserId(event.GetInt("userid")); int client = GetClientOfUserId(event.GetInt("userid"));
GetClientAuthId(client, AuthId_Steam2, clientId, sizeof(clientId)); GetClientAuthId(client, AuthId_Steam2, clientId, sizeof(clientId));
@ -59,7 +61,7 @@ public void SuperTest(Event event, const char[] name, bool dontBroadcast)
if (allGood) if (allGood)
{ {
allGood = false; allGood = false;
int i = 1; int i = 0;
while (!allGood && i < sizeof(teamCT)) while (!allGood && i < sizeof(teamCT))
{ {
if (!strcmp(teamCT[i], clientId, false)) if (!strcmp(teamCT[i], clientId, false))
@ -95,26 +97,19 @@ public Action SetTeam(int args)
int team = (!strcmp(arg, "ct", false) ? 1 : 2); int team = (!strcmp(arg, "ct", false) ? 1 : 2);
GetCmdArg(2, arg, sizeof(arg)); GetCmdArg(2, arg, sizeof(arg));
if (team == 1) if (team == 1)
{
ServerCommand("mp_teamname_1 %s", arg) ServerCommand("mp_teamname_1 %s", arg)
}
else else
{
ServerCommand("mp_teamname_2 %s", arg) ServerCommand("mp_teamname_2 %s", arg)
}
if (args == 7) if (args == 7)
{ {
for(int i = 3; i <= args; i++) for(int i = 3; i <= args; i++)
{ {
GetCmdArg(i, arg, sizeof(arg)); GetCmdArg(i, arg, sizeof(arg));
if (team == 1) if (team == 1)
{
teamCT[i - 3] = arg; teamCT[i - 3] = arg;
}
else else
{
teamT[i - 3] = arg; teamT[i - 3] = arg;
}
} }
} }
else else
@ -140,19 +135,46 @@ public void ServeurLibre(Event event, const char[] name, bool dontBroadcast)
SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT); SocketConnect(socket, SocketEnvoieVictoire, OnSocketReceive, OnSocketDisconnected, MY_SOCKET_IP, MY_SOCKET_PORT);
} }
public void SocketUpdateScore(Handle socket, int t, int ct) public void UpdateScoreBoard(Event event, const char[] name, bool dontBroadcast)
{ {
char requestStr[512]; // request post avec les scores des 2 équipes
PrintToServer("Coucou, je souis là");
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);
//CloseHandle(socket);
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);
//CloseHandle(socket);
return Plugin_Handled;
}
public void SocketUpdateScore(Handle socket, any arg)
{
char requestStr[2048];
FormatEx( FormatEx(
requestStr, requestStr,
sizeof(requestStr), sizeof(requestStr),
"POST /api/csgo/updatescore HTTP/1.0\r\n\ "POST /api/csgo/updatescore HTTP/1.1\r\n\
Host: Serveur\r\n\ Content-Type: application/json\r\n\
Content-type: text/json\r\n\ Content-Length: 32\r\n\r\n\
{\"port\": %d, \"ct\": %d, \"t\": %d}", port, {\"port\": %d, \"ct\": %d, \"t\": %d}",
CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T) port, CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T)
); );
SocketSend(socket, requestStr, 512); SocketSend(socket, requestStr, 2048);
SocketDisconnect(socket);
CloseHandle(socket);
} }
public void SocketEnvoieVictoire(Handle socket, any arg) public void SocketEnvoieVictoire(Handle socket, any arg)
@ -162,10 +184,12 @@ public void SocketEnvoieVictoire(Handle socket, any arg)
requestStr, requestStr,
sizeof(requestStr), sizeof(requestStr),
"POST /api/csgo/matchend HTTP/1.0\r\n\ "POST /api/csgo/matchend HTTP/1.0\r\n\
Host: Serveur\r\n\
Content-type: text/json\r\n\ Content-type: text/json\r\n\
{\"serverport\": %s, \"available\": \"true\"}" {\"serverport\": 512, \"available\": true}", port
); );
SocketSend(socket, requestStr, 512);
SocketDisconnect(socket);
CloseHandle(socket);
} }
public void OnSocketConnected(Handle socket, any arg) public void OnSocketConnected(Handle socket, any arg)
@ -186,16 +210,21 @@ public void OnSocketConnected(Handle socket, any arg)
SocketSend(socket, requestStr, 150); SocketSend(socket, requestStr, 150);
} }
public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg) {} public void OnSocketReceive(Handle socket, const char[] datas, const int data, any arg)
{
PrintToServer("=== INFO ===\n%s", datas);
}
public OnSocketDisconnected(Handle socket, any arg) public OnSocketDisconnected(Handle socket, any arg)
{ {
PrintToServer("Salut mon pote");
CloseHandle(socket); CloseHandle(socket);
} }
public void OnSocketError(Handle socket, const int errorType, const int errorNum, any arg) public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) {
{ // a socket error occured
PrintToServer("[ERROR] Le socket a rencontré une erreur !");
CloseHandle(socket);
}
PrintToServer("socket error %d (errno %d)", errorType, errorNum);
CloseHandle(hFile);
CloseHandle(socket);
}