Skip to content

Commit 1543c67

Browse files
committed
[feature] Allow reverting commands
1 parent 68a5e1c commit 1543c67

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

internal/app/controller/controller.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ const configFileName = "config/ssl-game-controller.yaml"
1616

1717
// GameController controls a game
1818
type GameController struct {
19-
Config config.Controller
20-
Publisher Publisher
21-
ApiServer ApiServer
22-
AutoRefServer *rcon.AutoRefServer
23-
TeamServer *rcon.TeamServer
24-
CiServer rcon.CiServer
25-
Engine Engine
26-
statePreserver StatePreserver
27-
numUiProtocolsLastPublish int
28-
outstandingTeamChoice *TeamChoice
29-
ConnectionMutex sync.Mutex
30-
PublishMutex sync.Mutex
31-
StateMutex sync.Mutex
32-
VisionReceiver *vision.Receiver
19+
Config config.Controller
20+
Publisher Publisher
21+
ApiServer ApiServer
22+
AutoRefServer *rcon.AutoRefServer
23+
TeamServer *rcon.TeamServer
24+
CiServer rcon.CiServer
25+
Engine Engine
26+
statePreserver StatePreserver
27+
uiProtocolLastPublish []*ProtocolEntry
28+
outstandingTeamChoice *TeamChoice
29+
ConnectionMutex sync.Mutex
30+
PublishMutex sync.Mutex
31+
StateMutex sync.Mutex
32+
VisionReceiver *vision.Receiver
3333
}
3434

3535
// NewGameController creates a new RefBox
@@ -40,6 +40,7 @@ func NewGameController() (c *GameController) {
4040
c.Publisher = NewPublisher(c.Config.Network.PublishAddress)
4141
c.ApiServer = ApiServer{}
4242
c.ApiServer.Consumer = c
43+
c.uiProtocolLastPublish = []*ProtocolEntry{}
4344

4445
c.VisionReceiver = vision.NewReceiver(c.Config.Network.VisionAddress)
4546
c.VisionReceiver.GeometryCallback = c.ProcessGeometry
@@ -161,9 +162,13 @@ func (c *GameController) publish() {
161162

162163
// publishUiProtocol publishes the UI protocol, if it has changed
163164
func (c *GameController) publishUiProtocol() {
164-
if len(c.Engine.PersistentState.Protocol) != c.numUiProtocolsLastPublish {
165+
numNewEntries := len(c.Engine.PersistentState.Protocol)
166+
numPreEntries := len(c.uiProtocolLastPublish)
167+
if numNewEntries != numPreEntries ||
168+
(numNewEntries > 0 && c.Engine.PersistentState.Protocol[numNewEntries-1] != c.uiProtocolLastPublish[numPreEntries-1]) {
165169
c.ApiServer.PublishUiProtocol(c.Engine.PersistentState.Protocol)
166-
c.numUiProtocolsLastPublish = len(c.Engine.PersistentState.Protocol)
170+
c.uiProtocolLastPublish = make([]*ProtocolEntry, numNewEntries)
171+
copy(c.uiProtocolLastPublish, c.Engine.PersistentState.Protocol)
167172
}
168173
}
169174

internal/app/controller/engine.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ func (e *Engine) countCardTime() bool {
212212
}
213213

214214
func (e *Engine) SendCommand(command RefCommand, forTeam Team) {
215+
216+
e.LogCommand(command, forTeam, e.State.DeepCopy())
217+
215218
e.State.PrevCommands = append(e.State.PrevCommands, e.State.Command)
216219
e.State.PrevCommandsFor = append(e.State.PrevCommandsFor, e.State.CommandFor)
217220
if len(e.State.PrevCommands) > 10 {
@@ -232,8 +235,6 @@ func (e *Engine) SendCommand(command RefCommand, forTeam Team) {
232235
e.State.PlacementPos = nil
233236
}
234237

235-
e.LogCommand()
236-
237238
if command == CommandTimeout {
238239
e.State.TeamState[forTeam].TimeoutsLeft--
239240
} else if command == CommandNormalStart {
@@ -501,18 +502,16 @@ func (e *Engine) processEvent(event Event) error {
501502
}
502503

503504
func (e *Engine) processRevertProtocolEntry(id string) error {
505+
log.Printf("Revert protocol entry %v", id)
506+
504507
err := e.PersistentState.RevertProtocolEntry(id)
505508
if err != nil {
506509
return err
507510
}
508511
e.GcState = e.PersistentState.CurrentState
509512
e.State = e.GcState.MatchState
510513

511-
if e.State.Command != CommandHalt {
512-
e.SendCommand(CommandHalt, "")
513-
}
514-
515-
log.Printf("Revert protocol entry %v", id)
514+
e.SendCommand(CommandHalt, "")
516515
return nil
517516
}
518517

internal/app/controller/protocol.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ func (e *Engine) LogIgnoredGameEvent(event GameEvent) {
7979
}
8080

8181
// LogCommand adds a command to the protocol
82-
func (e *Engine) LogCommand() {
82+
func (e *Engine) LogCommand(command RefCommand, commandFor Team, prevState *State) {
8383
description := ""
8484
if e.State.PlacementPos != nil {
8585
description = fmt.Sprintf("place pos: %v", e.State.PlacementPos)
8686
}
87-
log.Printf("Log command '%v': %v", string(e.State.Command), description)
87+
log.Printf("Log command '%v' for '%v': %v", string(command), commandFor, description)
8888
entry := ProtocolEntry{
89-
Id: e.newGlobalProtocolEntryId(),
90-
Timestamp: e.TimeProvider().UnixNano(),
91-
StageTime: e.State.StageTimeElapsed,
92-
Type: UiProtocolCommand,
93-
Name: string(e.State.Command),
94-
Team: e.State.CommandFor,
95-
Description: description,
89+
Id: e.newGlobalProtocolEntryId(),
90+
Timestamp: e.TimeProvider().UnixNano(),
91+
StageTime: e.State.StageTimeElapsed,
92+
Type: UiProtocolCommand,
93+
Name: string(command),
94+
Team: commandFor,
95+
Description: description,
96+
PreviousState: prevState,
9697
}
9798
e.PersistentState.Add(&entry)
9899
}

0 commit comments

Comments
 (0)