economyFuncs.js

An incomplete assortment of functions to make my life easier when building my economy system
 avatar
unknown
javascript
a year ago
3.5 kB
5
No Index
import * as lists from '../../utils/lists.js';
import User from '../../../@api/db/models/discord/DiscordAccount.js';

const { jobs, items, realestate, vehicles } = lists;

const compareFromList = async (listName, key) => {
    const list = lists[listName];
    return list ? list[key] : undefined;
}

const updateBalance = async (user, accountType, action, amount) => {
    const userID = user;
    try {
        const userAccount = await User.findOne({ userID });
        if (!userAccount) throw new Error('Account not found');

        if (action === 'add') {
            userAccount.lifesystem.economy.balance[accountType] += amount;
        } else if (action === 'subtract') {
            if (userAccount.lifesystem.economy.balance[accountType] < amount)
                userAccount.lifesystem.economy.balance[accountType] -= amount;
        }

        await userAccount.save();
    } catch (err) {
        console.error(err);
    }
}

const completeTransaction = async (user, amount, buyingChips) => {
    const userID = user;
    const userAccount = await User.findOne({ userID });
    const { balance } = userAccount.lifesystem.economy;
    const { wallet, bank } = balance;

    try {
        if (buyingChips) {
            if (bank < amount) throw new Error('Insufficient funds in bank');
            await updateBalance(userID, 'bank', 'subtract', amount);
            await updateBalance(userID, 'casinochips', 'add', amount);
        } else {
            if (wallet >= amount) {
                await updateBalance(userID, 'wallet', 'subtract', amount);
            } else {
                const remainingAmount = amount - wallet;
                if (bank < remainingAmount) throw new Error('Insufficient funds');

                await updateBalance(userID, 'wallet', 'subtract', wallet);
                await updateBalance(userID, 'bank', 'subtract', remainingAmount);
            }
            await updateBalance(userID, 'casinochips', 'subtract', amount);
        }
    }
    catch (err) {
        throw new Error(err);
    }
}

const buyItem = async (user, item, amount) => {
    const userID = user;
    const UserData = await User.findOne({ userID });
    const amt = amount || 1
    if (!UserData) {
        throw new Error('User not found');
    }
    const { balance, inventory } = UserData.lifesystem.economy;
    const { wallet, bank } = balance;
    const itemData = await compareFromList('items', `${item}`);
    //{
    //    "name": "Item Name",
    //    "price": 100,
    //    "amount": 1,
    //    "emoji": "emoji"
    //    //Booleans...//
    //}
    if (!itemData) {
        throw new Error('Item not found');
    }

    try {
        if (itemData.price * amt > wallet) {
            if (bank < itemData.price * amt - wallet) {
                throw new Error('Insufficient funds');
            }
            await completeTransaction(userID, itemData.price * amt, false);
        } else {
            await completeTransaction(userID, itemData.price * amt, true);
        }

        // Add item to inventory
        if (inventory.has(item)) {
            inventory.get(item).amount += amt;
        } else {
            inventory.set(item, {
                amount: amt
            });
        }
        await UserData.save();
    } catch (err) {
        throw new Error(err);
    }
}

export {
    compareFromList,
    updateBalance,
    completeTransaction,
    buyItem
}
Editor is loading...
Leave a Comment