diff --git a/discord_verificiation/scripting/unloze_player_time.sp b/discord_verificiation/scripting/unloze_player_time.sp index f309433..224934c 100644 --- a/discord_verificiation/scripting/unloze_player_time.sp +++ b/discord_verificiation/scripting/unloze_player_time.sp @@ -29,6 +29,9 @@ StringMap g_GroupOverrides; // group name -> comma-separated override list static Handle g_hForwardPlayerHours; static Handle g_hForwardPlayerTier; +bool G_bResponsePassed[MAXPLAYERS+1]; +bool G_bPreAdminChecked[MAXPLAYERS+1]; + public Plugin myinfo = { name = "UNLOZE_player_time", @@ -336,20 +339,20 @@ public void SQL_OnDatabaseConnect(Database db, const char[] error, any data) { if(IsValidClient(i) && !IsFakeClient(i)) { - OnClientPostAdminCheck(i); + OnClientAuthorized(i, g_csSID[i]); } } OnMapStart(); } -public void OnClientPostAdminCheck(int client) +public void OnClientAuthorized(int client, const char[] sSteamID32) { g_iPlayerNextTierHours[client] = 0; GetClientAuthId(client, AuthId_Steam2, g_csSID[client], sizeof(g_csSID[])); is_bot_player[client] = false; g_iPlayerTier[client] = -1; g_iPlayerTimeServer[client] = 0; - if(!IsValidClient(client) || IsFakeClient(client)) + if(IsFakeClient(client)) return; if (!g_hDatabase) { @@ -433,128 +436,23 @@ public void SQL_OnQueryCompletedTimeServer(Database db, DBResultSet results, con Call_PushCell(iMinutes_Server); Call_Finish(); - if (g_iPlayerTier[client] == -1) + if (g_iPlayerTier[client] == -1) { SetPlayerTier(client); - if (g_iPlayerTier[client] > 0) + G_bResponsePassed[client] = true; + if (G_bPreAdminChecked[client]) { - WritePlayerTierToFile(client); //checks if all tiers have to be written + NotifyPostAdminCheck(client); } } - else if (check_if_client_new_tier(client)) //called every 10 minutes to fill new times from DB + else if (check_if_client_new_tier(client)) { SetPlayerTier(client); - WritePlayerTierToFile(client); //write only the new tier to the file at the end. - } -} - -public void WritePlayerTierToFile(int client) -{ - char sPath[PLATFORM_MAX_PATH]; - BuildPath(Path_SM, sPath, sizeof(sPath), "configs/admins_simple.ini"); - - // --- Read entire file into memory --- - File f = OpenFile(sPath, "r"); - if (!f) - { - LogError("WritePlayerTierToFile: failed to open %s for reading", sPath); - return; - } - - bool foundSeparator = false; - //always write the steamIDs as steamID 2 format. - char sSID[64]; - GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); - bool write_all_tiers = true; - bool write_newest_tier = false; - - char line[256]; - while (f.ReadLine(line, sizeof(line))) - { - int len = strlen(line); - while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) - line[--len] = '\0'; - - if (StrEqual(line, "//here begins the unloze_play_time plugin tiers", false)) - { - foundSeparator = true; - continue; - } - if (!foundSeparator) - { - continue; - } - - // Each line looks like: "STEAM_0:0:12345" "@tier3" - // Extract steam ID and tier number - char steamid[65]; - char group[64]; - - int pos = 1; //skip opening quote - int sidStart = pos; - while (line[pos] != '"') - { - pos++; - } - int sidLen = pos - sidStart; - strcopy(steamid, sidLen + 1, line[sidStart]); - if (!StrEqual(steamid, sSID)) - { - continue; - } - //if steam ID does not exist write all tiers - write_all_tiers = false; - write_newest_tier = true; - - pos++; // skip closing quote of steamid - while (line[pos] == ' ' || line[pos] == '\t') pos++; // skip whitespace - pos++; // skip opening quote of group - - int grpStart = pos; - while (line[pos] != '"') pos++; - int grpLen = pos - grpStart; - strcopy(group, grpLen + 1, line[grpStart]); - - // group is "@tier3", extract tier number - char tierStr[8]; - strcopy(tierStr, sizeof(tierStr), group[5]); // skip "@tier" - int tierNum = StringToInt(tierStr); - if (tierNum == g_iPlayerTier[client]) - { - write_newest_tier = false; - break; //we confirmed the most recent tier was already written here. - } - } - delete f; - - if (write_all_tiers) - { - // Open file for appending - File fAppend = OpenFile(sPath, "a"); - - for (int t = 1; t <= g_iPlayerTier[client]; t++) - { - char linewrite[256]; - Format(linewrite, sizeof(linewrite), "\"%s\" \"@tier%i\"\n", sSID, t); - fAppend.WriteString(linewrite, false); - } - delete fAppend; - SetTierRewards(client); - - } - else if (write_newest_tier) - { - // Open file for appending - File fAppend = OpenFile(sPath, "a"); - char linewrite[256]; - Format(linewrite, sizeof(linewrite), "\"%s\" \"@tier%i\"\n", sSID, g_iPlayerTier[client]); - fAppend.WriteString(linewrite, false); - delete fAppend; SetTierRewards(client); } } -public bool check_if_client_new_tier(int client) +public bool check_if_client_new_tier(int client) //called every 10 minutes to fill new times from DB { char sPath[PLATFORM_MAX_PATH]; BuildPath(Path_SM, sPath, sizeof(sPath), "configs/unloze_playt_time_tiers.cfg"); @@ -598,20 +496,6 @@ public bool check_if_client_new_tier(int client) return upgrade_tier; } -public void SetTierRewards(int client) -{ - if (g_iPlayerTier[client] < 1) - { - return; - } - for (int i = 1; i <= g_iPlayerTier[client]; i++) - { - char groupname[64]; - Format(groupname, sizeof(groupname), "tier%i", i); - AddClientToGroup(client, groupname); - } -} - public void AddClientToGroup(int client, const char[] groupName) { AdminId id = GetUserAdmin(client); @@ -680,9 +564,7 @@ public void SetPlayerTier(int client) if (g_iPlayerTimeServer[client] < next_hours) { g_iPlayerNextTierHours[client] = next_hours - g_iPlayerTimeServer[client]; - PrintToChat(client, "You need %i hours to reach next tier. Check your features with !sm_tier", g_iPlayerNextTierHours[client]); } - else delete kv; } @@ -694,6 +576,8 @@ public void OnClientDisconnect(int client) Format(g_csSID[client], sizeof(g_csSID[]), ""); is_bot_player[client] = false; g_iPlayerTimeServer[client] = 0; + G_bPreAdminChecked[client] = false; + G_bResponsePassed[client] = false; } public void insert_client(int client) @@ -991,3 +875,69 @@ public int Handler_Menu(Menu menu, MenuAction action, int param1, int param2) } return 0; } + +public void SetTierRewards(int client) +{ + if (!G_bResponsePassed[client]) + { + return; + } + + PrintToChat(client, "You need %i hours to reach next tier. Check your features with !sm_tier", g_iPlayerNextTierHours[client]); + if (g_iPlayerTier[client] < 1) + { + return; + } + for (int i = 1; i <= g_iPlayerTier[client]; i++) + { + char groupname[64]; + Format(groupname, sizeof(groupname), "tier%i", i); + AddClientToGroup(client, groupname); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action OnRebuildAdminCachePost(Handle hTimer) +{ + for (int client = 1; client <= MaxClients; client++) + { + if(G_bResponsePassed[client] && G_bPreAdminChecked[client]) + SetTierRewards(client); + } + return Plugin_Handled; +} + +public Action OnClientPreAdminCheck(int client) +{ + G_bPreAdminChecked[client] = true; + + if (G_bResponsePassed[client]) + { + return Plugin_Continue; + } + + RunAdminCacheChecks(client); + return Plugin_Handled; +} + +public void OnClientConnected(int client) +{ + G_bPreAdminChecked[client] = false; + G_bResponsePassed[client] = false; +} + +public void OnClientPostAdminFilter(int client) +{ + SetTierRewards(client); +} + +public void OnRebuildAdminCache(AdminCachePart part) +{ + if (part != AdminCache_Admins) + return; + + CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); +} +