From 789e10eac412ac8e3f927be947c13c28c71be0ab Mon Sep 17 00:00:00 2001 From: kvan7 Date: Mon, 9 Dec 2024 17:28:39 -0600 Subject: [PATCH] feat(Update to 2): Overrides leagues api call Overrides the leagues api call since it doesn't have an endpoint rn --- renderer/src/web/background/Leagues.ts | 177 ++++++++++++++----------- 1 file changed, 99 insertions(+), 78 deletions(-) diff --git a/renderer/src/web/background/Leagues.ts b/renderer/src/web/background/Leagues.ts index 79078175..7c1985a7 100644 --- a/renderer/src/web/background/Leagues.ts +++ b/renderer/src/web/background/Leagues.ts @@ -1,97 +1,118 @@ -import { computed, shallowRef, readonly } from 'vue' -import { createGlobalState } from '@vueuse/core' -import { AppConfig, poeWebApi } from '@/web/Config' -import { Host } from './IPC' +import { computed, shallowRef, readonly } from 'vue'; +import { createGlobalState } from '@vueuse/core'; +import { AppConfig, poeWebApi } from '@/web/Config'; +import { Host } from './IPC'; // pc-ggg, pc-garena // const PERMANENT_SC = ['Standard', '標準模式'] -const PERMANENT_HC = ['Hardcore', '專家模式'] +const PERMANENT_HC = ['Hardcore', '專家模式']; interface ApiLeague { - id: string - event?: boolean - rules: Array<{ id: string }> + id: string; + event?: boolean; + rules: Array<{ id: string }>; } +const DEFAULT_POE2_LEAGUES: ApiLeague[] = [ + { id: 'Standard', rules: [] }, + { + id: 'Hardcore', + rules: [ + { + id: 'Hardcore', + }, + ], + }, +]; + interface League { - id: string - isPopular: boolean + id: string; + isPopular: boolean; } export const useLeagues = createGlobalState(() => { - const isLoading = shallowRef(false) - const error = shallowRef(null) - const tradeLeagues = shallowRef([]) + const isLoading = shallowRef(false); + const error = shallowRef(null); + const tradeLeagues = shallowRef([]); - const selectedId = computed({ - get () { - return (tradeLeagues.value.length) - ? AppConfig().leagueId - : undefined - }, - set (id) { - AppConfig().leagueId = id - } - }) + const selectedId = computed({ + get() { + return tradeLeagues.value.length ? AppConfig().leagueId : undefined; + }, + set(id) { + AppConfig().leagueId = id; + }, + }); - const selected = computed(() => { - const { leagueId } = AppConfig() - if (!tradeLeagues.value || !leagueId) return undefined - const listed = tradeLeagues.value.find(league => league.id === leagueId) - return { - id: leagueId, - realm: AppConfig().realm, - isPopular: !isPrivateLeague(leagueId) && Boolean(listed?.isPopular) - } - }) + const selected = computed(() => { + const { leagueId } = AppConfig(); + if (!tradeLeagues.value || !leagueId) return undefined; + const listed = tradeLeagues.value.find((league) => league.id === leagueId); + return { + id: leagueId, + realm: AppConfig().realm, + isPopular: !isPrivateLeague(leagueId) && Boolean(listed?.isPopular), + }; + }); - async function load () { - isLoading.value = true - error.value = null + async function load() { + isLoading.value = true; + error.value = null; - try { - const response = await Host.proxy(`${poeWebApi()}/api/leagues?type=main&realm=pc`) - if (!response.ok) throw new Error(JSON.stringify(Object.fromEntries(response.headers))) - const leagues: ApiLeague[] = await response.json() - tradeLeagues.value = leagues - .filter(league => - !PERMANENT_HC.includes(league.id) && - !league.rules.some(rule => rule.id === 'NoParties' || - (rule.id === 'HardMode' && !league.event))) - .map(league => { - return { id: league.id, isPopular: true } - }) + try { + const response = await Host.proxy( + `${poeWebApi()}/api/leagues?type=main&realm=pc` + ); + if (!response.ok) + throw new Error(JSON.stringify(Object.fromEntries(response.headers))); + // const leagues: ApiLeague[] = await response.json(); + const leagues: ApiLeague[] = DEFAULT_POE2_LEAGUES; + tradeLeagues.value = leagues + .filter( + (league) => + !PERMANENT_HC.includes(league.id) && + !league.rules.some( + (rule) => + rule.id === 'NoParties' || + (rule.id === 'HardMode' && !league.event) + ) + ) + .map((league) => { + return { id: league.id, isPopular: true }; + }); - const leagueIsAlive = tradeLeagues.value.some(league => league.id === selectedId.value) - if (!leagueIsAlive && !isPrivateLeague(selectedId.value ?? '')) { - if (tradeLeagues.value.length > 1) { - const TMP_CHALLENGE = 1 - selectedId.value = tradeLeagues.value[TMP_CHALLENGE].id - } else { - const STANDARD = 0 - selectedId.value = tradeLeagues.value[STANDARD].id - } - } - } catch (e) { - error.value = (e as Error).message - } finally { - isLoading.value = false - } - } + const leagueIsAlive = tradeLeagues.value.some( + (league) => league.id === selectedId.value + ); + if (!leagueIsAlive && !isPrivateLeague(selectedId.value ?? '')) { + if (tradeLeagues.value.length > 1) { + const TMP_CHALLENGE = 1; + selectedId.value = tradeLeagues.value[TMP_CHALLENGE].id; + } else { + const STANDARD = 0; + selectedId.value = tradeLeagues.value[STANDARD].id; + } + } + } catch (e) { + error.value = (e as Error).message; + } finally { + isLoading.value = false; + } + } - return { - isLoading, - error, - selectedId, - selected, - list: readonly(tradeLeagues), - load - } -}) + return { + isLoading, + error, + selectedId, + selected, + list: readonly(tradeLeagues), + load, + }; +}); -function isPrivateLeague (id: string) { - if (id.includes('Ruthless')) { - return true - } - return /\(PL\d+\)$/.test(id) +function isPrivateLeague(id: string) { + if (id.includes('Ruthless')) { + return true; + } + return /\(PL\d+\)$/.test(id); }