forked from space-syndicate/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added bonus payment to management console
- Loading branch information
Showing
12 changed files
with
393 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
Content.Client/AWS/Economy/UI/ManagementConsole/Tabs/BonusTab.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,192 @@ | ||
using Robust.Client.AutoGenerated; | ||
using Robust.Client.UserInterface.XAML; | ||
using Robust.Client.UserInterface; | ||
using Content.Shared.AWS.Economy; | ||
using System.Linq; | ||
using Robust.Client.UserInterface.Controls; | ||
|
||
namespace Content.Client.AWS.Economy.UI.ManagementConsole.Tabs; | ||
|
||
[GenerateTypedNameReferences] | ||
public sealed partial class BonusTab : Control | ||
{ | ||
[Dependency] private readonly EntityManager _entityManager = default!; | ||
|
||
private IReadOnlyList<Entity<EconomyBankAccountComponent>> _cachedAccounts = default!; | ||
private IReadOnlyList<Entity<EconomyBankAccountComponent>> _payerAccounts = default!; | ||
private List<EconomyBankAccountComponent> _selectedAccounts = new(); | ||
private EconomyBankAccountComponent? _selectedPayer; | ||
|
||
public Action<string, float, List<string>>? OnPayBonusPressed; | ||
|
||
public bool Priveleged; | ||
|
||
public BonusTab() | ||
{ | ||
IoCManager.InjectDependencies(this); | ||
RobustXamlLoader.Load(this); | ||
|
||
Initialize(); | ||
|
||
FindAccount.OnTextEntered += OnTextEnteredAccount; | ||
|
||
BonusPercentBox.AddLeftButton(-10, "-10%"); | ||
BonusPercentBox.AddLeftButton(-1, "-1%"); | ||
BonusPercentBox.AddRightButton(1, "+1%"); | ||
BonusPercentBox.AddRightButton(10, "+10%"); | ||
BonusPercentBox.ValueChanged += _ => | ||
{ | ||
FillInfo(); | ||
}; | ||
|
||
PayBonusButton.OnPressed += _ => | ||
{ | ||
if (_selectedAccounts.Count <= 0 || _selectedPayer is null) | ||
return; | ||
|
||
var accountIDs = new List<string>(); | ||
foreach (var account in _selectedAccounts) | ||
accountIDs.Add(account.AccountID); | ||
|
||
var bonusPercent = BonusPercentBox.Value / 100f; | ||
OnPayBonusPressed?.Invoke(_selectedPayer.AccountID, bonusPercent, accountIDs); | ||
}; | ||
ClearSelectedButton.OnPressed += _ => | ||
{ | ||
if (_selectedAccounts.Count <= 0) | ||
return; | ||
|
||
PayBonusButton.Disabled = true; | ||
SelectedAccountList.Clear(); | ||
_selectedAccounts.Clear(); | ||
FillList(); | ||
FillInfo(); | ||
}; | ||
} | ||
|
||
public void Initialize() | ||
{ | ||
var bankAccountSystem = _entityManager.System<EconomyBankAccountSystemShared>(); | ||
var tags = new List<BankAccountTag>() { BankAccountTag.Personal }; | ||
_cachedAccounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, tags).Values.ToList(); | ||
|
||
var payerTags = new List<BankAccountTag>() { BankAccountTag.Station }; | ||
_payerAccounts = bankAccountSystem.GetAccounts(EconomyBankAccountMask.ByTags, payerTags).Values.ToList(); | ||
_selectedPayer = _payerAccounts.FirstOrDefault().Comp; | ||
|
||
FillList(); | ||
FillInfo(); | ||
} | ||
|
||
public void OnUpdateState(bool priveleged) | ||
{ | ||
Priveleged = priveleged; | ||
PayBonusButton.Disabled = !Priveleged || | ||
_selectedPayer == null || | ||
_selectedAccounts.Count <= 0 || | ||
CalculateBonusPayment() > _selectedPayer.Balance; | ||
|
||
FillList(); | ||
FillInfo(); | ||
} | ||
|
||
private void FillList() | ||
{ | ||
AccountList.Clear(); | ||
foreach (var (key, value) in _cachedAccounts) | ||
{ | ||
if (_selectedAccounts.Contains(value)) | ||
continue; | ||
|
||
var field = AccountList.AddItem(FormFieldName(value)); | ||
field.Metadata = value; | ||
field.OnSelected += OnSelectAccount; | ||
} | ||
|
||
AccountList.SortItemsByText(); | ||
} | ||
|
||
private string FormFieldName(EconomyBankAccountComponent account) | ||
{ | ||
return account.AccountID + " — " + account.AccountName; | ||
} | ||
|
||
private void UpdateSelectedAccounts() | ||
{ | ||
SelectedAccountList.Clear(); | ||
foreach (var account in _selectedAccounts) | ||
{ | ||
var field = SelectedAccountList.AddItem(FormFieldName(account)); | ||
field.Metadata = account; | ||
field.OnSelected += OnSelectSelectedAccount; | ||
} | ||
|
||
SelectedAccountList.SortItemsByText(); | ||
} | ||
|
||
private void OnSelectAccount(ItemList.Item accountId) | ||
{ | ||
if (accountId.Metadata is not EconomyBankAccountComponent account) | ||
return; | ||
|
||
PayBonusButton.Disabled = !Priveleged || _selectedPayer == null || CalculateBonusPayment() > _selectedPayer.Balance; | ||
|
||
_selectedAccounts.Add(account); | ||
UpdateSelectedAccounts(); | ||
FillList(); | ||
FillInfo(); | ||
} | ||
|
||
private void OnSelectSelectedAccount(ItemList.Item accountId) | ||
{ | ||
if (accountId.Metadata is not EconomyBankAccountComponent account) | ||
return; | ||
|
||
_selectedAccounts.Remove(account); | ||
UpdateSelectedAccounts(); | ||
FillList(); | ||
FillInfo(); | ||
} | ||
|
||
private void FillInfo() | ||
{ | ||
PayerAccountIDLabel.Text = _selectedPayer?.AccountID; | ||
PayerAccountBalanceLabel.Text = string.Format("{0:N0}", _selectedPayer?.Balance); | ||
TotalBonusLabel.Text = string.Format("{0:N0}", CalculateBonusPayment()); | ||
} | ||
|
||
private ulong CalculateBonusPayment() | ||
{ | ||
if (_selectedAccounts.Count <= 0) | ||
return 0; | ||
|
||
var bonusPercent = BonusPercentBox.Value / 100f; | ||
ulong total = 0; | ||
foreach (var account in _selectedAccounts) | ||
total += account.Salary is not null ? (ulong)(account.Salary * bonusPercent) : 0; | ||
|
||
return total; | ||
} | ||
|
||
private void OnTextEnteredAccount(LineEdit.LineEditEventArgs eventArgs) | ||
{ | ||
AccountList.Clear(); | ||
var upText = eventArgs.Text.ToUpper(); | ||
foreach (var (key, value) in _cachedAccounts) | ||
{ | ||
var fieldName = FormFieldName(value); | ||
if (fieldName.Contains(upText)) | ||
{ | ||
var field = AccountList.AddItem(fieldName); | ||
field.Metadata = value; | ||
field.OnSelected += OnSelectAccount; | ||
} | ||
} | ||
if (AccountList.Count == 0) | ||
{ | ||
AccountList.AddItem("No data acquired"); | ||
return; | ||
} | ||
AccountList.SortItemsByText(); | ||
} | ||
} |
Oops, something went wrong.