chirpnest/client.go

109 lines
2.4 KiB
Go
Raw Normal View History

2019-05-30 06:12:01 +02:00
package main
import (
2019-07-09 09:12:03 +02:00
"errors"
2019-05-30 06:12:01 +02:00
"strings"
)
func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) {
2019-07-31 07:07:12 +02:00
muxClients.RLock()
2019-07-31 03:24:01 +02:00
if c, ok := clients[id]; ok {
c.Mux.Lock()
2019-07-31 08:19:09 +02:00
muxClients.RUnlock()
2019-07-31 03:24:01 +02:00
return c, true
} else if createMissing {
c := new(ChirpClient)
c.TGUserID64 = id
2019-07-31 06:45:42 +02:00
c.Active = false
2019-07-31 03:24:01 +02:00
c.Lock()
2019-07-31 08:19:09 +02:00
muxClients.RUnlock()
muxClients.Lock()
clients[id] = c
muxClients.Unlock()
2019-07-31 03:24:01 +02:00
return c, true
} else {
2019-07-31 08:19:09 +02:00
muxClients.RUnlock()
2019-07-31 03:24:01 +02:00
return 0, false
2019-06-14 05:24:43 +02:00
}
}
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-07-31 06:45:42 +02:00
if clt, ok := getLockedClient(m.Msg.UserID64, false); ok {
if clt.Active {
if clt.CWLastUpdate.Before(m.Msg.Date) {
clt.CWGuildID64 = m.GuildID64
clt.CWUserID64 = m.UserID64
clt.CWState = m.State
clt.CWLastUpdate = m.Msg.Date
if getObjGuildID(``) != m.GuildID64 && strings.Compare(clt.CWRole, ``) == 0 {
2019-05-30 08:08:39 +02:00
clientSendCWMsg(m.Msg.UserID64, "/g_roles")
2019-05-30 06:12:01 +02:00
}
}
}
2019-07-31 06:45:42 +02:00
clt.Unlock()
2019-05-30 06:12:01 +02:00
}
}
2019-05-30 12:57:00 +02:00
2019-07-09 06:56:41 +02:00
func clientMsgGoQuestAck(m *ChatWarsMessageGoQuestAck) {
2019-07-31 06:45:42 +02:00
if clt, ok := getLockedClient(m.Msg.UserID64, false); ok {
if clt.Active {
if clt.CWLastUpdate.Before(m.Msg.Date) {
clt.CWLastUpdate = m.Msg.Date
clt.CWBusyUntil = m.Msg.Date.Add(m.Duration)
2019-07-09 06:56:41 +02:00
}
}
2019-07-31 06:45:42 +02:00
clt.Unlock()
2019-07-09 06:56:41 +02:00
}
}
2019-05-30 12:57:00 +02:00
func clientMsgGRolesAck(m *ChatWarsMessageGRolesAck) {
2019-07-31 06:45:42 +02:00
if clt, ok := getLockedClient(m.Msg.UserID64, false); ok {
if clt.Active {
if clt.CWLastUpdate.Before(m.Msg.Date) {
if m.CommanderID64 == clt.CWUserID64 {
2019-05-30 12:57:00 +02:00
c.Role = `commander`
2019-07-31 06:45:42 +02:00
} else if m.BartenderID64 == clt.CWUserID64 {
2019-05-30 12:57:00 +02:00
c.Role = `bartender`
2019-07-31 06:45:42 +02:00
} else if m.SquireID64 == clt.CWUserID64 {
2019-05-30 12:57:00 +02:00
c.Role = `squire`
2019-07-31 06:45:42 +02:00
} else if m.TreasurerID64 == clt.CWUserID64 {
2019-05-30 12:57:00 +02:00
c.Role = `treasurer`
} else {
2019-05-30 12:58:01 +02:00
c.Role = `none`
2019-05-30 12:57:00 +02:00
}
2019-07-31 06:45:42 +02:00
clt.CWLastUpdate = m.Msg.Date
2019-05-30 12:57:00 +02:00
}
}
2019-07-31 06:45:42 +02:00
clt.Unlock()
2019-05-30 12:57:00 +02:00
}
}
2019-07-09 09:12:03 +02:00
2019-07-31 06:45:42 +02:00
func clientGetCWUserID64(tgUserID64 int64) (int64, error) {
if clt, ok := getLockedClient(m.Msg.UserID64, false); ok {
i := clt.CWUserID64
clt.Unlock()
return i, nil
2019-07-09 09:12:03 +02:00
}
return 0, errors.New("Unknown user_id.")
}