This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
events.go
287 lines (244 loc) · 9.23 KB
/
events.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package csminify
import (
rep "github.com/markus-wa/cs-demo-minifier/replay"
dem "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs"
events "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
)
// EventCollector provides the possibility of adding custom events to replays.
// First all demo-event handlers must be registered via AddHandler().
// The registered handlers can add replay-events to the collector via AddEvent().
// The handlers can access game-state information via Parser().
// After a tick ends all events that were added to the collector during the tick will be stored into the replay.
type EventCollector struct {
handlers []interface{}
events []rep.Event
parser dem.Parser
}
// AddHandler adds a handler which will be registered on the Parser to the collector.
// The handler should use EventCollector.AddEvent() and be of the type
// func(<EventType>) where EventType is the type of the event to be handled.
// The handler parameter is of type interface because lolnogenerics.
// See: github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs.Parser.RegisterEventHandler()
// GoDoc: https://pkg.go.dev/github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs?tab=doc#Parser
func (ec *EventCollector) AddHandler(handler interface{}) {
ec.handlers = append(ec.handlers, handler)
}
// AddEvent adds an event to the collector.
// The event will be added to the replay after the current tick ends.
func (ec *EventCollector) AddEvent(event rep.Event) {
ec.events = append(ec.events, event)
}
// Parser returns the demo-parser through which custom handlers can access game-state information.
// Returns nil before minification has started - so don't call this before you need it.
func (ec *EventCollector) Parser() dem.Parser {
return ec.parser
}
// EventHandlers provides functions for registering the out-of-the-box provided handlers on EventCollectors.
// The handlers are divided into two groups: Default and Extra.
// Default contains the handlers that are used if no custom EventCollector is specified.
// Extra contains other handlers that are usually not required (i.e. a 'footstep' handler).
var EventHandlers eventHandlers
type eventHandlers struct {
Default defaultEventHandlers
Extra extraEventHandlers
}
type defaultEventHandlers struct{}
func (defaultEventHandlers) RegisterAll(ec *EventCollector) {
EventHandlers.Default.RegisterMatchStarted(ec)
EventHandlers.Default.RegisterGamePhaseChanged(ec)
EventHandlers.Default.RegisterRoundStarted(ec)
EventHandlers.Default.RegisterRoundEnded(ec)
EventHandlers.Default.RegisterPlayerKilled(ec)
EventHandlers.Default.RegisterPlayerHurt(ec)
EventHandlers.Default.RegisterPlayerFlashed(ec)
EventHandlers.Default.RegisterPlayerJump(ec)
EventHandlers.Default.RegisterPlayerTeamChange(ec)
EventHandlers.Default.RegisterPlayerDisconnect(ec)
EventHandlers.Default.RegisterWeaponFired(ec)
EventHandlers.Default.RegisterChatMessage(ec)
EventHandlers.Default.RegisterGrenadeEvents(ec)
}
func (defaultEventHandlers) RegisterMatchStarted(ec *EventCollector) {
ec.AddHandler(func(e events.MatchStart) {
ec.AddEvent(createEvent(rep.EventMatchStarted))
})
}
func (defaultEventHandlers) RegisterGamePhaseChanged(ec *EventCollector) {
ec.AddHandler(func(e events.GamePhaseChanged) {
eb := buildEvent(rep.EventGamePhaseChanged)
eb.intAttr("oldGamePhase", int(e.OldGamePhase))
eb.intAttr("newGamePhase", int(e.NewGamePhase))
ec.AddEvent(eb.build())
})
}
func (defaultEventHandlers) RegisterRoundStarted(ec *EventCollector) {
ec.AddHandler(func(e events.RoundStart) {
ec.AddEvent(createEvent(rep.EventRoundStarted))
})
}
func (defaultEventHandlers) RegisterRoundEnded(ec *EventCollector) {
ec.AddHandler(func(e events.RoundEnd) {
eb := buildEvent(rep.EventRoundEnded)
eb.intAttr("winner", int(e.Winner))
eb.intAttr("reason", int(e.Reason))
ec.AddEvent(eb.build())
})
}
func (defaultEventHandlers) RegisterPlayerKilled(ec *EventCollector) {
ec.AddHandler(func(e events.Kill) {
eb := buildEvent(rep.EventKill)
eb.intAttr(rep.AttrKindVictim, e.Victim.EntityID)
eb.intAttr(rep.AttrKindWeapon, int(e.Weapon.Type))
if e.Killer != nil {
eb.intAttr(rep.AttrKindKiller, e.Killer.EntityID)
}
if e.Assister != nil {
eb.intAttr(rep.AttrKindAssister, e.Assister.EntityID)
}
ec.AddEvent(eb.build())
})
}
func (defaultEventHandlers) RegisterPlayerHurt(ec *EventCollector) {
ec.AddHandler(func(e events.PlayerHurt) {
ec.AddEvent(createEntityEvent(rep.EventHurt, e.Player.EntityID))
})
}
func (defaultEventHandlers) RegisterPlayerFlashed(ec *EventCollector) {
ec.AddHandler(func(e events.PlayerFlashed) {
ec.AddEvent(createEntityEvent(rep.EventFlashed, e.Player.EntityID))
})
}
func (defaultEventHandlers) RegisterPlayerJump(ec *EventCollector) {
ec.AddHandler(func(e events.PlayerJump) {
if e.Player == nil {
return
}
ec.AddEvent(createEntityEvent(rep.EventJump, e.Player.EntityID))
})
}
func (defaultEventHandlers) RegisterPlayerTeamChange(ec *EventCollector) {
ec.AddHandler(func(e events.PlayerTeamChange) {
if e.Player == nil {
return
}
ec.AddEvent(createEntityEvent(rep.EventSwapTeam, e.Player.EntityID))
})
}
func (defaultEventHandlers) RegisterPlayerDisconnect(ec *EventCollector) {
ec.AddHandler(func(e events.PlayerDisconnected) {
ec.AddEvent(createEntityEvent(rep.EventDisconnect, e.Player.EntityID))
})
}
func (defaultEventHandlers) RegisterWeaponFired(ec *EventCollector) {
ec.AddHandler(func(e events.WeaponFire) {
ec.AddEvent(createEntityEvent(rep.EventFire, e.Shooter.EntityID))
})
}
func (defaultEventHandlers) RegisterChatMessage(ec *EventCollector) {
ec.AddHandler(func(e events.ChatMessage) {
eb := buildEvent(rep.EventChatMessage)
eb.stringAttr(rep.AttrKindText, e.Text)
// Skip for now, probably always true anyway
//eb.boolAttr("isChatAll", e.IsChatAll)
if e.Sender != nil {
eb.intAttr(rep.AttrKindSender, e.Sender.EntityID)
}
ec.AddEvent(eb.build())
})
}
func (defaultEventHandlers) RegisterGrenadeEvents(ec *EventCollector) {
ec.AddHandler(func(smokeStartEvent events.SmokeStart) {
eb := withGrenadePosition(buildEvent(rep.EventSmokeStart), smokeStartEvent)
eb.intAttr(rep.AttrKindThrowerID, smokeStartEvent.Thrower.EntityID)
ec.AddEvent(eb.build())
})
ec.AddHandler(func(smokeExpired events.SmokeExpired) {
eb := withGrenadePosition(buildEvent(rep.EventSmokeExpired), smokeExpired)
eb.intAttr(rep.AttrKindThrowerID, smokeExpired.Thrower.EntityID)
ec.AddEvent(eb.build())
})
ec.AddHandler(func(decoyStart events.DecoyStart) {
eb := withGrenadePosition(buildEvent(rep.EventDecoyStart), decoyStart)
eb.intAttr(rep.AttrKindThrowerID, decoyStart.Thrower.EntityID)
ec.AddEvent(eb.build())
})
ec.AddHandler(func(decoyExpired events.DecoyExpired) {
eb := withGrenadePosition(buildEvent(rep.EventDecoyExpired), decoyExpired)
eb.intAttr(rep.AttrKindThrowerID, decoyExpired.Thrower.EntityID)
ec.AddEvent(eb.build())
})
ec.AddHandler(func(fireStart events.FireGrenadeStart) {
eb := withGrenadePosition(buildEvent(rep.EventFireGrenadeStart), fireStart)
// Adding a AttrKindThrowerID throws a Nil Reference
ec.AddEvent(eb.build())
})
ec.AddHandler(func(fireExpired events.FireGrenadeExpired) {
eb := withGrenadePosition(buildEvent(rep.EventFireGrenadeExpired), fireExpired)
// Adding a AttrKindThrowerID throws a Nil Reference
ec.AddEvent(eb.build())
})
ec.AddHandler(func(heEvent events.HeExplode) {
eb := withGrenadePosition(buildEvent(rep.EventHEGrenadeExplosion), heEvent)
eb.intAttr(rep.AttrKindThrowerID, heEvent.Thrower.EntityID)
ec.AddEvent(eb.build())
})
ec.AddHandler(func(flashExplode events.FlashExplode) {
eb := withGrenadePosition(buildEvent(rep.EventFlashExplosion), flashExplode)
eb.intAttr(rep.AttrKindThrowerID, flashExplode.Thrower.EntityID)
ec.AddEvent(eb.build())
})
}
type extraEventHandlers struct{}
func (extraEventHandlers) RegisterAll(ec *EventCollector) {
EventHandlers.Extra.RegisterFootstep(ec)
}
func (extraEventHandlers) RegisterFootstep(ec *EventCollector) {
ec.AddHandler(func(e events.Footstep) {
ec.AddEvent(createEntityEvent(rep.EventFootstep, e.Player.EntityID))
})
}
type eventBuilder struct {
event rep.Event
}
func (b *eventBuilder) stringAttr(key string, value string) *eventBuilder {
b.event.Attributes = append(b.event.Attributes, rep.EventAttribute{
Key: key,
StrVal: value,
})
return b
}
func (b *eventBuilder) intAttr(key string, value int) *eventBuilder {
b.event.Attributes = append(b.event.Attributes, rep.EventAttribute{
Key: key,
NumVal: float64(value),
})
return b
}
func (b *eventBuilder) floatAttr(key string, value float64) {
b.event.Attributes = append(b.event.Attributes, rep.EventAttribute{
Key: key,
NumVal: value,
})
}
func (b eventBuilder) build() rep.Event {
return b.event
}
func buildEvent(eventName string) *eventBuilder {
return &eventBuilder{
event: createEvent(eventName),
}
}
func createEvent(eventName string) rep.Event {
return rep.Event{
Name: eventName,
}
}
func createEntityEvent(eventName string, entityID int) rep.Event {
return buildEvent(eventName).intAttr(rep.AttrKindEntityID, entityID).build()
}
func withGrenadePosition(eb *eventBuilder, e events.GrenadeEventIf) *eventBuilder {
eb.floatAttr("x", e.Base().Position.X)
eb.floatAttr("y", e.Base().Position.Y)
eb.floatAttr("z", e.Base().Position.Z)
return eb
}