[F] Fix userbox

pull/96/head
Azalea 2024-12-17 20:09:48 -05:00
parent 073febe24a
commit 69ab9d96f7
3 changed files with 40 additions and 117 deletions

View File

@ -4,7 +4,6 @@
import { import {
UserBoxItemKind, UserBoxItemKind,
type AquaNetUser, type AquaNetUser,
type ChangeUserBoxReq,
} from "../libs/generalTypes"; } from "../libs/generalTypes";
import { USER, USERBOX } from "../libs/sdk"; import { USER, USERBOX } from "../libs/sdk";
import { t, ts } from "../libs/i18n"; import { t, ts } from "../libs/i18n";
@ -15,7 +14,6 @@
import Icon from "@iconify/svelte"; import Icon from "@iconify/svelte";
let user: AquaNetUser; let user: AquaNetUser;
let aimeId = "";
let loading = true; let loading = true;
let error = ""; let error = "";
let submitting = ""; let submitting = "";
@ -137,28 +135,20 @@
back: undefined, back: undefined,
} as Record<string, number | undefined>; } as Record<string, number | undefined>;
function submit(body: ChangeUserBoxReq, field: string) { function submit(obj: { field: string; value: string }) {
if (submitting) return; if (submitting) return
submitting = body.kind; submitting = obj.field
USERBOX.setUserBox(body) USERBOX.setUserBox(obj)
.then(() => { .then(() => changed = changed.filter((c) => c !== obj.field))
changed = changed.filter((c) => c !== field); .catch(e => error = e.message)
}) .finally(() => submitting = "")
.catch((e) => {
error = e.message;
submitting = "";
})
.finally(() => {
submitting = "";
});
} }
async function fetchData() { async function fetchData() {
const currentValues = await USERBOX.getProfile(aimeId).catch((e) => { const currentValues = await USERBOX.getProfile().catch((e) => {
loading = false; loading = false;
error = t("userbox.error.noprofile") error = t("userbox.error.noprofile")
return;
}); });
if(!currentValues) return; if(!currentValues) return;
@ -189,7 +179,7 @@
await Promise.all( await Promise.all(
userBoxItems.map(async (kind) => { userBoxItems.map(async (kind) => {
// Populate info about the items // Populate info about the items
return USERBOX.getUnlockedItems(aimeId, kind).then((items) => { return USERBOX.getUnlockedItems(kind).then((items) => {
switch (kind) { switch (kind) {
case UserBoxItemKind.nameplate: case UserBoxItemKind.nameplate:
// Add the item id and the label to the available options // Add the item id and the label to the available options
@ -291,65 +281,22 @@
loading = false; loading = false;
} }
const kindMap: Record<string, string> =
{ nameplate: "nameplateId", frame: "frameId", trophy: "trophyId", mapicon: "mapIconId", voice: "voiceId" }
const categories = ["wear", "head", "face", "skin", "item", "front", "back"]
function generateBodyFromKind( function generateBodyFromKind(
kind: kind: "frame" | "nameplate" | "trophy" | "mapicon" | "voice" | "wear" | "head" | "face" | "skin" | "item" | "front" | "back",
| "frame"
| "nameplate"
| "trophy"
| "mapicon"
| "voice"
| "wear"
| "head"
| "face"
| "skin"
| "item"
| "front"
| "back",
value: number, value: number,
aimeId: string, ) {
): ChangeUserBoxReq { if (kind in kindMap) return { field: kindMap[kind], value: `${value}` }
switch (kind) { return { field: "avatar", value: `${categories.indexOf(kind) + 1}:${value}` }
case "frame":
return { kind: "frame", frameId: value, aimeId };
case "nameplate":
return { kind: "plate", nameplateId: value, aimeId };
case "trophy":
return { kind: "trophy", trophyId: value, aimeId };
case "mapicon":
return { kind: "mapicon", mapiconid: value, aimeId };
case "voice":
return { kind: "sysvoice", voiceId: value, aimeId };
case "wear":
return { kind: "avatar", accId: value, category: 1, aimeId };
case "head":
return { kind: "avatar", accId: value, category: 2, aimeId };
case "face":
return { kind: "avatar", accId: value, category: 3, aimeId };
case "skin":
return { kind: "avatar", accId: value, category: 4, aimeId };
case "item":
return { kind: "avatar", accId: value, category: 5, aimeId };
case "front":
return { kind: "avatar", accId: value, category: 6, aimeId };
case "back":
return { kind: "avatar", accId: value, category: 7, aimeId };
}
} }
USER.me().then((u) => { USER.me().then(u => {
if (u) { if (!u) throw new Error(t("userbox.error.noprofile"))
user = u; user = u
const card = user.cards.length > 0 ? user.cards[0].luid : ""; fetchData()
}).catch((e) => { loading = false; error = e.message });
aimeId = card;
if (aimeId) {
fetchData();
} else {
loading = false;
}
}
});
</script> </script>
{#if !loading && !error} {#if !loading && !error}
@ -402,7 +349,7 @@
if (newValue === undefined) return; if (newValue === undefined) return;
submit(generateBodyFromKind(key, newValue, aimeId), key); submit(generateBodyFromKind(key, newValue));
}} }}
> >
{#if submitting === key} {#if submitting === key}

View File

@ -173,18 +173,4 @@ export enum UserBoxItemKind {
} }
// Define type only with the keys // Define type only with the keys
export type UserBoxItemKindStr = keyof typeof UserBoxItemKind; export type UserBoxItemKindStr = keyof typeof UserBoxItemKind
type ChangePlateReq = {kind:'plate', nameplateId:number}
type ChangeFrameReq = {kind:'frame', frameId:number}
type ChangeTrophyReq = {kind:'trophy',trophyId:number}
type ChangeMapIconReq = {kind:'mapicon',mapiconid:number}
type ChangeVoiceReq = {kind:'sysvoice',voiceId:number}
type ChangeAvatarReq = {
kind:'avatar',
accId:number,
category:number
}
export type ChangeUserBoxReq = {aimeId:string} & (ChangePlateReq | ChangeFrameReq | ChangeTrophyReq | ChangeMapIconReq | ChangeVoiceReq | ChangeAvatarReq);

View File

@ -8,7 +8,6 @@ import type {
TrendEntry, TrendEntry,
AquaNetUser, GameOption, AquaNetUser, GameOption,
UserBox, UserBox,
ChangeUserBoxReq,
UserBoxItemKind UserBoxItemKind
} from './generalTypes' } from './generalTypes'
import type { GameName } from './scoring' import type { GameName } from './scoring'
@ -94,6 +93,7 @@ export async function post(endpoint: string, params: Record<string, any> = {}, i
export async function get(endpoint: string, params:any,init?: RequestInitWithParams): Promise<any> { export async function get(endpoint: string, params:any,init?: RequestInitWithParams): Promise<any> {
// Add token if exists // Add token if exists
const token = localStorage.getItem('token') const token = localStorage.getItem('token')
if (token && !('token' in params)) params = { ...(params ?? {}), token }
if (init?.localCache) { if (init?.localCache) {
const cached = cache[endpoint + JSON.stringify(init)] const cached = cache[endpoint + JSON.stringify(init)]
@ -262,23 +262,13 @@ export const USER = {
} }
export const USERBOX = { export const USERBOX = {
getAimeId:(cardId:string):Promise<{luid:string}|null> =>realPost('/api/sega/aime/getByAccessCode',{ accessCode:cardId }), getProfile: (): Promise<UserBox> =>
getProfile:(aimeId:string):Promise<UserBox> =>get('/api/game/chuni/v2/profile',{ aimeId }), get('/api/v2/game/chu3/user-box', {}),
getUnlockedItems:(aimeId:string, itemId: UserBoxItemKind):Promise<{itemKind:number, itemId:number,stock:number,isValid:boolean}[]> => getUnlockedItems: (itemId: UserBoxItemKind): Promise<{ itemKind: number, itemId: number, stock: number, isValid: boolean }[]> =>
get(`/api/game/chuni/v2/item/${itemId}`,{ aimeId }), get(`/api/v2/game/chu3/user-box-item-by-kind`,{ itemId }),
getItemLabels:() => { getItemLabels: () => get(`/api/v2/game/chu3/user-box-all-items`, {}),
const kinds = [ 'nameplate', 'frame', 'trophy', 'mapicon', 'sysvoice', 'avatar' ] setUserBox: (d: { field: string, value: number | string }) =>
post(`/api/v2/game/chu3/user-detail-set`, d),
return Promise.all(kinds.map(it =>
get(`/api/game/chuni/v2/data/${it}`,{}).then((res:{id:number,name:string}[]) =>
// Use the id as the key
res.reduce((acc, cur) => ({ ...acc, [cur.id]: cur.name }), {}) as { [index: number]: string }
))).then(([ nameplate, frame, trophy, mapicon, sysvoice, avatar ]) => ({
nameplate, frame, trophy, mapicon, sysvoice, avatar
}))
},
setUserBox:({ kind,...body }:ChangeUserBoxReq) =>
put(`/api/game/chuni/v2/profile/${kind}`, body),
} }
export const CARD = { export const CARD = {