Skip to content

Commit

Permalink
Code cleanup && docs
Browse files Browse the repository at this point in the history
  • Loading branch information
murlodev committed Dec 20, 2024
1 parent 805bbd0 commit 9806593
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 140 deletions.
3 changes: 3 additions & 0 deletions Content.Client/AWS/Economy/IClientEconomyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public interface IClientEconomyManager : ISharedEconomyManager
/// </summary>
event EventHandler AccountUpdateReceived;

/// <summary>
/// Requests the server to send the account list.
/// </summary>
void AccountUpdateRequest();
}
77 changes: 0 additions & 77 deletions Content.Server/AWS/Economy/EconomyBankAccountSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Content.Server.Popups;
using Content.Shared.Emag.Components;
using Content.Shared.Emag.Systems;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Random;
using Robust.Shared.Prototypes;
Expand Down Expand Up @@ -43,7 +42,6 @@ public override void Initialize()
SubscribeLocalEvent<EconomyBankATMComponent, InteractUsingEvent>(OnATMInteracted);
SubscribeLocalEvent<EconomyBankATMComponent, EconomyBankATMWithdrawMessage>(OnATMWithdrawMessage);
SubscribeLocalEvent<EconomyBankATMComponent, EconomyBankATMTransferMessage>(OnATMTransferMessage);
/*SubscribeLocalEvent<EconomyBankAccountComponent, ComponentStartup>(OnAccountComponentStartup);*/
}

private string GenerateAccountId(string prefix, uint strik, uint numbersPerStrik, string? descriptor)
Expand Down Expand Up @@ -71,19 +69,12 @@ private string GenerateAccountId(string prefix, uint strik, uint numbersPerStrik
[PublicAPI]
public bool TryActivate(Entity<EconomyBankAccountComponent> entity)
{
// if (entity.Comp.ActivateOnSpawn)
// {
// entity.Comp.Activated = true;
// return true;
// }

if (!_prototypeManager.TryIndex(entity.Comp.AccountIdByProto, out EconomyAccountIdPrototype? proto))
return false;

var accountID = GenerateAccountId(proto.Prefix, proto.Streak, proto.NumbersPerStreak, proto.Descriptior);
var accountName = entity.Comp.AccountName;
var balance = (ulong)0;
//entity.Comp.AccountId = GenerateAccountId(proto.Prefix, proto.Streak, proto.NumbersPerStreak, proto.Descriptior);

if (TryComp<IdCardComponent>(entity, out var idCardComponent))
accountName = idCardComponent.FullName ?? entity.Comp.AccountName;
Expand Down Expand Up @@ -137,16 +128,6 @@ public bool TryActivate(Entity<EconomyBankAccountComponent> entity)
return false;
}

// [PublicAPI]
// public EconomyBankAccountComponent? FindAccountById(string id)
// {
// var accounts = GetAccounts();
// if (accounts.TryGetValue(id, out var comp))
// return comp;

// return null;
// }

private void Withdraw(EconomyBankAccountComponent component, EconomyBankATMComponent atm, ulong sum)
{
if (!_economyManager.TryChangeAccountBalance(component.AccountID, sum, false))
Expand Down Expand Up @@ -194,30 +175,6 @@ public Entity<EconomyMoneyHolderComponent> DropMoneyHandler(EntProtoId<EconomyMo
return (ent, moneyHolderComp);
}

// private void SendMoney(IEconomyMoneyHolder fromAccount, EconomyBankAccountComponent toSend, ulong amount)
// {
// fromAccount.Balance -= amount;
// toSend.Balance += amount;

// string senderAccoutId = "UNEXPECTED";
// if (fromAccount is EconomyBankAccountComponent)
// {
// var fromAccountComponent = (fromAccount as EconomyBankAccountComponent)!;
// fromAccountComponent.Logs.Add(new(_gameTiming.CurTime, Loc.GetString("economybanksystem-log-send-to",
// ("amount", amount), ("currencyName", toSend.AllowCurrency), ("accountId", toSend.AccountID))));

// senderAccoutId = fromAccountComponent.AccountID;
// }
// toSend.Logs.Add(new(_gameTiming.CurTime, Loc.GetString("economybanksystem-log-send-from",
// ("amount", amount), ("currencyName", toSend.AllowCurrency), ("accountId", senderAccoutId))));

// _entManager.Dirty((fromAccount as Component)!);
// _entManager.Dirty(toSend);
// }

/* TODO:
public void AddLog(EconomyBankAccountComponent comp, )*/

[PublicAPI]
public bool TrySendMoney(IEconomyMoneyHolder fromAccount, EconomyBankAccountComponent? recipientAccount, ulong amount, [NotNullWhen(false)] out string? errorMessage)
{
Expand All @@ -227,12 +184,6 @@ public bool TrySendMoney(IEconomyMoneyHolder fromAccount, EconomyBankAccountComp
{
if (recipientAccount is not null)
{
// if (fromAccount == recipientAccount)
// {
// errorMessage = "407";
// return false;
// }
// SendMoney(fromAccount, recipientAccount, amount);
return _economyManager.TryChangeAccountBalance(recipientAccount.AccountID, amount);
}

Expand Down Expand Up @@ -316,34 +267,6 @@ public bool TrySendMoney(string fromAccountId, string recipientAccountId, ulong
return _economyManager.TryTransferMoney(fromAccountId, recipientAccountId, amount);
}

// [PublicAPI]
// public Dictionary<string, EconomyBankAccountComponent> GetAccounts(EconomyBankAccountMask flag = EconomyBankAccountMask.NotBlocked)
// {
// Dictionary<string, EconomyBankAccountComponent> list = new();

// var accountsEnum = AllEntityQuery<EconomyBankAccountComponent>();
// while (accountsEnum.MoveNext(out var comp))
// {
// switch (flag)
// {
// case EconomyBankAccountMask.Activated:
// if (comp.Activated)
// list.Add(comp.AccountId, comp);
// break;
// case EconomyBankAccountMask.ActivatedBlocked:
// if (comp.Activated && comp.Blocked)
// list.Add(comp.AccountId, comp);
// break;
// case EconomyBankAccountMask.ActivatedNotBlocked:
// if (comp.Activated && !comp.Blocked)
// list.Add(comp.AccountId, comp);
// break;
// }
// }

// return list;
// }

private void OnAccountComponentInit(Entity<EconomyBankAccountComponent> entity, ref ComponentInit args)
{
// if has id card comp, then it will be initialized in other place
Expand Down
6 changes: 1 addition & 5 deletions Content.Server/AWS/Economy/EconomyPayDayRule.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System.Linq;
using Content.Shared.Dataset;
using Content.Server.Ghost.Roles.Components;
using Content.Server.StationEvents.Components;
using Content.Shared.GameTicking.Components;
using Content.Shared.Random.Helpers;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Content.Shared.AWS.Economy;
Expand All @@ -22,6 +17,7 @@ public sealed class EconomyPayDayRule : StationEventSystem<EconomyPayDayRuleComp
[Dependency] private readonly IEconomyManager _economyManager = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IRobustRandom _random = default!;

protected override void Started(EntityUid uid, EconomyPayDayRuleComponent ruleComponent, GameRuleComponent gameRule, GameRuleStartedEvent args)
{
var accounts = _economyManager.GetAccounts();
Expand Down
14 changes: 13 additions & 1 deletion Content.Server/AWS/Economy/IEconomyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ public interface IEconomyManager : ISharedEconomyManager
/// <summary>
/// Adds account to the account list.
/// </summary>
/// <param name="account"></param>
/// <returns>True if the account was successfully added, false otherwise.</returns>
bool TryAddAccount(EconomyBankAccount account);

/// <summary>
/// Changes the balance of the account.
/// </summary>
/// <param name="addition">Whether to add or substract the given amount.</param>
/// <returns></returns>
bool TryChangeAccountBalance(string accountID, ulong amount, bool addition = true);

/// <summary>
/// Transfer money from one account to another.
/// </summary>
/// <returns>True if the transfer was successful, false otherwise.</returns>
bool TryTransferMoney(string senderID, string receiverID, ulong amount);

/// <summary>
/// Adds a log to the account.
/// </summary>
void AddLog(string accountID, EconomyBankAccountLogField log);
}
36 changes: 7 additions & 29 deletions Content.Shared/AWS/Economy/EconomyBankAccountComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Robust.Shared.Prototypes;
using Content.Shared.Store;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;

Expand All @@ -8,47 +7,26 @@ namespace Content.Shared.AWS.Economy
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class EconomyBankAccountComponent : Component
{
// [ViewVariables(VVAccess.ReadWrite), DataField(required: true)]
// public ProtoId<CurrencyPrototype> AllowCurrency = "Thaler";
[ViewVariables(VVAccess.ReadWrite), DataField(required: true)]
public ProtoId<EconomyAccountIdPrototype> AccountIdByProto = "Nanotrasen";

[ViewVariables(VVAccess.ReadWrite), DataField(required: true)]
public EntProtoId<EconomyMoneyHolderComponent> MoneyHolderEntId = "ThalerHolder";

// [ViewVariables(VVAccess.ReadWrite), DataField]
// [AutoNetworkedField]
// public ulong Balance { get; set; } = 0;
// [ViewVariables(VVAccess.ReadWrite), DataField]
// [AutoNetworkedField]
// public ulong Penalty = 0;

/// <summary>
/// Set this up in <cref>EconomyBankAccountSetup</cref> to define the account, which this card will be using (referring to).
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
[AutoNetworkedField]
public string AccountID = "NO VALUE";

/// <summary>
/// Set this up in <cref>EconomyBankAccountSetup</cref> to define the name, which this card will be using.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
[AutoNetworkedField]
public string AccountName = "UNEXPECTED USER";

// [ViewVariables(VVAccess.ReadWrite)]
// [AutoNetworkedField]
// public bool Blocked = false;

// [ViewVariables(VVAccess.ReadOnly), DataField]
// public bool ActivateOnSpawn = false;

// [ViewVariables(VVAccess.ReadWrite)]
// [AutoNetworkedField]
// public bool Activated = false;

// [ViewVariables(VVAccess.ReadWrite)]
// [AutoNetworkedField]
// public bool CanReachPayDay = true;

// [ViewVariables(VVAccess.ReadWrite)]
// [AutoNetworkedField]
// public List<EconomyBankAccountLogField> Logs = new();

/// <summary>
/// Use this in prototypes for defining the account, which this card will be using (the account will be initialized on spawn).
/// Also, parameters beyond AccountName can be used with IDCards (if you want to setup other currency, for example).
Expand Down
26 changes: 0 additions & 26 deletions Content.Shared/AWS/Economy/EconomyBankAccountSystemShared.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.Utility;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Access.Components;
using Content.Shared.Examine;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Content.Shared.Interaction;
using Content.Shared.VendingMachines;
using Content.Shared.Popups;
using Robust.Shared.Network;
using Robust.Shared.Timing;
using JetBrains.Annotations;

namespace Content.Shared.AWS.Economy
{
public class EconomyBankAccountSystemShared : EntitySystem
{
[Dependency] protected readonly EntityManager _entManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly SharedUserInterfaceSystem _userInterfaceSystem = default!;
[Dependency] private readonly ISharedEconomyManager _economyManager = default!;

Expand All @@ -34,27 +20,15 @@ public override void Initialize()
SubscribeLocalEvent<EconomyBankTerminalComponent, ExaminedEvent>(OnBankTerminalExamine);
SubscribeLocalEvent<EconomyBankTerminalComponent, EconomyTerminalMessage>(OnTerminalMessage);

//SubscribeLocalEvent<EconomyBankAccountComponent, ComponentInit>(OnBankAccountComponentInit);
SubscribeLocalEvent<EconomyBankAccountComponent, ExaminedEvent>(OnBankAccountExamine);
SubscribeLocalEvent<EconomyMoneyHolderComponent, ExaminedEvent>(OnMoneyHolderExamine);

SubscribeLocalEvent<EconomyBankATMComponent, ComponentInit>(OnATMComponentInit);
SubscribeLocalEvent<EconomyBankATMComponent, ComponentRemove>(OnATMComponentRemove);
SubscribeLocalEvent<EconomyBankATMComponent, EntInsertedIntoContainerMessage>(OnATMItemSlotChanged);
SubscribeLocalEvent<EconomyBankATMComponent, EntRemovedFromContainerMessage>(OnATMItemSlotChanged);
//SubscribeLocalEvent<EconomyBankATMComponent, EconomyBankATMWithdrawMessage>(OnATMWithdrawMessage);
//SubscribeLocalEvent<EconomyBankATMComponent, EconomyBankATMTransferMessage>(OnATMTransferMessage);
}

// private void OnBankAccountComponentInit(EntityUid uid, EconomyBankAccountComponent comp, ComponentInit args)
// {
// if (comp.ActivateOnSpawn)
// {
// comp.Activated = true;
// Dirty(uid, comp);
// }
// }

private void OnBankAccountExamine(Entity<EconomyBankAccountComponent> entity, ref ExaminedEvent args)
{
if (!_economyManager.TryGetAccount(entity.Comp.AccountID, out var account))
Expand Down
3 changes: 3 additions & 0 deletions Content.Shared/AWS/Economy/IEconomyMoneyHolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Content.Shared.AWS.Economy
{
/// <summary>
/// Interface for components that hold money. Not to ones that can carry a link to the account (like a card).
/// </summary>
public interface IEconomyMoneyHolder
{
public ulong Balance { get; set; }
Expand Down
9 changes: 8 additions & 1 deletion Content.Shared/AWS/Economy/ISharedEconomyManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;

namespace Content.Shared.AWS.Economy;

public interface ISharedEconomyManager
{
/// <summary>
/// Checks if the account exists (valid).
/// </summary>
/// <returns>True if the account exists, false otherwise.</returns>
bool IsValidAccount(string accountID);

/// <summary>
/// Tries to get the account with the given ID.
/// </summary>
/// <returns>True if the fetching was successful, false otherwise.</returns>
bool TryGetAccount(string accountID, [NotNullWhen(true)] out EconomyBankAccount? account);

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion Content.Shared/AWS/Economy/MsgEconomyAccountList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using System.Collections.ObjectModel;
using Lidgren.Network;
using Robust.Shared.Network;
using Robust.Shared.Serialization;
Expand Down

0 comments on commit 9806593

Please sign in to comment.