chirpnest/client.go
2019-06-14 11:24:43 +08:00

104 lines
2.7 KiB
Go

package main
import (
"strings"
)
func clientKeepAlive(k, v interface{}) bool {
c := v.(MQKeepAlive)
if c.Date.Add(2 * KeepAliveHeartBeatSeconds * time.Second).Before(time.Now()) {
msgs, err := clientsQueue[v.UserID64].Channel.QueuePurge(clientsQueue[v.UserID64].Queue.Name, false)
logOnError(err, "clientKeepAlive : Channel.QueuePurge()")
err = clientsQueue[v.UserID64].Channel.Close()
logOnError(err, "clientKeepAlive : Channel.Close()")
err = clientsQueue[v.UserID64].Connection.Close()
logOnError(err, "clientKeepAlive : Connection.Close()")
c := TGCommand{
Type: commandSendMsg,
ToUserID64: v.UserID64,
Text: "Timeout, purging and closing command queue.",
}
TGCmdQueue <- c
c = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
Text: fmt.Sprintf("Client %s timed out (%d messages purged).", v.Nickname, msgs),
}
TGCmdQueue <- c
clientsKeepAlive.Delete(c.UserID64)
}
return true
}
func clientSendCWMsg(userID64 int64, s string) {
c := TGCommand{
Type: commandSendMsg,
Text: s,
FromUserID64: userID64,
ToChatID64: userID64ChtWrsBot,
}
MQTGCmdQueue <- c
}
func clientRefreshCWMsg(userID64 int64, chatID64 int64, msgID64 int64) {
c := TGCommand{
Type: commandRefreshMsg,
FromUserID64: userID64,
FromChatID64: chatID64,
FromMsgID64: msgID64,
}
MQTGCmdQueue <- c
}
func clientMsgMeAck(m *ChatWarsMessageMeAck) {
if _, ok := clientsQueue[m.Msg.UserID64]; ok {
if v, ok := clientsCW.Load(m.Msg.UserID64); ok {
c := v.(*ChatWarsClient)
if c.LastUpdate.Before(m.Msg.Date) {
c.GuildID64 = m.GuildID64
c.State = m.State
c.LastUpdate = m.Msg.Date
if getObjGuildID(``) != m.GuildID64 && strings.Compare(c.Role, ``) == 0 {
clientSendCWMsg(m.Msg.UserID64, "/g_roles")
}
}
} else {
c := ChatWarsClient{
UserID64: m.UserID64,
TelegramID64: m.Msg.UserID64,
GuildID64: m.GuildID64,
State: m.State,
LastUpdate: m.Msg.Date,
}
clientsCW.Store(m.Msg.UserID64, &c)
if getObjGuildID(``) != m.GuildID64 {
clientSendCWMsg(m.Msg.UserID64, "/g_roles")
}
}
}
}
func clientMsgGRolesAck(m *ChatWarsMessageGRolesAck) {
if _, ok := clientsQueue[m.Msg.UserID64]; ok {
if v, ok := clientsCW.Load(m.Msg.UserID64); ok {
c := v.(*ChatWarsClient)
if c.LastUpdate.Before(m.Msg.Date) {
if m.CommanderID64 == c.UserID64 {
c.Role = `commander`
} else if m.BartenderID64 == c.UserID64 {
c.Role = `bartender`
} else if m.SquireID64 == c.UserID64 {
c.Role = `squire`
} else if m.TreasurerID64 == c.UserID64 {
c.Role = `treasurer`
} else {
c.Role = `none`
}
c.LastUpdate = m.Msg.Date
}
}
}
}