Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/binmat. binmat simulator for game hackmud
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188import { assert } from "@samual/lib/assert"import type { Card, CardValue, Lane, Move } from "./shared"import { AttackerDeck, AttackerDiscardPile, CardModifier, CardSuit, MoveKind } from "./shared"
export const CardValues: CardValue[] = [ `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `a`, CardModifier.Trap, CardModifier.Wild, CardModifier.Bounce, CardModifier.Break]
export const CardSuits: CardSuit[] = [ CardSuit.Form, CardSuit.Kin, CardSuit.Data, CardSuit.Chaos, CardSuit.Void, CardSuit.Choice]
export function parseMove(move: string, extra = false): Move { assert(move.length > 1, `move must at least than 2 character, got ${move.length}`)
if (move == `--`) return { kind: MoveKind.Pass }
switch (move[0]) { case `d`: { assert(move.length == 2, `draw moves must always be 2 characters, got ${move.length}`)
if (move[1] == `a`) { return { kind: MoveKind.Draw, deck: AttackerDeck } }
const lane = Number(move[1])
assert(lane >= 0 && lane < 6, `invalid lane number "${move[1]}" (expected 0 - 5 or "a")`)
return { kind: MoveKind.Draw, deck: lane as Lane } }
case `c`: { assert(move.length == 2, `combat moves must always be 2 characters, got ${move.length}`)
const lane = Number(move[1])
assert(lane >= 0 && lane < 6, `invalid lane number "${move[1]}" (expected 0 - 5)`)
return { kind: MoveKind.Combat, lane: lane as Lane } }
case `p`: case `u`: { if (move.length == 3) { assert( CardValues.includes(move[1] as any), `invalid card value "${move[1]}" (expected one of ${CardValues.join(`, `)})` )
const lane = Number(move[2])
assert(lane >= 0 && lane < 6, `invalid lane number "${move[2]}" (expected 0 - 5)`)
return { kind: move[0] == `p` ? MoveKind.Play : MoveKind.PlayFaceUp, card: move[1] as CardValue, lane: lane as Lane } }
if (move.length == 4) { assert( CardValues.includes(move[1] as any), `invalid card value "${move[1]}" (expected one of ${CardValues.join(`, `)})` )
assert( CardSuits.includes(move[2] as any), `invalid card suit "${move[2]}" (expected one of ${CardSuits.join(`, `)})` )
const lane = Number(move[3])
assert(lane >= 0 && lane < 6, `invalid lane number "${move[3]}" (expected 0 - 5)`)
return { kind: move[0] == `p` ? MoveKind.Play : MoveKind.PlayFaceUp, card: move.slice(1, 3) as Card, lane: lane as Lane } }
throw new Error(`play moves must be 3 or 4 characters, got ${move.length}`) }
case `x`: { assert( CardValues.includes(move[1] as any), `invalid card value "${move[1]}" (expected one of ${CardValues.join(`, `)})` )
if (extra && move.length == 2) { return { kind: MoveKind.Discard, card: move[1] as CardValue, discardPile: AttackerDiscardPile } }
if (move.length == 3) { if (move[2] == `a`) { return { kind: MoveKind.Discard, card: move[1] as CardValue, discardPile: AttackerDiscardPile } }
if (extra && CardSuits.includes(move[2] as any)) { return { kind: MoveKind.Discard, card: move.slice(1, 3) as Card, discardPile: AttackerDiscardPile } }
const lane = Number(move[2])
assert(lane >= 0 && lane < 6, `invalid discard pile "${move[2]}" (expected 0 - 5 or "a")`)
return { kind: MoveKind.Discard, card: move[1] as CardValue, discardPile: lane as Lane } }
if (move.length == 4) { assert( CardSuits.includes(move[2] as any), `invalid card suit "${move[2]}" (expected one of ${CardSuits.join(`, `)})` )
if (move[3] == `a`) { return { kind: MoveKind.Discard, card: move.slice(1, 3) as Card, discardPile: AttackerDiscardPile } }
const lane = Number(move[3])
assert(lane >= 0 && lane < 6, `invalid discard pile "${move[3]}" (expected 0 - 5 or "a")`)
return { kind: MoveKind.Discard, card: move.slice(1, 3) as Card, discardPile: lane as Lane } }
throw new Error(`discard moves must be 3 or 4 characters, got ${move.length}`) }
default: throw new Error(`invalid move kind "${move[0]}" in "${move}"`) }}