chirpnest/client.go

111 lines
2.5 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 08:38:11 +02:00
c.Mux.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 08:36:53 +02:00
//c := new(ChirpClient)
//return c, false
2019-07-31 08:24:52 +02:00
return nil, 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 08:24:52 +02:00
clt.Mux.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 08:24:52 +02:00
clt.Mux.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-07-31 08:24:52 +02:00
clt.CWRole = `commander`
2019-07-31 06:45:42 +02:00
} else if m.BartenderID64 == clt.CWUserID64 {
2019-07-31 08:24:52 +02:00
clt.CWRole = `bartender`
2019-07-31 06:45:42 +02:00
} else if m.SquireID64 == clt.CWUserID64 {
2019-07-31 08:24:52 +02:00
clt.CWRole = `squire`
2019-07-31 06:45:42 +02:00
} else if m.TreasurerID64 == clt.CWUserID64 {
2019-07-31 08:24:52 +02:00
clt.CWRole = `treasurer`
2019-05-30 12:57:00 +02:00
} else {
2019-07-31 08:24:52 +02:00
clt.CWRole = `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 08:24:52 +02:00
clt.Mux.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
2019-07-31 08:24:52 +02:00
clt.Mux.Unlock()
2019-07-31 06:45:42 +02:00
return i, nil
2019-07-09 09:12:03 +02:00
}
return 0, errors.New("Unknown user_id.")
}