From d6491b4c0a327e555bab945a1992a60a9246deb9 Mon Sep 17 00:00:00 2001 From: niktekusho Date: Sat, 29 Jun 2024 17:47:01 +0200 Subject: [PATCH] Replace previous algorithm implementation --- src/morph/benchmark/benchmark.test.ts | 41 ++++---- src/morph/ruleset.test.ts | 136 +------------------------- src/morph/ruleset.ts | 44 --------- 3 files changed, 22 insertions(+), 199 deletions(-) diff --git a/src/morph/benchmark/benchmark.test.ts b/src/morph/benchmark/benchmark.test.ts index 7142cfd..f994b48 100644 --- a/src/morph/benchmark/benchmark.test.ts +++ b/src/morph/benchmark/benchmark.test.ts @@ -3,7 +3,7 @@ import { join } from "node:path"; import { test } from "vitest"; import { validateActionInstance } from "../actions"; import { validateRule } from "../rule"; -import { Ruleset, applyRuleset, applyRulesetNew } from "../ruleset"; +import { Ruleset, applyRuleset } from "../ruleset"; import { GOOD } from "@/good/good_spec"; test("validate 10.000 action instances", () => { @@ -66,24 +66,25 @@ test("morph 10.000 artifacts with 100 rules", () => { const good = JSON.parse(goodFile) as GOOD; - const oldStart = Date.now(); + console.time("morph 10.000 artifacts with 100 rules"); + // const oldStart = Date.now(); applyRuleset(ruleset, good); - const oldEnd = Date.now(); - - const newStart = Date.now(); - applyRulesetNew(ruleset, good); - const newEnd = Date.now(); - - const oldTime = oldEnd - oldStart; - const newTime = newEnd - newStart; - - const speedup = (Math.max(newTime, oldTime)) / Math.min(newTime, oldTime); - const stats = { - oldTime, - newTime, - fastest: newTime < oldTime ? 'new' : 'old', - speedup: `${speedup.toFixed(2)}x` - } - console.table(stats); - + console.timeEnd("morph 10.000 artifacts with 100 rules"); + // const oldEnd = Date.now(); + + // const newStart = Date.now(); + // applyRulesetNew(ruleset, good); + // const newEnd = Date.now(); + + // const oldTime = oldEnd - oldStart; + // const newTime = newEnd - newStart; + + // const speedup = (Math.max(newTime, oldTime)) / Math.min(newTime, oldTime); + // const stats = { + // oldTime, + // newTime, + // fastest: newTime < oldTime ? 'new' : 'old', + // speedup: `${speedup.toFixed(2)}x` + // } + // console.table(stats); }); diff --git a/src/morph/ruleset.test.ts b/src/morph/ruleset.test.ts index 1d75775..3c53325 100644 --- a/src/morph/ruleset.test.ts +++ b/src/morph/ruleset.test.ts @@ -1,6 +1,6 @@ import { GOOD } from "@/good/good_spec"; import { assert, test } from "vitest"; -import { Ruleset, applyRuleset, applyRulesetNew, validateRuleset } from "./ruleset"; +import { Ruleset, applyRuleset, validateRuleset } from "./ruleset"; const GOODFile: GOOD = { format: "GOOD", @@ -388,140 +388,6 @@ test("applyRuleset with 2 rules that both match should return expected morphed G assert.deepEqual(morphedGOOD, expectedGOODFile); }); -test("applyRulesetNew with 2 rules that both match should return expected morphed GOOD", async () => { - // Arrange - const ruleset: Ruleset = { - name: "test", - rules: [ - { - action: { - type: "equip", - to: "Xingqiu", - }, - filter: { - characterName: "Yelan", - type: "equippingCharacter", - }, - id: 1, - }, - { - action: { - type: "equip", - to: "Neuvillette", - }, - filter: { - characterName: "Barbara", - type: "equippingCharacter", - }, - id: 2, - }, - ], - }; - - // Act - const morphedGOOD = applyRulesetNew(ruleset, GOODFile); - - // Assert - const expectedGOODFile: GOOD = { - format: "GOOD", - source: "test", - version: 1, - artifacts: [ - { - setKey: "HeartOfDepth", - rarity: 5, - level: 20, - slotKey: "goblet", - mainStatKey: "hydro_dmg_", - substats: [ - { key: "hp_", value: 4.7 }, - { key: "critDMG_", value: 14.8 }, - { key: "hp", value: 538 }, - { key: "critRate_", value: 14.4 }, - ], - location: "Xingqiu", - lock: true, - }, - { - setKey: "EmblemOfSeveredFate", - rarity: 5, - level: 20, - slotKey: "sands", - mainStatKey: "hp_", - substats: [ - { key: "enerRech_", value: 16.8 }, - { key: "atk", value: 16 }, - { key: "eleMas", value: 42 }, - { key: "critRate_", value: 8.9 }, - ], - location: "Xingqiu", - lock: true, - }, - { - setKey: "EmblemOfSeveredFate", - rarity: 5, - level: 20, - slotKey: "circlet", - mainStatKey: "critRate_", - substats: [ - { key: "hp_", value: 15.7 }, - { key: "atk_", value: 10.5 }, - { key: "def_", value: 5.1 }, - { key: "critDMG_", value: 18.7 }, - ], - location: "Xingqiu", - lock: true, - }, - { - setKey: "EmblemOfSeveredFate", - rarity: 5, - level: 20, - slotKey: "plume", - mainStatKey: "atk", - substats: [ - { key: "enerRech_", value: 11.7 }, - { key: "critDMG_", value: 28.8 }, - { key: "hp_", value: 4.1 }, - { key: "hp", value: 448 }, - ], - location: "Xingqiu", - lock: true, - }, - { - setKey: "EmblemOfSeveredFate", - rarity: 5, - level: 20, - slotKey: "flower", - mainStatKey: "hp", - substats: [ - { key: "eleMas", value: 37 }, - { key: "critRate_", value: 3.1 }, - { key: "enerRech_", value: 21.4 }, - { key: "critDMG_", value: 7.8 }, - ], - location: "Xingqiu", - lock: true, - }, - { - setKey: "MarechausseeHunter", - rarity: 5, - level: 20, - slotKey: "flower", - mainStatKey: "hp", - substats: [ - { key: "eleMas", value: 37 }, - { key: "critRate_", value: 3.1 }, - { key: "enerRech_", value: 21.4 }, - { key: "critDMG_", value: 7.8 }, - ], - location: "Neuvillette", - lock: true, - }, - ], - }; - assert.deepEqual(morphedGOOD, expectedGOODFile); -}); - test("validateRuleset returns error when ruleset is not an object", () => { // Arrange // Act diff --git a/src/morph/ruleset.ts b/src/morph/ruleset.ts index fa0496f..2e5f218 100644 --- a/src/morph/ruleset.ts +++ b/src/morph/ruleset.ts @@ -117,49 +117,6 @@ export function applyRuleset(ruleset: Ruleset, good: GOOD): GOOD { return good; } - console.time("applyRuleset"); - - const { rules } = ruleset; - - const editedGood = clone(good); - - const artifacts = editedGood.artifacts!; - - // artifacts.filter((art) => art.location === "Yelan").forEach(console.log); - - for (let i = 0; i < artifacts.length; i++) { - const ogArtifact = artifacts[i]; - // TODO: naive code... - for (const rule of rules) { - const predicate = createPredicate(rule.filter); - const mutation = createMutation(rule.action); - if (predicate(ogArtifact)) { - artifacts[i] = mutation(ogArtifact); - } - } - } - - // console.log("editedGood", editedGood); - - console.timeEnd("applyRuleset"); - const stats = { - rules: ruleset.rules.length, - goodFile: good.artifacts.length, - }; - console.log("stats", stats); - return editedGood; -} - -export function applyRulesetNew(ruleset: Ruleset, good: GOOD): GOOD { - // TODO: Validate ruleset? - - // Exit early in case of no artifacts... - if (good.artifacts === undefined) { - return good; - } - - console.time("applyRuleset"); - const { rules } = ruleset; const ruleFns = rules.map((rule) => createRuleFunction(rule)); @@ -179,7 +136,6 @@ export function applyRulesetNew(ruleset: Ruleset, good: GOOD): GOOD { // console.log("editedGood", editedGood); - console.timeEnd("applyRuleset"); const stats = { rules: ruleset.rules.length, goodFile: good.artifacts.length,