Add cube
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing

This commit is contained in:
Hamcha 2019-09-11 17:55:58 +02:00
parent cd1c26249c
commit e238485b21
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
2 changed files with 56 additions and 0 deletions

View file

@ -151,3 +151,12 @@ export async function getCards(filter: CardFilter) {
}); });
return await results.toArray(); return await results.toArray();
} }
export async function cardFromIDs(cardIDs: string[]): Promise<Card[]> {
let table = Database.cards;
//TODO Replace with .bulkGet when upgrading to Dexie 3.x
return await table
.where("ID")
.anyOf(cardIDs)
.toArray();
}

47
src/mlpccg/draft/cube.ts Normal file
View file

@ -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<Cube> {
const cards = await cardFromIDs(cardIDs);
return new this(cards);
}
static async fromList(list: string): Promise<Cube> {
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);
}
}