Skip to content

Commit

Permalink
add abort button, some refactor, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeZer2 committed Jan 6, 2025
1 parent 08e7548 commit ab58cd1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
33 changes: 28 additions & 5 deletions Content.Client/SS220/Contractor/UI/ContractorBoundUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected override void ReceiveMessage(BoundUserInterfaceMessage message)
{
case ContractorUpdateStatsMessage:
UpdateStats();
UpdateContracts(EntMan.GetEntity(_contractorPdaComponent.PdaOwner)!.Value, Owner);
UpdateContracts(EntMan.GetEntity(_contractorPdaComponent.PdaOwner)!.Value);
break;

case ContractorCompletedContractMessage:
Expand Down Expand Up @@ -84,7 +84,7 @@ protected override void Open()

UpdateStats();

UpdateContracts(_playerManager.LocalSession.AttachedEntity.Value, Owner);
UpdateContracts(_playerManager.LocalSession.AttachedEntity.Value);
UpdateHub(_contractorPdaComponent.AvailableItems);
}

Expand Down Expand Up @@ -123,16 +123,16 @@ private void UpdateStats()
Loc.GetString("contractor-uplink-current-contracts-completed", ("amountContractsCompleted", _contractorComponent.ContractsCompleted));
}

private void UpdateContracts(EntityUid contractor, EntityUid pda)
private void UpdateContracts(EntityUid player)
{
if (_menu == null)
return;

_menu.ContractsListPanel.RemoveAllChildren();

var contracts = _contractorSystem.GetContractsForPda(contractor, pda);
var contracts = _contractorComponent!.Contracts;

if (contracts is null)
if (_contractorPdaComponent.PdaOwner != EntMan.GetNetEntity(player))
return;

foreach (var contract in contracts)
Expand Down Expand Up @@ -223,8 +223,29 @@ private void AddContract(NetEntity key, ContractorContract contract)
var isAlreadyAccepted = _contractorPdaComponent.CurrentContractEntity is not null &&
_contractorPdaComponent.CurrentContractData is not null; // todo only on server

var abortButton = new Button()
{
VerticalExpand = false,
HorizontalExpand = false,
Visible = false,
MaxSize = new Vector2(100, 30),
};

abortButton.OnPressed += _ =>
{
SendMessage(new ContractorAbortContractMessage(key));
topContainer.RemoveChild(abortButton);
};

topContainer.AddChild(abortButton);

foreach (var amountPosition in contract.AmountPositions)
{
if (_contractorPdaComponent.CurrentContractEntity == key)
{
abortButton.Visible = true;
}

var positionButton = new Button
{
Text = $"{amountPosition.Location} ({amountPosition.TcReward} ТК) ({amountPosition.Difficulty})",
Expand All @@ -242,6 +263,8 @@ private void AddContract(NetEntity key, ContractorContract contract)
amountPosition.TcReward,
amountPosition.Uid));

abortButton.Visible = true;

foreach (var buttons in _allPositionButtons)
{
buttons.Disabled = true;
Expand Down
41 changes: 37 additions & 4 deletions Content.Server/SS220/Contractor/ContractorServerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public override void Initialize()
SubscribeLocalEvent<ContractorPdaComponent, ContractorNewContractAcceptedMessage>(OnNewContractAccepted);
SubscribeLocalEvent<ContractorPdaComponent, ContractorWithdrawTcMessage>(OnWithdrawTc);
SubscribeLocalEvent<ContractorPdaComponent, ContractorExecutionButtonPressedMessage>(OnExecuteContract);
SubscribeLocalEvent<ContractorPdaComponent, ContractorAbortContractMessage>(AbortContract);

SubscribeLocalEvent<StoreBuyListingMessage>(OnBuyContractorKit);
}
Expand Down Expand Up @@ -220,7 +221,7 @@ public void HandleContractAccepted(NetEntity acceptedPlayer, EntityUid contracto
if (!contractorComp.Contracts.Remove(acceptedPlayer))
continue;

if (contractorComp.Contracts.Count >= 5)
if (contractorComp.Contracts.Count >= contractorComp.MaxAvailableContracts)
continue;

var newContract = GenerateContractForContractor((uid, contractorComp));
Expand Down Expand Up @@ -269,7 +270,7 @@ public void HandleContractAccepted(NetEntity acceptedPlayer, EntityUid contracto
if (_roleSystem.MindHasRole<TraitorRoleComponent>(mindId))
continue;

_jobs.MindTryGetJobName(mindId, out var jobName);
_jobs.MindTryGetJobName(mindId, out var jobName); // && jobName == "JobCaptain" - disable for testing

return (GetNetEntity(player),
new ContractorContract
Expand Down Expand Up @@ -310,9 +311,9 @@ public void GenerateContracts(Entity<ContractorComponent> ent)
if (ent.Comp.Contracts.ContainsKey(GetNetEntity(player)))
continue;

_jobs.MindTryGetJobName(mindId, out var jobName);
_jobs.MindTryGetJobName(mindId, out var jobName); // && jobName == "JobCaptain" - disable for testing

if (ent.Comp.Contracts is { Count: >= 5 })
if (ent.Comp.Contracts.Count == ent.Comp.MaxAvailableContracts)
return;

ent.Comp.Contracts.Add(GetNetEntity(player),
Expand Down Expand Up @@ -383,4 +384,36 @@ private bool IsCloseWithPosition(NetEntity playerNet)

return isPlayerCloseToPortal && isTargetCloseToPortal;
}

private void AbortContract(Entity<ContractorPdaComponent> ent, ref ContractorAbortContractMessage ev)
{
var pdaOwner = GetEntity(ent.Comp.PdaOwner);

if (ev.Actor != pdaOwner)
return;

if (!TryComp<ContractorComponent>(pdaOwner, out var contractorComponent))
return;

if (!contractorComponent.Contracts.Remove(ev.ContractEntity))
return;

_adminLogger.Add(
LogType.Action,
LogImpact.High,
$"Contractor {ev.Actor} aborted unknown contract {ev.ContractEntity}");

contractorComponent.MaxAvailableContracts--;
contractorComponent.Reputation--;
contractorComponent.CurrentContractEntity = null;
contractorComponent.CurrentContractData = null;

ent.Comp.CurrentContractEntity = null;
ent.Comp.CurrentContractData = null;

_uiSystem.ServerSendUiMessage(ent.Owner, ContractorPdaKey.Key, new ContractorUpdateStatsMessage());

Dirty(ent);
Dirty(pdaOwner.Value, contractorComponent);
}
}
2 changes: 2 additions & 0 deletions Content.Shared/SS220/Contractor/ContractorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public sealed partial class ContractorComponent : Component
public int ContractsCompleted = 0;

public readonly int ReputationAward = 2;

public int MaxAvailableContracts = 5;
}

[Serializable]
Expand Down
11 changes: 11 additions & 0 deletions Content.Shared/SS220/Contractor/ContractorMessagesUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public ContractorNewContractAcceptedMessage(NetEntity contractEntity, Contractor
}
}

[Serializable, NetSerializable]
public sealed class ContractorAbortContractMessage : BoundUserInterfaceMessage
{
public NetEntity ContractEntity;

public ContractorAbortContractMessage(NetEntity contractEntity)
{
ContractEntity = contractEntity;
}
}

[Serializable, NetSerializable]
public sealed class ContractorHubBuyItemMessage : BoundUserInterfaceMessage
{
Expand Down
22 changes: 0 additions & 22 deletions Content.Shared/SS220/Contractor/SharedContractorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ private void OnBuyItem(Entity<ContractorPdaComponent> ent, ref ContractorHubBuyI

Dirty(ev.Actor, contractorComponent);
}

/// <summary>
/// Used in bound ui on client, to get contracts for pda
/// </summary>
/// <returns>Return dictionary of contracts</returns>
public Dictionary<NetEntity, ContractorContract>? GetContractsForPda(EntityUid contractor, EntityUid pdaEntity)
{
AddContractsToPda(contractor, pdaEntity);

return !TryComp<ContractorPdaComponent>(pdaEntity, out var contractorPdaComponent) ? null : contractorPdaComponent.Contracts;
}

private void AddContractsToPda(EntityUid contractor, EntityUid pdaEntity)
{
if (!TryComp<ContractorPdaComponent>(pdaEntity, out var contractorPdaComponent))
return;
if (!TryComp<ContractorComponent>(contractor, out var contractorComponent))
return;

contractorPdaComponent.Contracts = contractorComponent.Contracts;
}

}

/// <summary>
Expand Down

0 comments on commit ab58cd1

Please sign in to comment.