Skip to content

Commit 8e29de4

Browse files
committed
[bugfix] Fix duration formatting and match duration on restarts
1 parent 5b9b369 commit 8e29de4

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

internal/app/controller/engine.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import (
99
)
1010

1111
type Engine struct {
12-
State *State
13-
MatchTimeStart time.Time
14-
StageTimes map[Stage]time.Duration
15-
config ConfigGame
16-
StateHistory []State
12+
State *State
13+
StageTimes map[Stage]time.Duration
14+
config ConfigGame
15+
StateHistory []State
1716
}
1817

1918
func NewEngine(config ConfigGame) (e Engine) {
@@ -29,17 +28,14 @@ func (e *Engine) ResetGame() {
2928
e.State.TeamState[TeamYellow].TimeoutTimeLeft = e.config.Normal.TimeoutDuration
3029
e.State.TeamState[TeamBlue].TimeoutsLeft = e.config.Normal.Timeouts
3130
e.State.TeamState[TeamYellow].TimeoutsLeft = e.config.Normal.Timeouts
32-
33-
e.MatchTimeStart = time.Unix(0, 0)
34-
3531
}
3632

3733
// Tick updates the times of the state and removes cards, if necessary
3834
func (e *Engine) Tick(delta time.Duration) {
3935
e.updateTimes(delta)
4036

41-
if e.MatchTimeStart.After(time.Unix(0, 0)) {
42-
e.State.MatchDuration = time.Now().Sub(e.MatchTimeStart)
37+
if e.State.MatchTimeStart.After(time.Unix(0, 0)) {
38+
e.State.MatchDuration = time.Now().Sub(e.State.MatchTimeStart)
4339
}
4440
}
4541

@@ -231,7 +227,7 @@ func (e *Engine) updateStage(stage Stage) (cmd *EventCommand) {
231227
}
232228

233229
if stage == StageFirstHalf {
234-
e.MatchTimeStart = time.Now()
230+
e.State.MatchTimeStart = time.Now()
235231
}
236232

237233
if stage == StageOvertimeFirstHalfPre {

internal/app/controller/state.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type State struct {
165165
GameStateFor *Team `json:"gameStateForTeam"`
166166
StageTimeElapsed time.Duration `json:"gameTimeElapsed"`
167167
StageTimeLeft time.Duration `json:"gameTimeLeft"`
168+
MatchTimeStart time.Time `json:"matchTimeStart"`
168169
MatchDuration time.Duration `json:"matchDuration"`
169170
TeamState map[Team]*TeamInfo `json:"teamState"`
170171
}
@@ -175,10 +176,10 @@ func NewState() (s *State) {
175176
s.Stage = StagePreGame
176177
s.GameState = GameStateHalted
177178

178-
// for some reason, the UI does not reset times correctly if duration is zero, so set it to 1ns
179-
s.StageTimeLeft = 1
180-
s.StageTimeElapsed = 1
181-
s.MatchDuration = 1
179+
s.StageTimeLeft = 0
180+
s.StageTimeElapsed = 0
181+
s.MatchDuration = 0
182+
s.MatchTimeStart = time.Unix(0, 0)
182183

183184
s.TeamState = map[Team]*TeamInfo{}
184185
s.TeamState[TeamYellow] = new(TeamInfo)

ui/src/TimestampFormatter.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ const formatNsDuration = function (timestamp) {
1313
return sign + padWithZero(minutes) + ':' + padWithZero(fullSeconds);
1414
};
1515

16+
const isNumeric = function (n) {
17+
return !isNaN(parseFloat(n)) && isFinite(n);
18+
};
19+
1620
const process = function (el, binding) {
17-
const timestamp = binding.value || el.innerHTML;
21+
let timestamp;
22+
if (isNumeric(binding.value)) {
23+
timestamp = binding.value;
24+
} else {
25+
timestamp = el.innerHTML;
26+
}
1827
if (!isNaN(timestamp)) {
1928
el.innerHTML = formatNsDuration(timestamp);
2029
}

0 commit comments

Comments
 (0)