Skip to content

Commit

Permalink
Merge pull request #2154 from DuckySoLucky/fixCoinsAPI
Browse files Browse the repository at this point in the history
fix: coins API endpoint
  • Loading branch information
metalcupcake5 authored Jan 9, 2024
2 parents 51f3cc2 + 34ceb79 commit a7fb548
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ app.all("/stats/:player/:profile?", async (req, res, next) => {
cacheOnly,
debugId,
updateLeaderboards: true,
updateGuild: true,
});

if (isFoolsDay) {
Expand Down
12 changes: 10 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export async function getStats(
allProfiles,
items,
packs,
options = { cacheOnly: false, debugId: `${helper.getClusterId()}/unknown@getStats`, updateLeaderboards: true }
options = {
cacheOnly: false,
debugId: `${helper.getClusterId()}/unknown@getStats`,
updateLeaderboards: false,
updateGuild: false,
}
) {
const output = {};

Expand Down Expand Up @@ -124,7 +129,9 @@ export async function getStats(
}

// fetches the guild and stores it in the database, on the front end it will be fetched from the database if the button is clicked
getGuild(db, profile.uuid, options);
if (options.updateGuild === true) {
getGuild(db, profile.uuid, options);
}

output.rank_prefix = helper.renderRank(hypixelProfile);
output.uuid = profile.uuid;
Expand Down Expand Up @@ -546,6 +553,7 @@ export async function getBingoProfile(
);
} catch (e) {
if (e?.response?.data?.cause === "No bingo data could be found") {
console.debug(`${options.debugId}: getBingoProfile returned. (${Date.now() - timeStarted}ms)`);
return null;
}

Expand Down
55 changes: 17 additions & 38 deletions src/routes/apiv2/coins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as lib from "../../lib.js";
import express from "express";

import { db } from "../../mongo.js";
import { getItems } from "../../stats.js";

const router = express.Router();

Expand All @@ -14,55 +13,35 @@ router.use((req, res, next) => {

router.get("/:player/:profile", async (req, res, next) => {
try {
const { profile, allProfiles } = await lib.getProfile(db, req.params.player, null, req.options);

let output = {
error: "Invalid Profile Name!",
};

for (const singleProfile of allProfiles) {
const cuteName = singleProfile.cute_name;

if (cuteName.toLowerCase() != req.params.profile.toLowerCase()) {
continue;
}

const items = await getItems(singleProfile.members[profile.uuid], false, "", req.options);
const data = await lib.getStats(db, singleProfile, allProfiles, items, req.options);

output = {
profile_id: singleProfile.profile_id,
cute_name: cuteName,
purse: data.purse,
bank: data.bank,
};
const { profile } = await lib.getProfile(db, req.params.player, req.params.profile, req.options);
if (profile.cute_name.toLowerCase() !== req.params.profile.toLowerCase()) {
throw new Error("Profile not found");
}

helper.sendMetric("endpoint_apiv2_coins_profile_success");
res.json(output);

res.json({
profile_id: profile.profile_id,
cute_name: profile.cute_name,
purse: profile.members[profile.uuid]?.currencies?.coin_purse ?? 0,
bank: profile.banking?.balance ?? 0,
});
} catch (e) {
next(e);
}
});

router.get("/:player", async (req, res, next) => {
try {
const { profile, allProfiles } = await lib.getProfile(db, req.params.player, null, req.options);
const bingoProfile = await lib.getBingoProfile(db, req.params.player, req.options);
const { allProfiles } = await lib.getProfile(db, req.params.player, null, req.options);

const output = { profiles: {} };

for (const singleProfile of allProfiles) {
const cuteName = singleProfile.cute_name;

const items = await getItems(singleProfile.members[profile.uuid], bingoProfile, false, "", req.options);
const data = await lib.getStats(db, singleProfile, bingoProfile, allProfiles, items, req.options);

output.profiles[singleProfile.profile_id] = {
profile_id: singleProfile.profile_id,
cute_name: cuteName,
purse: data.purse,
bank: data.bank,
for (const profile of allProfiles) {
output.profiles[profile.profile_id] = {
profile_id: profile.profile_id,
cute_name: profile.cute_name,
purse: profile.members[profile.uuid]?.currencies?.coin_purse ?? 0,
bank: profile.banking?.balance ?? 0,
};
}

Expand Down

0 comments on commit a7fb548

Please sign in to comment.