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