moving to postgres, part 1

This commit is contained in:
2025-09-17 17:51:22 +02:00
parent 8f87908505
commit 223add151c
19 changed files with 425 additions and 290 deletions

View File

@@ -3,7 +3,7 @@ import User from "user";
import { type userType, type specialExecuteArgs } from "commands";
type itemOptions = {
name: string;
name: items;
aliases: string[];
prettyName: string;
plural: string;
@@ -13,7 +13,7 @@ type itemOptions = {
};
export class Item {
public readonly name: string;
public readonly name: items;
public readonly prettyName: string;
public readonly plural: string;
public readonly description: string;
@@ -25,7 +25,7 @@ export class Item {
/** Creates an item object */
constructor(options: itemOptions) {
this.name = options.name.toLowerCase();
this.name = options.name;
this.prettyName = options.prettyName;
this.plural = options.plural;
this.description = options.description;
@@ -38,12 +38,11 @@ export class Item {
};
import { readdir } from 'node:fs/promises';
import type { userRecord } from "db/connection";
import { updateUserRecord } from "db/dbUser";
const items = new Map<string, Item>;
import { updateUserRecord, type inventoryUpdate } from "db/dbUser";
const itemMap = new Map<string, Item>;
const specialAliasItems = new Map<string, Item>;
const emptyInventory: inventory = {};
const itemarray: string[] = [];
const itemarray: items[] = [];
const files = await readdir(import.meta.dir);
for (const file of files) {
@@ -53,20 +52,22 @@ for (const file of files) {
emptyInventory[item.name] = 0;
itemarray.push(item.name);
for (const alias of item.aliases) {
items.set(alias, item); // Since it's not a primitive type the map is filled with references to the item, not the actual object
itemMap.set(alias, item); // Since it's not a primitive type the map is filled with references to the item, not the actual object
};
for (const alias of item.specialaliases) {
specialAliasItems.set(alias, item);
};
};
export default items;
export default itemMap;
export { emptyInventory, itemarray, specialAliasItems };
export type items = "blaster" | "silverbullet" | "grenade" | "tnt";
export type inventory = {
[key: string]: number;
[key in items]?: number;
};
export async function changeItemCount(user: User, userRecord: userRecord, itemname: string, amount = -1): Promise<false | userRecord> {
export async function changeItemCount(user: User, userRecord: inventoryUpdate, itemname: items, amount = -1): Promise<false | inventoryUpdate> {
userRecord.inventory[itemname] = userRecord.inventory[itemname]! += amount;
if (userRecord.inventory[itemname] < 0) return false;
await updateUserRecord(user, userRecord);

View File

@@ -20,7 +20,7 @@ export default new Item({
execution: async (msg, user, specialargs) => {
const userObj = await getUserRecord(user);
if (userObj.inventory[ITEMNAME]! < 1) { await sendMessage(`You don't have any silver bullets!`, msg.messageId); return; };
const messagequery = parseCommandArgs(msg.messageText);
const messagequery = parseCommandArgs(msg.messageText, specialargs?.activation);
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
const target = await User.initUsername(messagequery[0].toLowerCase());
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };