From 0a398222ae6c9b948faf08e5941e54bb7366c7bf Mon Sep 17 00:00:00 2001 From: jenz Date: Fri, 19 Jan 2024 12:13:49 +0100 Subject: [PATCH] first upload, simply divides racetimer points by 1000 and says its your level if you dont have a clantag --- RaceTimer/scripting/racetimer_rank.sp | 101 ++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 RaceTimer/scripting/racetimer_rank.sp diff --git a/RaceTimer/scripting/racetimer_rank.sp b/RaceTimer/scripting/racetimer_rank.sp new file mode 100644 index 00000000..b67ecd4c --- /dev/null +++ b/RaceTimer/scripting/racetimer_rank.sp @@ -0,0 +1,101 @@ +#include +#include +#include +#include + +public Plugin myinfo = +{ + name = "Racetimer Rank", + author = "jenz", + description = "Give racetimer points as a level clan tag to players", + version = "1.0", + url = "" +}; + +public void OnPluginStart() +{ + for(int client = 1; client <= MaxClients; client++) + { + if(IsClientInGame(client)) + { + OnClientPostAdminFilter(client); + } + } +} + +public void OnClientPostAdminFilter(int client) +{ + if(!IsClientAuthorized(client) || IsFakeClient(client) || IsClientSourceTV(client)) + return; + + if (!HasClanTag(client)) + { + check_client_racetimer_rank(client); + } +} + +public bool HasClanTag(int client) +{ + char sClanID[32]; + GetClientInfo(client, "cl_clanid", sClanID, sizeof(sClanID)); + return strlen(sClanID) > 1; //without a tag the len is 1. +} + +public void check_client_racetimer_rank(int client) +{ + char sSID[64]; + char sRequest[256]; + GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); + FormatEx(sRequest, sizeof(sRequest), "https://racebackend.unloze.com/racetimer_endpoints-1.0/api/timers/player/%s", sSID); + + Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest); + if (!hRequest || + !SteamWorks_SetHTTPCallbacks(hRequest, OnTransferComplete) || + !SteamWorks_SetHTTPRequestContextValue(hRequest, GetClientSerial(client)) || + !SteamWorks_SendHTTPRequest(hRequest)) + { + delete hRequest; + } +} + +public int OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode, int iSerial) +{ + int client = GetClientFromSerial(iSerial); + if (!client) //Player disconnected. + { + delete hRequest; + return 0; + } + + if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK) + { + delete hRequest; + LogError("Request-Error: %d", eStatusCode); + return 0; + } + + SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnTransferResponse, iSerial); + return 0; +} + +public int OnTransferResponse(char[] sData, int iSerial) +{ + int client = GetClientFromSerial(iSerial); + if (!client) //Player disconnected. + { + return 0; + } + + JSON_Object obj = json_decode(sData); //https://github.com/clugg/sm-json + + int I_player_points = obj.GetInt("PlayerPoints") //if no endpoint for steamID default value is -1 + if (I_player_points < 1000) + { + I_player_points = 1000; //setting level 1 + } + char tag[64]; + Format(tag, sizeof(tag), "[LVL %i]", I_player_points/1000); + CS_SetClientClanTag(client, tag); + json_cleanup_and_delete(obj); + return 0; +}