Skip to content

Commit

Permalink
ae
Browse files Browse the repository at this point in the history
  • Loading branch information
lunar-crater-ex committed Jul 4, 2024
1 parent 46ffe2c commit e24be8d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
28 changes: 20 additions & 8 deletions Content.IntegrationTests/Tests/_Citadel/Contracts/ContractsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,31 @@ public void Transitions()
var contract = SEntity<CitadelContractComponent>(Spawn(TestContractId));

// Nobody has signed on, shouldn't be able to sign it.
Assert.That(!_sharedContractSys.TrySignContract(contract));
Assert.That(contract.Comp.State is ContractStateUnsigned);
Assert.Multiple(() =>
{
Assert.That(_sharedContractSys.TrySignContract(contract) is TENoPartyA or TENoPartyB);
Assert.That(contract.Comp.State is ContractStateUnsigned);
});

// Should sign on fine.
Assert.That(_sharedContractSys.TrySignOn(contract, Spawn(TestSignerId), Party.PartyA));
Assert.That(_sharedContractSys.TrySignOn(contract, Spawn(TestSignerId), Party.PartyB));
Assert.Multiple(() =>
{
Assert.That(_sharedContractSys.TrySignOn(contract, Spawn(TestSignerId), Party.PartyA));
Assert.That(_sharedContractSys.TrySignOn(contract, Spawn(TestSignerId), Party.PartyB));
});

// And contract should be signable now.
Assert.That(_sharedContractSys.TrySignContract(contract));
Assert.That(contract.Comp.State is ContractStateSigned);
Assert.Multiple(() =>
{
Assert.That(_sharedContractSys.TrySignContract(contract), Is.Null);
Assert.That(contract.Comp.State is ContractStateSigned);
});

// Now we breach it, and blame party B. Poor party B, they're going to owe an amogillion dollars.
Assert.That(_sharedContractSys.TryBreachContract(contract, Party.PartyB));
Assert.That(contract.Comp.State is ContractStateBreached { BreachingParty: Party.PartyB });
Assert.Multiple(() =>
{
Assert.That(_sharedContractSys.TryBreachContract(contract, Party.PartyB), Is.Null);
Assert.That(contract.Comp.State is ContractStateBreached { BreachingParty: Party.PartyB });
});
}
}
31 changes: 19 additions & 12 deletions Content.Shared/_Citadel/Contracts/Systems/SharedContractSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ private void Transition(Entity<CitadelContractComponent> ent, ContractState newS
RaiseLocalEvent(ent, ref ev, broadcast: true);
}

public bool TrySignContract(Entity<CitadelContractComponent> ent)
public TransitionError? TrySignContract(Entity<CitadelContractComponent> ent)
{
if (ent.Comp.State is not ContractStateUnsigned)
return false;
return new TECannotTransitionFrom(ent.Comp.State);

if (ent.Comp.PartyA.Count == 0)
return false;
return new TENoPartyA();

if (ent.Comp.PartyB.Count == 0)
return false;
return new TENoPartyB();

Transition(ent, new ContractStateSigned());
return true;
return null;
}

public bool TryBreachContract(Entity<CitadelContractComponent> ent, Party party)
public TransitionError? TryBreachContract(Entity<CitadelContractComponent> ent, Party party)
{
if (ent.Comp.State is not ContractStateSigned)
return false;
return new TECannotTransitionFrom(ent.Comp.State);

Transition(ent, new ContractStateBreached { BreachingParty = party });
return true;
return null;
}

public bool TryCloseOutContract(Entity<CitadelContractComponent> ent)
public TransitionError? TryCloseOutContract(Entity<CitadelContractComponent> ent)
{
if (ent.Comp.State is not ContractStateSigned)
return false;
return new TECannotTransitionFrom(ent.Comp.State);

Transition(ent, new ContractStateClosedOut());
return true;
return null;
}

/// <remarks>
Expand Down Expand Up @@ -85,5 +85,12 @@ public enum TransitionStatus
{
Invalid = 0,
Success,

CannotTransitionFrom,
NoPartyA,
NoPartyB,
}

public abstract record TransitionError();
public sealed record TENoPartyA : TransitionError;
public sealed record TENoPartyB : TransitionError;
public sealed record TECannotTransitionFrom(ContractState State) : TransitionError;

0 comments on commit e24be8d

Please sign in to comment.