Repository: markus-wa/demoinfocs-golang
Pull request #413 (2023-09-06 22:47:25)

feat: add RoundFreezetimeChanged event
- Add a `RoundFreezetimeChanged` event
- Add `IsFreezetimePeriod()` in game state


Patch:
diff --git a/pkg/demoinfocs/datatables.go b/pkg/demoinfocs/datatables.go
index 387a90a..dfec4f7 100644
--- a/pkg/demoinfocs/datatables.go
+++ b/pkg/demoinfocs/datatables.go
@@ -1071,6 +1071,15 @@ func (p *parser) bindGameRules() {
 			roundTime = val.Int()
 		})
 
+		entity.Property(grPrefix("m_bFreezePeriod")).OnUpdate(func(val st.PropertyValue) {
+			newIsFreezetime := val.BoolVal()
+			p.eventDispatcher.Dispatch(events.RoundFreezetimeChanged{
+				OldIsFreezetime: p.gameState.isFreezetime,
+				NewIsFreezetime: newIsFreezetime,
+			})
+			p.gameState.isFreezetime = newIsFreezetime
+		})
+
 		entity.Property(grPrefix("m_gamePhase")).OnUpdate(func(val st.PropertyValue) {
 			oldGamePhase := p.gameState.gamePhase
 			p.gameState.gamePhase = common.GamePhase(val.Int())
diff --git a/pkg/demoinfocs/events/events.go b/pkg/demoinfocs/events/events.go
index af8114c..a78a05c 100644
--- a/pkg/demoinfocs/events/events.go
+++ b/pkg/demoinfocs/events/events.go
@@ -38,6 +38,12 @@ type RoundStart struct {
 // RoundFreezetimeEnd signals that the freeze time is over.
 type RoundFreezetimeEnd struct{}
 
+// RoundFreezetimeChanged signals that the freeze time period has changed.
+type RoundFreezetimeChanged struct {
+	OldIsFreezetime bool
+	NewIsFreezetime bool
+}
+
 // RoundEndReason is the type for the various RoundEndReasonXYZ constants.
 //
 // See RoundEnd.
diff --git a/pkg/demoinfocs/fake/game_state.go b/pkg/demoinfocs/fake/game_state.go
index 965668d..92134d9 100644
--- a/pkg/demoinfocs/fake/game_state.go
+++ b/pkg/demoinfocs/fake/game_state.go
@@ -85,6 +85,11 @@ func (gs *GameState) IsWarmupPeriod() bool {
 	return gs.Called().Bool(0)
 }
 
+// IsFreezetimePeriod is a mock-implementation of GameState.IsFreezetimePeriod().
+func (gs *GameState) IsFreezetimePeriod() bool {
+	return gs.Called().Bool(0)
+}
+
 // IsMatchStarted is a mock-implementation of GameState.IsMatchStarted().
 func (gs *GameState) IsMatchStarted() bool {
 	return gs.Called().Bool(0)
diff --git a/pkg/demoinfocs/game_state.go b/pkg/demoinfocs/game_state.go
index 61c232c..59f9e25 100644
--- a/pkg/demoinfocs/game_state.go
+++ b/pkg/demoinfocs/game_state.go
@@ -33,6 +33,7 @@ type gameState struct {
 	totalRoundsPlayed            int
 	gamePhase                    common.GamePhase
 	isWarmupPeriod               bool
+	isFreezetime                 bool
 	isMatchStarted               bool
 	overtimeCount                int
 	lastFlash                    lastFlash                              // Information about the last flash that exploded, used to find the attacker and projectile for player_blind events
@@ -192,6 +193,11 @@ func (gs gameState) IsWarmupPeriod() bool {
 	return gs.isWarmupPeriod
 }
 
+// IsFreezetimePeriod returns whether the game is currently in freezetime period according to CCSGameRulesProxy.
+func (gs gameState) IsFreezetimePeriod() bool {
+	return gs.isFreezetime
+}
+
 // IsMatchStarted returns whether the match has started according to CCSGameRulesProxy.
 func (gs gameState) IsMatchStarted() bool {
 	return gs.isMatchStarted
diff --git a/pkg/demoinfocs/game_state_interface.go b/pkg/demoinfocs/game_state_interface.go
index 14c9ff9..511aab8 100644
--- a/pkg/demoinfocs/game_state_interface.go
+++ b/pkg/demoinfocs/game_state_interface.go
@@ -55,6 +55,8 @@ type GameState interface {
 	GamePhase() common.GamePhase
 	// IsWarmupPeriod returns whether the game is currently in warmup period according to CCSGameRulesProxy.
 	IsWarmupPeriod() bool
+	// IsFreezetimePeriod returns whether the game is currently in freezetime period according to CCSGameRulesProxy.
+	IsFreezetimePeriod() bool
 	// IsMatchStarted returns whether the match has started according to CCSGameRulesProxy.
 	IsMatchStarted() bool
 	// OvertimeCount returns the number of overtime according to CCSGameRulesProxy.


Test patch:
diff --git a/pkg/demoinfocs/game_state_test.go b/pkg/demoinfocs/game_state_test.go
index fe77fdd..5dbdc68 100644
--- a/pkg/demoinfocs/game_state_test.go
+++ b/pkg/demoinfocs/game_state_test.go
@@ -353,6 +353,12 @@ func TestGameRules(t *testing.T) {
 	assert.Equal(t, ErrFailedToRetrieveGameRule, err)
 }
 
+func TestGameRules_IsFreezetimePeriod(t *testing.T) {
+	gs := gameState{isFreezetime: true}
+
+	assert.Equal(t, true, gs.IsFreezetimePeriod())
+}
+
 func newPlayerS1() *common.Player {
 	pl := newPlayerWithEntityIDS1(1)
 	return pl