chirpnest/client.go

118 lines
2.7 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"
2019-08-24 07:37:57 +02:00
"time"
2019-05-30 06:12:01 +02:00
)
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:50:33 +02:00
//c := new(ChirpClient)
//return c, false
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) {
2019-08-23 08:03:22 +02:00
clientSendCWMsgDelay(userID64, s, 0)
}
2019-08-24 07:37:30 +02:00
func clientSendCWMsgDelay(userID64 int64, s string, d time.Duration) {
2019-05-30 06:12:01 +02:00
c := TGCommand{
Type: commandSendMsg,
Text: s,
FromUserID64: userID64,
ToChatID64: userID64ChtWrsBot,
2019-08-24 07:37:30 +02:00
Delay: int64(d),
2019-05-30 06:12:01 +02:00
}
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,
2019-08-23 08:03:22 +02:00
Delay: 0,
2019-06-11 04:19:27 +02:00
}
MQTGCmdQueue <- c
}
2019-05-30 07:55:53 +02:00
func clientMsgMeAck(m *ChatWarsMessageMeAck) {
2019-07-31 10:57:45 +02:00
if clt, ok := getLockedClient(m.Msg.TGUserID64, false); ok {
2019-07-31 06:45:42 +02:00
if clt.Active {
if clt.CWLastUpdate.Before(m.Msg.Date) {
2019-07-31 10:59:11 +02:00
clt.CWGuildID64 = m.CWGuildID64
clt.CWUserID64 = m.CWUserID64
2019-07-31 06:45:42 +02:00
clt.CWState = m.State
clt.CWLastUpdate = m.Msg.Date
2019-07-31 10:59:11 +02:00
if getObjGuildID(``) != m.CWGuildID64 && strings.Compare(clt.CWRole, ``) == 0 {
2019-07-31 10:57:45 +02:00
clientSendCWMsg(m.Msg.TGUserID64, "/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 10:57:45 +02:00
if clt, ok := getLockedClient(m.Msg.TGUserID64, false); ok {
2019-07-31 06:45:42 +02:00
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 10:57:45 +02:00
if clt, ok := getLockedClient(m.Msg.TGUserID64, false); ok {
2019-07-31 06:45:42 +02:00
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) {
2019-07-31 08:41:48 +02:00
if clt, ok := getLockedClient(tgUserID64, false); ok {
2019-07-31 06:45:42 +02:00
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.")
}