feat: marketplace, économie à crédits, perks temps réel & pubs réelles
Transforme XIP en réseau social satirique complet : monnaie fictive, marketplace, cosmétiques visibles de tous, messages riches sandboxés, pubs pilotées par les données, et tous les compteurs mock rendus réels. Backend (Bun + Hono + Prisma + Redis) - Économie par IP : modèles Wallet/Purchase/Entitlement, lib/wallet.ts avec spend() atomique (point unique du paywall) + recharge gratuite. - isLocalhost() → mode gratuit (README « si localhost: pas de paywall »). - Marketplace : lib/catalog.ts (achat transactionnel, stock limité, limites par IP) + routes/shop.ts ; 10 produits seedés (idempotent). - Perks : lib/perks.ts (cache Redis busté à l'achat) ; authorPerks injecté dans les payloads messages + endpoint batch /api/perks ; frame WS « perks » global pour MAJ live des messages déjà affichés. - Messages riches : Message.richMode/richContent, gating par entitlement. - Pubs réelles : modèle Ad seedé avec les 4 pubs (ex-hardcodées), rotation par API, comptage d'impressions réel + réconciliation. - WebSocket : IP capturée par connexion → broadcastToIp / broadcast ; frames wallet/perks/ads/alert. - Pièces jointes : lib/storage.ts (UUID, jamais exécuté) + routes/uploads.ts (limite 1 Mo sauf déblocage/localhost, Content-Disposition: attachment). - Alerte audio : routes/alert.ts (cooldown serveur Redis NX, clamp durée). - Compteur « argent extorqué » réel : impressions×CPM + crédits dépensés. Frontend (Vue 3 + Vite) - /shop : ShopPage + ProductCard fidèles aux maquettes ; composables useWallet/useShop/usePerks/useAds/useAttachments/useAlert. - UI de réponse (bannière + sous-threads), solde + lien Shop dans le header. - Perks rendus : Style Doré (or), Pets autour de l'IP, NoAds masque les pubs. - RichContent.vue : iframe sandbox verrouillée (htmlcss sans script ; js allow-scripts seul, jamais allow-same-origin) + CSP. - AdBand/InlineCasinoAd pilotés par l'API ; barre de saisie avec 📎, compteur de caractères, composer riche et bouton alerte. Infra - Migration economy_ads_attachments_rich ; seed idempotent (produits+pubs). - vite.config : usePolling (HMR fiable sur /mnt/c via WSL). - backend/.gitignore : uploads/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
72
frontend/src/composables/useWallet.ts
Normal file
72
frontend/src/composables/useWallet.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
/**
|
||||
* Wallet store (module-level singleton so the header, shop, and composer all
|
||||
* share one balance). Credits are CENTI-CREDITS server-side; `displayBalance`
|
||||
* converts to a human "crédits" number. Live updates arrive via the WS `wallet`
|
||||
* frame, routed here through useMessages' realtime hook (applyWalletFrame).
|
||||
*/
|
||||
|
||||
export interface WalletView {
|
||||
ip: string;
|
||||
balance: number; // centi-credits, or a huge sentinel in free mode
|
||||
freeMode: boolean;
|
||||
}
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:3000';
|
||||
|
||||
const ip = ref<string>('');
|
||||
const balanceRaw = ref<number>(0); // centi-credits
|
||||
const freeMode = ref<boolean>(false);
|
||||
const loaded = ref<boolean>(false);
|
||||
|
||||
function apply(view: WalletView): void {
|
||||
ip.value = view.ip;
|
||||
balanceRaw.value = view.balance;
|
||||
freeMode.value = view.freeMode;
|
||||
loaded.value = true;
|
||||
}
|
||||
|
||||
/** Called by the realtime `wallet` frame handler. */
|
||||
export function applyWalletFrame(data: WalletView): void {
|
||||
apply(data);
|
||||
}
|
||||
|
||||
async function fetchWallet(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/wallet`);
|
||||
if (res.ok) apply((await res.json()) as WalletView);
|
||||
} catch {
|
||||
/* offline — keep last known */
|
||||
}
|
||||
}
|
||||
|
||||
async function topUp(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/wallet/topup`, { method: 'POST' });
|
||||
if (res.ok) apply((await res.json()) as WalletView);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/** Human-readable balance ("∞" in free mode, else credits with 2 decimals). */
|
||||
function displayBalance(): string {
|
||||
if (freeMode.value) return '∞';
|
||||
return (balanceRaw.value / 100).toLocaleString('fr-FR', {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
}
|
||||
|
||||
export function useWallet() {
|
||||
return {
|
||||
ip,
|
||||
balanceRaw,
|
||||
freeMode,
|
||||
loaded,
|
||||
fetchWallet,
|
||||
topUp,
|
||||
displayBalance,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user