jmou/yakatak
git clone https://github.com/jmou/yakatak.git
git clone git@github.com:jmou/yakatak.git
Loading…
(top)/ui/src/lib/common.ts
language: TypeScript
1006 Bytes / 48 lines / 40 loc
History
View raw
class AssertionError extends Error {}

export function assert(expr: unknown): asserts expr {
  if (expr === false || expr === null || expr === undefined) {
    throw new AssertionError();
  }
}

export function checked<T>(value: T): NonNullable<T> {
  assert(value);
  return value;
}

export interface CardData {
  readonly id: string;
  readonly url: string;
  readonly title: string;
  readonly numTiles: number;
}

export class Card {
  readonly id: string;
  readonly url: string;
  readonly title: string;
  readonly numTiles: number;
  elem: Element | null = null;

  constructor(data: CardData) {
    this.id = data.id;
    this.url = data.url;
    this.title = data.title;
    this.numTiles = data.numTiles;
  }
}

export class Pile {
  name = "";
  cards: Card[] = [];
  picked = 0;
  elem: Element | null = null;

  static readonly DISCARD = 0;
  static readonly START = 1;

  pickCardClamped(cardIndex: number) {
    this.picked = Math.max(0, Math.min(cardIndex, this.cards.length - 1));
  }
}