Skip to content

Commit a6a4ef9

Browse files
committed
[bugfix] Fix undo function by doing deep copies of all states
1 parent 31982a6 commit a6a4ef9

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

internal/app/controller/engine.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,23 @@ func (e *Engine) AddGameEvent(gameEvent GameEvent) {
8585
func (e *Engine) UndoLastAction() {
8686
lastIndex := len(e.History) - 2
8787
if lastIndex >= 0 {
88-
*e.State = e.History[lastIndex].State
89-
e.RefereeEvents = e.History[lastIndex].RefereeEvents
88+
*e.State = e.History[lastIndex].State.DeepCopy()
89+
e.RefereeEvents = append(e.History[lastIndex].RefereeEvents[:0:0],
90+
e.History[lastIndex].RefereeEvents...)
9091
e.History = e.History[0:lastIndex]
9192
}
9293
}
9394

95+
func (e *Engine) appendHistory() {
96+
var entry HistoryEntry
97+
entry.State = e.State.DeepCopy()
98+
entry.RefereeEvents = append(e.RefereeEvents[:0:0], e.RefereeEvents...)
99+
e.History = append(e.History, entry)
100+
if len(e.History) > maxHistorySize {
101+
e.History = e.History[1:]
102+
}
103+
}
104+
94105
func (e *Engine) Continue() {
95106
substitutionIntend := e.State.BotSubstitutionIntend()
96107
if substitutionIntend != TeamUnknown {
@@ -230,13 +241,6 @@ func (e *Engine) updateMaxBots() {
230241
}
231242
}
232243

233-
func (e *Engine) appendHistory() {
234-
e.History = append(e.History, HistoryEntry{*e.State, e.RefereeEvents})
235-
if len(e.History) > maxHistorySize {
236-
e.History = e.History[1:]
237-
}
238-
}
239-
240244
func (e *Engine) LogGameEvent(event GameEvent) {
241245
gameEvent := RefereeEvent{
242246
Timestamp: e.TimeProvider().UnixNano(),

internal/app/controller/state.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ type TeamInfo struct {
252252
BotSubstitutionIntend bool `json:"botSubstitutionIntend"`
253253
}
254254

255+
func (t TeamInfo) DeepCopy() (c TeamInfo) {
256+
c = t
257+
copy(c.YellowCardTimes, t.YellowCardTimes)
258+
return
259+
}
260+
255261
type GameEventBehavior string
256262

257263
const (
@@ -318,6 +324,30 @@ func NewState() (s *State) {
318324
return
319325
}
320326

327+
func (s State) DeepCopy() (c State) {
328+
c = s
329+
c.GameEvents = []*GameEvent{}
330+
copy(c.GameEvents, s.GameEvents)
331+
c.AutoRefsConnected = []string{}
332+
copy(c.AutoRefsConnected, s.AutoRefsConnected)
333+
c.GameEventProposals = []*GameEventProposal{}
334+
copy(c.GameEventProposals, s.GameEventProposals)
335+
if s.PlacementPos != nil {
336+
c.PlacementPos = new(Location)
337+
*c.PlacementPos = *s.PlacementPos
338+
}
339+
c.TeamState = make(map[Team]*TeamInfo)
340+
for k, v := range s.TeamState {
341+
c.TeamState[k] = new(TeamInfo)
342+
*c.TeamState[k] = v.DeepCopy()
343+
}
344+
c.GameEventBehavior = make(map[GameEventType]GameEventBehavior)
345+
for k, v := range s.GameEventBehavior {
346+
c.GameEventBehavior[k] = v
347+
}
348+
return
349+
}
350+
321351
func (s State) GameState() GameState {
322352
switch s.Command {
323353
case CommandHalt:

0 commit comments

Comments
 (0)