From aad5d37308004277e8d68623964b597aa811e666 Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Tue, 4 Jan 2022 20:03:19 -0600 Subject: [PATCH] Implement self-correcting economy --- scripts/server/economy.js | 86 +++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 13 deletions(-) diff --git a/scripts/server/economy.js b/scripts/server/economy.js index 801db35d..51d36c5f 100644 --- a/scripts/server/economy.js +++ b/scripts/server/economy.js @@ -42,6 +42,30 @@ function playerPayDay(client) { messagePlayerInfo(client, `Paycheck: {ALTCOLOUR}$${grossIncome}`); messagePlayerInfo(client, `Taxes: {ALTCOLOUR}$${incomeTaxAmount}`); messagePlayerInfo(client, `You receive: {ALTCOLOUR}$${netIncome}`); + if(netIncome < incomeTaxAmount) { + let totalCash = getPlayerCash(client); + let canPayNow = totalCash+netIncome; + if(incomeTaxAmount <= canPayNow) { + takePlayerCash(client, canPayNow); + messagePlayerInfo(client, `You covered the remaining taxes with {ALTCOLOUR}$${canPayNow} {MAINCOLOUR}in cash.`); + messagePlayerAlert(client, `{orange}You lost money since your taxes are more than your paycheck!`); + messagePlayerAlert(client, `{orange}If you don't have enough cash to cover taxes on next paycheck, you will lose stuff!`); + } else { + messagePlayerInfo(client, `{orange}You don't have enough cash to pay your taxes!`); + takePlayerCash(client, canPayNow); + + let vehicleCount = getAllVehiclesOwnedByPlayer(client).length; + let houseCount = getAllHousesOwnedByPlayer(client).length; + let businessCount = getAllBusinessesOwnedByPlayer(client).length; + + attemptRepossession(client, incomeTaxAmount-canPayNow); + + let newVehicleCount = getAllVehiclesOwnedByPlayer(client).length; + let newHouseCount = getAllHousesOwnedByPlayer(client).length; + let newBusinessCount = getAllBusinessesOwnedByPlayer(client).length; + messagePlayerInfo(client, `{orange}You lost ${newVehicleCount-vehicleCount} vehicles, ${newHouseCount-houseCount} houses, and ${newBusinessCount-businessCount} businesses to cover the remaining tax.`); + } + } givePlayerCash(client, netIncome); } @@ -87,28 +111,64 @@ function forcePlayerPayDayCommand(command, params, client) { // =========================================================================== function taxInfoCommand(command, params, client) { - if(areParamsEmpty(params)) { - messagePlayerSyntax(client, getCommandSyntaxText(command)); - return false; - } - let wealth = calculateWealth(client); let tax = calculateIncomeTax(wealth); - messagePlayerInfo(client, `Your tax on payday is: $${tax}. Use {ALTCOLOUR}/help tax {MAINCOLOUR}for more information.`); } // =========================================================================== function wealthInfoCommand(command, params, client) { - if(areParamsEmpty(params)) { - messagePlayerSyntax(client, getCommandSyntaxText(command)); - return false; - } - let wealth = calculateWealth(client); - messagePlayerInfo(client, `Your wealth is: $${wealth}. Use {ALTCOLOUR}/help wealth {MAINCOLOUR}for more information.`); } -// =========================================================================== \ No newline at end of file +// =========================================================================== + +function attemptRepossession(client, totalToPay) { + let leftToPay = totalToPay; + + while(leftToPay > 0) { + let repossessionValue = repossessFirstAsset(client); + leftToPay = leftToPay - repossessionValue; + } + return true; +} + +function repossessFirstAsset(client) { + let vehicles = getAllVehiclesOwnedByPlayer(client); + if(vehicles.length > 0) { + deleteVehicle(vehicles[0]) + return getGlobalConfig().economy.upKeepCosts.upKeepPerVehicle; + } + + let houses = getAllHousesOwnedByPlayer(client); + if(houses.length > 0) { + deleteHouse(houses[0].index); + return getGlobalConfig().economy.upKeepCosts.upKeepPerHouse; + } + + let businesses = getAllBusinessesOwnedByPlayer(client); + if(businesses.length > 0) { + deleteBusiness(businesses[0].index); + return getGlobalConfig().economy.upKeepCosts.upKeepPerBusiness; + } +} + +// =========================================================================== + +function getAllVehiclesOwnedByPlayer(client) { + return getServerData().vehicles.filter((v) => v.ownerType == VRR_VEHOWNER_PLAYER && v.ownerId == getPlayerCurrentSubAccount(client).databaseId); +} + +// =========================================================================== + +function getAllBusinessesOwnedByPlayer(client) { + return getServerData().businesses.filter((b) => b.ownerType == VRR_BIZOWNER_PLAYER && b.ownerId == getPlayerCurrentSubAccount(client).databaseId); +} + +// =========================================================================== + +function getAllHousesOwnedByPlayer(client) { + return getServerData().houses.filter((h) => h.ownerType == VRR_HOUSEOWNER_PLAYER && h.ownerId == getPlayerCurrentSubAccount(client).databaseId); +} \ No newline at end of file