made toplvl into own plugin, had bs problem with response size. added endpoint to mitigate problem

This commit is contained in:
jenz 2024-01-27 15:58:29 +01:00
parent 31e0568705
commit 50cff40f7c
5 changed files with 176 additions and 9 deletions

View File

@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DTO;
import entity.Player;
import entity.UrlBanners;
import static facade.Facade.convertSteamIdToCommunityId;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author install1
*/
public class PlayerDTOMini {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPlayerPoints() {
return PlayerPoints;
}
public void setPlayerPoints(int PlayerPoints) {
this.PlayerPoints = PlayerPoints;
}
private int PlayerPoints;
public PlayerDTOMini(String name, int PlayerPoints) {
this.name = name;
this.PlayerPoints = PlayerPoints;
}
}

View File

@ -5,6 +5,7 @@ import DTO.MapBoardDTO;
import DTO.MapBoardDTO2;
import DTO.PlayerMapBoardDTO;
import DTO.PlayerDTO;
import DTO.PlayerDTOMini;
import DataMapper.DataMapperCalls;
import com.google.common.collect.MapMaker;
import entity.MapBoard;
@ -228,7 +229,7 @@ public class Facade {
}
return new PlayerDTO(playerCache.get(steamid));
}
public Collection<PlayerDTO> getleaderBoard(int iterator) {
int playerAddiditionCap = 99;
Collection<Player> playerAvatarsCaching = new ArrayList();
@ -261,6 +262,22 @@ public class Facade {
return playersDTO;
}
public Collection<PlayerDTOMini> getleaderBoardMinified(int iterator) {
int playerAddiditionCap = 99;
Collection<PlayerDTOMini> playersDTO = new ArrayList();
int counter = 0;
for (Player player : playerCache.values()) {
if (counter >= iterator && counter <= iterator + playerAddiditionCap) {
playersDTO.add(new PlayerDTOMini(player.getName(), player.getPlayerPoints()));
}
if (counter > iterator + playerAddiditionCap) {
break;
}
counter++;
}
return playersDTO;
}
private ConcurrentMap<String, Player> addNewPlayerValuesToCache(Collection<Player> keyCollection, ConcurrentMap<String, Collection<Integer>> playerUrlBanners) {
//2 = user, 6 = mapper, 7 = admin, 8 = technical staff, 10 = leader, 12 = vip, 13 = trial admin, 15 = event winner, 19 = discord manager
// 20 NSFW, 21 = Retired admin, 25 = event manager, 11 = GA

View File

@ -31,6 +31,14 @@ public class TimerResource {
facade.checkTimeElapse();
return gson.toJson(facade.getleaderBoard(iterator));
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("leaderboard/minified/{iterator}")
public String retrieveLeaderBoardMinified(@PathParam("iterator") int iterator) {
facade.checkTimeElapse();
return gson.toJson(facade.getleaderBoardMinified(iterator));
}
@GET
@Produces(MediaType.APPLICATION_JSON)

View File

@ -24,14 +24,6 @@ public void OnPluginStart()
Database.Connect(SQL_OnDatabaseConnect, "racetimercss");
return;
}
for(int client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client))
{
OnClientPostAdminFilter(client);
}
}
}
public void OnMapStart()

View File

@ -0,0 +1,103 @@
#include <sourcemod>
#include <SteamWorks>
#include <json>
#include <cstrike>
public Plugin myinfo =
{
name = "Toplvl",
author = "jenz",
description = "show toplvl for racetimer",
version = "1.0",
url = ""
};
char g_cTimeRecords[100][128];
public void OnPluginStart()
{
RegConsoleCmd("sm_toplvl", Command_topLVL, "Displays top 100 players with highest level");
OnMapStart();
}
public int OnTransferResponse(char[] sData)
{
JSON_Object obj = json_decode(sData); //https://github.com/clugg/sm-json
int counter = 0;
while (counter < 100)
{
char s_counter[3];
IntToString(counter, s_counter, sizeof(s_counter))
JSON_Object position_obj = obj.GetObject(s_counter);
int I_player_points = position_obj.GetInt("PlayerPoints");
char player_name[64];
position_obj.GetString("name", player_name, sizeof(player_name));
Format(g_cTimeRecords[counter], sizeof(g_cTimeRecords[]), "%s LVL: %i", player_name, I_player_points/1000);
counter++;
json_cleanup_and_delete(position_obj);
}
json_cleanup_and_delete(obj);
return 0;
}
public int OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode)
{
if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
{
delete hRequest;
LogError("Request-Error: %d", eStatusCode);
return 0;
}
SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnTransferResponse);
return 0;
}
public void OnMapStart()
{
char sRequest[256];
//fucking bitch ass nigger shit was not saying anything about the http response being too large for heap in sourcepawn. instead of
//saying something or throwing an error message the shit just kept failing sillently and it took me fucking 1-2 days before i realized
//its because of the fucking json response being too large for sourcemod heap or something shit. fucking shit.
FormatEx(sRequest, sizeof(sRequest), "https://racebackend.unloze.com/racetimer_endpoints-1.0/api/timers/leaderboard/minified/0");
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest);
if (!hRequest ||
!SteamWorks_SetHTTPCallbacks(hRequest, OnTransferComplete) ||
!SteamWorks_SetHTTPRequestContextValue(hRequest, 5) ||
!SteamWorks_SendHTTPRequest(hRequest))
{
delete hRequest;
}
}
public int MenuHandler1(Menu menu, MenuAction action, int param1, int param2)
{
switch(action)
{
case MenuAction_End:
{
delete menu;
}
}
return 0;
}
public Action Command_topLVL(int client, int args)
{
char sTitle[64];
Format(sTitle, sizeof(sTitle), "[UNLOZE RaceTimer] Top 100 highest levels");
Menu menu = new Menu(MenuHandler1);
menu.SetTitle(sTitle);
for (int i = 0; i < sizeof(g_cTimeRecords); i++)
{
menu.AddItem("-1", g_cTimeRecords[i], ITEMDRAW_DISABLED);
}
menu.ExitButton = true;
menu.Display(client, 0);
return Plugin_Handled;
}