export type Mechanism = 'LMSR' | 'ORDER_BOOK'; export type Status = 'DRAFT' | 'ACTIVE' | 'SETTLED'; export type FeeTiming = 'ON_PURCHASE' | 'ON_CLOSE'; export interface EventSummary { key: string; displayId: number; name: string; description: string; commissionPercent: number; commissionTiming: FeeTiming; mechanism: Mechanism; status: Status; optionNames: string[]; accountBalance: string; marketMaker: string; } export interface Market { key: string; name: string; price: string; volume: string; } export interface BookOrder { side: string; user: string; quantity: number; price: string; isBid: boolean; } export interface OrderBook { optionKey: string; optionName: string; bestBid: string | null; bestAsk: string | null; spread: string | null; orders: BookOrder[]; } export interface TradeRow { at: string; userName: string; optionName: string; quantity: string; pricePaid: string; } export interface Participant { user: string; tag: string; yes: number; no: number; value: string; fees: string; pnl: string; } export interface EventDetail { summary: EventSummary; state: { markets: Market[]; accountBalance: string; commissionCollected: string }; history: TradeRow[]; settledMarket: string; orderBooks: OrderBook[]; participants: Participant[]; } export interface LedgerEntry { seq: number; at: string; type: string; amount: string; balanceAfter: string; note: string; } export interface Ledger { owner: string; balance: string; blocked: boolean; entries: LedgerEntry[]; } export interface UserSummary { username: string; balance: string; marketMaker: boolean; online: boolean; } export interface Sync { changed: boolean; version: number; events: EventSummary[]; users: UserSummary[]; account: Ledger; } export interface ChatMessage { id: number; at: string; sender: string; text: string; } export interface MarketData { events: EventSummary[]; details: Record; users: UserSummary[]; account: Ledger; } export class ApiError extends Error { constructor( public status: number, message: string, ) { super(message); } } const BASE = '/guess-market/api'; const UNREACHABLE = 'Cannot reach the server. Is Tomcat running on port 8080?'; export async function api(path: string, form?: Record): Promise { let res: Response; try { res = await fetch( BASE + path, form ? { method: 'POST', body: new URLSearchParams(form) } : undefined, ); } catch { throw new ApiError(0, UNREACHABLE); } const text = await res.text(); let body: any = null; try { body = text ? JSON.parse(text) : null; } catch { } if (!res.ok) { throw new ApiError(res.status, body?.message ?? (res.status >= 500 ? UNREACHABLE : `The server answered ${res.status}.`)); } return body as T; } export const eventPath = (name: string) => '/events/' + encodeURIComponent(name); export function num(value: string | number | null | undefined): number { const n = typeof value === 'number' ? value : Number(String(value ?? '').replace(/[$,]/g, '')); return Number.isNaN(n) ? 0 : n; } export const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase(); export function money(value: string | number | null | undefined): string { if (value === null || value === undefined || value === '') return '—'; const n = typeof value === 'number' ? value : Number(String(value).replace(/[$,]/g, '')); if (Number.isNaN(n)) return String(value); return (n < 0 ? '-$' : '$') + Math.abs(n).toFixed(2); } export const mechanismLabel = (m: Mechanism) => (m === 'LMSR' ? 'LMSR' : 'Order Book'); export const CHEVRON = '\ue0b1'; export const SEP = '\ueb04'; export const optionTitle = (e: EventSummary, i: number) => `${i === 0 ? 'YES' : 'NO'} ${SEP} ${e.optionNames[i]}`; export const feeLabel = (e: EventSummary) => `${e.commissionPercent}% ${e.commissionTiming === 'ON_PURCHASE' ? 'on purchase' : 'on close'}`; export const GLYPH = { file: '\uf15b', bolt: '\uf0e7', users: '\uf0c0', chat: '\u{f0b79}' };