forge/arma/server/addons/task/functions/fnc_handleTaskRewards.sqf
Jacob Schmidt ff7ff0c4e5 Implement org credit line debt and bank repayment flow (#2)
## Summary

This finishes the org credit line workflow so it behaves like reserved treasury-backed credit instead of a simple member allowance.

## What changed

- reserve org funds immediately when a credit line is assigned
- track credit lines with:
  - approved amount
  - available amount
  - outstanding principal
  - interest rate
  - amount due
- consume reserved credit during store checkout without charging org funds a second time
- add credit line repayment through the bank app
- sync richer credit line state into org and bank payloads/UI
- keep legacy `amount` compatibility mapped to available credit for older consumers

## User-facing behavior

- assigning a credit line now reduces available org funds immediately
- spending on `credit_line` reduces available credit and creates debt with interest
- the bank app now shows outstanding credit debt and allows repayment from personal bank funds
- the org treasury view now shows reserved credit and outstanding due totals

## Validation

- `cargo fmt`
- `npm run build:webui`
- `cargo test -p forge-services --quiet`
- `cargo test -p forge-server --quiet`

## Follow-up checks

- validate in-game that assigning a credit line reduces org funds immediately
- validate store checkout with `credit_line` updates available credit and debt correctly
- validate bank repayment decreases player bank balance, increases org funds, and reduces amount due

Co-authored-by: Jacob Schmidt <innovativestudios@outlook.com>
Reviewed-on: #2
2026-04-02 16:50:38 -05:00

226 lines
7.7 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: IDSolutions
* Handles task completion rewards for organizations.
*
* Arguments:
* 0: Task ID <STRING>
* 1: Reward Data <HASHMAP>
* - funds: Amount of money to award <NUMBER>
* - equipment: Array of equipment classnames to award <ARRAY>
* - supplies: Array of supply classnames to award <ARRAY>
* - weapons: Array of weapon classnames to award <ARRAY>
* - vehicles: Array of vehicle classnames to award <ARRAY>
* - special: Array of special item classnames to award <ARRAY>
*
* Return Value:
* Success <BOOLEAN>
*
* Example:
* private _rewards = createHashMapFromArray [
* ["funds", 10000],
* ["reputation", 50],
* ["equipment", ["ItemGPS", "ItemCompass"]],
* ["supplies", ["FirstAidKit", "Medikit"]],
* ["weapons", ["arifle_MX_F"]],
* ["vehicles", ["B_MRAP_01_F"]],
* ["special", ["B_UAV_01_F"]]
* ];
* ["task_1", _rewards] call forge_server_task_fnc_handleTaskRewards;
*
* Public: No
*/
params [["_taskID", ""], ["_rewards", createHashMap]];
if (_taskID == "") exitWith {
["ERROR", "No task ID provided for rewards"] call EFUNC(common,log);
false
};
private _rewardContext = GVAR(TaskStore) call ["resolveRewardContext", [_taskID]];
private _requesterUid = _rewardContext getOrDefault ["requesterUid", ""];
private _orgID = _rewardContext getOrDefault ["orgID", ""];
private _memberUids = _rewardContext getOrDefault ["memberUids", []];
if (_orgID isEqualTo "") exitWith {
["ERROR", format ["No organization reward context found for task %1.", _taskID]] call EFUNC(common,log);
false
};
private _success = true;
private _funds = _rewards getOrDefault ["funds", 0];
private _rewardMessages = [];
private _resolveRewardLabel = {
params [["_className", "", [""]]];
if (_className isEqualTo "") exitWith { "" };
{
private _cfg = _x >> _className;
if (isClass _cfg) exitWith {
private _displayName = getText (_cfg >> "displayName");
[_displayName, _className] select (_displayName isEqualTo "");
};
} forEach [
configFile >> "CfgWeapons",
configFile >> "CfgMagazines",
configFile >> "CfgVehicles",
configFile >> "CfgGlasses"
];
_className
};
private _notifyMembers = {
params [["_type", "info", [""]], ["_title", "Tasks", [""]], ["_message", "", [""]]];
if (_message isEqualTo "") exitWith {};
{
private _player = [_x] call EFUNC(common,getPlayer);
if (isNull _player) then { continue; };
[CRPC(notifications,recieveNotification), [_type, _title, _message], _player] call CFUNC(targetEvent);
} forEach _memberUids;
};
private _syncOrgPatch = {
params [["_patch", createHashMap, [createHashMap]]];
if (_patch isEqualTo createHashMap) exitWith {};
{
private _player = [_x] call EFUNC(common,getPlayer);
if (isNull _player) then { continue; };
[CRPC(org,responseSyncOrg), [_patch], _player] call CFUNC(targetEvent);
} forEach _memberUids;
};
if (_funds > 0) then {
private _org = EGVAR(org,OrgStore) call ["loadById", [_orgID]];
if (_org isEqualTo createHashMap) then {
["ERROR", format ["Failed to load organization %1 for task %2 funds reward.", _orgID, _taskID]] call EFUNC(common,log);
_success = false;
} else {
private _nextFunds = (_org getOrDefault ["funds", 0]) + _funds;
_org set ["funds", _nextFunds];
private _updatedOrg = EGVAR(org,OrgStore) call [
"callHotOrg",
[
"org:hot:override",
[_orgID, toJSON _org]
]
];
if (_updatedOrg isEqualTo createHashMap) then {
["ERROR", format ["Failed to update organization %1 funds for task %2.", _orgID, _taskID]] call EFUNC(common,log);
_success = false;
} else {
private _patch = createHashMapFromArray [["funds", _nextFunds]];
[_patch] call _syncOrgPatch;
_rewardMessages pushBack format ["$%1 org funds", [_funds] call EFUNC(common,formatNumber)];
};
};
};
private _grantOrgAssets = {
params [["_category", "items", [""]], ["_items", [], [[]]]];
if (_items isEqualTo []) exitWith {};
private _assetEntries = _items apply {
createHashMapFromArray [
["classname", _x],
["category", _category],
["quantity", 1]
]
};
private _grantResult = EGVAR(org,OrgStore) call ["addAssets", [_requesterUid, _assetEntries, false, _orgID]];
if !(_grantResult getOrDefault ["success", false]) then {
["ERROR", format ["Failed to award %1 assets for task %2: %3", _category, _taskID, _grantResult getOrDefault ["message", "Unknown error."]]] call EFUNC(common,log);
_success = false;
} else {
[_grantResult getOrDefault ["patch", createHashMap]] call _syncOrgPatch;
private _labels = _items apply { [_x] call _resolveRewardLabel };
_rewardMessages pushBack format ["%1: %2", _category, _labels joinString ", "];
};
};
private _grantOrgFleet = {
params [["_vehicles", [], [[]]]];
if (_vehicles isEqualTo []) exitWith {};
private _vehicleEntries = _vehicles apply {
private _category = "other";
if (_x isKindOf "Car") then { _category = "cars"; };
if (_x isKindOf "Tank") then { _category = "armor"; };
if (_x isKindOf "Helicopter") then { _category = "helis"; };
if (_x isKindOf "Plane") then { _category = "planes"; };
if (_x isKindOf "Ship") then { _category = "naval"; };
createHashMapFromArray [
["classname", _x],
["category", _category]
]
};
private _fleetResult = EGVAR(org,OrgStore) call ["addFleetVehicles", [_requesterUid, _vehicleEntries, false, _orgID]];
if !(_fleetResult getOrDefault ["success", false]) then {
["ERROR", format ["Failed to award vehicle rewards for task %2: %1", _fleetResult getOrDefault ["message", "Unknown error."], _taskID]] call EFUNC(common,log);
_success = false;
} else {
[_fleetResult getOrDefault ["patch", createHashMap]] call _syncOrgPatch;
private _labels = _vehicles apply { [_x] call _resolveRewardLabel };
_rewardMessages pushBack format ["vehicles: %1", _labels joinString ", "];
};
};
private _equipment = _rewards getOrDefault ["equipment", []];
if (count _equipment > 0) then {
["equipment", _equipment] call _grantOrgAssets;
};
private _supplies = _rewards getOrDefault ["supplies", []];
if (count _supplies > 0) then {
["supplies", _supplies] call _grantOrgAssets;
};
private _weapons = _rewards getOrDefault ["weapons", []];
if (count _weapons > 0) then {
["weapons", _weapons] call _grantOrgAssets;
};
private _special = _rewards getOrDefault ["special", []];
if (count _special > 0) then {
["special", _special] call _grantOrgAssets;
};
private _vehicles = _rewards getOrDefault ["vehicles", []];
if (count _vehicles > 0) then {
[_vehicles] call _grantOrgFleet;
};
if (_success) then {
private _orgName = "";
private _org = EGVAR(org,OrgStore) call ["loadById", [_orgID]];
if (_org isNotEqualTo createHashMap) then {
_orgName = _org getOrDefault ["name", _orgID];
};
if (_orgName isEqualTo "") then { _orgName = _orgID; };
private _message = format ["Task rewards added to %1.", _orgName];
if (_rewardMessages isNotEqualTo []) then {
_message = format ["%1 %2", _message, _rewardMessages joinString ", "];
};
["INFO", _message] call EFUNC(common,log);
["success", "Tasks", _message] call _notifyMembers;
} else {
["warning", "Tasks", format ["Task %1 completed, but one or more org rewards failed to apply.", _taskID]] call _notifyMembers;
};
_success