Inventory broke, added delinv command - inventory rewrite necessary?
This commit is contained in:
parent
6a21aa0cbd
commit
bbce118370
|
@ -7,9 +7,9 @@ export const inventory = new Command(
|
||||||
"get bozo's inventory",
|
"get bozo's inventory",
|
||||||
"inventory",
|
"inventory",
|
||||||
msg => {
|
msg => {
|
||||||
const items = msg.inventory.items as unknown as Item[];
|
const items = msg.inventory.items as string;
|
||||||
console.log(msg.inventory);
|
console.log(items);
|
||||||
const list = items
|
const list = JSON.parse(items)
|
||||||
.map(
|
.map(
|
||||||
i =>
|
i =>
|
||||||
`${i.name}${
|
`${i.name}${
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { JsonArray, JsonValue } from "@prisma/client/runtime/library";
|
import { JsonArray, JsonValue } from "@prisma/client/runtime/library";
|
||||||
import { KekklefruitTree } from "../../../economy/kekkle";
|
import { KekklefruitTree } from "../../../economy/kekkle";
|
||||||
import { Command } from "../../Command";
|
import { Command } from "../../Command";
|
||||||
import { updateInventory } from "../../../data/inventory";
|
import { addItem, updateInventory } from "../../../data/inventory";
|
||||||
|
|
||||||
export const pick = new Command(
|
export const pick = new Command(
|
||||||
"pick",
|
"pick",
|
||||||
|
@ -14,10 +14,7 @@ export const pick = new Command(
|
||||||
if (!fruit)
|
if (!fruit)
|
||||||
return `There are not enough fruit on the kekklefruit tree.`;
|
return `There are not enough fruit on the kekklefruit tree.`;
|
||||||
|
|
||||||
(msg.inventory.items as JsonArray).push(fruit as unknown as JsonValue);
|
addItem(msg.p._id, fruit);
|
||||||
console.log("updating inventory");
|
|
||||||
await updateInventory(msg.inventory);
|
|
||||||
|
|
||||||
return `(insert random boring message about ${fruit.name} here)`;
|
return `(insert random boring message about ${fruit.name} here)`;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -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.`
|
||||||
|
}
|
||||||
|
);
|
|
@ -18,6 +18,7 @@ import { branch } from "./commands/utility/branch";
|
||||||
import { tree } from "./commands/economy/tree";
|
import { tree } from "./commands/economy/tree";
|
||||||
import { pick } from "./commands/economy/pick";
|
import { pick } from "./commands/economy/pick";
|
||||||
import { grow } from "./commands/economy/grow";
|
import { grow } from "./commands/economy/grow";
|
||||||
|
import { delinv } from "./commands/utility/delinv";
|
||||||
|
|
||||||
export function loadCommands() {
|
export function loadCommands() {
|
||||||
// cringe
|
// cringe
|
||||||
|
@ -45,7 +46,8 @@ export function loadCommands() {
|
||||||
ic,
|
ic,
|
||||||
uptime,
|
uptime,
|
||||||
permissions,
|
permissions,
|
||||||
branch
|
branch,
|
||||||
|
delinv
|
||||||
]);
|
]);
|
||||||
globalThis.commandHandler.addCommandGroup(utility);
|
globalThis.commandHandler.addCommandGroup(utility);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,3 +50,17 @@ export async function updateInventory(data: Omit<Inventory, "id">) {
|
||||||
export async function deleteInventory(userId: Inventory["userId"]) {
|
export async function deleteInventory(userId: Inventory["userId"]) {
|
||||||
return await prisma.inventory.delete({ where: { 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;
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
import { JsonObject } from "@prisma/client/runtime/library";
|
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) {
|
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) {
|
if (!store) {
|
||||||
// throw new Error("Unable to access key-value 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;
|
const data = store.data as JsonObject;
|
||||||
data[key] = value;
|
data[key] = value;
|
||||||
|
|
||||||
|
await globalThis.prisma.keyValueStore.update({
|
||||||
|
where: { id: 1 },
|
||||||
|
data: { data: data }
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function get<T = unknown>(key: string) {
|
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) {
|
if (!store) {
|
||||||
// throw new Error("Unable to access key-value store.");
|
// throw new Error("Unable to access key-value store.");
|
||||||
await prisma.keyValueStore.create({
|
await globalThis.prisma.keyValueStore.create({
|
||||||
data: {}
|
data: {}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,10 @@ function reload() {
|
||||||
globalThis.commandHandler.commandGroups = new Array<CommandGroup>();
|
globalThis.commandHandler.commandGroups = new Array<CommandGroup>();
|
||||||
loadCommands();
|
loadCommands();
|
||||||
|
|
||||||
|
// Reload services
|
||||||
|
globalThis.serviceLoader.unloadServices();
|
||||||
|
globalThis.serviceLoader.loadServices();
|
||||||
|
|
||||||
// Set console prompt
|
// Set console prompt
|
||||||
globalThis.serviceLoader.agents.forEach(agent => {
|
globalThis.serviceLoader.agents.forEach(agent => {
|
||||||
if (agent.platform === "console")
|
if (agent.platform === "console")
|
||||||
|
|
|
@ -66,7 +66,12 @@ export function hasPermission(role: Role, permission: string) {
|
||||||
return false;
|
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 = {
|
export type TRole = {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
|
|
|
@ -65,7 +65,10 @@ export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
|
||||||
if (text.startsWith("/")) {
|
if (text.startsWith("/")) {
|
||||||
out = await MicroHandler.handleMicroCommand(message, this);
|
out = await MicroHandler.handleMicroCommand(message, this);
|
||||||
} else {
|
} else {
|
||||||
out = await CommandHandler.handleCommand(message, this);
|
out = await globalThis.commandHandler.handleCommand(
|
||||||
|
message,
|
||||||
|
this
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out) {
|
if (out) {
|
||||||
|
|
|
@ -56,7 +56,7 @@ export class DiscordAgent extends ServiceAgent<Discord.Client> {
|
||||||
|
|
||||||
let args = msg.content.split(" ");
|
let args = msg.content.split(" ");
|
||||||
|
|
||||||
const str = await CommandHandler.handleCommand(
|
const str = await globalThis.commandHandler.handleCommand(
|
||||||
{
|
{
|
||||||
m: "command",
|
m: "command",
|
||||||
a: msg.content,
|
a: msg.content,
|
||||||
|
|
|
@ -100,4 +100,11 @@ export class ServiceLoader {
|
||||||
this.addAgent(consoleAgent);
|
this.addAgent(consoleAgent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static unloadServices() {
|
||||||
|
for (const agent of this.agents) {
|
||||||
|
agent.stop();
|
||||||
|
this.agents.splice(this.agents.indexOf(agent), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ export class MPPAgent extends ServiceAgent<Client> {
|
||||||
let args = msg.a.split(" ");
|
let args = msg.a.split(" ");
|
||||||
|
|
||||||
// Run command and get output
|
// Run command and get output
|
||||||
const str = await CommandHandler.handleCommand(
|
const str = await globalThis.commandHandler.handleCommand(
|
||||||
{
|
{
|
||||||
m: "command",
|
m: "command",
|
||||||
a: msg.a,
|
a: msg.a,
|
||||||
|
|
|
@ -46,7 +46,7 @@ export function loadConfig<T>(configPath: string, defaultConfig: T): T {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply any missing default values
|
// Apply any missing default values
|
||||||
mix(config, defRecord);
|
// mix(config, defRecord);
|
||||||
|
|
||||||
// Save config if modified
|
// Save config if modified
|
||||||
if (changed) writeConfig(configPath, config);
|
if (changed) writeConfig(configPath, config);
|
||||||
|
|
Loading…
Reference in New Issue