4f2e4e14ea
Server: - daily (/api/daily, /api/daily/claim) + shop (/api/shop/buy) + ChargeEntry - GameRoom (via IServiceScopeFactory) deducts ranked entry at match start and applies match rewards at match-over, broadcasting profile + reward over the hub - tested: daily, shop (owned-guard), ranked entry deduction pushed over hub Client: - SignalrService routes profile/coins/plan/daily/shop/match to the server (Bearer); onProfile/onReward hub events; guest/offline fall back to local - session-store syncs profile from hub; game-store serverReward; GameScreen shows live ranked reward from hub (no double submit), submits client-run games - single source of truth in live mode (no economy divergence) Postgres-ready via config (Provider=postgres); EnsureCreated for now. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import { create } from "zustand";
|
|
import { getService } from "./online/service";
|
|
import { AuthSession, UserProfile } from "./online/types";
|
|
|
|
interface SessionStore {
|
|
session: AuthSession | null;
|
|
profile: UserProfile | null;
|
|
loading: boolean;
|
|
isAuthed: boolean;
|
|
|
|
init: () => Promise<void>;
|
|
refreshProfile: () => Promise<void>;
|
|
setProfile: (p: UserProfile) => void;
|
|
|
|
requestOtp: (phone: string) => Promise<{ devCode?: string }>;
|
|
verifyOtp: (phone: string, code: string) => Promise<void>;
|
|
signInEmail: (email: string, password: string) => Promise<void>;
|
|
signUpEmail: (email: string, password: string, name: string) => Promise<void>;
|
|
signInGoogle: () => Promise<void>;
|
|
signOut: () => Promise<void>;
|
|
|
|
updateProfile: (
|
|
patch: Partial<Pick<UserProfile, "displayName" | "avatar" | "avatarImage" | "title" | "cardFront" | "cardBack">>
|
|
) => Promise<void>;
|
|
upgradePlan: () => Promise<void>;
|
|
}
|
|
|
|
export const useSessionStore = create<SessionStore>((set, get) => ({
|
|
session: null,
|
|
profile: null,
|
|
loading: true,
|
|
isAuthed: false,
|
|
|
|
init: async () => {
|
|
const svc = getService();
|
|
// keep the profile in sync with server-pushed updates (entry charge, reward…)
|
|
svc.onProfile((p) => set({ profile: p }));
|
|
const restored = await svc.restore();
|
|
if (restored) {
|
|
set({ session: restored.session, profile: restored.profile, isAuthed: true, loading: false });
|
|
} else {
|
|
// ensure a (guest) profile exists so the top bar can render
|
|
const profile = await svc.getProfile();
|
|
set({ profile, isAuthed: false, loading: false });
|
|
}
|
|
},
|
|
|
|
refreshProfile: async () => {
|
|
const profile = await getService().getProfile();
|
|
set({ profile });
|
|
},
|
|
|
|
setProfile: (p) => set({ profile: p }),
|
|
|
|
requestOtp: (phone) => getService().requestOtp(phone),
|
|
|
|
verifyOtp: async (phone, code) => {
|
|
const session = await getService().verifyOtp(phone, code);
|
|
const profile = await getService().getProfile();
|
|
set({ session, profile, isAuthed: true });
|
|
},
|
|
|
|
signInEmail: async (email, password) => {
|
|
const session = await getService().signInEmail(email, password);
|
|
const profile = await getService().getProfile();
|
|
set({ session, profile, isAuthed: true });
|
|
},
|
|
|
|
signUpEmail: async (email, password, name) => {
|
|
const session = await getService().signUpEmail(email, password, name);
|
|
const profile = await getService().getProfile();
|
|
set({ session, profile, isAuthed: true });
|
|
},
|
|
|
|
signInGoogle: async () => {
|
|
const session = await getService().signInGoogle();
|
|
const profile = await getService().getProfile();
|
|
set({ session, profile, isAuthed: true });
|
|
},
|
|
|
|
signOut: async () => {
|
|
await getService().signOut();
|
|
set({ session: null, isAuthed: false });
|
|
},
|
|
|
|
updateProfile: async (patch) => {
|
|
const profile = await getService().updateProfile(patch);
|
|
set({ profile });
|
|
},
|
|
|
|
upgradePlan: async () => {
|
|
const profile = await getService().upgradePlan();
|
|
set({ profile });
|
|
},
|
|
}));
|