Skip to content

Commit 2e0939b

Browse files
committed
Return time.Duration where applicable
FrameTime(), TickTime() + CurrentTime() Also added DemoHeader.TickRate()
1 parent 1f08ad2 commit 2e0939b

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

common/structs.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"math/rand"
5+
"time"
56

67
r3 "github.com/golang/geo/r3"
78
s2 "github.com/golang/geo/s2"
@@ -26,14 +27,24 @@ type DemoHeader struct {
2627

2728
// FrameRate returns the frame rate of the demo (frames / demo-ticks per second).
2829
// Not necessarily the tick-rate the server ran on during the game.
29-
// VolvoPlx128TixKTnxBye
3030
func (h DemoHeader) FrameRate() float32 {
3131
return float32(h.PlaybackFrames) / h.PlaybackTime
3232
}
3333

3434
// FrameTime returns the time a frame / demo-tick takes in seconds.
35-
func (h DemoHeader) FrameTime() float32 {
36-
return h.PlaybackTime / float32(h.PlaybackFrames)
35+
func (h DemoHeader) FrameTime() time.Duration {
36+
return time.Duration(h.PlaybackTime / float32(h.PlaybackFrames) * float32(time.Second))
37+
}
38+
39+
// TickRate returns the tick-rate the server ran on during the game.
40+
// VolvoPlx128TixKTnxBye
41+
func (h DemoHeader) TickRate() float32 {
42+
return float32(h.PlaybackTicks) / h.PlaybackTime
43+
}
44+
45+
// TickTime returns the time a single tick takes in seconds.
46+
func (h DemoHeader) TickTime() time.Duration {
47+
return time.Duration(h.PlaybackTime / float32(h.PlaybackTicks) * float32(time.Second))
3748
}
3849

3950
// Player contains mostly game-relevant player information.

demoinfocs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestDemoInfoCs(t *testing.T) {
5959
if err != nil {
6060
t.Fatal(err)
6161
}
62-
fmt.Printf("Header: %v - FrameRate()=%.2f frames/s ; FrameTime()=%.1fms\n", h, h.FrameRate(), h.FrameTime()*1000)
62+
fmt.Printf("Header: %v - FrameRate()=%.2f frames/s ; FrameTime()=%s ; TickRate()=%.2f frames/s ; TickTime()=%s\n", h, h.FrameRate(), h.FrameTime(), h.TickRate(), h.TickTime())
6363
h2 := p.Header()
6464
if h != h2 {
6565
t.Errorf("Headers returned by ParseHeader() & Header(), respectively, aren't equal; ParseHeader(): %v - Header(): %v", h, h2)
@@ -93,7 +93,7 @@ func TestDemoInfoCs(t *testing.T) {
9393
ingameTick := gs.IngameTick()
9494
currentFrame := p.CurrentFrame()
9595
// Score + 1 for winner because it hasn't actually been updated yet
96-
fmt.Printf("Round finished: score=%d:%d ; winnerSide=%s ; clanName=%q ; teamId=%d ; teamFlag=%s ; ingameTime=%.1fs ; progress=%.1f%% ; tick=%d ; frame=%d\n", winner.Score()+1, loser.Score(), winnerSide, winnerClan, winnerId, winnerFlag, ingameTime, progressPercent, ingameTick, currentFrame)
96+
fmt.Printf("Round finished: score=%d:%d ; winnerSide=%s ; clanName=%q ; teamId=%d ; teamFlag=%s ; ingameTime=%s ; progress=%.1f%% ; tick=%d ; frame=%d\n", winner.Score()+1, loser.Score(), winnerSide, winnerClan, winnerId, winnerFlag, ingameTime, progressPercent, ingameTick, currentFrame)
9797
if len(winnerClan) == 0 || winnerId == 0 || len(winnerFlag) == 0 || ingameTime == 0 || progressPercent == 0 || ingameTick == 0 || currentFrame == 0 {
9898
t.Error("Unexprected default value, check output of last round")
9999
}

parser.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package demoinfocs
33
import (
44
"io"
55
"sync"
6+
"time"
67

78
r3 "github.com/golang/geo/r3"
89
dp "github.com/markus-wa/godispatch"
@@ -104,9 +105,9 @@ func (p *Parser) CurrentFrame() int {
104105
return p.currentFrame
105106
}
106107

107-
// CurrentTime returns the ingame time in seconds since the start of the demo.
108-
func (p *Parser) CurrentTime() float32 {
109-
return float32(p.currentFrame) * p.header.FrameTime()
108+
// CurrentTime returns the time elapsed since the start of the demo
109+
func (p *Parser) CurrentTime() time.Duration {
110+
return time.Duration(p.currentFrame) * p.header.FrameTime()
110111
}
111112

112113
// Progress returns the parsing progress from 0 to 1.

0 commit comments

Comments
 (0)