Skip to content

Commit bcaa2d5

Browse files
committed
Chat message example
1 parent 31d2816 commit bcaa2d5

File tree

5 files changed

+83
-21
lines changed

5 files changed

+83
-21
lines changed

events/events.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,30 +257,30 @@ type PlayerDisconnectEvent struct {
257257

258258
// SayTextEvent signals a chat message. It contains the raw
259259
// network message data for admin / console messages.
260-
// EntityIndex will probably always be 0
260+
// EntIdx will probably always be 0
261261
// See ChatMessageEvent and SayText2Event for player chat messages.
262262
type SayTextEvent struct {
263-
EntityIndex int
264-
Text string
265-
IsChat bool
266-
IsChatAll bool
263+
EntIdx int // Not sure what this is, doesn't seem to be the entity-ID
264+
Text string
265+
IsChat bool // Not sure, from the net-message
266+
IsChatAll bool // Seems to always be false, team chat might not be recorded
267267
}
268268

269-
// SayText2Event signals a chat message. It just contains the raw network message
270-
// for player chat messages, ChatMessageEvent may be more interesting.
269+
// SayText2Event signals a chat message. It just contains the raw network message.
270+
// For player chat messages, ChatMessageEvent may be more interesting.
271271
// Team chat is generally not recorded so IsChatAll will probably always be false.
272272
// See SayTextEvent for admin / console messages.
273273
type SayText2Event struct {
274-
Sender *common.Player
275-
MsgName string
276-
Params []string
277-
IsChat bool
278-
IsChatAll bool
274+
EntIdx int // Not sure what this is, doesn't seem to be the entity-ID
275+
MsgName string // The message type, e.g. Cstrike_Chat_All for global chat
276+
Params []string // The message's parameters, for Cstrike_Chat_All parameter 1 is the player and 2 the message for example
277+
IsChat bool // Not sure, from the net-message
278+
IsChatAll bool // Seems to always be false, team chat might not be recorded
279279
}
280280

281281
// ChatMessageEvent signals a player generated chat message.
282282
// Since team chat is generally not recorded IsChatAll will probably always be false.
283-
// See SayTextEvent for admin / console messages and SayText2Event for raw network packages.
283+
// See SayTextEvent for admin / console messages and SayText2Event for raw network package data.
284284
type ChatMessageEvent struct {
285285
Sender *common.Player
286286
Text string

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Here you can find a overview of examples on how to use demoinfocs-golang.
77
|[heatmap](heatmap)|Creating a heatmap from positions where players fired shots from|
88
|[nade-trajectories](nade-trajectories)|Map overview with grenade trajectories|
99
|[net-messages](net-messages)|Parsing and handling custom net-messages|
10-
|[print-events](print-events)|Printig kills & scores|
10+
|[print-events](print-events)|Printing kills, scores & chat messages|

examples/print-events/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Printing event infos
22

3-
This example shows how to use the library to print out information of some key game events - kills and team scores.
3+
This example shows how to use the library to print out information of some key game events - kills, team scores & chat messages.
44

55
## Running the example
66

@@ -11,6 +11,10 @@ This example shows how to use the library to print out information of some key g
1111
```
1212
Map: de_cache
1313
[T]xms*ASUS ♥ /F/ <AK-47 (HS)> [CT]crisby
14+
Chat - [T]to1nou * Seagate says: hf
15+
Chat - [CT]syrsoNR says: hfhf
16+
Chat - [CT]keev says: ,.+*`*+.,_,.+*`*+., GL & HF ,.+*`*+.,_,.+*`*+.,
17+
Chat - [T]ALEX * Intel says: hfhf
1418
[CT]tiziaN <USP-S (HS)> [T]Ex6TenZ-BALLISTIX
1519
[CT]tiziaN <USP-S> [T]mistou * Cooler Master
1620
[CT]tiziaN <USP-S (HS)> [T]ALEX * Intel
@@ -26,8 +30,10 @@ Round finished: winnerSide=CT ; score=1:0
2630
[CT]crisby <UMP-45 (HS)> [T]to1nou * Seagate
2731
[CT]kzy LJ∼ <FAMAS> [T]Ex6TenZ-BALLISTIX
2832
[CT]kzy LJ∼ <FAMAS (HS)> [T]xms*ASUS ♥ /F/
33+
Chat - [CT]syrsoNR says: ns
2934
[CT]keev <UMP-45> [T]mistou * Cooler Master
3035
Round finished: winnerSide=CT ; score=2:0
36+
Chat - [T]Ex6TenZ-BALLISTIX says: ty
3137
[CT]crisby <UMP-45 (HS)> [T]to1nou * Seagate
3238
[CT]kzy LJ∼ <FAMAS (HS)> [T]Ex6TenZ-BALLISTIX
3339
[CT]crisby <USP-S> [CT]kzy LJ∼
@@ -54,6 +60,9 @@ Round finished: winnerSide=CT ; score=4:0
5460
[CT]tiziaN <AK-47> [T]ALEX * Intel
5561
[CT]keev <AWP> [T]Ex6TenZ-BALLISTIX
5662
Round finished: winnerSide=CT ; score=5:0
63+
Chat - [T]ALEX * Intel says: hp tiz?
64+
Chat - [CT]syrsoNR says: 48
65+
Chat - [T]ALEX * Intel says: ty
5766
[CT]keev <AWP> [T]mistou * Cooler Master
5867
[T]Ex6TenZ-BALLISTIX <Tec-9 (HS)> [CT]keev
5968
[T]Ex6TenZ-BALLISTIX <Tec-9 (HS)> [CT]tiziaN
@@ -130,6 +139,10 @@ Round finished: winnerSide=CT ; score=11:3
130139
[T]to1nou * Seagate <AK-47> [CT]kzy LJ∼
131140
[T]xms*ASUS ♥ /F/ <AK-47> [CT]crisby
132141
Round finished: winnerSide=T ; score=4:11
142+
Chat - [T]to1nou * Seagate says: gh
143+
Chat - [CT]syrsoNR says: gh
144+
Chat - [T]ALEX * Intel says: gh
145+
Chat - [T]keev says: ,.+*`*+.,_,.+*`*+., GL & HF ,.+*`*+.,_,.+*`*+.,
133146
[CT]mistou * Cooler Master <P2000 (HS)> [T]tiziaN
134147
[CT]mistou * Cooler Master <P2000 (HS)> [T]syrsoNR
135148
[CT]mistou * Cooler Master <P2000 (HS)> [T]keev
@@ -203,6 +216,15 @@ Round finished: No winner (tie)
203216
[CT]Ex6TenZ-BALLISTIX <World> [CT]Ex6TenZ-BALLISTIX
204217
[CT]to1nou * Seagate <World> [CT]to1nou * Seagate
205218
Round finished: No winner (tie)
219+
Chat - [T]keev says: we have no money
220+
Chat - [CT]mistou * Cooler Master says: same here
221+
Chat - [CT]ALEX * Intel says: we have everything :/
222+
Chat - [T]crisby says: load backup again
223+
Chat - [CT]ALEX * Intel says: oh mistou dsnt
224+
Chat - [T]keev says: haha
225+
Chat - [T]keev says: need backup agaiN
226+
Chat - [CT]ALEX * Intel says: yy
227+
Chat - [CT]mistou * Cooler Master says: yup
206228
[CT]ALEX * Intel <World> [CT]ALEX * Intel
207229
[CT]xms*ASUS ♥ /F/ <World> [CT]xms*ASUS ♥ /F/
208230
[CT]to1nou * Seagate <World> [CT]to1nou * Seagate
@@ -214,6 +236,22 @@ Round finished: No winner (tie)
214236
[T]keev <World> [T]keev
215237
[T]crisby <World> [T]crisby
216238
Round finished: No winner (tie)
239+
Chat - [CT]mistou * Cooler Master says: good
240+
Chat - [T]keev says: fine
241+
Chat - [T]keev says: rdy?
242+
Chat - [CT]to1nou * Seagate says: 1s
243+
Chat - [CT]Ex6TenZ-BALLISTIX says: 2s
244+
Chat - [T]keev says: 1 or 2
245+
Chat - [CT]ALEX * Intel says: 10
246+
Chat - [CT]to1nou * Seagate says: 3
247+
Chat - [T]keev says: :spazdunno:
248+
Chat - [T]keev says: :D
249+
Chat - [CT]to1nou * Seagate says: hf rdy
250+
Chat - [CT]Ex6TenZ-BALLISTIX says: rdy
251+
Chat - [CT]ALEX * Intel says: !unpause
252+
Chat - [T]keev says: !pause
253+
Chat - [T]keev says: cs buG
254+
Chat - [T]keev says: but liv
217255
[CT]mistou * Cooler Master <AWP> [T]tiziaN
218256
[T]kzy LJ∼ <p250> [CT]mistou * Cooler Master
219257
[T]keev <Tec-9> [CT]xms*ASUS ♥ /F/
@@ -222,6 +260,11 @@ Round finished: No winner (tie)
222260
[CT]to1nou * Seagate <AWP> [T]syrsoNR
223261
[CT]to1nou * Seagate <AWP> [T]crisby
224262
Round finished: winnerSide=T ; score=14:10
263+
Chat - [T]keev says: rdy?
264+
Chat - [CT]to1nou * Seagate says: yes
265+
Chat - [T]keev says: !unpause
266+
Chat - [CT]to1nou * Seagate says: hf
267+
Chat - [T]keev says: hfhf
225268
[CT]to1nou * Seagate <AWP> [T]crisby
226269
[T]tiziaN <AK-47> [CT]Ex6TenZ-BALLISTIX
227270
[CT]mistou * Cooler Master <Five-SeveN (HS)> [T]syrsoNR
@@ -262,4 +305,10 @@ Round finished: winnerSide=CT ; score=13:15
262305
[T]crisby <AK-47 (HS)> [CT]Ex6TenZ-BALLISTIX
263306
[T]keev <Tec-9> [CT]to1nou * Seagate
264307
Round finished: winnerSide=T ; score=16:13
308+
Chat - [CT]xms*ASUS ♥ /F/ says: gg
309+
Chat - [CT]ALEX * Intel says: gg
310+
Chat - [CT]to1nou * Seagate says: gg
311+
Chat - [CT]Ex6TenZ-BALLISTIX says: wp
312+
Chat - [T]tiziaN says: gg
313+
Chat - [T]syrsoNR says: gggg
265314
```

examples/print-events/print_events.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func main() {
5252
}
5353
})
5454

55+
// Register handler for chat messages to print them
56+
p.RegisterEventHandler(func(e events.ChatMessageEvent) {
57+
fmt.Printf("Chat - %s says: %s\n", formatPlayer(e.Sender), e.Text)
58+
})
59+
5560
// Parse to end
5661
err = p.ParseToEnd()
5762
checkError(err)

game_events.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ func (p *Parser) handleUserMessage(um *msg.CSVCMsg_UserMessage) {
492492
}
493493

494494
p.eventDispatcher.Dispatch(events.SayTextEvent{
495-
EntityIndex: int(st.EntIdx),
496-
IsChat: st.Chat,
497-
IsChatAll: st.Textallchat,
498-
Text: st.Text,
495+
EntIdx: int(st.EntIdx),
496+
IsChat: st.Chat,
497+
IsChatAll: st.Textallchat,
498+
Text: st.Text,
499499
})
500500

501501
case msg.ECstrike15UserMessages_CS_UM_SayText2:
@@ -505,9 +505,8 @@ func (p *Parser) handleUserMessage(um *msg.CSVCMsg_UserMessage) {
505505
p.eventDispatcher.Dispatch(events.ParserWarnEvent{Message: fmt.Sprintf("Failed to decode SayText2 message: %s", err.Error())})
506506
}
507507

508-
sender := p.gameState.players[int(st.EntIdx)]
509508
p.eventDispatcher.Dispatch(events.SayText2Event{
510-
Sender: sender,
509+
EntIdx: int(st.EntIdx),
511510
IsChat: st.Chat,
512511
IsChatAll: st.Textallchat,
513512
MsgName: st.MsgName,
@@ -518,6 +517,15 @@ func (p *Parser) handleUserMessage(um *msg.CSVCMsg_UserMessage) {
518517
case "Cstrike_Chat_All":
519518
fallthrough
520519
case "Cstrike_Chat_AllDead":
520+
var sender *common.Player
521+
for _, pl := range p.gameState.players {
522+
// This could be a problem if the player changed his name
523+
// as the name is only set initially and never updated
524+
if pl.Name == st.Params[0] {
525+
sender = pl
526+
}
527+
}
528+
521529
p.eventDispatcher.Dispatch(events.ChatMessageEvent{
522530
Sender: sender,
523531
Text: st.Params[1],

0 commit comments

Comments
 (0)