Telecrystals (and a bit more ECS) (#4775)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Shared.Traitor.Uplink;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Server.Traitor.Uplink.Account
|
||||
{
|
||||
/// <summary>
|
||||
/// Manage all registred uplink accounts and their balance
|
||||
/// </summary>
|
||||
public class UplinkAccountsSystem : EntitySystem
|
||||
{
|
||||
[Dependency]
|
||||
private readonly UplinkListingSytem _listingSystem = default!;
|
||||
|
||||
private readonly HashSet<UplinkAccount> _accounts = new();
|
||||
|
||||
public bool AddNewAccount(UplinkAccount acc)
|
||||
{
|
||||
return _accounts.Add(acc);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Add TC to uplinks account balance
|
||||
/// </summary>
|
||||
public bool AddToBalance(UplinkAccount account, int toAdd)
|
||||
{
|
||||
account.Balance += toAdd;
|
||||
|
||||
RaiseLocalEvent(new UplinkAccountBalanceChanged(account, toAdd));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Charge TC from uplinks account balance
|
||||
/// </summary>
|
||||
public bool RemoveFromBalance(UplinkAccount account, int price)
|
||||
{
|
||||
if (account.Balance - price < 0)
|
||||
return false;
|
||||
|
||||
account.Balance -= price;
|
||||
|
||||
RaiseLocalEvent(new UplinkAccountBalanceChanged(account, -price));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Force-set TC uplinks account balance to a new value
|
||||
/// </summary>
|
||||
public bool SetBalance(UplinkAccount account, int newBalance)
|
||||
{
|
||||
if (newBalance < 0)
|
||||
return false;
|
||||
|
||||
var dif = newBalance - account.Balance;
|
||||
account.Balance = newBalance;
|
||||
RaiseLocalEvent(new UplinkAccountBalanceChanged(account, dif));
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public bool TryPurchaseItem(UplinkAccount acc, string itemId, EntityCoordinates spawnCoords, [NotNullWhen(true)] out IEntity? purchasedItem)
|
||||
{
|
||||
purchasedItem = null;
|
||||
|
||||
if (!_listingSystem.TryGetListing(itemId, out var listing))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (acc.Balance < listing.Price)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!RemoveFromBalance(acc, listing.Price))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
purchasedItem = EntityManager.SpawnEntity(listing.ItemId, spawnCoords);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user