-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
59 lines (46 loc) · 1.28 KB
/
chat.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
package chat
import (
"../settings"
"time"
"../database"
"encoding/base64"
)
type EntChatMsg struct {
TimeStamp int64 //ms
Text string
SteamID64 string //who sent it
NicknameBase64 string //who sent it
AvatarSmall string //who sent it
}
var ArrayChatMsgs []EntChatMsg;
var ChanSend = make(chan EntChatMsg);
var ChanGetUniqTime = make(chan int64);
var ChanRead = make(chan []EntChatMsg);
var I64LastGlobalChatUpdate int64;
func ChannelWatchers() {
for {
select {
case oChatMsg := <-ChanSend:
if (len(ArrayChatMsgs) >= settings.ChatStoreMaxMsgs) {
ArrayChatMsgs = ArrayChatMsgs[1:];
}
ArrayChatMsgs = append(ArrayChatMsgs, oChatMsg);
go database.PublicChatLog(oChatMsg.TimeStamp, oChatMsg.NicknameBase64, oChatMsg.SteamID64, base64.StdEncoding.EncodeToString([]byte(oChatMsg.Text)));
case ChanRead <- func()([]EntChatMsg) {
arChatMsgs := make([]EntChatMsg, len(ArrayChatMsgs));
copy(arChatMsgs, ArrayChatMsgs);
return arChatMsgs;
}():
case ChanGetUniqTime <- func()(int64) {
time.Sleep(1 * time.Millisecond);
return time.Now().UnixMilli();
}():
}
}
}
func AddChatMessage(oChatMsg EntChatMsg) {
if (len(ArrayChatMsgs) >= settings.ChatStoreMaxMsgs) {
ArrayChatMsgs = ArrayChatMsgs[1:];
}
ArrayChatMsgs = append(ArrayChatMsgs, oChatMsg);
}