chirpnest/client.go

106 lines
2.7 KiB
Go
Raw Normal View History

2019-05-30 06:12:01 +02:00
package main
import (
2019-06-14 05:27:43 +02:00
"fmt"
2019-05-30 06:12:01 +02:00
"strings"
2019-06-14 05:26:05 +02:00
"time"
2019-05-30 06:12:01 +02:00
)
2019-06-14 05:24:43 +02:00
func clientKeepAlive(k, v interface{}) bool {
2019-06-14 05:31:33 +02:00
clt := v.(*MQKeepAlive)
2019-06-14 06:54:09 +02:00
if clt.Date.Add(3 * KeepAliveHeartBeatSeconds * time.Second).Before(time.Now()) {
2019-06-14 05:27:43 +02:00
msgs, err := clientsQueue[clt.UserID64].Channel.QueuePurge(clientsQueue[clt.UserID64].Queue.Name, false)
2019-06-14 05:24:43 +02:00
logOnError(err, "clientKeepAlive : Channel.QueuePurge()")
2019-06-14 05:27:43 +02:00
err = clientsQueue[clt.UserID64].Channel.Close()
2019-06-14 05:24:43 +02:00
logOnError(err, "clientKeepAlive : Channel.Close()")
2019-06-14 05:27:43 +02:00
err = clientsQueue[clt.UserID64].Connection.Close()
2019-06-14 05:24:43 +02:00
logOnError(err, "clientKeepAlive : Connection.Close()")
c := TGCommand{
Type: commandSendMsg,
2019-06-14 05:27:43 +02:00
ToUserID64: clt.UserID64,
2019-06-14 05:24:43 +02:00
Text: "Timeout, purging and closing command queue.",
}
TGCmdQueue <- c
c = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
2019-06-14 05:27:43 +02:00
Text: fmt.Sprintf("Client %s timed out (%d messages purged).", clt.Nickname, msgs),
2019-06-14 05:24:43 +02:00
}
TGCmdQueue <- c
2019-06-14 05:27:43 +02:00
clientsKeepAlive.Delete(clt.UserID64)
2019-06-14 05:24:43 +02:00
}
return true
}
2019-05-30 06:12:01 +02:00
func clientSendCWMsg(userID64 int64, s string) {
c := TGCommand{
Type: commandSendMsg,
Text: s,
FromUserID64: userID64,
ToChatID64: userID64ChtWrsBot,
}
MQTGCmdQueue <- c
}
2019-06-11 05:37:06 +02:00
func clientRefreshCWMsg(userID64 int64, chatID64 int64, msgID64 int64) {
2019-06-11 04:19:27 +02:00
c := TGCommand{
Type: commandRefreshMsg,
2019-06-11 05:37:06 +02:00
FromUserID64: userID64,
2019-06-11 04:19:27 +02:00
FromChatID64: chatID64,
FromMsgID64: msgID64,
}
MQTGCmdQueue <- c
}
2019-05-30 07:55:53 +02:00
func clientMsgMeAck(m *ChatWarsMessageMeAck) {
2019-05-30 08:08:39 +02:00
if _, ok := clientsQueue[m.Msg.UserID64]; ok {
2019-06-03 10:45:39 +02:00
if v, ok := clientsCW.Load(m.Msg.UserID64); ok {
2019-06-03 10:43:22 +02:00
c := v.(*ChatWarsClient)
2019-05-30 12:57:41 +02:00
if c.LastUpdate.Before(m.Msg.Date) {
2019-05-30 07:49:11 +02:00
c.GuildID64 = m.GuildID64
c.State = m.State
2019-05-30 08:08:39 +02:00
c.LastUpdate = m.Msg.Date
2019-05-31 05:15:19 +02:00
if getObjGuildID(``) != m.GuildID64 && strings.Compare(c.Role, ``) == 0 {
2019-05-30 08:08:39 +02:00
clientSendCWMsg(m.Msg.UserID64, "/g_roles")
2019-05-30 06:12:01 +02:00
}
}
} else {
c := ChatWarsClient{
2019-05-30 12:57:00 +02:00
UserID64: m.UserID64,
TelegramID64: m.Msg.UserID64,
GuildID64: m.GuildID64,
State: m.State,
LastUpdate: m.Msg.Date,
2019-05-30 06:12:01 +02:00
}
2019-06-03 10:46:27 +02:00
clientsCW.Store(m.Msg.UserID64, &c)
2019-05-31 05:15:19 +02:00
if getObjGuildID(``) != m.GuildID64 {
2019-05-30 08:08:39 +02:00
clientSendCWMsg(m.Msg.UserID64, "/g_roles")
2019-05-30 06:12:01 +02:00
}
}
}
}
2019-05-30 12:57:00 +02:00
func clientMsgGRolesAck(m *ChatWarsMessageGRolesAck) {
if _, ok := clientsQueue[m.Msg.UserID64]; ok {
2019-06-03 10:43:22 +02:00
if v, ok := clientsCW.Load(m.Msg.UserID64); ok {
c := v.(*ChatWarsClient)
2019-05-30 12:57:00 +02:00
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 {
2019-05-30 12:58:01 +02:00
c.Role = `none`
2019-05-30 12:57:00 +02:00
}
c.LastUpdate = m.Msg.Date
}
}
}
}