| 1 | |
| 2 | |
| 3 | |
| 4 | import type { Card, Suit } from './deskpark/PlayingCard'; |
| 5 | |
| 6 | export const ORDER = '3456789TJQKA2BR'; |
| 7 | const RANK: Record<string, number> = { T: 10, J: 11, Q: 12, K: 13, A: 14 }; |
| 8 | const CHAR: Record<number, string> = { 10: 'T', 11: 'J', 12: 'Q', 13: 'K', 14: 'A', 16: 'B', 17: 'R' }; |
| 9 | |
| 10 | export function card(s: string): Card { |
| 11 | if (s === 'BJ' || s === 'RJ') return { id: s === 'BJ' ? 116 : 117, suit: 'J', rank: s === 'BJ' ? 16 : 17 }; |
| 12 | const rank = RANK[s.slice(1)] ?? Number(s.slice(1)); |
| 13 | return { id: 'SHDC'.indexOf(s[0]) * 20 + rank, suit: s[0] as Suit, rank }; |
| 14 | } |
| 15 | |
| 16 | export const rankChar = (c: Card) => CHAR[c.rank] ?? String(c.rank); |
| 17 | const value = (c: Card) => ORDER.indexOf(rankChar(c)); |
| 18 | |
| 19 | |
| 20 | export const sortHand = (cards: Card[]) => [...cards].sort((a, b) => value(b) - value(a) || a.id - b.id); |
| 21 | |
| 22 | |
| 23 | export const moveOf = (cards: Card[]) => |
| 24 | cards |
| 25 | .map(rankChar) |
| 26 | .sort((a, b) => ORDER.indexOf(a) - ORDER.indexOf(b)) |
| 27 | .join(''); |
| 28 | |
| 29 | export const isBomb = (m: string) => m === 'BR' || (m.length === 4 && new Set(m).size === 1); |
| 30 | |
| 31 | |
| 32 | export function cardsFor(hand: Card[], move: string): Card[] { |
| 33 | const left = [...hand]; |
| 34 | const out: Card[] = []; |
| 35 | for (const r of move) { |
| 36 | const i = left.findIndex((c) => rankChar(c) === r); |
| 37 | if (i >= 0) out.push(...left.splice(i, 1)); |
| 38 | } |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | export type Move = null | 'pass' | Card[]; |
| 43 | |
| 44 | export interface View { |
| 45 | names: string[]; |
| 46 | bots: boolean[]; |
| 47 | landlord: number | null; |
| 48 | bottom: Card[]; |
| 49 | hands: (Card[] | null)[]; |
| 50 | counts: number[]; |
| 51 | |
| 52 | moves: Move[]; |
| 53 | turn: number | null; |
| 54 | played: Card[]; |
| 55 | bombs: number; |
| 56 | step: number; |
| 57 | |
| 58 | fx: string | null; |
| 59 | lastBy: number; |
| 60 | result: null | { landlordWon: boolean | null; points: number[]; spring: boolean }; |
| 61 | } |
| 62 | |
| 63 | export function blank(): View { |
| 64 | return { |
| 65 | names: ['player 0', 'player 1', 'player 2'], |
| 66 | bots: [false, false, false], |
| 67 | landlord: null, |
| 68 | bottom: [], |
| 69 | hands: [null, null, null], |
| 70 | counts: [0, 0, 0], |
| 71 | moves: [null, null, null], |
| 72 | turn: null, |
| 73 | played: [], |
| 74 | bombs: 0, |
| 75 | step: 0, |
| 76 | fx: null, |
| 77 | lastBy: -1, |
| 78 | result: null, |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | function apply(v: View, seat: number, cards: Card[] | null, move: string) { |
| 84 | if (v.lastBy === seat) v.moves = [null, null, null]; |
| 85 | v.moves[seat] = cards ?? 'pass'; |
| 86 | v.fx = null; |
| 87 | if (cards) { |
| 88 | v.lastBy = seat; |
| 89 | v.counts[seat] -= cards.length; |
| 90 | v.played.push(...cards); |
| 91 | if (isBomb(move)) { |
| 92 | v.bombs++; |
| 93 | v.fx = move === 'BR' ? '王炸' : '炸弹'; |
| 94 | } |
| 95 | } |
| 96 | v.step++; |
| 97 | v.turn = (seat + 1) % 3; |
| 98 | } |
| 99 | |
| 100 | export interface Replay { |
| 101 | initHands: string[]; |
| 102 | landlordCards: string; |
| 103 | bottom?: string[]; |
| 104 | moveHistory: { playerIdx: number; move: string }[]; |
| 105 | landlordWon: boolean | null; |
| 106 | points: number[]; |
| 107 | spring: boolean; |
| 108 | playerInfo?: { name: string }[]; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | export function replayView(R: Replay, t: number): View { |
| 113 | const v = blank(); |
| 114 | if (R.playerInfo) v.names = R.playerInfo.map((p) => p.name); |
| 115 | |
| 116 | |
| 117 | const hands = R.initHands.map((h) => h.split(' ').map(card)); |
| 118 | v.landlord = 0; |
| 119 | v.bottom = R.bottom ? R.bottom.map(card) : cardsFor(hands[0], R.landlordCards); |
| 120 | v.hands = hands; |
| 121 | v.counts = hands.map((h) => h.length); |
| 122 | v.turn = 0; |
| 123 | for (const m of R.moveHistory.slice(0, t)) { |
| 124 | const hand = hands[m.playerIdx]; |
| 125 | let cards: Card[] | null = null; |
| 126 | if (m.move !== 'pass') { |
| 127 | cards = cardsFor(hand, m.move); |
| 128 | for (const c of cards) hand.splice(hand.indexOf(c), 1); |
| 129 | } |
| 130 | apply(v, m.playerIdx, cards, m.move); |
| 131 | } |
| 132 | if (t >= R.moveHistory.length) { |
| 133 | v.turn = null; |
| 134 | v.result = { landlordWon: R.landlordWon, points: R.points, spring: R.spring }; |
| 135 | if (R.spring) v.fx = '春天'; |
| 136 | } else v.turn = R.moveHistory[t].playerIdx; |
| 137 | return v; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | export interface Msg { |
| 142 | n: number; |
| 143 | to?: string; |
| 144 | version?: number; |
| 145 | obs?: any; |
| 146 | legal?: string[]; |
| 147 | event?: any; |
| 148 | end?: { result: string; scores: Record<string, number> }; |
| 149 | } |
| 150 | |
| 151 | export interface Req { |
| 152 | seat: number; |
| 153 | version: number; |
| 154 | legal: string[]; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | export function liveView(msgs: Msg[], you: string[]): { v: View; reqs: Req[] } { |
| 159 | const v = blank(); |
| 160 | const open: (Req | null)[] = [null, null, null]; |
| 161 | let winner: string | null = null; |
| 162 | let spring = false; |
| 163 | for (const m of msgs) { |
| 164 | const e = m.event; |
| 165 | if (e) { |
| 166 | const s = Number(e.seat); |
| 167 | if (e.type === 'deal') { |
| 168 | v.landlord = Number(e.landlord); |
| 169 | v.bottom = e.bottom.map(card); |
| 170 | v.counts = [0, 1, 2].map((i) => e.left[i]); |
| 171 | for (const [k, p] of Object.entries<any>(e.seats ?? {})) { |
| 172 | v.names[Number(k)] = p.name; |
| 173 | v.bots[Number(k)] = p.kind === 'bot' || p.kind === 'auto'; |
| 174 | } |
| 175 | v.turn = v.landlord; |
| 176 | } else if (e.type === 'play' || e.type === 'pass') { |
| 177 | const cards = e.type === 'play' ? (e.cards as string[]).map(card) : null; |
| 178 | const hand = v.hands[s]; |
| 179 | if (hand && cards) v.hands[s] = hand.filter((c) => !cards.some((x) => x.id === c.id)); |
| 180 | apply(v, s, cards, e.move ?? 'pass'); |
| 181 | open[s] = null; |
| 182 | } else if (e.type === 'control') { |
| 183 | v.names[s] = e.name; |
| 184 | v.bots[s] = e.kind === 'bot' || e.kind === 'auto'; |
| 185 | } else if (e.type === 'over') { |
| 186 | v.hands = [0, 1, 2].map((i) => (e.hands[i] as string[]).map(card)); |
| 187 | winner = e.winner; |
| 188 | spring = !!e.spring; |
| 189 | } |
| 190 | } else if (m.end) { |
| 191 | v.turn = null; |
| 192 | v.result = { |
| 193 | landlordWon: winner === null ? null : winner === 'landlord', |
| 194 | points: [0, 1, 2].map((i) => m.end!.scores[i] ?? 0), |
| 195 | spring, |
| 196 | }; |
| 197 | v.fx = spring ? '春天' : null; |
| 198 | } else if (m.to !== undefined && m.obs) { |
| 199 | const s = Number(m.to); |
| 200 | if (m.obs.hand) v.hands[s] = (m.obs.hand as string[]).map(card); |
| 201 | if (m.version !== undefined) open[s] = { seat: s, version: m.version, legal: m.legal ?? [] }; |
| 202 | } |
| 203 | } |
| 204 | return { v, reqs: open.filter((r): r is Req => !!r && you.includes(String(r.seat))) }; |
| 205 | } |