Skip to content

Commit df78d90

Browse files
committed
[refactoring] Move engine history code to history file
1 parent 3a5e977 commit df78d90

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

internal/app/controller/engine.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"time"
1111
)
1212

13-
const maxHistorySize = 10
14-
1513
type Engine struct {
1614
State *State
1715
UiProtocol []UiProtocolEntry
@@ -81,27 +79,6 @@ func (e *Engine) AddGameEvent(gameEvent GameEvent) {
8179
e.LogGameEvent(gameEvent)
8280
}
8381

84-
// UndoLastAction restores the last state from internal history
85-
func (e *Engine) UndoLastAction() {
86-
lastIndex := len(e.History) - 2
87-
if lastIndex >= 0 {
88-
*e.State = e.History[lastIndex].State.DeepCopy()
89-
e.UiProtocol = append(e.History[lastIndex].UiProtocol[:0:0],
90-
e.History[lastIndex].UiProtocol...)
91-
e.History = e.History[0:lastIndex]
92-
}
93-
}
94-
95-
func (e *Engine) appendHistory() {
96-
var entry HistoryEntry
97-
entry.State = e.State.DeepCopy()
98-
entry.UiProtocol = append(e.UiProtocol[:0:0], e.UiProtocol...)
99-
e.History = append(e.History, entry)
100-
if len(e.History) > maxHistorySize {
101-
e.History = e.History[1:]
102-
}
103-
}
104-
10582
func (e *Engine) Continue() {
10683
substitutionIntend := e.State.BotSubstitutionIntend()
10784
if substitutionIntend != TeamUnknown {

internal/app/controller/history.go renamed to internal/app/controller/engineHistory.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
)
1010

11+
const maxHistorySize = 10
1112
const historyFileName = "history.json"
1213

1314
type HistoryPreserver struct {
@@ -79,3 +80,25 @@ func (r *HistoryPreserver) Save(history History) {
7980
log.Print("Could not sync history file", err)
8081
}
8182
}
83+
84+
// UndoLastAction restores the last state from internal history
85+
func (e *Engine) UndoLastAction() {
86+
lastIndex := len(e.History) - 2
87+
if lastIndex >= 0 {
88+
*e.State = e.History[lastIndex].State.DeepCopy()
89+
e.UiProtocol = append(e.History[lastIndex].UiProtocol[:0:0],
90+
e.History[lastIndex].UiProtocol...)
91+
e.History = e.History[0:lastIndex]
92+
}
93+
}
94+
95+
// appendHistory appends the current state to the history
96+
func (e *Engine) appendHistory() {
97+
var entry HistoryEntry
98+
entry.State = e.State.DeepCopy()
99+
entry.UiProtocol = append(e.UiProtocol[:0:0], e.UiProtocol...)
100+
e.History = append(e.History, entry)
101+
if len(e.History) > maxHistorySize {
102+
e.History = e.History[1:]
103+
}
104+
}

0 commit comments

Comments
 (0)