Inventory broke, added delinv command - inventory rewrite necessary?

This commit is contained in:
Hri7566 2023-12-24 12:32:16 -05:00
parent 6a21aa0cbd
commit bbce118370
13 changed files with 78 additions and 18 deletions

View File

@ -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}${

View File

@ -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)`;
}
);

View File

@ -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.`
}
);

View File

@ -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);
}

View File

@ -50,3 +50,17 @@ export async function updateInventory(data: Omit<Inventory, "id">) {
export async function deleteInventory(userId: Inventory["userId"]) {
return await prisma.inventory.delete({ where: { userId } });
}
export async function addItem<T extends Item>(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;
}

View File

@ -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<T = unknown>(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: {}
});

View File

@ -30,6 +30,10 @@ function reload() {
globalThis.commandHandler.commandGroups = new Array<CommandGroup>();
loadCommands();
// Reload services
globalThis.serviceLoader.unloadServices();
globalThis.serviceLoader.loadServices();
// Set console prompt
globalThis.serviceLoader.agents.forEach(agent => {
if (agent.platform === "console")

View File

@ -66,7 +66,12 @@ export function hasPermission(role: Role, permission: string) {
return false;
}
export const roles = new Map<Role, TRole>();
declare global {
var roles: Map<Role, TRole>;
}
globalThis.roles ??= new Map<Role, TRole>();
export const roles = globalThis.roles;
export type TRole = {
displayName: string;

View File

@ -65,7 +65,10 @@ export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
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) {

View File

@ -56,7 +56,7 @@ export class DiscordAgent extends ServiceAgent<Discord.Client> {
let args = msg.content.split(" ");
const str = await CommandHandler.handleCommand(
const str = await globalThis.commandHandler.handleCommand(
{
m: "command",
a: msg.content,

View File

@ -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);
}
}
}

View File

@ -61,7 +61,7 @@ export class MPPAgent extends ServiceAgent<Client> {
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,

View File

@ -46,7 +46,7 @@ export function loadConfig<T>(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);