49 lines
1.0 KiB
TypeScript
49 lines
1.0 KiB
TypeScript
import { Card, cardFromIDs } from "@/mlpccg";
|
|
import { PackSchema } from "./types";
|
|
import axios from "axios";
|
|
|
|
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, {
|
|
responseType: "text"
|
|
});
|
|
return await this.fromList(res.data);
|
|
}
|
|
}
|