From bbce1183708dfefeb49e4fb5c99bee0404345f61 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sun, 24 Dec 2023 12:32:16 -0500 Subject: [PATCH] Inventory broke, added delinv command - inventory rewrite necessary? --- src/commands/commands/economy/inventory.ts | 6 +++--- src/commands/commands/economy/pick.ts | 7 ++----- src/commands/commands/utility/delinv.ts | 14 ++++++++++++++ src/commands/index.ts | 4 +++- src/data/inventory.ts | 14 ++++++++++++++ src/data/prisma.ts | 22 ++++++++++++++++++---- src/index.ts | 4 ++++ src/permissions/index.ts | 7 ++++++- src/services/console/index.ts | 5 ++++- src/services/discord/index.ts | 2 +- src/services/index.ts | 7 +++++++ src/services/mpp/index.ts | 2 +- src/util/config.ts | 2 +- 13 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 src/commands/commands/utility/delinv.ts diff --git a/src/commands/commands/economy/inventory.ts b/src/commands/commands/economy/inventory.ts index e2fd845..a566aa1 100644 --- a/src/commands/commands/economy/inventory.ts +++ b/src/commands/commands/economy/inventory.ts @@ -7,9 +7,9 @@ export const inventory = new Command( "get bozo's inventory", "inventory", msg => { - const items = msg.inventory.items as unknown as Item[]; - console.log(msg.inventory); - const list = items + const items = msg.inventory.items as string; + console.log(items); + const list = JSON.parse(items) .map( i => `${i.name}${ diff --git a/src/commands/commands/economy/pick.ts b/src/commands/commands/economy/pick.ts index 0b311bc..3391a07 100644 --- a/src/commands/commands/economy/pick.ts +++ b/src/commands/commands/economy/pick.ts @@ -1,7 +1,7 @@ import { JsonArray, JsonValue } from "@prisma/client/runtime/library"; import { KekklefruitTree } from "../../../economy/kekkle"; import { Command } from "../../Command"; -import { updateInventory } from "../../../data/inventory"; +import { addItem, updateInventory } from "../../../data/inventory"; export const pick = new Command( "pick", @@ -14,10 +14,7 @@ export const pick = new Command( if (!fruit) return `There are not enough fruit on the kekklefruit tree.`; - (msg.inventory.items as JsonArray).push(fruit as unknown as JsonValue); - console.log("updating inventory"); - await updateInventory(msg.inventory); - + addItem(msg.p._id, fruit); return `(insert random boring message about ${fruit.name} here)`; } ); diff --git a/src/commands/commands/utility/delinv.ts b/src/commands/commands/utility/delinv.ts new file mode 100644 index 0000000..63e8acb --- /dev/null +++ b/src/commands/commands/utility/delinv.ts @@ -0,0 +1,14 @@ +import { deleteInventory } from "../../../data/inventory"; +import { Command } from "../../Command"; + +export const delinv = new Command( + "delinv", + ["delinv"], + "delete a bozo's inventory", + "delinv [id]", + async (msg) => { + let userId = msg.argv[1] ? msg.argv[1] : msg.p._id; + await deleteInventory(userId); + return `Inventory of \`${userId}\` deleted.` + } +); diff --git a/src/commands/index.ts b/src/commands/index.ts index 5cdbc45..2f33597 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -18,6 +18,7 @@ import { branch } from "./commands/utility/branch"; import { tree } from "./commands/economy/tree"; import { pick } from "./commands/economy/pick"; import { grow } from "./commands/economy/grow"; +import { delinv } from "./commands/utility/delinv"; export function loadCommands() { // cringe @@ -45,7 +46,8 @@ export function loadCommands() { ic, uptime, permissions, - branch + branch, + delinv ]); globalThis.commandHandler.addCommandGroup(utility); } diff --git a/src/data/inventory.ts b/src/data/inventory.ts index f625f6f..7baac39 100644 --- a/src/data/inventory.ts +++ b/src/data/inventory.ts @@ -50,3 +50,17 @@ export async function updateInventory(data: Omit) { export async function deleteInventory(userId: Inventory["userId"]) { return await prisma.inventory.delete({ where: { userId } }); } + +export async function addItem(userId: Inventory["userId"], item: T) { + let inventory = await readInventory(userId); + if (!inventory) return false; + + console.log(inventory.items); + + inventory.items = JSON.stringify(JSON.parse(inventory.items as string).push(item)); + collapseInventory(inventory.items as unknown as Item[]); + + await updateInventory(inventory); + + return true; +} diff --git a/src/data/prisma.ts b/src/data/prisma.ts index a442c58..4dac992 100644 --- a/src/data/prisma.ts +++ b/src/data/prisma.ts @@ -1,10 +1,17 @@ import { PrismaClient } from "@prisma/client"; import { JsonObject } from "@prisma/client/runtime/library"; -export const prisma = new PrismaClient(); +declare global { + var prisma: PrismaClient; +} + +globalThis.prisma ??= new PrismaClient(); +export const prisma = globalThis.prisma; export async function set(key: string, value: any) { - const store = await prisma.keyValueStore.findUnique({ where: { id: 1 } }); + const store = await globalThis.prisma.keyValueStore.findUnique({ + where: { id: 1 } + }); if (!store) { // throw new Error("Unable to access key-value store."); @@ -17,15 +24,22 @@ export async function set(key: string, value: any) { const data = store.data as JsonObject; data[key] = value; + + await globalThis.prisma.keyValueStore.update({ + where: { id: 1 }, + data: { data: data } + }); return; } export async function get(key: string) { - const store = await prisma.keyValueStore.findUnique({ where: { id: 1 } }); + const store = await globalThis.prisma.keyValueStore.findUnique({ + where: { id: 1 } + }); if (!store) { // throw new Error("Unable to access key-value store."); - await prisma.keyValueStore.create({ + await globalThis.prisma.keyValueStore.create({ data: {} }); diff --git a/src/index.ts b/src/index.ts index cd89d40..18ee3a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,10 @@ function reload() { globalThis.commandHandler.commandGroups = new Array(); loadCommands(); + // Reload services + globalThis.serviceLoader.unloadServices(); + globalThis.serviceLoader.loadServices(); + // Set console prompt globalThis.serviceLoader.agents.forEach(agent => { if (agent.platform === "console") diff --git a/src/permissions/index.ts b/src/permissions/index.ts index 8414938..d9bdc31 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -66,7 +66,12 @@ export function hasPermission(role: Role, permission: string) { return false; } -export const roles = new Map(); +declare global { + var roles: Map; +} + +globalThis.roles ??= new Map(); +export const roles = globalThis.roles; export type TRole = { displayName: string; diff --git a/src/services/console/index.ts b/src/services/console/index.ts index dc136ed..ebd2b59 100644 --- a/src/services/console/index.ts +++ b/src/services/console/index.ts @@ -65,7 +65,10 @@ export class ConsoleAgent extends ServiceAgent { if (text.startsWith("/")) { out = await MicroHandler.handleMicroCommand(message, this); } else { - out = await CommandHandler.handleCommand(message, this); + out = await globalThis.commandHandler.handleCommand( + message, + this + ); } if (out) { diff --git a/src/services/discord/index.ts b/src/services/discord/index.ts index 80125f4..d9fc6a0 100644 --- a/src/services/discord/index.ts +++ b/src/services/discord/index.ts @@ -56,7 +56,7 @@ export class DiscordAgent extends ServiceAgent { let args = msg.content.split(" "); - const str = await CommandHandler.handleCommand( + const str = await globalThis.commandHandler.handleCommand( { m: "command", a: msg.content, diff --git a/src/services/index.ts b/src/services/index.ts index e490cb8..d9b0791 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -100,4 +100,11 @@ export class ServiceLoader { this.addAgent(consoleAgent); } } + + public static unloadServices() { + for (const agent of this.agents) { + agent.stop(); + this.agents.splice(this.agents.indexOf(agent), 1); + } + } } diff --git a/src/services/mpp/index.ts b/src/services/mpp/index.ts index 155e4c1..1d202f1 100644 --- a/src/services/mpp/index.ts +++ b/src/services/mpp/index.ts @@ -61,7 +61,7 @@ export class MPPAgent extends ServiceAgent { let args = msg.a.split(" "); // Run command and get output - const str = await CommandHandler.handleCommand( + const str = await globalThis.commandHandler.handleCommand( { m: "command", a: msg.a, diff --git a/src/util/config.ts b/src/util/config.ts index 3684402..57841ab 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -46,7 +46,7 @@ export function loadConfig(configPath: string, defaultConfig: T): T { } // Apply any missing default values - mix(config, defRecord); + // mix(config, defRecord); // Save config if modified if (changed) writeConfig(configPath, config);