Skip to content

Commit 2045dae

Browse files
committed
common: Better field types for DemoHeader
+ Tests
1 parent b4691cb commit 2045dae

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

common/common.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,39 @@ const (
2121

2222
// DemoHeader contains information from a demo's header.
2323
type DemoHeader struct {
24-
Filestamp string // aka. File-type, must be HL2DEMO
25-
Protocol int // Should be 4
26-
NetworkProtocol int // Not sure what this is for
27-
ServerName string // Server's 'hostname' config value
28-
ClientName string // Usually 'GOTV Demo'
29-
MapName string // E.g. de_cache, de_nuke, cs_office, etc.
30-
GameDirectory string // Usually 'csgo'
31-
PlaybackTime float32 // Demo duration in seconds (= PlaybackTicks / Server's tickrate)
32-
PlaybackTicks int // Game duration in ticks (= PlaybackTime * Server's tickrate)
33-
PlaybackFrames int // Amount of 'frames' aka demo-ticks recorded (= PlaybackTime * Demo's recording rate)
34-
SignonLength int // Length of the Signon package in bytes
24+
Filestamp string // aka. File-type, must be HL2DEMO
25+
Protocol int // Should be 4
26+
NetworkProtocol int // Not sure what this is for
27+
ServerName string // Server's 'hostname' config value
28+
ClientName string // Usually 'GOTV Demo'
29+
MapName string // E.g. de_cache, de_nuke, cs_office, etc.
30+
GameDirectory string // Usually 'csgo'
31+
PlaybackTime time.Duration // Demo duration in seconds (= PlaybackTicks / Server's tickrate)
32+
PlaybackTicks int // Game duration in ticks (= PlaybackTime * Server's tickrate)
33+
PlaybackFrames int // Amount of 'frames' aka demo-ticks recorded (= PlaybackTime * Demo's recording rate)
34+
SignonLength int // Length of the Signon package in bytes
3535
}
3636

3737
// FrameRate returns the frame rate of the demo (frames / demo-ticks per second).
3838
// Not necessarily the tick-rate the server ran on during the game.
39-
func (h DemoHeader) FrameRate() float32 {
40-
return float32(h.PlaybackFrames) / h.PlaybackTime
39+
func (h DemoHeader) FrameRate() float64 {
40+
return float64(h.PlaybackFrames) / h.PlaybackTime.Seconds()
4141
}
4242

4343
// FrameTime returns the time a frame / demo-tick takes in seconds.
4444
func (h DemoHeader) FrameTime() time.Duration {
45-
return time.Duration(h.PlaybackTime / float32(h.PlaybackFrames) * float32(time.Second))
45+
return time.Duration(h.PlaybackTime.Nanoseconds() / int64(h.PlaybackFrames))
4646
}
4747

4848
// TickRate returns the tick-rate the server ran on during the game.
4949
// VolvoPlx128TixKTnxBye
50-
func (h DemoHeader) TickRate() float32 {
51-
return float32(h.PlaybackTicks) / h.PlaybackTime
50+
func (h DemoHeader) TickRate() float64 {
51+
return float64(h.PlaybackTicks) / h.PlaybackTime.Seconds()
5252
}
5353

5454
// TickTime returns the time a single tick takes in seconds.
5555
func (h DemoHeader) TickTime() time.Duration {
56-
return time.Duration(h.PlaybackTime / float32(h.PlaybackTicks) * float32(time.Second))
56+
return time.Duration(h.PlaybackTime.Nanoseconds() / int64(h.PlaybackTicks))
5757
}
5858

5959
// GrenadeProjectile is a grenade thrown intentionally by a player. It is used to track grenade projectile

common/common_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
r3 "github.com/golang/geo/r3"
8+
assert "github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestBombPosition(t *testing.T) {
12+
groundPos := r3.Vector{X: 1, Y: 2, Z: 3}
13+
bomb := Bomb{
14+
LastOnGroundPosition: groundPos,
15+
}
16+
17+
assert.Equal(t, groundPos, bomb.Position(), "Bomb position should be LastOnGroundPosition")
18+
19+
playerPos := r3.Vector{X: 4, Y: 5, Z: 6}
20+
bomb.Carrier = &Player{Position: playerPos}
21+
assert.Equal(t, playerPos, bomb.Position(), "Bomb position should be Player.Position")
22+
}
23+
24+
func TestGrenadeProjectileUniqueID(t *testing.T) {
25+
assert.NotEqual(t, NewGrenadeProjectile().UniqueID(), NewGrenadeProjectile().UniqueID(), "UniqueIDs of different grenade projectiles should be different")
26+
}
27+
28+
func TestDemoHeader(t *testing.T) {
29+
header := DemoHeader{
30+
PlaybackFrames: 256,
31+
PlaybackTicks: 512,
32+
PlaybackTime: time.Duration(4 * time.Second),
33+
}
34+
35+
assert.Equal(t, float64(64), header.FrameRate(), "FrameRate should be 64")
36+
assert.Equal(t, time.Duration(time.Second/64), header.FrameTime(), "FrameTime should be 1/64 sec")
37+
38+
assert.Equal(t, float64(128.0), header.TickRate(), "TickRate should be 128")
39+
assert.Equal(t, time.Duration(time.Second/128), header.TickTime(), "TickTime should be 1/128")
40+
}

parsing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"time"
78

89
common "github.com/markus-wa/demoinfocs-golang/common"
910
events "github.com/markus-wa/demoinfocs-golang/events"
@@ -44,7 +45,7 @@ func (p *Parser) ParseHeader() (common.DemoHeader, error) {
4445
h.ClientName = p.bitReader.ReadCString(maxOsPath)
4546
h.MapName = p.bitReader.ReadCString(maxOsPath)
4647
h.GameDirectory = p.bitReader.ReadCString(maxOsPath)
47-
h.PlaybackTime = p.bitReader.ReadFloat()
48+
h.PlaybackTime = time.Duration(p.bitReader.ReadFloat() * float32(time.Second))
4849
h.PlaybackTicks = p.bitReader.ReadSignedInt(32)
4950
h.PlaybackFrames = p.bitReader.ReadSignedInt(32)
5051
h.SignonLength = p.bitReader.ReadSignedInt(32)

0 commit comments

Comments
 (0)