Skip to content

Commit

Permalink
Merge pull request #10 from queenochaos/dev
Browse files Browse the repository at this point in the history
Minor patches
  • Loading branch information
retraigo authored Jun 30, 2022
2 parents f0b3327 + 8e00545 commit 42cfde0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
32 changes: 18 additions & 14 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Gacha items provided to the Gacha Machine.
*/
export interface GachaData<ItemType> {
export interface GachaData<ItemType> {
tier: number;
result: ItemType;
chance: number;
Expand Down Expand Up @@ -49,7 +49,7 @@ export class GachaMachine<ItemType> {
* @param {GachaData[]} items - Array of gacha items
*/
configItems(items: GachaData<ItemType>[]): void {
let newItems: GachaData<ItemType>[] = items = items.sort((a, b) =>
const newItems: GachaData<ItemType>[] = items = items.sort((a, b) =>
a.tier - b.tier
)
.map((x) => ({
Expand All @@ -64,7 +64,7 @@ export class GachaMachine<ItemType> {
* @param {GachaData[]} items - Array of gacha items.
*/
configTiers(items: GachaData<ItemType>[]): void {
let tiers: GachaTier[] = [];
const tiers: GachaTier[] = [];
const pool = this.pool;
for (let i = 0; i < pool.length; ++i) {
tiers[pool[i]] = { items: 0, chance: 0, tier: pool[i] };
Expand All @@ -74,11 +74,13 @@ export class GachaMachine<ItemType> {
tiers[items[i - 1].tier].items += 1;
tiers[items[i - 1].tier].chance += items[i - 1].chance;
}
let tierList = [];
for (let i in tiers) {
tierList.push(tiers[i]);
}
this.tiers = tierList;
// I don't know why I did this...
// const tierList = [];
// for (const i in tiers) {
// tierList.push(tiers[i]);
// }
// this.tiers = tierList;
this.tiers = tiers;
}
/**
* Roll a number of items from the machine.
Expand All @@ -93,14 +95,13 @@ export class GachaMachine<ItemType> {
pool: number[] = this.pool,
): GachaChoice<ItemType>[] | ItemType[] {
if (detailed) {
let result: GachaChoice<ItemType>[] = [];
const result: GachaChoice<ItemType>[] = [];
for (let i = num; i > 0; --i) {
result.push(this.choose(pool, detailed));
}
return result;
} else {
let result: ItemType[] = [];

const result: ItemType[] = [];
for (let i = num; i > 0; --i) {
result.push(this.choose(pool));
}
Expand All @@ -118,30 +119,33 @@ export class GachaMachine<ItemType> {
pool: number[] = this.pool,
detailed?: boolean,
): GachaChoice<ItemType> | ItemType {
let tier = GachaMachine.roll<number>(
const tier = GachaMachine.roll<number>(
this.tiers.filter((x) => pool.includes(x.tier)).map((x) => ({
chance: x.chance,
result: x.tier,
})),
);
const result = GachaMachine.roll<ItemType>(
this.items.filter((x) => x.tier == tier.result),
//this.items.filter((x) => pool.includes(x.tier)), => Tried this but this slows everything down.
);
return detailed ? result : result.result;
}
/**
* Roll one from an array of gacha choices.
* @param {GachaChoice[]} choices - Choices to roll from.
* @param {number} totalChance - Sum of all chance properties.
* @returns {GachaChoice} Items rolled.
*/
static roll<ItemType>(
choices: GachaChoice<ItemType>[],
totalChance?: number,
): GachaChoice<ItemType> {
const total = choices.reduce(
const total = totalChance || choices.reduce(
(acc: number, val: GachaChoice<ItemType>) => acc + val.chance,
0,
);
let result = Math.random() * total;
const result = Math.random() * total;
let going = 0.0;
for (let i = 0; i < choices.length; ++i) {
going += choices[i].chance;
Expand Down
23 changes: 12 additions & 11 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { GachaMachine } from "./mod.ts";
import { assertAlmostEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { Duration } from "https://deno.land/x/durationjs@v3.1.1/mod.ts";
import { Duration } from "https://deno.land/x/durationjs@v4.0.0/mod.ts";

import pokemon from "./testdata/pokemon.json" assert { type: "json" };

function countDupes(arr: string[]): Record<string, number> {
let items: Record<string, number> = {};
arr.forEach((v, i) => {
const items: Record<string, number> = {};
arr.forEach((v) => {
if (items[v]) items[v]++;
else items[v] = 1;
});
Expand Down Expand Up @@ -54,39 +54,40 @@ Deno.test("Range Check", () => {
const pk = pokemon.find((y) => y.id === Number(x[0]));
return { name: pk?.name, count: x[1], tier: pk?.tier };
}).sort((a, b) => a.count - b.count).reverse().forEach((x) => {
if (x.tier === "normal") assertAlmostEquals(x.count / 10000, 0.67, 4e-2);
if (x.tier === "normal") assertAlmostEquals(x.count / 10000, 0.67, 1e-1);
else if (x.tier === "legendary") {
assertAlmostEquals(x.count / 10000, 0.26, 4e-2);
} else if (x.tier === "mythic") {
assertAlmostEquals(x.count / 10000, 0.03, 4e-2);
}
});
});

Deno.test("Time Check", () => {
assertAlmostEquals((timeRoll - timeInit) / 1000, 1.56, 1e-1)
})
// TESTS END

console.log("-----REPORT-----");
console.log(
"Time taken to configure:",
Duration.between(timeConfig, timeStart).stringify(
Duration.between(timeConfig, timeStart).toShortString(
["s", "ms", "us", "ns"],
true,
),
);
console.log(
"Time taken to setup machine:",
Duration.between(timeConfig, timeInit).stringify(
Duration.between(timeConfig, timeInit).toShortString(
["s", "ms", "us", "ns"],
true,
),
);
console.log(
"Time taken to roll 1000000 items:",
Duration.between(timeInit, timeRoll).stringify(["s", "ms", "us", "ns"], true),
Duration.between(timeInit, timeRoll).toShortString(["s", "ms", "us", "ns"]),
);
console.log(
"Time taken to reduce data into result:",
Duration.between(timeReduce, timeRoll).stringify(
Duration.between(timeReduce, timeRoll).toShortString(
["s", "ms", "us", "ns"],
true,
),
);

0 comments on commit 42cfde0

Please sign in to comment.