InvestWeb/pages/api/fakeUsers.ts

852 lines
17 KiB
TypeScript
Raw Normal View History

2023-01-20 05:38:23 -05:00
import type { NextApiRequest, NextApiResponse } from "next";
2023-01-28 04:46:11 -05:00
import UserBadge from "../../interfaces/UserBadge";
import UserFakeDataEntry from "../../interfaces/UserFakeDataEntry";
import UserJSONEntry from "../../interfaces/UserJSONEntry";
2023-01-20 05:38:23 -05:00
import { createRedisInstance } from "../../misc/redis";
import { getUserByName } from "../../misc/TwitchAPI";
import { fakePrices } from "./fakePrices";
type Data = {
[key: string]: any;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
const username = req.query.u ? (req.query.u as string) : undefined;
const sortBy = req.query.s ? (req.query.s as string) : undefined;
const sortAsc = req.query.a ? (req.query.a as string) : undefined;
const redis = createRedisInstance();
2023-01-22 20:25:04 -05:00
if (!redis) {
res.status(500).json({
error: { message: "Internal API is down", code: 50100 },
});
return;
}
2023-01-20 05:38:23 -05:00
2023-01-28 04:46:11 -05:00
let userJSON: UserJSONEntry[];
let userList: UserFakeDataEntry[] = fakeData;
let avatarURL = "/img/logo.webp"; // default avatar
2023-01-20 05:38:23 -05:00
// if username is specified, only return that user
if (username) {
2023-01-28 04:46:11 -05:00
// filter for user, add required types
userList = userList.filter((u) => u.name === username);
if (userList.length === 0) {
2023-01-20 05:38:23 -05:00
res
.status(404)
.json({ error: { message: "User not found", code: 20000 } });
return;
}
// get twitch data for user
2023-01-20 18:20:40 -05:00
let twitchData: { data: { [key: string]: any }[] };
try {
twitchData = await getUserByName(redis, username);
} catch (e) {
2023-01-20 23:06:18 -05:00
res.status(500).json({
error: { message: "Twitch or internal API is down", code: 10100 },
});
2023-01-20 18:20:40 -05:00
return;
}
2023-01-20 05:38:23 -05:00
// if data is empty, user does not exist
if (twitchData.data.length === 0) {
// temp who cares
twitchData.data[0] = {};
twitchData.data[0].profile_image_url = "/img/logo.webp";
2023-01-28 04:46:11 -05:00
} else {
avatarURL = twitchData.data[0].profile_image_url;
2023-01-20 05:38:23 -05:00
}
}
2023-01-28 04:46:11 -05:00
userJSON = userList.map((user) => {
return {
...user,
// calculate total assets held (shares)
shares: user.assets.reduce((a, b) => a + b.count, 0),
// sort users badges by priority
badges: (user.badges ?? []).sort((a, b) => b.priority - a.priority ?? 0),
avatar_url: avatarURL,
rank: 0,
// sort users assets by total value
assets: user.assets.sort(
(a, b) =>
(fakePrices[b.name] ?? 0) * b.count -
(fakePrices[a.name] ?? 0) * a.count
),
// calculate net worth
net_worth:
user.points +
user.assets.reduce(
(a, b) => a + b.count * (fakePrices[b.name] ?? 0),
0
),
};
});
// calculate ranking based on net worth
userJSON = userJSON.sort((a, b) => (b.net_worth ?? 0) - (a.net_worth ?? 0));
userJSON = userJSON.map((u, i) => {
return {
...u,
rank: i + 1,
};
});
2023-01-20 05:38:23 -05:00
if (sortBy) {
if (sortBy === "daily_change") {
2023-01-28 04:46:11 -05:00
userJSON = userJSON.sort((a, b) => b.daily_change - a.daily_change);
2023-01-20 05:38:23 -05:00
} else if (sortBy === "daily_change_percent") {
2023-01-28 04:46:11 -05:00
userJSON = userJSON.sort(
2023-01-20 05:38:23 -05:00
(a, b) => b.daily_change_percent - a.daily_change_percent
);
} else if (sortBy === "shares") {
2023-01-28 04:46:11 -05:00
userJSON = userJSON.sort((a, b) => (b.shares ?? 0) - (a.shares ?? 0));
2023-01-20 05:38:23 -05:00
} else if (sortBy === "points") {
2023-01-28 04:46:11 -05:00
userJSON = userJSON.sort((a, b) => b.points - a.points);
2023-01-20 05:38:23 -05:00
} else if (sortBy === "name") {
2023-01-28 04:46:11 -05:00
userJSON = userJSON.sort((a, b) => a.name.localeCompare(b.name));
2023-01-20 05:38:23 -05:00
}
if (sortAsc === "true") {
// slow but only needed for temporary fake data anyway
2023-01-28 04:46:11 -05:00
userJSON = userJSON.reverse();
2023-01-20 05:38:23 -05:00
}
}
// fake loading time
await new Promise((resolve) =>
setTimeout(resolve, 250 + Math.random() * 1000)
);
2023-01-28 04:46:11 -05:00
res.status(200).json({ data: userJSON });
2023-01-20 05:38:23 -05:00
}
2023-01-28 04:46:11 -05:00
const adminBadge: UserBadge = {
2023-01-20 05:38:23 -05:00
name: "Admin",
color: "#CC3333",
priority: 99999,
};
2023-01-28 04:46:11 -05:00
const CEOBadge: UserBadge = {
2023-01-20 05:38:23 -05:00
name: "CEO",
color: "#F97316",
priority: 100000,
};
2023-01-28 04:46:11 -05:00
const webDevBadge: UserBadge = {
2023-01-20 05:38:23 -05:00
name: "Web Dev",
color: "#a855f7",
priority: 50000,
};
2023-01-28 04:46:11 -05:00
const botDevBadge: UserBadge = {
2023-01-20 05:38:23 -05:00
name: "Bot Dev",
color: "#48b2f1",
priority: 50001,
};
2023-01-28 04:46:11 -05:00
const fakeData: UserFakeDataEntry[] = [
2023-01-20 05:38:23 -05:00
{
id: 4,
name: "3zachm",
points: 7420, // uninvested points
daily_change: -500,
daily_change_percent: -0.0498504486540378863409770687936,
assets: [
{
name: "JIGACHAD",
count: 420,
provider: "7tv",
},
{
name: "annykiss",
count: 8,
2023-01-21 05:36:29 -05:00
provider: "7tv",
2023-01-20 05:38:23 -05:00
},
{
name: "HUH",
count: 1,
2023-01-21 05:36:29 -05:00
provider: "7tv",
},
{
name: "annytfSigh",
count: 1,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
{
name: "GabeN",
count: 3,
2023-01-20 05:38:23 -05:00
provider: "bttv",
},
{
2023-01-21 05:36:29 -05:00
name: "widepeepoBlanket",
2023-01-20 05:38:23 -05:00
count: 1,
provider: "ffz",
},
{
name: "plink",
count: 1,
provider: "7tv",
},
],
badges: [adminBadge, webDevBadge],
},
{
id: 1,
name: "ModulatingForce",
points: 10020,
daily_change: 5420,
daily_change_percent: 0.0379259673920649359736897347981,
assets: [
{
name: "OhTheMiseryEverybodyWantsToBeMyEnemySpareTheSympathyEverybodyWantsToBeMyEnemy-y-y-y-y",
count: 39,
provider: "7tv",
},
{
name: "Clueless",
count: 727,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "AnnySilly",
count: 4,
provider: "bttv",
},
{
name: "annytfHeart",
count: 98,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
{
name: "Catge",
count: 4,
provider: "ffz",
},
2023-01-20 05:38:23 -05:00
],
badges: [adminBadge, botDevBadge],
},
{
id: 2,
name: "notohh",
points: 10020,
daily_change: 0,
daily_change_percent: 0,
assets: [
{
name: "YourMom",
count: 81,
provider: "7tv",
},
{
name: "CumTime",
count: 92,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "KissaVei",
count: 1,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "SNIFFA",
count: 1219,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "FeelsBirthdayMan",
count: 1,
provider: "ffz",
},
{
name: "annytfRave",
count: 5,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
badges: [adminBadge, botDevBadge],
},
{
id: 3,
name: "SecondSockSan",
points: 15020,
daily_change: -10432,
daily_change_percent: -0.06796312583471774324896576435715,
assets: [
{
name: "AYAYAjam",
count: 46,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "GabeN",
count: 3,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "ThisStream",
count: 210,
provider: "7tv",
},
{
name: "BAND",
count: 91,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfMelt",
count: 16,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
badges: [CEOBadge, adminBadge],
},
{
id: 0,
name: "mzntori",
points: 922022,
daily_change: 329444422,
daily_change_percent: 4.2932124926634939999741296760186,
assets: [
{
name: "peepoSnow",
count: 72,
provider: "7tv",
},
{
2023-01-21 05:36:29 -05:00
name: "annyHop",
count: 61,
provider: "bttv",
2023-01-20 05:38:23 -05:00
},
{
name: "annyExcitedHug",
count: 26,
provider: "7tv",
},
{
name: "AAAA",
count: 65,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "peepoWTF",
count: 60,
provider: "ffz",
},
{
name: "annytfAngy",
count: 90,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
badges: [adminBadge, botDevBadge],
},
{
id: 5,
name: "luckytohavefoundyou14252",
points: 423,
daily_change: 9,
daily_change_percent: 0.00112163509471585244267198404786,
assets: [
{
name: "HACKERMANS",
count: 59,
provider: "7tv",
},
{
name: "THIS",
count: 70,
provider: "7tv",
},
{
name: "lebronJAM",
count: 66,
provider: "7tv",
},
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 6,
name: "ZeroxZerich",
points: 88542,
daily_change: -10219,
daily_change_percent: -0.01213384153219582279533121979601,
assets: [
{
name: "WeebRun",
count: 10,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annySaur",
count: 7,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "BAND",
count: 49,
provider: "7tv",
},
{
name: "SNIFFA",
count: 78,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "PepegaPhone",
count: 142,
provider: "ffz",
},
{
name: "annytfHug",
count: 19,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 7,
name: "joeeyo",
points: 99979,
daily_change: 1,
daily_change_percent: 0.0000001,
assets: [
{
name: "Siti",
count: 32,
provider: "7tv",
},
{
2023-01-21 05:36:29 -05:00
name: "annytfLUL",
count: 9,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-20 05:38:23 -05:00
},
{
name: "peepoSnow",
count: 37,
provider: "7tv",
},
{
name: "MadgeJuice",
count: 70,
provider: "7tv",
},
{
2023-01-21 05:36:29 -05:00
name: "annyBlankies",
count: 88,
provider: "bttv",
},
{
name: "TWINGO",
count: 98,
provider: "ffz",
2023-01-20 05:38:23 -05:00
},
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 8,
name: "dd_maru",
points: 208421,
daily_change: 85192,
daily_change_percent: 0.00824830823607984221402284047097,
assets: [
{
name: "BocchiPossessed",
count: 56,
provider: "7tv",
},
{
name: "toffeeConfused",
count: 64,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfBanana",
count: 15,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "ewLeague",
count: 64,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfPain",
count: 37,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 9,
name: "Goldeneye128",
points: 6521,
daily_change: -1942,
daily_change_percent: -0.03331503465312564331297605160228,
assets: [
{
name: "PagMan",
count: 52,
provider: "7tv",
},
{
name: "CapybaraStare",
count: 47,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "GabeN",
count: 52,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "GroupWankge",
count: 38,
provider: "7tv",
},
{
name: "annyCucumber",
count: 90,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfKnuckles",
count: 2,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 10,
name: "lilpastatv",
points: 40,
daily_change: 921821,
daily_change_percent: 0.12577857662228222197571019682439,
assets: [
{
name: "Wigglecat",
count: 205,
provider: "7tv",
},
{
name: "guraWink",
count: 5,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyPls",
count: 5,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "golive",
count: 46,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "COPIUM",
count: 82,
provider: "ffz",
},
{
name: "annytfCheer",
count: 54,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 11,
name: "domiswitch",
points: 5002,
daily_change: 2429,
daily_change_percent: 0.05610995610995610995610995610996,
assets: [
{
name: "peepoFlute",
count: 81,
provider: "7tv",
},
{
name: "WhoAsked",
count: 44,
provider: "7tv",
},
{
name: "pL",
count: 24,
provider: "7tv",
},
{
name: "peepoSnow",
count: 13,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfBlink",
count: 10,
provider: "bttv",
},
{
name: "annytfBonk",
count: 77,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 12,
name: "minosura",
points: 32901,
daily_change: 94821,
daily_change_percent: 0.10485244291894091524314186887943,
assets: [
{
name: "Okayeg",
count: 100,
provider: "7tv",
},
{
name: "burh",
count: 100,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyHop",
count: 16,
provider: "bttv",
},
{
name: "AndKnuckles",
count: 17,
provider: "ffz",
},
2023-01-20 05:38:23 -05:00
{
name: "yoshiJAM",
count: 67,
provider: "7tv",
},
{
name: "WhoAsked",
count: 59,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfSit",
count: 53,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 13,
name: "scienceteam_member",
points: 958,
daily_change: -7964,
daily_change_percent: -0.22823408035765461110792686421734,
assets: [
{
name: "LULE",
count: 43,
provider: "7tv",
},
{
name: "Madgeclap",
count: 82,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyDFast",
count: 22,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "PeepoKittyHug",
count: 7,
provider: "7tv",
},
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 14,
name: "witchdev",
points: 8532,
daily_change: -421,
daily_change_percent: -0.0000044605531984433792422085896,
assets: [
{
name: "SNIFFA",
count: 76,
provider: "7tv",
},
{
name: "annyCD",
count: 62,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyBlankies",
count: 74,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "anyatf",
count: 24,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfGamba",
count: 32,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 15,
name: "justone123879",
points: 86333,
daily_change: 53289,
daily_change_percent: 0.00599485461051669551653183334284,
assets: [
{
name: "Homi",
count: 9,
provider: "7tv",
},
{
name: "wideVIBE",
count: 61,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "Annie",
count: 24,
provider: "bttv",
},
2023-01-20 05:38:23 -05:00
{
name: "Lagging",
count: 92,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annytfFlower",
count: 33,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 16,
name: "marcelr_",
points: 39291,
daily_change: 1329,
daily_change_percent: 0.00331976948959480827019776234047,
assets: [
{
name: "peepoStuck",
count: 91,
provider: "7tv",
},
{
name: "PokiShare",
count: 13,
provider: "7tv",
},
{
name: "VeryBased",
count: 7,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyHopper",
count: 24,
provider: "bttv",
},
{
name: "annytfFlower",
count: 79,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
{
id: 17,
name: "fossabot",
points: 0,
daily_change: -31042,
daily_change_percent: -1.5517120719820044988752811797051,
assets: [
{
name: "catbaby",
count: 41,
provider: "7tv",
},
{
name: "peepoCat",
count: 41,
provider: "7tv",
},
{
name: "plink",
count: 32,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "AngelThump",
count: 41,
provider: "bttv",
},
{
name: "annytfSad",
count: 2,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 05:38:23 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 05:38:23 -05:00
},
2023-01-20 23:06:18 -05:00
{
id: 18,
name: "Headdesking1",
points: 429,
daily_change: 0,
daily_change_percent: 0,
assets: [
{
name: "anyaPls",
count: 92,
provider: "7tv",
},
{
name: "toffeeDinkDonk",
count: 6,
provider: "7tv",
},
{
name: "SoCute",
count: 99,
provider: "7tv",
},
2023-01-21 05:36:29 -05:00
{
name: "annyBlankies",
count: 42,
provider: "bttv",
},
{
name: "annytfHeart",
count: 63,
2023-01-23 03:36:04 -05:00
provider: "twitch",
2023-01-21 05:36:29 -05:00
},
2023-01-20 23:06:18 -05:00
],
2023-01-28 04:46:11 -05:00
badges: [],
2023-01-20 23:06:18 -05:00
},
2023-01-20 05:38:23 -05:00
];