diff --git a/test.sp b/test.sp index 7afb5da..3c9d6f9 100644 --- a/test.sp +++ b/test.sp @@ -5,6 +5,7 @@ #include "socket.inc" #define MY_SOCKET_IP "localhost" +//#define MY_SOCKET_IP "127.0.0.1" #define MY_SOCKET_PORT 3500 #define STEAMID_LENGTH 128 @@ -28,9 +29,12 @@ public Plugin myinfo = public void OnPluginStart() { HookEvent("cs_win_panel_match", ServeurLibre); - HookEvent("player_activate", SuperTest); + HookEvent("player_activate", SetTeamPlayer); + HookEvent("cs_win_panel_round", UpdateScoreBoard); RegServerCmd("!team", SetTeam); + RegServerCmd("!tests", TestSocket); + RegServerCmd("!testsA", TestSocketA); PrintToServer("Hello world!"); } @@ -38,13 +42,11 @@ public void OnPluginStart() public void OnConfigsExecuted() { if (port < 0) - { port = GetConVarInt(FindConVar("hostport")); - } } //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")); GetClientAuthId(client, AuthId_Steam2, clientId, sizeof(clientId)); @@ -59,7 +61,7 @@ public void SuperTest(Event event, const char[] name, bool dontBroadcast) if (allGood) { allGood = false; - int i = 1; + int i = 0; while (!allGood && i < sizeof(teamCT)) { if (!strcmp(teamCT[i], clientId, false)) @@ -95,26 +97,19 @@ public Action SetTeam(int args) 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 @@ -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); } -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( requestStr, sizeof(requestStr), - "POST /api/csgo/updatescore HTTP/1.0\r\n\ - Host: Serveur\r\n\ - Content-type: text/json\r\n\ - {\"port\": %d, \"ct\": %d, \"t\": %d}", port, - CS_GetTeamScore(CS_TEAM_CT), CS_GetTeamScore(CS_TEAM_T) + "POST /api/csgo/updatescore HTTP/1.1\r\n\ + Content-Type: application/json\r\n\ + Content-Length: 32\r\n\r\n\ + {\"port\": %d, \"ct\": %d, \"t\": %d}", + 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) @@ -162,10 +184,12 @@ public void SocketEnvoieVictoire(Handle socket, any arg) requestStr, sizeof(requestStr), "POST /api/csgo/matchend HTTP/1.0\r\n\ - Host: Serveur\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) @@ -186,16 +210,21 @@ public void OnSocketConnected(Handle socket, any arg) 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) { + PrintToServer("Salut mon pote"); CloseHandle(socket); } -public void OnSocketError(Handle socket, const int errorType, const int errorNum, any arg) -{ - PrintToServer("[ERROR] Le socket a rencontré une erreur !"); - CloseHandle(socket); -} +public void OnSocketError(Socket socket, const int errorType, const int errorNum, any hFile) { + // a socket error occured + PrintToServer("socket error %d (errno %d)", errorType, errorNum); + CloseHandle(hFile); + CloseHandle(socket); +}