Add draft te st and fix spanByRarity
This commit is contained in:
parent
4486bcb356
commit
4e2afd4db3
3 changed files with 31 additions and 3 deletions
|
@ -192,7 +192,7 @@ export class PackBuilder {
|
|||
}
|
||||
|
||||
// Yields random cards from a chosen pool
|
||||
function* randomProvider(pool: Card[]) {
|
||||
export function* randomProvider(pool: Card[]) {
|
||||
while (true) {
|
||||
const idx = Math.floor(Math.random() * pool.length);
|
||||
yield pool[idx];
|
||||
|
@ -201,12 +201,12 @@ function* randomProvider(pool: Card[]) {
|
|||
|
||||
// Divides a list of card to a map of rarities
|
||||
// ie. [ff14, ff16, ff17] => { "C" : ["ff14"], "U": ["ff17"], "R": ["ff16"] }
|
||||
function spanByRarity(pool: Card[]): Record<string, Card[]> {
|
||||
export function spanByRarity(pool: Card[]): Record<string, Card[]> {
|
||||
return pool.reduce((map, current) => {
|
||||
if (!(current.Rarity in map)) {
|
||||
map[current.Rarity] = [];
|
||||
}
|
||||
map[current.Rarity] = current;
|
||||
map[current.Rarity].push(current);
|
||||
return map;
|
||||
}, Object.create(null));
|
||||
}
|
||||
|
|
3
src/mlpccg/draft/index.ts
Normal file
3
src/mlpccg/draft/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export * from "./cube";
|
||||
export * from "./booster";
|
||||
export * from "./types";
|
25
src/tests/unit/draft.spec.ts
Normal file
25
src/tests/unit/draft.spec.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { setupIDBShim } from "@/testing/IDBShim";
|
||||
import { initDB, loadSets, Database } from "@/mlpccg";
|
||||
import { PackBuilder, spanByRarity } from "@/mlpccg/draft";
|
||||
|
||||
setupIDBShim();
|
||||
|
||||
describe("mlpccg/draft", () => {
|
||||
beforeAll(async () => {
|
||||
jest.setTimeout(15000);
|
||||
initDB();
|
||||
await loadSets();
|
||||
});
|
||||
|
||||
test("Set booster packs are generated correctly", async () => {
|
||||
expect(Database).toBeTruthy();
|
||||
const builder = await PackBuilder.fromSet("FF");
|
||||
const pack = builder.buildPack();
|
||||
// Check pack size
|
||||
expect(pack).toHaveLength(12);
|
||||
const rarities = spanByRarity(pack);
|
||||
// Check pack distribution
|
||||
expect(rarities["R"]).toHaveLength(1);
|
||||
expect(rarities["U"]).toHaveLength(3);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue