chirpnest/client.go

354 lines
7.9 KiB
Go
Raw Normal View History

2019-05-30 06:12:01 +02:00
package main
import (
2020-01-16 14:31:56 +01:00
"encoding/json"
2019-07-09 09:12:03 +02:00
"errors"
2019-12-15 09:06:15 +01:00
"fmt"
2020-01-16 14:31:56 +01:00
"io/ioutil"
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"
2020-01-16 14:28:34 +01:00
"github.com/jinzhu/copier"
2019-05-30 06:12:01 +02:00
)
2020-01-16 14:33:34 +01:00
func loadClients() error {
2020-01-16 14:28:34 +01:00
var clts []ChirpClient
b, err := ioutil.ReadFile("./clients.json")
logOnError(err, "loadClients : ReadFile(./clients.json)")
if err != nil {
2020-01-16 14:33:34 +01:00
return err
2020-01-16 14:28:34 +01:00
}
err = json.Unmarshal(b, &clts)
logOnError(err, "loadClients : Unmarshal(./clients.json)")
2020-01-16 14:32:34 +01:00
if err != nil {
2020-01-16 14:33:34 +01:00
return err
2020-01-16 14:32:34 +01:00
}
2020-01-16 14:28:34 +01:00
muxClients.Lock()
for _, c := range clts {
cx := ChirpClient{}
copier.Copy(&cx, &c)
cx.Active = false
2020-01-16 16:18:25 +01:00
/*
cx.MQ.Connection, err = amqp.Dial("amqp://" + cx.MQ.User + ":" + cx.MQ.Password + "@" + cx.MQ.Host + "/" + cx.MQ.Path)
logOnError(err, "loadClients : Failed to connect to RabbitMQ")
*/
2020-01-16 14:31:56 +01:00
clients[cx.TGUserID64] = &cx
2020-01-16 14:41:46 +01:00
log.Printf("loadClients[%s] : done\n", cx.Login)
2020-01-16 16:18:25 +01:00
2020-01-16 14:28:34 +01:00
}
muxClients.Unlock()
2020-01-16 14:33:34 +01:00
return nil
2020-01-16 14:32:34 +01:00
2020-01-16 14:28:34 +01: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 {
2020-01-15 11:17:38 +01:00
if clt, ok := getLockedClient(userID64, false); ok {
if from.UTC().Add(duration).After(time.Now().UTC()) {
2020-02-03 03:54:34 +01:00
clt.GameIdle = false
2019-12-15 08:37:58 +01:00
clt.CWBusyUntil = from.UTC().Add(duration)
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
} else {
2020-01-16 07:24:52 +01:00
log.Printf("setClientBusy[%s] : not updated.\n", clt.Login)
2019-12-15 08:37:58 +01:00
}
2020-01-15 11:17:38 +01:00
clt.Mux.Unlock()
return nil
} else {
return errors.New("Client not found.")
2019-12-15 08:37:58 +01:00
}
}
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()) {
clt.CWBusyUntil = from
2020-02-03 03:54:34 +01:00
clt.GameIdle = true
2019-12-15 08:37:58 +01:00
clt.CWLastUpdate = from
2019-12-15 09:28:05 +01:00
} else {
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 {
2020-02-03 03:54:34 +01:00
if c.GameIdle {
2019-12-15 08:40:43 +01:00
ids = append(ids, 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))
RndMux.Unlock()
clients[ids[id]].Mux.Lock()
return clients[ids[id]], nil
}
2020-02-06 16:23:54 +01:00
func getLockedAllIdleClientID64() ([]int64, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.GameIdle {
ids = append(ids, c.TGUserID64)
2020-02-06 16:25:48 +01:00
clients[c.TGUserID64].Mux.Lock()
2020-02-06 16:23:54 +01:00
}
}
muxClients.RUnlock()
return ids, nil
}
func getAllIdleClientID64() ([]int64, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.GameIdle {
ids = append(ids, c.TGUserID64)
}
}
muxClients.RUnlock()
return ids, nil
}
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 {
2020-01-16 14:28:34 +01:00
c := ChirpClient{
TGUserID64: id,
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()
2020-01-16 14:31:56 +01:00
clients[id] = &c
2019-07-31 08:19:09 +02:00
muxClients.Unlock()
2020-01-16 14:31:56 +01:00
return &c, true
2019-07-31 03:24:01 +02:00
} 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
}
2020-02-05 04:28:34 +01:00
func clientCallbackDelay(userID64 int64, msgID64 int64, chatID64 int64, name string, data []byte, d time.Duration) {
c := TGCommand{
Type: commandCallback,
FromUserID64: userID64,
FromChatID64: chatID64,
FromMsgID64: msgID64,
Delay: d,
CallbackName: name,
CallbackData: data,
}
MQTGCmdQueue <- c
}
2020-02-05 04:29:30 +01:00
func clientCallback(userID64 int64, msgID64 int64, chatID64 int64, name string, data []byte) {
clientCallbackDelay(userID64, msgID64, chatID64, name, data, 0)
}
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` {
2020-02-03 03:54:34 +01:00
clt.GameIdle = true
2019-12-15 09:28:05 +01:00
}
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
}