chirpnest/workers.go

745 lines
26 KiB
Go
Raw Normal View History

2019-05-05 13:29:28 +02:00
package main
import (
2019-05-06 04:54:24 +02:00
"encoding/json"
2019-05-16 14:41:21 +02:00
"fmt"
2019-05-05 13:29:28 +02:00
"log"
2019-05-16 14:41:21 +02:00
"math/rand"
2019-05-05 13:29:28 +02:00
"strconv"
2019-06-10 11:14:03 +02:00
"strings"
2019-05-08 12:17:07 +02:00
"time"
2019-05-06 04:54:24 +02:00
"github.com/streadway/amqp"
2019-05-16 04:21:36 +02:00
tb "gopkg.in/tucnak/telebot.v2"
2019-05-05 13:29:28 +02:00
)
2019-06-28 11:27:16 +02:00
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
//log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-06-28 10:34:15 +02:00
var err error
2019-06-28 11:27:16 +02:00
c := MQClient{
User: cfg.Rabbit.User,
Password: cfg.Rabbit.Password,
Host: cfg.Rabbit.Host,
Path: cfg.Rabbit.Path,
SSL: false,
2019-06-28 10:34:15 +02:00
}
2019-06-25 11:37:28 +02:00
for true {
2019-06-28 11:27:16 +02:00
c.Connection, err = amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Cannot open MQ connection")
if err != nil {
2019-06-29 09:46:30 +02:00
//c.Connection.Close()
2019-06-28 11:27:16 +02:00
time.Sleep(15 * time.Second)
continue
2019-06-28 11:12:27 +02:00
}
2019-06-28 10:04:52 +02:00
2019-06-28 11:27:16 +02:00
c.Channel, err = c.Connection.Channel()
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Cannot open MQ channel")
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
2019-06-28 10:04:52 +02:00
2019-06-28 11:27:16 +02:00
c.Queue, err = c.Channel.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
if err != nil {
2019-06-28 11:27:16 +02:00
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
2019-06-28 10:18:48 +02:00
2019-06-28 11:27:16 +02:00
m, err := c.Channel.Consume(
c.Queue.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
2019-06-28 11:19:17 +02:00
2019-06-28 11:27:16 +02:00
for d := range m {
var x ChatWarsMessage
2019-07-31 10:10:05 +02:00
//log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
2019-06-28 11:27:16 +02:00
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
if err == nil {
msgs <- x
}
}
c.Channel.Close()
c.Connection.Close()
2019-05-06 04:54:24 +02:00
}
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
2019-06-28 11:30:49 +02:00
func MQKeepAliveWorker() {
//log.Printf("MQKeepAliveWorker : Starting.")
var err error
c := MQClient{
User: cfg.Rabbit.User,
Password: cfg.Rabbit.Password,
Host: cfg.Rabbit.Host,
Path: cfg.Rabbit.Path,
SSL: false,
}
for true {
c.Connection, err = amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path)
2019-06-28 11:31:44 +02:00
logOnError(err, "MQKeepAliveWorker : Cannot open MQ connection")
2019-06-28 11:30:49 +02:00
if err != nil {
time.Sleep(15 * time.Second)
continue
}
c.Channel, err = c.Connection.Channel()
2019-06-28 11:31:44 +02:00
logOnError(err, "MQKeepAliveWorker : Cannot open MQ channel")
2019-06-28 11:30:49 +02:00
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
c.Queue, err = c.Channel.QueueDeclare(
"keepalive", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
2019-06-28 11:31:44 +02:00
logOnError(err, "MQKeepAliveWorker : Failed to declare a queue")
2019-06-28 11:30:49 +02:00
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
m, err := c.Channel.Consume(
c.Queue.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
2019-06-28 11:31:44 +02:00
logOnError(err, "MQKeepAliveWorker] : Failed to register a consumer")
2019-06-28 11:30:49 +02:00
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
continue
}
for d := range m {
x := MQKeepAlive{}
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
if err == nil {
2019-07-31 09:43:17 +02:00
//log.Printf("MQKeepAliveWorker : Received message from %s (%d).\n", x.Nickname, x.TGUserID64)
2019-06-28 11:30:49 +02:00
if x.Date.Add(10 * time.Second).Before(time.Now()) {
// outdated keep-alive coming from client
2019-07-31 03:24:01 +02:00
} else if clt, ok := getLockedClient(x.TGUserID64, true); ok {
clt.HeartBeat = x.Date
if clt.Active {
2019-07-31 09:43:17 +02:00
//log.Printf("MQKeepAliveWorker : Client %s (%d) already active.\n", clt.Login, clt.TGUserID64)
2019-07-31 03:24:01 +02:00
clt.Mux.Unlock()
} else {
2019-07-31 09:22:02 +02:00
clt.Login = x.Nickname
2019-07-31 09:43:17 +02:00
//log.Printf("MQKeepAliveWorker : Connecting to %s (%d).\n", clt.Login, clt.TGUserID64)
2019-07-31 09:22:02 +02:00
clt.MQ.User = cfg.Rabbit.User
clt.MQ.Password = cfg.Rabbit.Password
clt.MQ.Host = cfg.Rabbit.Host
clt.MQ.Path = x.Queue
clt.MQ.SSL = false
2019-07-31 06:45:42 +02:00
clt.MQ.Connection, err = amqp.Dial("amqp://" + clt.MQ.User + ":" + clt.MQ.Password + "@" + clt.MQ.Host + "/" + clt.MQ.Path)
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
2019-06-28 11:42:33 +02:00
if err != nil {
2019-07-31 06:45:42 +02:00
clt.MQ.Connection.Close()
2019-06-28 11:42:33 +02:00
} else {
2019-07-31 06:45:42 +02:00
clt.MQ.Channel, err = clt.MQ.Connection.Channel()
logOnError(err, "MQKeepAliveWorker : Failed to open a channel")
2019-06-28 11:42:33 +02:00
if err != nil {
2019-07-31 06:45:42 +02:00
clt.MQ.Channel.Close()
clt.MQ.Connection.Close()
} else {
clt.MQ.Queue, err = clt.MQ.Channel.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
logOnError(err, "MQKeepAliveWorker : Failed to declare a queue")
if err != nil {
clt.MQ.Channel.Close()
clt.MQ.Connection.Close()
} else {
clt.Active = true
2019-07-31 09:43:17 +02:00
//log.Printf("MQKeepAliveWorker : Connected to %s.\n", x.Nickname)
2019-07-31 06:45:42 +02:00
}
2019-06-28 11:42:33 +02:00
}
}
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-07-31 09:43:17 +02:00
if clt.Active {
2020-01-16 14:28:34 +01:00
if clt.CWRole == `` {
c := TGCommand{
Type: commandSendMsg,
ToUserID64: x.TGUserID64,
Text: "Your client is connected.",
}
TGCmdQueue <- c
c = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
Text: fmt.Sprintf("Client `%s` is connected.", x.Nickname),
}
TGCmdQueue <- c
2019-06-28 11:30:49 +02:00
2020-01-16 14:28:34 +01:00
clientSendCWMsg(x.TGUserID64, `🏅Me`)
} else {
// silent reconnection
}
2019-07-31 09:43:17 +02:00
} else {
2019-07-31 09:44:08 +02:00
c := TGCommand{
2019-07-31 09:43:17 +02:00
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
Text: fmt.Sprintf("Cannot connect to client `%s`.", x.Nickname),
}
TGCmdQueue <- c
2019-07-31 06:45:42 +02:00
}
}
2019-06-28 11:30:49 +02:00
}
}
}
c.Channel.Close()
c.Connection.Close()
}
log.Printf("MQKeepAliveWorker : Closing.")
}
2019-06-28 12:32:16 +02:00
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
for c := range cmds {
2019-07-31 06:45:42 +02:00
if clt, ok := getLockedClient(c.FromUserID64, false); ok {
2019-06-28 12:32:16 +02:00
j, err := json.Marshal(c)
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
2020-02-05 06:02:32 +01:00
//log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
2020-01-16 16:18:25 +01:00
for clt.MQ.Connection == nil || clt.MQ.Connection.IsClosed() {
2019-07-31 06:45:42 +02:00
clt.Active = false
log.Printf("MQTGCmdWorker : Resetting MQ connection for #%d.\n", c.FromUserID64)
clt.MQ.Connection, err = amqp.Dial("amqp://" + clt.MQ.User + ":" + clt.MQ.Password + "@" + clt.MQ.Host + "/" + clt.MQ.Path)
logOnError(err, "MQTGCmdWorker : Cannot open MQ connection")
2019-06-28 12:32:16 +02:00
if err != nil {
2019-07-31 06:45:42 +02:00
clt.MQ.Connection.Close()
2019-06-28 12:32:16 +02:00
time.Sleep(15 * time.Second)
continue
}
2019-07-31 06:45:42 +02:00
clt.MQ.Channel, err = clt.MQ.Connection.Channel()
logOnError(err, "MQTGCmdWorker : Cannot open MQ channel")
2019-06-28 12:32:16 +02:00
if err != nil {
2019-07-31 06:45:42 +02:00
clt.MQ.Channel.Close()
clt.MQ.Connection.Close()
2019-06-28 12:32:16 +02:00
time.Sleep(15 * time.Second)
continue
}
2019-07-31 06:45:42 +02:00
clt.MQ.Queue, err = clt.MQ.Channel.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
2019-06-28 12:32:16 +02:00
)
2019-07-31 06:45:42 +02:00
logOnError(err, "MQTGCmdWorker : Failed to declare a queue")
2019-06-28 12:32:16 +02:00
if err != nil {
2019-07-31 06:45:42 +02:00
clt.MQ.Channel.Close()
clt.MQ.Connection.Close()
2019-06-28 12:32:16 +02:00
time.Sleep(15 * time.Second)
continue
}
}
2019-07-31 06:45:42 +02:00
err = clt.MQ.Channel.Publish(
"", // exchange
clt.MQ.Queue.Name, // routing key
false, // mandatory
false, // immediate
2019-06-28 12:32:16 +02:00
amqp.Publishing{
ContentType: "application/json",
Body: []byte(j),
})
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Publishing message.")
2020-02-05 06:02:32 +01:00
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Message published.")
2019-07-31 08:19:09 +02:00
clt.Mux.Unlock()
2019-06-28 12:32:16 +02:00
} else {
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : client %d offline.\n", c.FromUserID64)
}
}
log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
}
2019-05-06 04:54:24 +02:00
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage, objIds chan<- int64) {
2019-05-25 13:27:08 +02:00
//log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-05-05 13:29:28 +02:00
for m := range msgs {
2020-02-05 03:38:44 +01:00
objId, err := addObjMsg(m)
2019-05-06 04:54:24 +02:00
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
2019-05-11 09:01:24 +02:00
if err == nil && objId != 0 {
2019-05-11 07:46:14 +02:00
// log.Printf("SQLCWMsgWorker["+strconv.Itoa(id)+"] : Message inserted (%d).\n", objId)
2019-05-06 04:54:24 +02:00
objIds <- objId
2019-07-31 09:55:23 +02:00
} else {
2019-08-13 04:42:49 +02:00
log.Printf("SQLCWMsgWorker["+strconv.Itoa(id)+"] : Message in error\n%s\n", m.Text)
2019-05-05 13:29:28 +02:00
}
}
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
2019-05-25 13:27:08 +02:00
//log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-05-05 13:29:28 +02:00
for objId := range objIds {
2019-06-09 14:17:05 +02:00
m, err := getObjMsg(objId)
2019-05-06 04:54:24 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving message.")
if err == nil {
2019-05-11 07:47:32 +02:00
//log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message retrieved (%d)\n%s\n", objId, m.Text)
2019-08-21 08:37:53 +02:00
rule, err := getMsgParsingRule(m)
if err != nil {
2019-08-21 09:15:32 +02:00
//logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : msgRegex.")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message unidentified (%d)\n%s\n", objId, m.Text)
2019-05-06 09:28:34 +02:00
} else {
2019-12-29 09:42:31 +01:00
err = setObjSubTypeId(objId, rule.MsgTypeID64)
2019-06-02 08:48:16 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId")
2019-12-29 09:42:31 +01:00
switch rule.MsgTypeID64 {
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_groles_req`]:
case cacheObjSubType[`msg_go`]:
case cacheObjSubType[`msg_report_req`]:
case cacheObjSubType[`msg_g_report_req`]:
case cacheObjSubType[`msg_hero_req`]:
case cacheObjSubType[`msg_me_req`]:
case cacheObjSubType[`msg_inv_req`]:
case cacheObjSubType[`msg_time_req`]:
case cacheObjSubType[`msg_pledge`]:
case cacheObjSubType[`msg_pillage_go`]:
case cacheObjSubType[`msg_pillage_timeout`]:
case cacheObjSubType[`msg_pillage_win`]:
case cacheObjSubType[`msg_pillage_loss`]:
case cacheObjSubType[`msg_go_quest_req`]:
case cacheObjSubType[`msg_fast_fight`]:
case cacheObjSubType[`msg_go_arena`]:
2020-01-02 16:21:52 +01:00
case cacheObjSubType[`msg_top_req`]:
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_menu`]:
case cacheObjSubType[`msg_buy_req`]:
case cacheObjSubType[`msg_sell_req`]:
case cacheObjSubType[`msg_orderbook_req`]:
case cacheObjSubType[`msg_withdraw_req`]:
case cacheObjSubType[`msg_withdraw_code`]:
case cacheObjSubType[`msg_withdraw_rcv`]:
2019-10-11 03:21:09 +02:00
cwm, err := parseSubTypeMessageWithdrawRcv(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_withdraw_rcv`]")
2019-10-11 03:21:09 +02:00
for _, i := range cwm.ItemList {
err = insertMsgItem(cwm.Msg.ObjID64, i.ItemID64, i.Quantity)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Inserting cacheObjSubType[`msg_withdraw_rcv`]")
2019-10-11 03:21:09 +02:00
}
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_stock_req`]:
case cacheObjSubType[`msg_misc_req`]:
case cacheObjSubType[`msg_gstock_res_req`]:
case cacheObjSubType[`msg_gstock_alch_req`]:
case cacheObjSubType[`msg_gstock_misc_req`]:
case cacheObjSubType[`msg_gstock_rec_req`]:
case cacheObjSubType[`msg_gstock_part_req`]:
case cacheObjSubType[`msg_gstock_oth_req`]:
case cacheObjSubType[`msg_gstock_any_ack`]:
2019-08-21 08:37:53 +02:00
_, err := parseSubTypeMessageGStockAnyAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_gstock_any_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_report_ack`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageReportAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_report_ack`]")
2019-05-18 09:44:25 +02:00
cwm.ObjID64 = objId
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_quest_res_ambush`]:
2019-10-23 04:24:48 +02:00
cwm, err := parseSubTypeMessageQuestResultAmbush(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_quest_res_ambush`]")
2019-10-20 14:28:16 +02:00
if m.Date.Add(3 * time.Minute).After(time.Now().UTC()) {
2019-10-21 04:25:41 +02:00
if m.ChatID64 == chtwrsbotID64 && m.TGSenderUserID64 == chtwrsbotID64 {
2019-10-20 14:28:16 +02:00
s := TGCommand{
Type: commandForwardMsg,
FromUserID64: m.TGUserID64,
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
2019-10-25 06:39:25 +02:00
ToChatID64: angrybirbsbotID64,
2019-10-20 14:28:16 +02:00
}
MQTGCmdQueue <- s
} else if m.ChatID64 == cfg.Bot.Mainchat {
2019-10-21 08:45:47 +02:00
err = clientSpreadQuestResultAmbush(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : clientSpreadQuestResultAmbush.")
2019-10-20 14:09:25 +02:00
}
}
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_pillage_inc`]:
2020-02-05 07:53:30 +01:00
fallthrough
case cacheObjSubType[`msg_pillage_inc2`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessagePillageInc(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_pillage_inc`]")
2019-05-16 14:39:12 +02:00
cwm.ObjID64 = objId
err = insertMsgPillageInc(cwm)
2019-05-18 10:33:58 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgPillageInc")
2019-05-27 06:03:04 +02:00
// only catch live pillages
2019-10-20 14:09:25 +02:00
if m.Date.Add(3*time.Minute).After(time.Now()) && m.ChatID64 == chtwrsbotID64 {
2019-05-18 12:38:24 +02:00
s := TGCommand{
Type: commandSendMsg,
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)),
2019-07-31 10:57:45 +02:00
ToUserID64: m.TGUserID64,
2019-05-18 12:38:24 +02:00
}
TGCmdQueue <- s
2019-05-27 06:03:04 +02:00
p := JobPayloadPillage{
ObjID64: objId,
2019-08-04 11:13:42 +02:00
Date: m.Date,
2019-05-27 06:03:04 +02:00
}
b, _ := json.Marshal(&p)
2020-01-02 13:04:12 +01:00
_, err = createJob(cacheObjSubType[`job_pillage`], objJobPriority, m.TGUserID64, m.ObjID64, m.Date.Add(time.Duration(25+rand.Intn(35))*time.Second), b)
2019-05-27 06:03:04 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
2019-05-18 12:59:43 +02:00
}
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_me_ack`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageMeAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_me_ack`]")
2019-05-30 07:56:57 +02:00
clientMsgMeAck(cwm)
2019-07-31 10:57:45 +02:00
_, err = addObjXP(cwm.CWUserID64, cwm.ExpNow, cwm.ExpLvl, cwm.Level, m.Date)
2019-05-31 04:02:21 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_go_quest_ack`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageGoQuestAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_go_quest_ack`]")
2019-07-31 10:57:45 +02:00
uid, err := clientGetCWUserID64(m.TGUserID64)
2019-07-09 10:19:28 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving UserID64.")
2019-07-09 09:12:03 +02:00
if err != nil {
2020-01-02 12:27:33 +01:00
err = setObjSubTypeId(objId, cacheObjSubType[`msg`])
2019-07-09 09:12:03 +02:00
} else {
clientMsgGoQuestAck(cwm)
2020-01-02 16:41:00 +01:00
_, err = addObjQuest(uid, cwm.QuestTypeID64, cwm.Duration, m.Date.UTC())
2019-07-09 09:12:03 +02:00
}
2020-01-12 10:56:08 +01:00
err = setClientBusy(m.TGUserID64, m.Date, cwm.Duration)
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_duel_fight`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageDuelFight(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_duel_fight`]")
2019-05-18 16:17:22 +02:00
cwm.ObjID64 = objId
2019-05-19 14:02:06 +02:00
err = insertMsgDuelFight(cwm)
2019-06-06 07:52:22 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgDuelFight")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_union_war`]:
2019-08-21 08:37:53 +02:00
_, err := parseSubTypeMessageUnionWar(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_union_war`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_groles_ack`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageGRolesAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_groles_ack`]")
2019-05-30 12:57:00 +02:00
clientMsgGRolesAck(cwm)
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_auction_announce`]:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageAuctionAnnounce(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_auction_announce`]")
2019-06-06 08:18:42 +02:00
cwm.ObjID64 = objId
2019-06-06 07:55:22 +02:00
err = insertMsgAuctionAnnounce(cwm)
2019-05-14 04:28:26 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce")
2019-06-11 08:36:08 +02:00
if cwm.End.After(time.Now().UTC()) || strings.Compare(cwm.Status, `#active`) == 0 {
2019-06-08 17:28:07 +02:00
p := JobPayloadMsgRefresh{
ObjID64: m.ObjID64,
}
b, _ := json.Marshal(&p)
2020-01-02 13:04:12 +01:00
_, err = createJob(cacheObjSubType[`job_msg_refresh`], objJobPriority, m.TGUserID64, m.ObjID64, cwm.End.Add(5*time.Minute).UTC(), b)
2019-08-15 10:57:10 +02:00
/* hack for autobid - FIXME */
2019-10-09 04:07:36 +02:00
/*
if cwm.Price == 0 && cwm.ItemID64 == getObjItemID(`k05`, `Hunter blade`) {
clientSendCWMsg(cfg.Bot.Admin, fmt.Sprintf("/bet_%d_1", cwm.LotID))
}
*/
2019-06-08 17:28:07 +02:00
}
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_time_ack`]:
2019-12-19 04:31:28 +01:00
_, err := parseSubTypeMessageTimeAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_time_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_orderbook_acl`]:
2019-08-21 08:57:48 +02:00
_, err := parseSubTypeMessageOrderbookAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_orderbook_acl`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_stock_ack`]:
2019-08-22 11:31:16 +02:00
_, err := parseSubTypeMessageStockAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_stock_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_g_deposit_req`]:
2019-08-30 04:48:38 +02:00
_, err := parseSubTypeMessageGDepositReq(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_g_deposit_req`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_g_deposit_ack`]:
2019-10-10 12:14:29 +02:00
cwm, err := parseSubTypeMessageGDepositAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_g_deposit_ack`]")
2019-10-10 12:14:29 +02:00
err = insertMsgItem(cwm.Msg.ObjID64, cwm.ItemID64, cwm.Quantity)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Inserting cacheObjSubType[`msg_g_deposit_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_stock_any_ack`]:
2019-08-30 09:11:49 +02:00
_, err := parseSubTypeMessageStockAnyAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_stock_any_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_exchange_ack`]:
2019-09-14 10:27:38 +02:00
_, err := parseSubTypeMessageExchangeAck(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_exchange_ack`]")
2020-01-02 12:27:33 +01:00
case cacheObjSubType[`msg_quest_res`]:
2019-10-03 05:35:24 +02:00
_, err := parseSubTypeMessageQuestResult(m, rule.re)
2020-01-12 10:56:08 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_quest_res`]")
err = setClientIdle(m.TGUserID64, m.Date)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setClientIdle")
2020-01-17 03:47:03 +01:00
case cacheObjSubType[`msg_job_gwithdraw_ack`]:
2020-01-17 05:24:07 +01:00
_, err := parseSubTypeMessageJobGWithdrawAck(m, rule.re)
2020-01-17 03:47:03 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_quest_res`]")
2020-01-26 14:40:20 +01:00
case cacheObjSubType[`msg_bot_g_stock`]:
botGStock(m)
2020-01-26 11:30:37 +01:00
case cacheObjSubType[`msg_bot_craft_item`]:
2020-01-27 05:40:55 +01:00
botCraftItem(m, rule.re)
2020-01-26 12:06:28 +01:00
case cacheObjSubType[`msg_bot_craft_all`]:
2020-02-01 08:25:33 +01:00
botCraftAll(m, rule.re)
2020-02-03 05:06:41 +01:00
case cacheObjSubType[`msg_tributes_stats_req`]:
case cacheObjSubType[`msg_tributes_stats_ack`]:
2020-02-03 07:21:16 +01:00
cwm, err := parseSubTypeMessageTributesStatsAck(m, rule.re)
2020-02-03 05:06:41 +01:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_tributes_stats_ack`]")
2020-02-03 07:18:17 +01:00
err = insertMsgTributesStats(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgTributesStats")
2019-05-08 12:31:52 +02:00
default:
2019-12-29 09:42:31 +01:00
//log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Unknwon message type in rule %d : %d (%d)\n%s\n", msgParsingRules[i].ID, msgParsingRules[i].MsgTypeID64, objId, m.Text)
2019-05-08 12:31:52 +02:00
}
2019-08-17 08:18:25 +02:00
muxCallbacks.Lock()
2019-08-17 08:38:37 +02:00
if mc1, mok1 := callbacks[m.TGUserID64]; mok1 {
2019-12-29 09:42:31 +01:00
if mc2, mok2 := mc1[rule.MsgTypeID64]; mok2 {
2019-08-17 08:46:14 +02:00
for j := range mc2 {
2019-08-21 05:46:42 +02:00
err := rescheduleJob(mc2[j], m.ObjID64, time.Now().UTC())
2019-08-17 08:38:37 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering")
}
2019-12-29 09:42:31 +01:00
mc1[rule.MsgTypeID64] = nil
2019-08-17 08:18:25 +02:00
}
}
2020-01-17 05:24:07 +01:00
if mc1, mok1 := callbacks[int64(bot.Me.ID)]; mok1 {
if mc2, mok2 := mc1[rule.MsgTypeID64]; mok2 {
for j := range mc2 {
err := rescheduleJob(mc2[j], m.ObjID64, time.Now().UTC())
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering")
}
mc1[rule.MsgTypeID64] = nil
}
}
2019-08-17 08:18:25 +02:00
muxCallbacks.Unlock()
2019-05-06 09:25:57 +02:00
}
2019-05-06 04:54:24 +02:00
}
2019-05-05 13:29:28 +02:00
}
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
2019-05-07 13:15:25 +02:00
2019-05-31 14:24:27 +02:00
func SQLJobWorker() {
//log.Printf("SQLJobWorker : Starting.")
2019-05-07 13:15:25 +02:00
for true {
2019-05-31 14:24:27 +02:00
jobs, err := loadCurrentJobs()
logOnError(err, "SQLJobWorker : loadCurrentJobs")
2019-06-11 08:03:58 +02:00
/*
if len(jobs) > 0 {
log.Printf("SQLJobWorker : %d jobs.\n", len(jobs))
}
*/
2019-08-01 05:49:05 +02:00
if err == nil {
for _, j := range jobs {
JobQueue <- j
}
if len(jobs) < SQLJobSliceSize {
time.Sleep(100 * time.Millisecond)
}
} else {
time.Sleep(1 * time.Second)
2019-05-10 11:27:55 +02:00
}
2019-05-07 13:15:25 +02:00
}
2019-05-31 14:24:27 +02:00
log.Printf("SQLJobWorker : Closing.")
}
2019-05-31 14:30:08 +02:00
func JobWorker(id int, jobs <-chan Job) {
2019-05-31 14:24:27 +02:00
//log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-08-23 14:09:08 +02:00
// FIXME : discard timed out jobs
2019-05-31 14:24:27 +02:00
for j := range jobs {
2019-08-29 14:15:26 +02:00
//log.Printf("JobWorker[%d] : Starting job %d (timeout : %s).\n", id, j.ID64, j.Timeout.Format(time.RFC3339))
2019-08-24 07:32:55 +02:00
if time.Now().UTC().Before(j.Timeout) {
2020-01-02 13:07:46 +01:00
switch j.JobTypeID64 {
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_rescan_msg`]:
2019-08-24 07:32:55 +02:00
jobRescan(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_set_done`]:
2019-08-24 07:32:55 +02:00
jobSetDone(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_pillage`]:
2019-08-24 07:32:55 +02:00
jobPillage(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_msg_client`]:
2019-08-24 07:32:55 +02:00
jobMsgClient(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_msg_refresh`]:
2019-08-24 07:32:55 +02:00
jobMsgRefresh(j)
2020-01-07 11:32:20 +01:00
case cacheObjSubType[`job_msg_fwd`]:
jobMsgFwd(j)
case cacheObjSubType[`job_msg_del`]:
jobMsgDelete(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_backup_export`]:
2019-08-24 07:32:55 +02:00
jobBackupExport(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_backup_import`]:
2019-08-24 07:32:55 +02:00
jobBackupImport(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_gstock`]:
2019-08-24 07:32:55 +02:00
jobGStock(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_gdeposit`]:
2019-08-24 07:32:55 +02:00
jobGDeposit(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_gdeposit_fwd`]:
2019-08-30 04:16:48 +02:00
jobGDepositForward(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_gwithdraw`]:
2019-10-04 12:38:03 +02:00
jobGWithdraw(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_vault_user_status`]:
2019-10-12 07:47:40 +02:00
jobVaultUserStatus(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_vault_item_status`]:
2019-10-12 07:47:40 +02:00
jobVaultItemStatus(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_set_def`]:
2019-12-13 09:34:32 +01:00
jobSetDef(j)
2020-01-02 13:04:12 +01:00
case cacheObjSubType[`job_get_hammer_time`]:
2019-12-19 04:31:28 +01:00
jobGetHammerTime(j)
2020-01-26 14:40:20 +01:00
case cacheObjSubType[`job_get_vault`]:
jobGetVault(j)
2020-01-27 05:05:38 +01:00
case cacheObjSubType[`job_craft_item`]:
jobCraftItem(j)
2020-01-29 15:56:19 +01:00
case cacheObjSubType[`job_craft_all`]:
jobCraftAll(j)
2020-02-03 03:54:34 +01:00
case cacheObjSubType[`job_check_vault_limit`]:
jobCheckVaultLimit(j)
2019-08-24 07:32:55 +02:00
default:
2020-01-02 13:07:46 +01:00
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64)
2019-08-24 07:32:55 +02:00
}
} else {
log.Printf("JobWorker[%d] : Job %d timed out.\n", id, j.ID64)
setJobDone(j.ID64)
2019-05-31 14:24:27 +02:00
}
}
log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Closing.")
2019-05-07 13:15:25 +02:00
}
2019-05-11 05:15:42 +02:00
2019-05-16 04:16:35 +02:00
func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
2019-05-25 13:27:08 +02:00
//log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-05-16 04:16:35 +02:00
for c := range cmds {
2019-06-11 06:03:24 +02:00
//j, err := json.Marshal(c)
//logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
2019-06-11 06:02:54 +02:00
//log.Printf("TGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
2019-06-10 11:14:03 +02:00
opt := tb.SendOptions{}
2019-06-10 11:05:59 +02:00
switch c.ParseMode {
case cmdParseModePlain:
opt.ParseMode = tb.ModeDefault
case cmdParseModeMarkDown:
opt.ParseMode = tb.ModeMarkdown
case cmdParseModeHTML:
opt.ParseMode = tb.ModeHTML
default:
opt.ParseMode = tb.ModeDefault
}
2019-05-16 13:34:26 +02:00
switch c.Type {
2019-05-17 09:20:50 +02:00
case commandSendMsg:
if c.ToChatID64 != 0 {
ch := tb.Chat{
ID: c.ToChatID64,
}
2019-06-10 11:20:20 +02:00
_, err := b.Send(&ch, c.Text, &opt)
2019-06-08 17:41:50 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg Chat")
2019-05-17 09:20:50 +02:00
} else if c.ToUserID64 != 0 {
2019-06-09 10:02:10 +02:00
u := tb.User{
ID: int(c.ToUserID64),
2019-05-17 09:20:50 +02:00
}
2019-06-10 11:20:20 +02:00
_, err := b.Send(&u, c.Text, &opt)
2019-06-08 17:41:50 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg User")
2019-05-17 09:20:50 +02:00
}
2019-06-09 09:34:08 +02:00
case commandReplyMsg:
ch := tb.Chat{
ID: c.FromChatID64,
}
m := tb.Message{
ID: int(c.FromMsgID64),
Chat: &ch,
}
2019-06-10 11:20:20 +02:00
_, err := b.Reply(&m, c.Text, &opt)
2019-06-09 09:34:08 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : ReplyMsg")
2019-06-09 08:50:04 +02:00
case commandSendDocument:
if c.ToChatID64 != 0 {
ch := tb.Chat{
2019-06-09 09:58:08 +02:00
ID: c.ToChatID64,
2019-06-09 08:50:04 +02:00
}
2019-06-09 09:46:12 +02:00
d := c.Document
_, err := b.Send(&ch, &d)
2019-06-09 09:35:01 +02:00
//_, err := c.Document.Send(b, ch, nil)
2019-06-09 08:50:04 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
} else if c.ToUserID64 != 0 {
2019-06-09 09:58:08 +02:00
u := tb.User{
2019-06-09 10:02:10 +02:00
ID: int(c.ToUserID64),
2019-06-09 08:50:04 +02:00
}
2019-06-09 09:46:12 +02:00
d := c.Document
2019-06-09 09:58:08 +02:00
_, err := b.Send(&u, &d)
2019-06-09 09:35:01 +02:00
//_, err := c.Document.Send(b, &ch, nil)
2019-06-09 08:50:04 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
}
2019-05-16 13:34:26 +02:00
default:
2019-08-29 13:59:06 +02:00
log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Unknown command.\n")
2019-05-16 13:34:26 +02:00
}
2019-05-16 04:16:35 +02:00
}
log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
}
2019-05-16 14:39:12 +02:00
2019-05-17 09:02:35 +02:00
func MQTidyKeepAliveWorker() {
2019-05-25 13:27:08 +02:00
//log.Printf("MQTidyKeepAliveWorker : Starting.")
2019-05-17 09:02:35 +02:00
for true {
t := time.Now()
2019-07-31 03:24:01 +02:00
muxClients.Lock()
for id, clt := range clients {
2019-07-31 08:54:20 +02:00
clt.Mux.Lock()
2019-07-31 08:41:48 +02:00
if clt.Active && clt.HeartBeat.Add(3*KeepAliveHeartBeatSeconds*time.Second).Before(time.Now()) {
msgs, err := clt.MQ.Channel.QueuePurge(clt.MQ.Queue.Name, false)
2019-07-31 03:24:01 +02:00
logOnError(err, "MQTidyKeepAliveWorker : Channel.QueuePurge()")
err = clt.MQ.Channel.Close()
logOnError(err, "MQTidyKeepAliveWorker : Channel.Close()")
err = clt.MQ.Connection.Close()
logOnError(err, "MQTidyKeepAliveWorker : Connection.Close()")
cmd := TGCommand{
Type: commandSendMsg,
2019-07-31 08:44:58 +02:00
ToUserID64: id,
2019-07-31 03:24:01 +02:00
Text: "Timeout, purging and closing command queue.",
}
TGCmdQueue <- cmd
cmd = TGCommand{
Type: commandSendMsg,
ToUserID64: cfg.Bot.Admin,
2019-07-31 09:23:38 +02:00
Text: fmt.Sprintf("Client %s timed out (%d messages purged).", clt.Login, msgs),
2019-07-31 03:24:01 +02:00
}
TGCmdQueue <- cmd
clt.Active = false
2019-07-31 09:16:12 +02:00
} else if clt.Active {
2019-07-31 09:43:17 +02:00
//log.Printf("MQTidyKeepAliveWorker : Client %s is active.\n", clt.Login)
2019-07-31 09:15:05 +02:00
} else {
2019-07-31 09:43:17 +02:00
//log.Printf("MQTidyKeepAliveWorker : Client %s is inactive.\n", clt.Login)
2019-07-31 03:24:01 +02:00
}
2019-07-31 08:54:20 +02:00
clt.Mux.Unlock()
2019-07-31 03:24:01 +02:00
}
muxClients.Unlock()
2019-05-17 09:07:47 +02:00
time.Sleep(time.Until(t.Add(time.Second)))
2019-05-17 09:02:35 +02:00
}
log.Printf("MQTidyKeepAliveWorker : Closing.")
}