From c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 15 Oct 2026 19:35:48 +0300 Subject: ui-web: add browser client for the Ex4 bonus A React + TypeScript single-page client served by Vite, working against the same servlets as the desktop client: login, the events screen with filters, details, order books and trading, and the account screen with deposit, holdings, ledger rows and the user directory. No file upload, as the exercise specifies. It looks like the desktop client on purpose: the three skins of theme.css as CSS variables, the four animations redone with SVG and keyframes, and the desktop's Nerd Fonts, which npm run dev copies from ui-fx instead of keeping a second copy in the repo. The shared chat sits in a drawer above the status bar, as on the desktop, on its own poll because chat lines don't move the market version. Vite forwards /guess-market to Tomcat on 8080, so the page and the API share an origin and the session cookie needs no CORS. The client polls /api/sync once a second and reloads only when the version moved. run-web.bat installs the packages and starts it on localhost:5173. package.sh stages the sources under build/dist/ex4, without node_modules. --- ui-web/src/api.ts | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 ui-web/src/api.ts (limited to 'ui-web/src/api.ts') diff --git a/ui-web/src/api.ts b/ui-web/src/api.ts new file mode 100644 index 0000000..2ce1a8d --- /dev/null +++ b/ui-web/src/api.ts @@ -0,0 +1,176 @@ + +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}' }; -- cgit v1.2.3