82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
import { Card, cardFromIDs } from "@/mlpccg";
|
|
import {
|
|
PackSchema,
|
|
I8PCubeSchema,
|
|
I8PPackSchema,
|
|
I8PFileSchema,
|
|
DraftSchema
|
|
} from "./types";
|
|
import axios from "axios";
|
|
import { PackBuilder } from "./booster";
|
|
|
|
export class I8PCube {
|
|
private pools: Record<string, Card[]>;
|
|
private packschema: I8PPackSchema[];
|
|
private problemCount: number;
|
|
|
|
constructor(cubefile: I8PCubeSchema) {
|
|
this.pools = cubefile.Cards;
|
|
this.packschema = cubefile.Schema;
|
|
this.problemCount = cubefile.ProblemPackSize;
|
|
}
|
|
|
|
schema(): DraftSchema {
|
|
return {
|
|
boosters: {
|
|
main: 4,
|
|
problem: 1
|
|
},
|
|
factories: {
|
|
main: new PackBuilder({
|
|
slots: this.packschema.map(s => ({
|
|
amount: s.Amount,
|
|
provider: this.provider(s.Type),
|
|
alternate: []
|
|
}))
|
|
}),
|
|
problem: new PackBuilder({
|
|
slots: [
|
|
{
|
|
amount: this.problemCount,
|
|
provider: this.provider("problem"),
|
|
alternate: []
|
|
}
|
|
]
|
|
})
|
|
}
|
|
};
|
|
}
|
|
|
|
*provider(name: string | "all") {
|
|
let poolname = name;
|
|
while (true) {
|
|
if (name == "all") {
|
|
const pools = Object.keys(this.pools);
|
|
const idx = Math.floor(Math.random() * pools.length);
|
|
poolname = pools[idx];
|
|
}
|
|
const pool = this.pools[poolname];
|
|
if (pool.length <= 0) {
|
|
return;
|
|
}
|
|
const idx = Math.floor(Math.random() * pool.length);
|
|
const card = pool.splice(idx, 1);
|
|
yield card[0];
|
|
}
|
|
}
|
|
|
|
static async fromURL(url: string) {
|
|
const res = await axios(url);
|
|
const cubefile = res.data as I8PFileSchema;
|
|
let cards: Record<string, Card[]> = {};
|
|
for (const pool in cubefile.Cards) {
|
|
cards[pool] = await cardFromIDs(cubefile.Cards[pool]);
|
|
}
|
|
return new this({
|
|
Cards: cards,
|
|
ProblemPackSize: cubefile.ProblemPackSize,
|
|
Schema: cubefile.Schema
|
|
});
|
|
}
|
|
}
|