Compare commits

..

No commits in common. "4e2afd4db3173859aa3333f11b0125132fd7b28b" and "e238485b21220e43d02d2e654a1587d44ef7ac6c" have entirely different histories.

7 changed files with 10 additions and 54 deletions

View file

@ -1,10 +1,10 @@
import Dexie from "dexie"; import Dexie from "dexie";
import { Card, CardFilter, StoredImage } from "./types"; import { Card, CardFilter, StoredImages } from "./types";
import { cardFullName } from "./card"; import { cardFullName } from "./card";
class CardDatabase extends Dexie { class CardDatabase extends Dexie {
public cards: Dexie.Table<Card, string>; public cards: Dexie.Table<Card, string>;
public images: Dexie.Table<StoredImage, string>; public images: Dexie.Table<StoredImages, string>;
public constructor() { public constructor() {
super("CardDatabase"); super("CardDatabase");
@ -20,10 +20,8 @@ class CardDatabase extends Dexie {
export let Database: CardDatabase | null = null; export let Database: CardDatabase | null = null;
export function initDB() { export function initDB() {
if (Database == null) {
Database = new CardDatabase(); Database = new CardDatabase();
} }
}
export async function getCards(filter: CardFilter) { export async function getCards(filter: CardFilter) {
if (Database == null) { if (Database == null) {
@ -155,9 +153,6 @@ export async function getCards(filter: CardFilter) {
} }
export async function cardFromIDs(cardIDs: string[]): Promise<Card[]> { 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; let table = Database.cards;
//TODO Replace with .bulkGet when upgrading to Dexie 3.x //TODO Replace with .bulkGet when upgrading to Dexie 3.x
return await table return await table

View file

@ -192,7 +192,7 @@ export class PackBuilder {
} }
// Yields random cards from a chosen pool // Yields random cards from a chosen pool
export function* randomProvider(pool: Card[]) { function* randomProvider(pool: Card[]) {
while (true) { while (true) {
const idx = Math.floor(Math.random() * pool.length); const idx = Math.floor(Math.random() * pool.length);
yield pool[idx]; yield pool[idx];
@ -201,12 +201,12 @@ export function* randomProvider(pool: Card[]) {
// Divides a list of card to a map of rarities // Divides a list of card to a map of rarities
// ie. [ff14, ff16, ff17] => { "C" : ["ff14"], "U": ["ff17"], "R": ["ff16"] } // ie. [ff14, ff16, ff17] => { "C" : ["ff14"], "U": ["ff17"], "R": ["ff16"] }
export function spanByRarity(pool: Card[]): Record<string, Card[]> { function spanByRarity(pool: Card[]): Record<string, Card[]> {
return pool.reduce((map, current) => { return pool.reduce((map, current) => {
if (!(current.Rarity in map)) { if (!(current.Rarity in map)) {
map[current.Rarity] = []; map[current.Rarity] = [];
} }
map[current.Rarity].push(current); map[current.Rarity] = current;
return map; return map;
}, Object.create(null)); }, Object.create(null));
} }

View file

@ -1,3 +0,0 @@
export * from "./cube";
export * from "./booster";
export * from "./types";

View file

@ -2,7 +2,7 @@ export type Rarity = "C" | "U" | "R" | "SR" | "UR" | "RR";
export type PowerRequirement = { [key: string]: number }; export type PowerRequirement = { [key: string]: number };
export interface StoredImage { export interface StoredImages {
id: string; id: string;
image: Blob; image: Blob;
} }

View file

@ -1,11 +0,0 @@
import Dexie from "dexie";
let init = false;
export function setupIDBShim() {
if (!init) {
const setGlobalVars = require("indexeddbshim");
setGlobalVars(Dexie.dependencies);
init = true;
}
}

View file

@ -1,7 +1,7 @@
import { loadSets, getCards, Database, initDB, cardFullName } from "@/mlpccg"; import { loadSets, getCards, Database, initDB, cardFullName } from "@/mlpccg";
import { setupIDBShim } from "@/testing/IDBShim"; import Dexie from "dexie";
const setGlobalVars = require("indexeddbshim");
setupIDBShim(); setGlobalVars(Dexie.dependencies);
describe("mlpccg/Database", () => { describe("mlpccg/Database", () => {
beforeAll(async () => { beforeAll(async () => {

View file

@ -1,25 +0,0 @@
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);
});
});