From 17d89d4a44b05b230e5af7bb99690a5228feb1ed Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 21:38:23 +0100 Subject: [PATCH 1/2] Create EffectHelper class --- src/database/entities/app/UserEffect.ts | 9 +++++ src/helpers/EffectHelper.ts | 49 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/helpers/EffectHelper.ts diff --git a/src/database/entities/app/UserEffect.ts b/src/database/entities/app/UserEffect.ts index 3b518b1..fa1b584 100644 --- a/src/database/entities/app/UserEffect.ts +++ b/src/database/entities/app/UserEffect.ts @@ -1,5 +1,6 @@ import {Column, Entity} from "typeorm"; import AppBaseEntity from "../../../contracts/AppBaseEntity"; +import AppDataSource from "../../dataSources/appDataSource"; @Entity() export default class UserEffect extends AppBaseEntity { @@ -48,4 +49,12 @@ export default class UserEffect extends AppBaseEntity { return false; } + + public static async FetchOneByUserIdAndName(userId: string, name: string): Promise { + const repository = AppDataSource.getRepository(UserEffect); + + const single = await repository.findOne({ where: { UserId: userId, Name: name } }); + + return single; + } } diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts new file mode 100644 index 0000000..14c2f43 --- /dev/null +++ b/src/helpers/EffectHelper.ts @@ -0,0 +1,49 @@ +import UserEffect from "../database/entities/app/UserEffect"; + +export default class EffectHelper { + public static async AddEffectToUserInventory(userId: string, name: string, quantity: number = 1) { + let effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + + if (!effect) { + effect = new UserEffect(name, userId, quantity); + } else { + effect.AddUnused(quantity); + } + + await effect.Save(UserEffect, effect); + } + + public static async UseEffect(userId: string, name: string, whenExpires: Date): Promise { + const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + const now = new Date(); + + if (!effect || effect.Unused == 0) { + return false; + } + + if (effect.WhenExpires && now < effect.WhenExpires) { + return false; + } + + effect.UseEffect(whenExpires); + + await effect.Save(UserEffect, effect); + + return true; + } + + public static async HasEffect(userId: string, name: string): Promise { + const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + const now = new Date(); + + if (!effect || !effect.WhenExpires) { + return false; + } + + if (now > effect.WhenExpires) { + return false; + } + + return true; + } +} From 2becf6b95aa15c61a165d9b754a8bba259543f0a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 21:48:24 +0100 Subject: [PATCH 2/2] WIP: Start of planning tests --- package.json | 2 +- .../database/entities/app/UserEffect.test.ts | 41 ++++++++++++ tests/helpers/EffectHelper.test.ts | 0 tests/registry.test.ts | 66 ------------------- 4 files changed, 42 insertions(+), 67 deletions(-) create mode 100644 tests/database/entities/app/UserEffect.test.ts create mode 100644 tests/helpers/EffectHelper.test.ts delete mode 100644 tests/registry.test.ts diff --git a/package.json b/package.json index 8b068bc..2509358 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/bot.js", - "test": "echo true", + "test": "jest", "lint": "eslint .", "lint:fix": "eslint . --fix", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", diff --git a/tests/database/entities/app/UserEffect.test.ts b/tests/database/entities/app/UserEffect.test.ts new file mode 100644 index 0000000..7b5c16b --- /dev/null +++ b/tests/database/entities/app/UserEffect.test.ts @@ -0,0 +1,41 @@ +describe("AddUnused", () => { + test.todo("EXPECT unused to be the amount more"); +}); + +describe("UseEffect", () => { + describe("GIVEN Unused is 0", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN Unused is greater than 0", () => { + test.todo("EXPECT true returned"); + + test.todo("EXPECT Unused to be subtracted by 1"); + + test.todo("EXPECT WhenExpires to be set"); + }); +}); + +describe("IsEffectActive", () => { + describe("GIVEN WhenExpires is null", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN WhenExpires is defined", () => { + describe("AND WhenExpires is in the past", () => { + test.todo("EXPECT false returned"); + }); + + describe("AND WhenExpires is in the future", () => { + test.todo("EXPECT true returned"); + }); + }); +}); + +describe("FetchOneByUserIdAndName", () => { + test.todo("EXPECT entity to be returned"); + + test.todo("EXPECT AppDataSource.getRepository to have been called"); + + test.todo("EXPECT repository.findOne to have been called"); +}); diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/tests/registry.test.ts b/tests/registry.test.ts deleted file mode 100644 index 71d80db..0000000 --- a/tests/registry.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import {CoreClient} from "../src/client/client"; -import Registry from "../src/registry"; -import fs from "fs"; -import path from "path"; - -describe("RegisterCommands", () => { - test("EXPECT every command in the commands folder to be registered", () => { - const registeredCommands: string[] = []; - - CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => { - registeredCommands.push(name); - }); - - Registry.RegisterCommands(); - - const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) - .filter(x => x.endsWith(".ts")); - - for (const file of commandFiles) { - expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); - } - - expect(commandFiles.length).toBe(registeredCommands.length); - }); -}); - -describe("RegisterButtonEvents", () => { - test("EXEPCT every button event in the button events folder to be registered", () => { - const registeredButtonEvents: string[] = []; - - CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => { - registeredButtonEvents.push(name); - }); - - Registry.RegisterButtonEvents(); - - const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) - .filter(x => x.endsWith(".ts")); - - for (const file of eventFiles) { - expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); - } - - expect(eventFiles.length).toBe(registeredButtonEvents.length); - }); -}); - -function getFilesInDirectory(dir: string): string[] { - let results: string[] = []; - const list = fs.readdirSync(dir); - - list.forEach(file => { - file = path.join(dir, file); - const stat = fs.statSync(file); - - if (stat && stat.isDirectory()) { - /* recurse into a subdirectory */ - results = results.concat(getFilesInDirectory(file)); - } else { - /* is a file */ - results.push(file); - } - }); - - return results; -}