Skip to content

Commit d9d3f45

Browse files
committed
common: add Player.FlashDurationTimeRemaining()
1 parent e4a59f2 commit d9d3f45

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

common/player.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func (p *Player) IsAlive() bool {
4949
}
5050

5151
// IsBlinded returns true if the player is currently flashed.
52-
// This is more accurate than 'FlashDuration != 0' as it takes into account the FlashTick and GameState.IngameTick().
52+
// This is more accurate than 'FlashDuration != 0' as it also takes into account FlashTick, DemoHeader.TickRate() and GameState.IngameTick().
5353
func (p *Player) IsBlinded() bool {
54-
return p.FlashDuration > float32(p.ingameTickProvider()-p.FlashTick)/float32(p.tickRate)
54+
return p.FlashDurationTimeRemaining() > 0
5555
}
5656

5757
// FlashDurationTime returns the duration of the blinding effect as time.Duration instead of float32 in seconds.
@@ -60,9 +60,30 @@ func (p *Player) FlashDurationTime() time.Duration {
6060
if !p.IsBlinded() {
6161
return time.Duration(0)
6262
}
63+
return p.flashDurationTimeFull()
64+
}
65+
66+
func (p *Player) flashDurationTimeFull() time.Duration {
6367
return time.Duration(float32(time.Second) * p.FlashDuration)
6468
}
6569

70+
// FlashDurationTimeRemaining returns the remaining duration of the blinding effect (or 0 if the player is not currently blinded).
71+
// It takes into consideration FlashDuration, FlashTick, DemoHeader.TickRate() and GameState.IngameTick().
72+
func (p *Player) FlashDurationTimeRemaining() time.Duration {
73+
// In case the demo header is broken
74+
// TODO: read tickRate from CVARs as fallback
75+
if p.tickRate == 0 {
76+
return time.Duration(p.FlashDuration) * time.Second
77+
}
78+
79+
timeSinceFlash := time.Duration(float64(p.ingameTickProvider()-p.FlashTick) / p.tickRate * float64(time.Second))
80+
remaining := p.flashDurationTimeFull() - timeSinceFlash
81+
if remaining < 0 {
82+
return 0
83+
}
84+
return remaining
85+
}
86+
6687
/*
6788
Some interesting data regarding flashes.
6889

common/player_test.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,56 @@ func TestPlayerFlashed_FlashDuration_Over(t *testing.T) {
6969
assert.False(t, pl.IsBlinded(), "Should not be flashed")
7070
}
7171

72+
func TestPlayer_FlashDurationTime_Default(t *testing.T) {
73+
pl := newPlayer(0)
74+
75+
assert.Equal(t, time.Duration(0), pl.FlashDurationTime())
76+
}
77+
7278
func TestPlayer_FlashDurationTime(t *testing.T) {
73-
p := newPlayer(0)
79+
pl := newPlayer(0)
80+
81+
pl.FlashDuration = 2.3
82+
83+
assert.Equal(t, 2300*time.Millisecond, pl.FlashDurationTime())
84+
}
85+
86+
func TestPlayer_FlashDurationTimeRemaining_Default(t *testing.T) {
87+
pl := NewPlayer(0, tickProvider(128))
88+
89+
assert.Equal(t, time.Duration(0), pl.FlashDurationTimeRemaining())
90+
}
7491

75-
assert.Equal(t, time.Duration(0), p.FlashDurationTime())
92+
func TestPlayer_FlashDurationTimeRemaining(t *testing.T) {
93+
pl := newPlayer(128 * 2)
94+
95+
pl.FlashDuration = 3
96+
pl.FlashTick = 128
97+
assert.Equal(t, 2*time.Second, pl.FlashDurationTimeRemaining())
98+
}
99+
100+
func TestPlayer_FlashDurationTimeRemaining_Zero(t *testing.T) {
101+
pl := newPlayer(128 * 4)
102+
103+
pl.FlashDuration = 3
104+
pl.FlashTick = 128
105+
assert.Equal(t, time.Duration(0), pl.FlashDurationTimeRemaining())
106+
}
107+
108+
func TestPlayer_FlashDurationTimeRemaining_FlashDuration_Over(t *testing.T) {
109+
pl := newPlayer(128 * 4)
110+
111+
pl.FlashDuration = 1
112+
pl.FlashTick = 128
113+
assert.Equal(t, time.Duration(0), pl.FlashDurationTimeRemaining())
114+
}
76115

77-
p.FlashDuration = 2.3
116+
func TestPlayer_FlashDurationTimeRemaining_Fallback(t *testing.T) {
117+
pl := NewPlayer(0, tickProvider(128))
78118

79-
assert.Equal(t, 2300*time.Millisecond, p.FlashDurationTime())
119+
pl.FlashDuration = 2
120+
pl.FlashTick = 128 * 2
121+
assert.Equal(t, 2*time.Second, pl.FlashDurationTimeRemaining())
80122
}
81123

82124
func newPlayer(tick int) *Player {

0 commit comments

Comments
 (0)