Make funding allocation computer more configurable (#36790)

* Make funding allocation computer more configurable

* admin logging

* unused

* ccvar enabled

---------

Co-authored-by: ScarKy0 <scarky0@onet.eu>
This commit is contained in:
pathetic meowmeow
2025-04-22 08:34:53 -04:00
committed by GitHub
parent 1197d9b038
commit 907f4b39cd
11 changed files with 155 additions and 26 deletions

View File

@@ -1,12 +1,15 @@
using System.Collections.Generic;
using System.Linq;
using Content.Client.Message;
using Content.Client.UserInterface.Controls;
using Content.Shared.Cargo.Components;
using Content.Shared.Cargo.Prototypes;
using Content.Shared.CCVar;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
@@ -15,14 +18,21 @@ namespace Content.Client.Cargo.UI;
[GenerateTypedNameReferences]
public sealed partial class FundingAllocationMenu : FancyWindow
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly EntityQuery<StationBankAccountComponent> _bankQuery;
public event Action<Dictionary<ProtoId<CargoAccountPrototype>, int>>? OnSavePressed;
public event Action<Dictionary<ProtoId<CargoAccountPrototype>, int>, double, double>? OnSavePressed;
private EntityUid? _station;
private bool _allowPrimaryAccountAllocation;
private bool _allowPrimaryCutAdjustment;
private bool _lockboxCutEnabled;
private double _primaryCut;
private double _lockboxCut;
private readonly HashSet<Control> _addedControls = new();
private readonly List<SpinBox> _spinBoxes = new();
@@ -35,30 +45,65 @@ public sealed partial class FundingAllocationMenu : FancyWindow
_bankQuery = _entityManager.GetEntityQuery<StationBankAccountComponent>();
PrimaryCut.ValueChanged += args =>
{
_primaryCut = (double)args.Value / 100.0;
UpdateButtonDisabled();
};
LockboxCut.ValueChanged += args =>
{
_lockboxCut = 1.0 - (double)args.Value / 100.0;
UpdateButtonDisabled();
};
SaveButton.OnPressed += _ =>
{
if (!_entityManager.TryGetComponent<StationBankAccountComponent>(_station, out var bank))
return;
var accounts = bank.Accounts.Keys.OrderBy(p => p.Id).ToList();
var accounts = EditableAccounts(bank).OrderBy(p => p.Key).Select(p => p.Key).ToList();
var dicts = new Dictionary<ProtoId<CargoAccountPrototype>, int>();
for (var i = 0; i< accounts.Count; i++)
{
dicts.Add(accounts[i], _spinBoxes[i].Value);
}
OnSavePressed?.Invoke(dicts);
OnSavePressed?.Invoke(dicts, _primaryCut, _lockboxCut);
SaveButton.Disabled = true;
};
_cfg.OnValueChanged(CCVars.AllowPrimaryAccountAllocation, enabled => { _allowPrimaryAccountAllocation = enabled; }, true);
_cfg.OnValueChanged(CCVars.AllowPrimaryCutAdjustment, enabled => { _allowPrimaryCutAdjustment = enabled; }, true);
_cfg.OnValueChanged(CCVars.LockboxCutEnabled, enabled => { _lockboxCutEnabled = enabled; }, true);
BuildEntries();
}
private IEnumerable<KeyValuePair<ProtoId<CargoAccountPrototype>, int>> EditableAccounts(StationBankAccountComponent bank)
{
foreach (var kvp in bank.Accounts)
{
if (_allowPrimaryAccountAllocation || kvp.Key != bank.PrimaryAccount)
{
yield return kvp;
}
}
}
private void BuildEntries()
{
if (!_entityManager.TryGetComponent<StationBankAccountComponent>(_station, out var bank))
return;
HelpLabel.Text = Loc.GetString("cargo-funding-alloc-console-label-help",
("percent", (int) (bank.PrimaryCut * 100)));
if (_allowPrimaryCutAdjustment)
{
HelpLabel.Text = Loc.GetString("cargo-funding-alloc-console-label-help-adjustible");
}
else
{
HelpLabel.Text = Loc.GetString("cargo-funding-alloc-console-label-help-non-adjustible",
("percent", (int) (bank.PrimaryCut * 100)));
}
foreach (var ctrl in _addedControls)
{
@@ -69,7 +114,21 @@ public sealed partial class FundingAllocationMenu : FancyWindow
_spinBoxes.Clear();
_balanceLabels.Clear();
var accounts = bank.Accounts.ToList().OrderBy(p => p.Key);
_primaryCut = bank.PrimaryCut;
_lockboxCut = bank.LockboxCut;
LockboxCut.OverrideValue(100 - (int)(_lockboxCut * 100));
PrimaryCut.OverrideValue((int)(_primaryCut * 100));
LockboxCut.IsValid = val => val is >= 0 and <= 100;
PrimaryCut.IsValid = val => val is >= 0 and <= 100;
LockboxCut.Visible = _lockboxCutEnabled;
LockboxCutLabel.Visible = _lockboxCutEnabled;
PrimaryCut.Visible = _allowPrimaryCutAdjustment;
PrimaryCutLabel.Visible = _allowPrimaryCutAdjustment;
var accounts = EditableAccounts(bank).OrderBy(p => p.Key);
foreach (var (account, balance) in accounts)
{
var accountProto = _prototypeManager.Index(account);
@@ -127,7 +186,7 @@ public sealed partial class FundingAllocationMenu : FancyWindow
var incorrectSum = sum != 100;
var differs = false;
var accounts = bank.Accounts.Keys.OrderBy(p => p.Id).ToList();
var accounts = EditableAccounts(bank).OrderBy(p => p.Key).Select(p => p.Key).ToList();
for (var i = 0; i < accounts.Count; i++)
{
var percent = _spinBoxes[i].Value;
@@ -137,6 +196,7 @@ public sealed partial class FundingAllocationMenu : FancyWindow
break;
}
}
differs = differs || _primaryCut != bank.PrimaryCut || _lockboxCut != bank.LockboxCut;
SaveButton.Disabled = !differs || incorrectSum;