Moving PDA to ECS (#4538)

* Moved pen slot to separate component

* Moved it all to more generic item slot class

* Add sounds

* Item slots now supports many slots

* Some clean-up

* Refactored slots a bit

* Moving ID card out

* Moving pda to system

* Moving PDA owner to ECS

* Moved PDA flashlight to separate component

* Toggle lights work through events

* Fixing UI

* Moving uplink to separate component

* Continue moving uplink to separate component

* More cleaning

* Removing pda shared

* Nuked shared pda component

* Fixed flashlight

* Pen slot now showed in UI

* Light toggle now shows correctly in UI

* Small refactoring of item slots

* Added contained entity

* Fixed tests

* Finished with PDA

* Moving PDA uplink to separate window

* Adding-removing uplink should show new button

* Working on a better debug

* Debug command to add uplink

* Uplink send state to UI

* Almost working UI

* Uplink correcty updates when you buy-sell items

* Ups

* Moved localization to separate file

* Minor fixes

* Removed item slots methods events

* Removed PDA owner name

* Removed one uplink event

* Deleted all uplink events

* Removed flashlight events

* Update Content.Shared/Traitor/Uplink/UplinkVisuals.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/GameTicking/Presets/PresetTraitorDeathMatch.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Item slots system review

* Flashlight review

* PDA to XAML

* Move UplinkMenu to seperate class, fix WeightedColors methods

* Move UI to XAML

* Moved events to entity id

* Address review

* Removed uplink extensions

* Minor fix

* Moved item slots to shared

* My bad Robust...

* Fixed pda sound

* Fixed pda tests

* Fixed pda test again

Co-authored-by: Alexander Evgrashin <evgrashin.adl@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Visne <vincefvanwijk@gmail.com>
This commit is contained in:
Alex Evgrashin
2021-10-03 07:05:52 +03:00
committed by GitHub
parent 39270d3ec7
commit e5df8dbee3
50 changed files with 1815 additions and 1313 deletions

View File

@@ -0,0 +1,93 @@
using Content.Server.Administration;
using Content.Server.PDA.Managers;
using Content.Server.Traitor.Uplink.Components;
using Content.Server.Traitor.Uplink.Systems;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using Content.Shared.Traitor.Uplink;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Traitor.Uplink.Commands
{
[AdminCommand(AdminFlags.Fun)]
public class AddUplinkCommand : IConsoleCommand
{
public string Command => "adduplink";
public string Description => "Creates uplink on selected item and link it to users account";
public string Help => "Usage: adduplink <username> <item-id>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 1)
{
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
return;
}
// Get player entity
if (!IoCManager.Resolve<IPlayerManager>().TryGetSessionByUsername(args[0], out var session))
{
shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist"));
return;
}
if (session.AttachedEntity == null)
{
shell.WriteLine(Loc.GetString("Selected player doesn't controll any entity"));
return;
}
var user = session.AttachedEntity;
// Get target item
IEntity? uplinkEntity = null;
var entityManager = IoCManager.Resolve<IEntityManager>();
if (args.Length >= 2)
{
if (!int.TryParse(args[1], out var itemID))
{
shell.WriteLine(Loc.GetString("shell-entity-uid-must-be-number"));
return;
}
var eUid = new EntityUid(itemID);
if (!eUid.IsValid() || !entityManager.EntityExists(eUid))
{
shell.WriteLine(Loc.GetString("shell-invalid-entity-id"));
return;
}
uplinkEntity = entityManager.GetEntity(eUid);
}
// Get TC count
var configManager = IoCManager.Resolve<IConfigurationManager>();
var tcCount = configManager.GetCVar(CCVars.TraitorStartingBalance);
// Get account
var uplinkManager = IoCManager.Resolve<IUplinkManager>();
if (!uplinkManager.TryGetAccount(user.Uid, out UplinkAccount? uplinkAccount))
{
uplinkAccount = new UplinkAccount(user.Uid, tcCount);
if (!uplinkManager.AddNewAccount(uplinkAccount))
{
shell.WriteLine(Loc.GetString("Can't create new uplink account"));
return;
}
}
// Finally add uplink
if (!entityManager.EntitySysManager.GetEntitySystem<UplinkSystem>()
.AddUplink(user, uplinkAccount!, uplinkEntity))
{
shell.WriteLine(Loc.GetString("Failed to add uplink to the player"));
return;
}
}
}
}

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.PDA;
using Content.Shared.Traitor.Uplink;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
namespace Content.Server.PDA.Managers
{
public interface IUplinkManager
{
public IReadOnlyDictionary<string, UplinkListingData> FetchListings { get; }
void Initialize();
public bool AddNewAccount(UplinkAccount acc);
public bool ChangeBalance(UplinkAccount acc, int amt);
public bool TryGetAccount(EntityUid owner, out UplinkAccount? acc);
public bool TryPurchaseItem(
UplinkAccount? acc,
string itemId,
EntityCoordinates spawnCoords,
[NotNullWhen(true)] out IEntity? purchasedItem);
}
}

View File

@@ -0,0 +1,117 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Server.Mind.Components;
using Content.Shared.PDA;
using Content.Shared.Traitor.Uplink;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Server.PDA.Managers
{
public class UplinkManager : IUplinkManager
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private readonly Dictionary<EntityUid, UplinkAccount> _accounts = new();
private readonly Dictionary<string, UplinkListingData> _listings = new();
public IReadOnlyDictionary<string, UplinkListingData> FetchListings => _listings;
public void Initialize()
{
foreach (var item in _prototypeManager.EnumeratePrototypes<UplinkStoreListingPrototype>())
{
var newListing = new UplinkListingData(item.ListingName, item.ItemId, item.Price, item.Category,
item.Description);
RegisterUplinkListing(newListing);
}
}
private void RegisterUplinkListing(UplinkListingData listing)
{
if (!ContainsListing(listing))
{
_listings.Add(listing.ItemId, listing);
}
}
private bool ContainsListing(UplinkListingData listing)
{
return _listings.ContainsKey(listing.ItemId);
}
public bool AddNewAccount(UplinkAccount acc)
{
var entity = _entityManager.GetEntity(acc.AccountHolder);
if (entity.TryGetComponent(out MindComponent? mindComponent) && !mindComponent.HasMind)
{
return false;
}
if (_accounts.ContainsKey(acc.AccountHolder))
{
return false;
}
_accounts.Add(acc.AccountHolder, acc);
return true;
}
public bool TryGetAccount(EntityUid owner, out UplinkAccount? acc)
{
return _accounts.TryGetValue(owner, out acc);
}
public bool ChangeBalance(UplinkAccount acc, int amt)
{
var account = _accounts.GetValueOrDefault(acc.AccountHolder);
if (account == null)
{
return false;
}
if (account.Balance + amt < 0)
{
return false;
}
account.ModifyAccountBalance(account.Balance + amt);
return true;
}
public bool TryPurchaseItem(UplinkAccount? acc, string itemId, EntityCoordinates spawnCoords, [NotNullWhen(true)] out IEntity? purchasedItem)
{
purchasedItem = null;
if (acc == null)
{
return false;
}
if (!_listings.TryGetValue(itemId, out var listing))
{
return false;
}
if (acc.Balance < listing.Price)
{
return false;
}
if (!ChangeBalance(acc, -listing.Price))
{
return false;
}
purchasedItem = _entityManager.SpawnEntity(listing.ItemId, spawnCoords);
return true;
}
}
}

View File

@@ -0,0 +1,24 @@
using Content.Shared.Sound;
using Content.Shared.Traitor.Uplink;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Traitor.Uplink.Components
{
[RegisterComponent]
public class UplinkComponent : Component
{
public override string Name => "Uplink";
[ViewVariables]
[DataField("buySuccessSound")]
public SoundSpecifier BuySuccessSound = new SoundPathSpecifier("/Audio/Effects/kaching.ogg");
[ViewVariables]
[DataField("insufficientFundsSound")]
public SoundSpecifier InsufficientFundsSound = new SoundPathSpecifier("/Audio/Effects/error.ogg");
[ViewVariables] public UplinkAccount? UplinkAccount;
}
}

View File

@@ -0,0 +1,21 @@
using Content.Server.Traitor.Uplink.Components;
using Content.Shared.Traitor.Uplink;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
namespace Content.Server.Traitor.Uplink
{
public class UplinkInitEvent : EntityEventArgs
{
public UplinkComponent Uplink;
public UplinkInitEvent(UplinkComponent uplink)
{
Uplink = uplink;
}
}
public class UplinkRemovedEvent : EntityEventArgs
{
}
}

View File

@@ -0,0 +1,166 @@
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Server.PDA;
using Content.Server.PDA.Managers;
using Content.Server.Traitor.Uplink.Components;
using Content.Server.UserInterface;
using Content.Shared.Traitor.Uplink;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Player;
using System;
using System.Linq;
namespace Content.Server.Traitor.Uplink.Systems
{
public class UplinkSystem : EntitySystem
{
[Dependency] private readonly IUplinkManager _uplinkManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<UplinkComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<UplinkComponent, ComponentRemove>(OnRemove);
}
public void SetAccount(UplinkComponent component, UplinkAccount account)
{
if (component.UplinkAccount != null)
{
Logger.Error("Can't init one uplink with different account!");
return;
}
component.UplinkAccount = account;
component.UplinkAccount.BalanceChanged += (acc) =>
{
UpdateUserInterface(component);
};
}
private void OnInit(EntityUid uid, UplinkComponent component, ComponentInit args)
{
var ui = component.Owner.GetUIOrNull(UplinkUiKey.Key);
if (ui != null)
ui.OnReceiveMessage += (msg) => OnUIMessage(component, msg);
RaiseLocalEvent(uid, new UplinkInitEvent(component));
}
private void OnRemove(EntityUid uid, UplinkComponent component, ComponentRemove args)
{
RaiseLocalEvent(uid, new UplinkRemovedEvent());
}
public void ToggleUplinkUI(UplinkComponent component, IPlayerSession session)
{
var ui = component.Owner.GetUIOrNull(UplinkUiKey.Key);
ui?.Toggle(session);
UpdateUserInterface(component);
}
private void OnUIMessage(UplinkComponent uplink, ServerBoundUserInterfaceMessage message)
{
switch (message.Message)
{
case UplinkRequestUpdateInterfaceMessage _:
UpdateUserInterface(uplink);
break;
case UplinkBuyListingMessage buyMsg:
{
var player = message.Session.AttachedEntity;
if (player == null) break;
if (!_uplinkManager.TryPurchaseItem(uplink.UplinkAccount, buyMsg.ItemId,
player.Transform.Coordinates, out var entity))
{
SoundSystem.Play(Filter.SinglePlayer(message.Session), uplink.InsufficientFundsSound.GetSound(),
uplink.Owner, AudioParams.Default);
RaiseNetworkEvent(new UplinkInsufficientFundsMessage(), message.Session.ConnectedClient);
break;
}
if (player.TryGetComponent(out HandsComponent? hands) &&
entity.TryGetComponent(out ItemComponent? item))
{
hands.PutInHandOrDrop(item);
}
SoundSystem.Play(Filter.SinglePlayer(message.Session), uplink.BuySuccessSound.GetSound(),
uplink.Owner, AudioParams.Default.WithVolume(-2f));
RaiseNetworkEvent(new UplinkBuySuccessMessage(), message.Session.ConnectedClient);
break;
}
}
}
private void UpdateUserInterface(UplinkComponent component)
{
var ui = component.Owner.GetUIOrNull(UplinkUiKey.Key);
if (ui == null)
return;
var listings = _uplinkManager.FetchListings.Values.ToArray();
var acc = component.UplinkAccount;
UplinkAccountData accData;
if (acc != null)
accData = new UplinkAccountData(acc.AccountHolder, acc.Balance);
else
accData = new UplinkAccountData(null, 0);
ui.SetState(new UplinkUpdateState(accData, listings));
}
public bool AddUplink(IEntity user, UplinkAccount account, IEntity? uplinkEntity = null)
{
// Try to find target item
if (uplinkEntity == null)
{
uplinkEntity = FindUplinkTarget(user);
if (uplinkEntity == null)
return false;
}
var uplink = uplinkEntity.EnsureComponent<UplinkComponent>();
SetAccount(uplink, account);
return true;
}
private IEntity? FindUplinkTarget(IEntity user)
{
// Try to find PDA in inventory
if (user.TryGetComponent(out InventoryComponent? inventory))
{
var foundPDA = inventory.LookupItems<PDAComponent>().FirstOrDefault();
if (foundPDA != null)
return foundPDA.Owner;
}
// Also check hands
if (user.TryGetComponent(out IHandsComponent? hands))
{
var heldItems = hands.GetAllHeldItems();
foreach (var item in heldItems)
{
if (item.Owner.HasComponent<PDAComponent>())
return item.Owner;
}
}
return null;
}
}
}