2019-05-30 06:12:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-09 09:12:03 +02:00
|
|
|
"errors"
|
2019-12-15 09:06:15 +01:00
|
|
|
"fmt"
|
2019-10-23 04:24:22 +02:00
|
|
|
"log"
|
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
|
|
|
)
|
|
|
|
|
2020-01-14 05:10:52 +01:00
|
|
|
func getLockedRoleClient(role string) (*ChirpClient, error) {
|
|
|
|
muxClients.RLock()
|
|
|
|
defer muxClients.RUnlock()
|
|
|
|
|
|
|
|
ids := make([]int64, 0)
|
|
|
|
for _, c := range clients {
|
|
|
|
if c.CWRole == role {
|
|
|
|
ids = append(ids, c.TGUserID64)
|
|
|
|
fmt.Printf("getLockedRoleClient(%s) : appending %s (%d).\n", role, c.Login, c.TGUserID64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ids) == 0 {
|
2020-01-14 05:11:28 +01:00
|
|
|
return nil, errors.New("No client for the role.")
|
2020-01-14 05:10:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RndMux.Lock()
|
|
|
|
id := RndSrc.Intn(len(ids))
|
|
|
|
RndMux.Unlock()
|
|
|
|
|
|
|
|
clients[ids[id]].Mux.Lock()
|
|
|
|
|
|
|
|
return clients[ids[id]], nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 03:45:10 +01:00
|
|
|
func getLockedRandomClient() (*ChirpClient, error) {
|
|
|
|
muxClients.RLock()
|
|
|
|
ids := make([]int64, 0)
|
|
|
|
for _, c := range clients {
|
|
|
|
if c.Active {
|
|
|
|
ids = append(ids, c.TGUserID64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
muxClients.RUnlock()
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("No active client.")
|
|
|
|
}
|
|
|
|
|
|
|
|
RndMux.Lock()
|
|
|
|
id := RndSrc.Intn(len(ids))
|
|
|
|
RndMux.Unlock()
|
|
|
|
|
|
|
|
clients[ids[id]].Mux.Lock()
|
|
|
|
|
|
|
|
return clients[ids[id]], nil
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:37:58 +01:00
|
|
|
func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error {
|
|
|
|
if from.UTC().Add(duration).After(time.Now().UTC()) {
|
2019-12-15 08:38:58 +01:00
|
|
|
if clt, ok := getLockedClient(userID64, false); ok {
|
2019-12-15 08:37:58 +01:00
|
|
|
clt.CWIdle = false
|
|
|
|
clt.CWBusyUntil = from.UTC().Add(duration)
|
|
|
|
clt.Mux.Unlock()
|
2020-01-14 07:47:23 +01:00
|
|
|
log.Printf("setClientBusy[%s] : set for %s.\n", clt.Login, duration.String())
|
2019-12-15 08:37:58 +01:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("Client not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setClientIdle(userID64 int64, from time.Time) error {
|
2019-12-15 08:38:58 +01:00
|
|
|
if clt, ok := getLockedClient(userID64, false); ok {
|
2019-12-15 08:37:58 +01:00
|
|
|
if from.UTC().After(clt.CWLastUpdate.UTC()) {
|
2019-12-15 09:24:51 +01:00
|
|
|
fmt.Printf("setClientIdle : updated.\n")
|
2019-12-15 08:37:58 +01:00
|
|
|
clt.CWBusyUntil = from
|
|
|
|
clt.CWIdle = true
|
|
|
|
clt.CWLastUpdate = from
|
2020-01-14 07:47:23 +01:00
|
|
|
log.Printf("setClientIdle[%s] : updated.\n", clt.Login)
|
2019-12-15 09:28:05 +01:00
|
|
|
} else {
|
2020-01-14 07:47:23 +01:00
|
|
|
log.Printf("setClientIdle[%s] : not updated.\n", clt.Login)
|
2019-12-15 08:37:58 +01:00
|
|
|
}
|
|
|
|
clt.Mux.Unlock()
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("Client not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLockedIdleClient() (*ChirpClient, error) {
|
|
|
|
muxClients.RLock()
|
|
|
|
ids := make([]int64, 0)
|
2019-12-15 08:42:10 +01:00
|
|
|
for _, c := range clients {
|
2019-12-15 08:37:58 +01:00
|
|
|
if c.CWIdle {
|
2019-12-15 08:40:43 +01:00
|
|
|
ids = append(ids, c.TGUserID64)
|
2019-12-15 09:05:44 +01:00
|
|
|
fmt.Printf("getLockedIdleClient : appending %s (%d).\n", c.Login, c.TGUserID64)
|
2019-12-15 08:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
muxClients.RUnlock()
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("No idle client.")
|
|
|
|
}
|
|
|
|
|
|
|
|
RndMux.Lock()
|
|
|
|
id := RndSrc.Intn(len(ids))
|
2020-01-14 07:58:34 +01:00
|
|
|
fmt.Printf("getLockedIdleClient : pulled %s.\n", clients[ids[id]].Login)
|
2019-12-15 08:37:58 +01:00
|
|
|
RndMux.Unlock()
|
|
|
|
|
|
|
|
clients[ids[id]].Mux.Lock()
|
|
|
|
|
|
|
|
return clients[ids[id]], nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:48:37 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 09:30:41 +01:00
|
|
|
func clientDelTGMsg(userID64 int64, fromMsgID64 int64, fromChatID64 int64) {
|
2019-12-13 12:19:52 +01:00
|
|
|
c := TGCommand{
|
|
|
|
Type: commandDeleteMsg,
|
|
|
|
FromUserID64: userID64,
|
|
|
|
FromMsgID64: fromMsgID64,
|
|
|
|
FromChatID64: fromChatID64,
|
|
|
|
}
|
|
|
|
MQTGCmdQueue <- c
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:03:54 +02:00
|
|
|
func clientFwdCWMsg(userID64 int64, fromMsgID64 int64, fromChatID64 int64, toChatID64 int64) {
|
2019-08-27 17:10:57 +02:00
|
|
|
c := TGCommand{
|
|
|
|
Type: commandForwardMsg,
|
2019-08-29 14:03:54 +02:00
|
|
|
FromUserID64: userID64,
|
2019-08-27 17:10:57 +02:00
|
|
|
FromMsgID64: fromMsgID64,
|
|
|
|
FromChatID64: fromChatID64,
|
|
|
|
ToChatID64: toChatID64,
|
|
|
|
}
|
|
|
|
MQTGCmdQueue <- c
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:20:00 +01:00
|
|
|
func clientSendTGMsgDelay(userID64 int64, chatID64 int64, s string, d time.Duration) {
|
2019-05-30 06:12:01 +02:00
|
|
|
c := TGCommand{
|
|
|
|
Type: commandSendMsg,
|
|
|
|
Text: s,
|
|
|
|
FromUserID64: userID64,
|
2020-01-06 05:20:00 +01:00
|
|
|
ToChatID64: chatID64,
|
2019-08-24 08:02:07 +02:00
|
|
|
Delay: d,
|
2019-05-30 06:12:01 +02:00
|
|
|
}
|
|
|
|
MQTGCmdQueue <- c
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:20:00 +01:00
|
|
|
func clientSendTGMsg(userID64 int64, chatID64 int64, s string) {
|
2020-01-06 05:21:00 +01:00
|
|
|
clientSendTGMsgDelay(userID64, chatID64, s, 0)
|
2020-01-06 05:20:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func clientSendCWMsg(userID64 int64, s string) {
|
|
|
|
clientSendCWMsgDelay(userID64, s, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func clientSendCWMsgDelay(userID64 int64, s string, d time.Duration) {
|
|
|
|
clientSendTGMsgDelay(userID64, userID64ChtWrsBot, s, d)
|
|
|
|
}
|
|
|
|
|
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
|
2020-01-08 10:17:36 +01:00
|
|
|
clt.CWClass = m.Class
|
2019-07-31 06:45:42 +02:00
|
|
|
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-12-15 09:28:05 +01:00
|
|
|
if m.State == `🛌Rest` {
|
|
|
|
clt.CWIdle = true
|
|
|
|
}
|
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.")
|
|
|
|
}
|
2019-10-21 08:45:47 +02:00
|
|
|
|
2019-10-23 04:24:22 +02:00
|
|
|
func clientSpreadQuestResultAmbush(cwm *ChatWarsMessageQuestResultAmbush) error {
|
2019-10-21 08:45:47 +02:00
|
|
|
/*
|
|
|
|
muxClients.RLock()
|
|
|
|
var ret string
|
|
|
|
for id, c := range clients {
|
|
|
|
if c.Active {
|
|
|
|
ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (online)\n", ret, c.Login, c.CWUserID64, id)
|
|
|
|
} else {
|
|
|
|
ret = fmt.Sprintf("%s%s | UserID : %d | TelegramID : %d (offline)\n", ret, c.Login, c.CWUserID64, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
muxClients.RUnlock()
|
|
|
|
*/
|
2019-10-23 04:23:13 +02:00
|
|
|
log.Printf("clientSpreadQuestResultAmbush : spreading.")
|
2019-10-21 08:45:47 +02:00
|
|
|
return nil
|
|
|
|
}
|