forked from markus-wa/demoinfocs-golang
/
team_switch.go
75 lines (63 loc) · 2.73 KB
/
team_switch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package fuzzy
import (
dem "github.com/markus-wa/demoinfocs-golang"
common "github.com/markus-wa/demoinfocs-golang/common"
events "github.com/markus-wa/demoinfocs-golang/events"
dp "github.com/markus-wa/godispatch"
)
// TeamSwitchEvent signals that the teams have switched.
//
// See also: ValveMatchmakingTeamSwitchEmitter
type TeamSwitchEvent struct{}
// ValveMatchmakingTeamSwitchEmitter emits a TeamSwitchEvent for Valve MM demos.
// Sadly this WON'T work for Major games as it currently doesn't account for overtime.
//
// This is a beta feature and may be changed or replaced without notice.
//
// See also: github.com/markus-wa/demoinfocs-golang/ParserConfig.AdditionalEventEmitters
type ValveMatchmakingTeamSwitchEmitter struct {
parser *dem.Parser
dispatch func(interface{})
currentHandlerID dp.HandlerIdentifier
tScoreBeforeSwitch int
ctScoreBeforeSwitch int
}
// Register registers the emitter on the parser. It should only be used by the parser.
func (em *ValveMatchmakingTeamSwitchEmitter) Register(parser *dem.Parser, dispatch func(interface{})) {
em.parser = parser
em.dispatch = dispatch
em.currentHandlerID = parser.RegisterEventHandler(em.handleLastRoundHalf)
}
// Get to the last round of the first half
func (em *ValveMatchmakingTeamSwitchEmitter) handleLastRoundHalf(events.LastRoundHalfEvent) {
em.parser.UnregisterEventHandler(em.currentHandlerID)
em.currentHandlerID = em.parser.RegisterEventHandler(em.handleRoundEnded)
}
// Get scores before switch
func (em *ValveMatchmakingTeamSwitchEmitter) handleRoundEnded(ev events.RoundEndedEvent) {
em.tScoreBeforeSwitch = em.parser.GameState().TState().Score()
em.ctScoreBeforeSwitch = em.parser.GameState().CTState().Score()
// Score hasn't been updated yet because CS:GO demos are weird
switch ev.Winner {
case common.TeamTerrorists:
em.tScoreBeforeSwitch++
case common.TeamCounterTerrorists:
em.ctScoreBeforeSwitch++
}
em.parser.UnregisterEventHandler(em.currentHandlerID)
em.currentHandlerID = em.parser.RegisterEventHandler(em.handleRoundStarted)
}
// Find first round of second half
func (em *ValveMatchmakingTeamSwitchEmitter) handleRoundStarted(events.RoundStartedEvent) {
em.parser.UnregisterEventHandler(em.currentHandlerID)
em.currentHandlerID = em.parser.RegisterEventHandler(em.handleTickDone)
}
// Wait for score to update - this isn't (necessarily?) the case after RoundStartedEvent
func (em *ValveMatchmakingTeamSwitchEmitter) handleTickDone(events.TickDoneEvent) {
tScoreOk := em.parser.GameState().TState().Score() == em.ctScoreBeforeSwitch
ctScoreOk := em.parser.GameState().CTState().Score() == em.tScoreBeforeSwitch
if tScoreOk && ctScoreOk {
em.dispatch(TeamSwitchEvent{})
em.parser.UnregisterEventHandler(em.currentHandlerID)
}
}