chirpnest/client.go
2019-07-09 12:56:41 +08:00

118 lines
3.0 KiB
Go

package main
import (
"fmt"
"strings"
"time"
)
func clientKeepAlive(k, v interface{}) bool {
clt := v.(*MQKeepAlive)
if clt.Date.Add(3 * KeepAliveHeartBeatSeconds * time.Second).Before(time.Now()) {
msgs, err := clientsQueue[clt.UserID64].Channel.QueuePurge(clientsQueue[clt.UserID64].Queue.Name, false)
logOnError(err, "clientKeepAlive : Channel.QueuePurge()")
err = clientsQueue[clt.UserID64].Channel.Close()
logOnError(err, "clientKeepAlive : Channel.Close()")
err = clientsQueue[clt.UserID64].Connection.Close()
logOnError(err, "clientKeepAlive : Connection.Close()")
c := TGCommand{
Type: commandSendMsg,
ToUserID64: clt.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).", clt.Nickname, msgs),
}
TGCmdQueue <- c
clientsKeepAlive.Delete(clt.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 clientMsgGoQuestAck(m *ChatWarsMessageGoQuestAck) {
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.LastUpdate = m.Msg.Date
c.BusyUntil = m.Msg.Date.Add(m.Duration)
}
}
}
}
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
}
}
}
}