Skip to content

Commit 8c642b5

Browse files
committed
TeamState: fix Opponent containing wrong ref
fixes #90 (pointers are hard)
1 parent 012df0e commit 8c642b5

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

game_events_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package demoinfocs
2+
3+
import (
4+
"crypto/rand"
5+
"github.com/markus-wa/demoinfocs-golang/common"
6+
"github.com/markus-wa/demoinfocs-golang/events"
7+
"github.com/markus-wa/demoinfocs-golang/msg"
8+
"github.com/stretchr/testify/assert"
9+
"testing"
10+
)
11+
12+
// See #90
13+
func TestRoundEnd_LoserState_Score(t *testing.T) {
14+
p := NewParser(rand.Reader)
15+
16+
p.gameState.tState.Score = 1
17+
p.gameState.ctState.Score = 2
18+
19+
eventOccurred := 0
20+
p.RegisterEventHandler(func(e events.RoundEnd) {
21+
eventOccurred++
22+
assert.Equal(t, e, events.RoundEnd{
23+
Winner: common.TeamTerrorists,
24+
WinnerState: p.GameState().TeamTerrorists(),
25+
LoserState: p.GameState().TeamCounterTerrorists(),
26+
Message: "test",
27+
Reason: events.RoundEndReasonTerroristsWin,
28+
})
29+
})
30+
31+
p.gameEventDescs = map[int32]*msg.CSVCMsg_GameEventListDescriptorT{
32+
1: {
33+
Name: "round_end",
34+
Keys: []*msg.CSVCMsg_GameEventListKeyT{
35+
{Name: "winner"},
36+
{Name: "message"},
37+
{Name: "reason"},
38+
},
39+
},
40+
}
41+
42+
ge := new(msg.CSVCMsg_GameEvent)
43+
ge.Eventid = 1
44+
ge.EventName = "round_end"
45+
ge.Keys = []*msg.CSVCMsg_GameEventKeyT{
46+
{ValByte: 2},
47+
{ValString: "test"},
48+
{ValByte: 9},
49+
}
50+
p.handleGameEvent(ge)
51+
52+
assert.Equal(t, 1, eventOccurred)
53+
}

parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Parser struct {
5151
eventDispatcher dp.Dispatcher
5252
currentFrame int // Demo-frame, not ingame-tick
5353
header *common.DemoHeader // Pointer so we can check for nil
54-
gameState GameState
54+
gameState *GameState
5555
cancelChan chan struct{} // Non-anime-related, used for aborting the parsing
5656
err error // Contains a error that occurred during parsing if any
5757
errLock sync.Mutex // Used to sync up error mutations between parsing & handling go-routines
@@ -103,7 +103,7 @@ func (p *Parser) Header() common.DemoHeader {
103103
// GameState returns the current game-state.
104104
// It contains most of the relevant information about the game such as players, teams, scores, grenades etc.
105105
func (p *Parser) GameState() IGameState {
106-
return &p.gameState
106+
return p.gameState
107107
}
108108

109109
// CurrentFrame return the number of the current frame, aka. 'demo-tick' (Since demos often have a different tick-rate than the game).
@@ -232,7 +232,7 @@ func NewParserWithConfig(demostream io.Reader, config ParserConfig) *Parser {
232232
p.rawPlayers = make(map[int]*playerInfo)
233233
p.triggers = make(map[int]*boundingBoxInformation)
234234
p.cancelChan = make(chan struct{}, 1)
235-
p.gameState = *newGameState()
235+
p.gameState = newGameState()
236236
p.grenadeModelIndices = make(map[int]common.EquipmentElement)
237237

238238
// Attach proto msg handlers

0 commit comments

Comments
 (0)