kekklefruit

This commit is contained in:
Hri7566 2023-12-10 04:05:28 -05:00
parent 35ba9ea0cc
commit 6db28fd2bc
9 changed files with 184 additions and 2 deletions

View File

@ -34,3 +34,8 @@ enum Role {
ADMINISTRATOR
OWNER
}
model KeyValueStore {
id Int @id @unique @default(1)
data Json @default("{}")
}

View File

@ -0,0 +1,23 @@
import { KekklefruitTree } from "../../../economy/kekkle";
import { Command } from "../../Command";
export const grow = new Command(
"grow",
["grow"],
"grow bozo's kekklefruit (forcefully)",
"grow [number]",
async msg => {
let num: number;
if (msg.argv[1]) {
num = parseInt(msg.argv[1]);
if (isNaN(num)) return `Need number bozo`;
} else {
num = 1;
}
await KekklefruitTree.growFruit(num);
return `You grew ${num} fruit.`;
}
);

View File

@ -8,6 +8,7 @@ export const inventory = new Command(
"inventory",
msg => {
const items = msg.inventory.items as unknown as Item[];
console.log(msg.inventory);
const list = items
.map(
i =>

View File

@ -0,0 +1,23 @@
import { JsonArray, JsonValue } from "@prisma/client/runtime/library";
import { KekklefruitTree } from "../../../economy/kekkle";
import { Command } from "../../Command";
import { updateInventory } from "../../../data/inventory";
export const pick = new Command(
"pick",
["pick"],
"bozo will pick fruit off the kekklefruit tree",
"pick",
async msg => {
const fruit = await KekklefruitTree.pickFruit();
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);
return `(insert random boring message about ${fruit.name} here)`;
}
);

View File

@ -0,0 +1,12 @@
import { KekklefruitTree } from "../../../economy/kekkle";
import { Command } from "../../Command";
export const tree = new Command(
"tree",
["tree"],
"bozo will get the amount of fruit on the kekklefruit tree",
"tree",
async msg => {
return `There are ${KekklefruitTree.getFruitCount()} kekklefruit on the tree.`;
}
);

View File

@ -16,6 +16,9 @@ import { uptime } from "./commands/utility/uptime";
import { balance } from "./commands/economy/balance";
import { permissions } from "./commands/utility/permissions";
import { branch } from "./commands/utility/branch";
import { tree } from "./commands/economy/tree";
import { pick } from "./commands/economy/pick";
import { grow } from "./commands/economy/grow";
export function loadCommands() {
// cringe
@ -24,7 +27,7 @@ export function loadCommands() {
CommandHandler.addCommandGroup(general);
const economy = new CommandGroup("economy", "💸 Economy");
economy.addCommands([inventory, balance]);
economy.addCommands([inventory, balance, tree, pick, grow]);
CommandHandler.addCommandGroup(economy);
const fun = new CommandGroup("fun", "✨ Fun");

View File

@ -1,5 +1,7 @@
import { Inventory } from "@prisma/client";
import { prisma } from "./prisma";
import { JsonArray } from "@prisma/client/runtime/library";
import { Item, StackableItem } from "../economy/Item";
export async function createInventory(data: Omit<Inventory, "id">) {
await prisma.inventory.create({
@ -14,10 +16,34 @@ export async function readInventory(userId: Inventory["userId"]) {
return await prisma.inventory.findUnique({ where: { userId: userId } });
}
export function collapseInventory(inventoryData: Item[]) {
for (let i = 0; i < inventoryData.length; i++) {
if (i > 0) {
if (inventoryData[i].id == inventoryData[i - 1].id) {
if (
typeof (inventoryData[i - 1] as StackableItem).count ==
"number" &&
typeof (inventoryData[i] as StackableItem).count == "number"
) {
(inventoryData[i - 1] as StackableItem).count += (
inventoryData[i] as StackableItem
).count;
inventoryData.splice(i, 1);
i--;
}
}
}
}
}
export async function updateInventory(data: Omit<Inventory, "id">) {
collapseInventory(data.items as unknown as Item[]);
return await prisma.inventory.update({
where: { userId: data.userId },
data: {}
data: {
balance: data.balance,
items: data.items as any
}
});
}

View File

@ -1,3 +1,37 @@
import { PrismaClient } from "@prisma/client";
import { JsonObject } from "@prisma/client/runtime/library";
export const prisma = new PrismaClient();
export async function set(key: string, value: any) {
const store = await prisma.keyValueStore.findUnique({ where: { id: 1 } });
if (!store) {
// throw new Error("Unable to access key-value store.");
await prisma.keyValueStore.create({
data: {}
});
return set(key, value);
}
const data = store.data as JsonObject;
data[key] = value;
return;
}
export async function get<T = unknown>(key: string) {
const store = await prisma.keyValueStore.findUnique({ where: { id: 1 } });
if (!store) {
// throw new Error("Unable to access key-value store.");
await prisma.keyValueStore.create({
data: {}
});
return get(key);
}
const data = store.data as JsonObject;
return data[key] as T;
}

55
src/economy/kekkle.ts Normal file
View File

@ -0,0 +1,55 @@
import { get, set } from "../data/prisma";
import { Logger } from "../util/Logger";
import { FoodItem } from "./Item";
export class KekklefruitTree {
protected static fruit: number = 0;
public static logger = new Logger("Kekklefruit Tree");
public static async saveFruit() {
return set("kekklefruit-tree", this.fruit);
}
public static async loadFruit() {
let fruit = await get<number>("kekklefruit-tree");
let save = false;
if (!fruit) {
fruit = 0;
save = true;
}
this.fruit = fruit;
if (save) this.saveFruit();
}
public static async growFruit(amount: number = 1) {
this.fruit += amount;
await this.saveFruit();
}
public static getFruitCount() {
return this.fruit;
}
public static async pickFruit() {
if (this.fruit > 0) {
this.fruit--;
await this.saveFruit();
return this.randomFruit();
} else {
return undefined;
}
}
public static randomFruit() {
return {
id: "kekklefruit",
name: "Kekklefruit",
consumable: true,
edible: true
} as FoodItem;
}
}
await KekklefruitTree.loadFruit();