diff --git a/store/wsProtocol.ts b/store/wsProtocol.ts new file mode 100644 index 0000000..6237968 --- /dev/null +++ b/store/wsProtocol.ts @@ -0,0 +1,165 @@ +import { fetchDLCs, fetchItems, fetchOfferings, fetchAddons, fetchPerks } from '../lib/db'; +import { DLC } from '../lib/utils'; + +export interface SpooferConfig { + camperItems: Record; + camperAddons: Record; + slasherAddons: Record; + camperOfferings: Record; + slasherOfferings: Record; + globalOfferings: Record; + camperPerks: string[]; + slasherPerks: string[]; + catalogItemIds: string[]; + dlcListGRDK: string[]; + dlcListEGS: string[]; + dlcListSteam: string[]; +} + +export interface Toggles { + spoofItems: boolean; + spoofPerks: boolean; + spoofCatalog: boolean; + spoofDLCs: boolean; +} + +export async function mapStoreToSpooferConfig(state: { + unlockedCharacters: string[]; + unlockedCustomizations: string[]; + unlockedDLCs: string[]; + unlockedPerks: string[]; + items: Record; + offerings: Record; + addons: Record; +}): Promise { + const [dlcs, allItems, allOfferings, allAddons, allPerks] = await Promise.all([ + fetchDLCs(), + fetchItems(), + fetchOfferings(), + fetchAddons(), + fetchPerks() + ]); + + const dlcMap = new Map(); + dlcs.forEach((d: DLC) => dlcMap.set(d.id, d)); + + const itemIdSet = new Set(); + allItems.forEach((i: { id: string }) => itemIdSet.add(i.id)); + + const offeringMap = new Map(); + allOfferings.forEach((o: { id: string; role: number }) => offeringMap.set(o.id, o)); + + const addonMap = new Map(); + allAddons.forEach((a: { id: string; role: number }) => addonMap.set(a.id, a)); + + const perkMap = new Map(); + allPerks.forEach((p: { id: string; role: number }) => perkMap.set(p.id, p)); + + const dlcListSteam: string[] = []; + const dlcListEGS: string[] = []; + const dlcListGRDK: string[] = []; + + for (const id of state.unlockedDLCs) { + const dlc = dlcMap.get(id); + if (!dlc?.dlcIds) continue; + if (dlc.dlcIds.steam && dlc.dlcIds.steam !== '0' && dlc.dlcIds.steam !== '-1') + dlcListSteam.push(dlc.dlcIds.steam); + if (dlc.dlcIds.epic && dlc.dlcIds.epic !== 'FFFFFFFFFFFFFFFF' && dlc.dlcIds.epic !== '0') + dlcListEGS.push(dlc.dlcIds.epic); + if (dlc.dlcIds.grdk && dlc.dlcIds.grdk !== '9ZZZZZZZZZZZ' && dlc.dlcIds.grdk !== '0') + dlcListGRDK.push(dlc.dlcIds.grdk); + } + + const camperItems: Record = {}; + for (const [id, qty] of Object.entries(state.items)) + if (qty > 0 && itemIdSet.has(id)) camperItems[id] = qty; + + + const camperOfferings: Record = {}; + const slasherOfferings: Record = {}; + const globalOfferings: Record = {}; + + for (const [id, qty] of Object.entries(state.offerings)) { + if (qty <= 0) continue; + const offering = offeringMap.get(id); + if (!offering) continue; + if (offering.role === 2) camperOfferings[id] = qty; + else if (offering.role === 1) slasherOfferings[id] = qty; + else globalOfferings[id] = qty; + } + + const camperAddons: Record = {}; + const slasherAddons: Record = {}; + + for (const [id, qty] of Object.entries(state.addons)) { + if (qty <= 0) continue; + const addon = addonMap.get(id); + if (!addon) continue; + if (addon.role === 1) slasherAddons[id] = qty; + else camperAddons[id] = qty; + } + + const camperPerks: string[] = []; + const slasherPerks: string[] = []; + + for (const id of state.unlockedPerks) { + const perk = perkMap.get(id); + if (!perk) continue; + if (perk.role === 1) slasherPerks.push(id); + else camperPerks.push(id); + } + + return { + camperItems, + camperAddons, + slasherAddons, + camperOfferings, + slasherOfferings, + globalOfferings, + camperPerks, + slasherPerks, + catalogItemIds: [...state.unlockedCustomizations], + dlcListSteam, + dlcListEGS, + dlcListGRDK, + }; +} + +export async function mapSpooferConfigToStore(profile: SpooferConfig) { + const dlcs = await fetchDLCs(); + + const unlockedDLCs: string[] = []; + for (const dlc of dlcs as DLC[]) { + if (!dlc.dlcIds) continue; + const hasSteam = dlc.dlcIds.steam && profile.dlcListSteam?.includes(dlc.dlcIds.steam); + const hasEpic = dlc.dlcIds.epic && profile.dlcListEGS?.includes(dlc.dlcIds.epic); + const hasGrdk = dlc.dlcIds.grdk && profile.dlcListGRDK?.includes(dlc.dlcIds.grdk); + if (hasSteam || hasEpic || hasGrdk) unlockedDLCs.push(dlc.id); + } + + const offerings: Record = { + ...(profile.globalOfferings || {}), + ...(profile.camperOfferings || {}), + ...(profile.slasherOfferings || {}), + }; + + const addons: Record = { + ...(profile.camperAddons || {}), + ...(profile.slasherAddons || {}), + }; + + const unlockedPerks: string[] = [ + ...(profile.camperPerks || []), + ...(profile.slasherPerks || []), + ]; + + return { + unlockedCharacters: [] as string[], + unlockedCustomizations: profile.catalogItemIds || [], + unlockedDLCs, + unlockedPerks, + items: profile.camperItems || {}, + offerings, + addons, + }; +} \ No newline at end of file