From e238485b21220e43d02d2e654a1587d44ef7ac6c Mon Sep 17 00:00:00 2001 From: Hamcha Date: Wed, 11 Sep 2019 17:55:58 +0200 Subject: [PATCH] Add cube --- src/mlpccg/database.ts | 9 ++++++++ src/mlpccg/draft/cube.ts | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/mlpccg/draft/cube.ts diff --git a/src/mlpccg/database.ts b/src/mlpccg/database.ts index 0fdb78b..088f5ab 100644 --- a/src/mlpccg/database.ts +++ b/src/mlpccg/database.ts @@ -151,3 +151,12 @@ export async function getCards(filter: CardFilter) { }); return await results.toArray(); } + +export async function cardFromIDs(cardIDs: string[]): Promise { + let table = Database.cards; + //TODO Replace with .bulkGet when upgrading to Dexie 3.x + return await table + .where("ID") + .anyOf(cardIDs) + .toArray(); +} diff --git a/src/mlpccg/draft/cube.ts b/src/mlpccg/draft/cube.ts new file mode 100644 index 0000000..b92338e --- /dev/null +++ b/src/mlpccg/draft/cube.ts @@ -0,0 +1,47 @@ +import { Card } from "../types"; +import { getCards, cardFromIDs } from "../database"; +import axios from "axios"; +import { PackSchema } from "./types"; + +export class Cube { + private pool: Card[]; + + constructor(pool: Card[]) { + this.pool = pool; + } + + schema(): PackSchema { + return { + slots: [ + { + amount: 15, + provider: this.provider(), + alternate: [] + } + ] + }; + } + + *provider() { + while (this.pool.length > 0) { + const idx = Math.floor(Math.random() * this.pool.length); + const card = this.pool.splice(idx, 1); + yield card[0]; + } + } + + static async fromCardIDs(cardIDs: string[]): Promise { + const cards = await cardFromIDs(cardIDs); + return new this(cards); + } + + static async fromList(list: string): Promise { + const ids = list.split("\n").map(x => x.trim()); + return await this.fromCardIDs(ids); + } + + static async fromURL(url: string) { + const res = await axios(url); + return await this.fromList(res.data); + } +}