Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON Parsing Error for Ladder Top Retrieval #10745

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions server/chat-plugins/seasons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,19 @@ export function generateFormatSchedule() {
export async function getLadderTop(format: string) {
try {
const results = await Net(`https://${Config.routes.root}/ladder/?format=${toID(format)}&json`).get();
const reply = JSON.parse(results);
let reply;
try {
reply = JSON.parse(results);
} catch (parseError) {
Monitor.crashlog(parseError, "Invalid JSON response from ladder request");
return null;
}

// Check that toplist is defined and is an array
if (!reply || !Array.isArray(reply.toplist)) {
Monitor.crashlog(new Error(`Invalid toplist format: ${JSON.stringify(reply)}`), "Ladder toplist read error");
return null;
}
return reply.toplist;
} catch (e) {
Monitor.crashlog(e, "A season ladder request");
Expand All @@ -159,22 +171,41 @@ export async function updateBadgeholders() {
if (!data.badgeholders[period]) {
data.badgeholders[period] = {};
}
for (const formatName of data.formatSchedule[findPeriod()]) {

const currentPeriod = findPeriod();
const scheduledFormats = data.formatSchedule[currentPeriod];
if (!scheduledFormats) {
Monitor.crashlog(new Error(`No format schedule found for period ${currentPeriod}`), "Season badge update");
return;
}

for (const formatName of scheduledFormats) {
const formatid = `gen${Dex.gen}${formatName}`;
const response = await getLadderTop(formatid);
if (!response) continue; // ??

// Ensure response is an array

const newHolders: Record<string, string[]> = {};
for (const [i, row] of response.entries()) {
let badgeType = null;
if (!row) {
continue;
}
const userid = toID(row.userid);
if (!userid || userid.length > 18) {
continue; // skip if userid isn't readable
}

let badgeType: string | null = null;
for (const type in BADGE_THRESHOLDS) {
if ((i + 1) <= BADGE_THRESHOLDS[type]) {
badgeType = type;
break;
}
}

if (!badgeType) break;
if (!newHolders[badgeType]) newHolders[badgeType] = [];
newHolders[badgeType].push(row.userid);
newHolders[badgeType].push(userid);
}
data.badgeholders[period][formatid] = newHolders;
}
Expand Down
Loading