Basic draft library #19
2 changed files with 56 additions and 0 deletions
|
@ -151,3 +151,12 @@ export async function getCards(filter: CardFilter) {
|
|||
});
|
||||
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
47
src/mlpccg/draft/cube.ts
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue