Compare commits
3 commits
e238485b21
...
4e2afd4db3
Author | SHA1 | Date | |
---|---|---|---|
4e2afd4db3 | |||
4486bcb356 | |||
cf47d5db4a |
7 changed files with 54 additions and 10 deletions
|
@ -1,10 +1,10 @@
|
|||
import Dexie from "dexie";
|
||||
import { Card, CardFilter, StoredImages } from "./types";
|
||||
import { Card, CardFilter, StoredImage } from "./types";
|
||||
import { cardFullName } from "./card";
|
||||
|
||||
class CardDatabase extends Dexie {
|
||||
public cards: Dexie.Table<Card, string>;
|
||||
public images: Dexie.Table<StoredImages, string>;
|
||||
public images: Dexie.Table<StoredImage, string>;
|
||||
|
||||
public constructor() {
|
||||
super("CardDatabase");
|
||||
|
@ -20,8 +20,10 @@ class CardDatabase extends Dexie {
|
|||
export let Database: CardDatabase | null = null;
|
||||
|
||||
export function initDB() {
|
||||
if (Database == null) {
|
||||
Database = new CardDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCards(filter: CardFilter) {
|
||||
if (Database == null) {
|
||||
|
@ -153,6 +155,9 @@ export async function getCards(filter: CardFilter) {
|
|||
}
|
||||
|
||||
export async function cardFromIDs(cardIDs: string[]): Promise<Card[]> {
|
||||
if (Database == null) {
|
||||
throw new Error("Database was not initialized, init with 'initDB()'");
|
||||
}
|
||||
let table = Database.cards;
|
||||
//TODO Replace with .bulkGet when upgrading to Dexie 3.x
|
||||
return await table
|
||||
|
|
|
@ -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";
|
|
@ -2,7 +2,7 @@ export type Rarity = "C" | "U" | "R" | "SR" | "UR" | "RR";
|
|||
|
||||
export type PowerRequirement = { [key: string]: number };
|
||||
|
||||
export interface StoredImages {
|
||||
export interface StoredImage {
|
||||
id: string;
|
||||
image: Blob;
|
||||
}
|
||||
|
|
11
src/testing/IDBShim.ts
Normal file
11
src/testing/IDBShim.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import Dexie from "dexie";
|
||||
|
||||
let init = false;
|
||||
|
||||
export function setupIDBShim() {
|
||||
if (!init) {
|
||||
const setGlobalVars = require("indexeddbshim");
|
||||
setGlobalVars(Dexie.dependencies);
|
||||
init = true;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import { loadSets, getCards, Database, initDB, cardFullName } from "@/mlpccg";
|
||||
import Dexie from "dexie";
|
||||
const setGlobalVars = require("indexeddbshim");
|
||||
setGlobalVars(Dexie.dependencies);
|
||||
import { setupIDBShim } from "@/testing/IDBShim";
|
||||
|
||||
setupIDBShim();
|
||||
|
||||
describe("mlpccg/Database", () => {
|
||||
beforeAll(async () => {
|
||||
|
|
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