Skip to content

Commit f0a27ee

Browse files
author
Joanna May
authored
Merge pull request #4 from chickensoft-games/fix/callbacks
fix: ensure correct callback behavior with reusable states
2 parents 13cbb60 + a9f2af9 commit f0a27ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1025
-382
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ obj/
99
.generated/
1010
.vs/
1111
.DS_Store
12-
*.g.puml

Chickensoft.LogicBlocks.Example/VendingMachine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract record State(Context Context) : StateLogic(Context) {
1717
public record Idle : State,
1818
IGet<Input.SelectionEntered>, IGet<Input.PaymentReceived> {
1919
public Idle(Context context) : base(context) {
20-
context.OnEnter<Idle>((previous) => context.Output(
20+
OnEnter<Idle>((previous) => context.Output(
2121
new Output.ClearTransactionTimeOutTimer()
2222
));
2323
}
@@ -55,7 +55,7 @@ public TransactionActive(
5555
Price = price;
5656
AmountReceived = amountReceived;
5757

58-
Context.OnEnter<TransactionActive>(
58+
OnEnter<TransactionActive>(
5959
(previous) => Context.Output(
6060
new Output.RestartTransactionTimeOutTimer()
6161
)
@@ -97,7 +97,7 @@ public record Started : TransactionActive,
9797
public Started(
9898
Context context, ItemType type, int price, int amountReceived
9999
) : base(context, type, price, amountReceived) {
100-
context.OnEnter<Started>(
100+
OnEnter<Started>(
101101
(previous) => context.Output(new Output.TransactionStarted())
102102
);
103103
}
@@ -128,7 +128,7 @@ public Vending(Context context, ItemType type, int price) :
128128
Type = type;
129129
Price = price;
130130

131-
context.OnEnter<Vending>(
131+
OnEnter<Vending>(
132132
(previous) => Context.Output(new Output.BeginVending())
133133
);
134134
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@startuml VendingMachine
2+
state "VendingMachine State" as Chickensoft_LogicBlocks_Example_VendingMachine_State {
3+
state "Idle" as Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle {
4+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : OnEnterClearTransactionTimeOutTimer
5+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : OnPaymentReceivedMakeChange
6+
}
7+
state "TransactionActive" as Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive {
8+
state "Started" as Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started {
9+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started : OnEnterTransactionStarted
10+
}
11+
state "PaymentPending" as Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_PaymentPending
12+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive : OnEnterRestartTransactionTimeOutTimer
13+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive : OnPaymentReceivedMakeChange, TransactionCompleted
14+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive : OnTransactionTimedOutMakeChange
15+
}
16+
state "Vending" as Chickensoft_LogicBlocks_Example_VendingMachine_State_Vending {
17+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Vending : OnEnterBeginVending
18+
}
19+
}
20+
21+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : PaymentReceived
22+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : SelectionEntered
23+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle --> Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started : SelectionEntered
24+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : TransactionTimedOut
25+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive --> Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_PaymentPending : PaymentReceived
26+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Vending : PaymentReceived
27+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : SelectionEntered
28+
Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started --> Chickensoft_LogicBlocks_Example_VendingMachine_State_TransactionActive_Started : SelectionEntered
29+
Chickensoft_LogicBlocks_Example_VendingMachine_State_Vending --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle : VendingCompleted
30+
31+
[*] --> Chickensoft_LogicBlocks_Example_VendingMachine_State_Idle
32+
@enduml

Chickensoft.LogicBlocks.Generator.Tests/GeneratorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void GeneratesUml() {
1111

1212
result.Outputs["ToasterOven.puml.g.cs"].ShouldBe("""
1313
@startuml ToasterOven
14-
state "ToasterOven" as State {
14+
state "ToasterOven State" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State {
1515
state "Heating" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Heating {
1616
state "Toasting" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting {
1717
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting : OnEnter → SetTimer

Chickensoft.LogicBlocks.Generator.Tests/test_cases/Heater.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,25 @@ public Heating(Context context, double targetTemp) : base(
5252
) {
5353
var tempSensor = context.Get<ITemperatureSensor>();
5454

55-
context.OnEnter<Heating>(
55+
OnEnter<Heating>(
5656
(previous) => tempSensor.OnTemperatureChanged += OnTemperatureChanged
5757
);
5858

59-
context.OnExit<Heating>(
59+
OnExit<Heating>(
6060
(next) => tempSensor.OnTemperatureChanged -= OnTemperatureChanged
6161
);
6262
}
6363

64-
State IGet<Input.TurnOff>.On(Input.TurnOff input)
65-
=> new Off(Context, TargetTemp);
64+
public State On(Input.TurnOff input) => new Off(Context, TargetTemp);
6665

67-
State IGet<Input.AirTempSensorChanged>.On(
68-
Input.AirTempSensorChanged input
69-
) => input.AirTemp >= TargetTemp
70-
? new Idle(Context, TargetTemp)
71-
: this;
66+
public State On(Input.AirTempSensorChanged input) =>
67+
input.AirTemp >= TargetTemp
68+
? new Idle(Context, TargetTemp)
69+
: this;
7270

73-
State IGet<Input.TargetTempChanged>.On(Input.TargetTempChanged input)
74-
=> this with { TargetTemp = input.Temp };
71+
public State On(Input.TargetTempChanged input) => this with {
72+
TargetTemp = input.Temp
73+
};
7574

7675
private void OnTemperatureChanged(double airTemp) {
7776
Context.Input(new Input.AirTempSensorChanged(airTemp));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@startuml Heater
2+
state "Heater State" as Chickensoft_LogicBlocks_Generator_Tests_Heater_State {
3+
state "Off" as Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Off
4+
state "Idle" as Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Idle
5+
state "Heating" as Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating {
6+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating : OnTemperatureChanged() → AirTempChanged
7+
}
8+
}
9+
10+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating : AirTempSensorChanged
11+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating : TargetTempChanged
12+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Idle : AirTempSensorChanged
13+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Off : TurnOff
14+
Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Off --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Heating : TurnOn
15+
16+
[*] --> Chickensoft_LogicBlocks_Generator_Tests_Heater_State_Off
17+
@enduml
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@startuml LightSwitch
2+
state "LightSwitch State" as Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State {
3+
state "On" as Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_On
4+
state "Off" as Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_Off
5+
}
6+
7+
Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_Off --> Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_On : Toggle
8+
Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_On --> Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_Off : Toggle
9+
10+
[*] --> Chickensoft_LogicBlocks_Generator_Tests_LightSwitch_State_Off
11+
@enduml

Chickensoft.LogicBlocks.Generator.Tests/test_cases/ToasterOven.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public record StartToasting(int ToastColor) : Input;
1515
public abstract record State(Context Context) : StateLogic(Context) {
1616
public record Heating : State, IGet<Input.OpenDoor> {
1717
public Heating(Context context) : base(context) {
18-
Context.OnEnter<Heating>(
18+
OnEnter<Heating>(
1919
(previous) => Context.Output(new Output.TurnHeaterOn())
2020
);
21-
Context.OnExit<Heating>(
21+
OnExit<Heating>(
2222
(next) => Context.Output(new Output.TurnHeaterOff())
2323
);
2424
}
@@ -32,10 +32,10 @@ public record Toasting : Heating, IGet<Input.StartBaking> {
3232
public Toasting(Context context, int toastColor) : base(context) {
3333
ToastColor = toastColor;
3434

35-
Context.OnEnter<Toasting>(
35+
OnEnter<Toasting>(
3636
(previous) => Context.Output(new Output.SetTimer(ToastColor))
3737
);
38-
Context.OnExit<Toasting>(
38+
OnExit<Toasting>(
3939
(next) => Context.Output(new Output.ResetTimer())
4040
);
4141
}
@@ -51,10 +51,10 @@ public record Baking : Heating, IGet<Input.StartToasting> {
5151
public Baking(Context context, int temperature) : base(context) {
5252
Temperature = temperature;
5353

54-
Context.OnEnter<Baking>(
54+
OnEnter<Baking>(
5555
(previous) => Context.Output(new Output.SetTemperature(Temperature))
5656
);
57-
Context.OnExit<Baking>(
57+
OnExit<Baking>(
5858
(next) => Context.Output(new Output.SetTemperature(0))
5959
);
6060
}
@@ -66,10 +66,10 @@ public Baking(Context context, int temperature) : base(context) {
6666

6767
public record DoorOpen : State, IGet<Input.CloseDoor> {
6868
public DoorOpen(Context context) : base(context) {
69-
Context.OnEnter<DoorOpen>(
69+
OnEnter<DoorOpen>(
7070
(previous) => Context.Output(new Output.TurnLampOn())
7171
);
72-
Context.OnExit<DoorOpen>(
72+
OnExit<DoorOpen>(
7373
(next) => Context.Output(new Output.TurnLampOff())
7474
);
7575
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@startuml ToasterOven
2+
state "ToasterOven State" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State {
3+
state "Heating" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Heating {
4+
state "Toasting" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting {
5+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting : OnEnterSetTimer
6+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting : OnExitResetTimer
7+
}
8+
state "Baking" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Baking {
9+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Baking : OnEnterSetTemperature
10+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Baking : OnExitSetTemperature
11+
}
12+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Heating : OnEnterTurnHeaterOn
13+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Heating : OnExitTurnHeaterOff
14+
}
15+
state "DoorOpen" as Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_DoorOpen {
16+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_DoorOpen : OnEnterTurnLampOn
17+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_DoorOpen : OnExitTurnLampOff
18+
}
19+
}
20+
21+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Baking --> Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting : StartToasting
22+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_DoorOpen --> Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting : CloseDoor
23+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Heating --> Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_DoorOpen : OpenDoor
24+
Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting --> Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Baking : StartBaking
25+
26+
[*] --> Chickensoft_LogicBlocks_Generator_Tests_ToasterOven_State_Toasting
27+
@enduml

Chickensoft.LogicBlocks.Generator.Tests/test_cases/partial_split_across_files/PartialLogic1.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ public abstract record Output {
1616
public record OutputA : Output;
1717
public record OutputEnterA : Output;
1818
public record OutputExitA : Output;
19+
public record OutputSomething : Output;
1920
}
2021
}

0 commit comments

Comments
 (0)