chirpnest/workers.go

678 lines
23 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 {
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
2019-07-31 09:43:17 +02:00
clientSendCWMsg(x.TGUserID64, `🏅Me`)
} 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)")
2019-08-29 14:15:26 +02:00
//log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
2019-07-31 06:45:42 +02:00
for clt.MQ.Connection.IsClosed() {
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.")
2019-07-09 09:16:02 +02: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 {
2019-07-31 10:57:45 +02:00
objId, err := addObjMsg(m.ID64, m.ChatID64, m.TGUserID64, m.TGSenderUserID64, m.Date, m.Text)
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-08-21 08:37:53 +02:00
err = setObjSubTypeId(objId, rule.MsgTypeID)
2019-06-02 08:48:16 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId")
2019-08-21 08:37:53 +02:00
switch rule.MsgTypeID {
2019-06-02 11:30:04 +02:00
case objSubTypeMessageGRolesReq:
case objSubTypeMessageGo:
case objSubTypeMessageReportReq:
case objSubTypeMessageGReportReq:
case objSubTypeMessageHeroReq:
case objSubTypeMessageMeReq:
case objSubTypeMessageInventoryReq:
case objSubTypeMessageTimeReq:
case objSubTypeMessagePledge:
case objSubTypeMessagePillageGo:
case objSubTypeMessagePillageTimeout:
case objSubTypeMessagePillageWin:
case objSubTypeMessagePillageLoss:
2019-10-03 13:23:56 +02:00
case objSubTypeMessageGoQuestReq:
2019-06-02 11:30:04 +02:00
case objSubTypeMessageGoFastFight:
case objSubTypeMessageGoArena:
case objSubTypeMessageTop:
case objSubTypeMessageMenu:
case objSubTypeMessageBuyReq:
case objSubTypeMessageSellReq:
case objSubTypeMessageOrderbookReq:
case objSubTypeMessageWithdrawReq:
case objSubTypeMessageWithdrawCode:
case objSubTypeMessageWithdrawRcv:
2019-10-11 03:21:09 +02:00
cwm, err := parseSubTypeMessageWithdrawRcv(m, rule.re)
2019-10-10 12:14:29 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageWithdrawRcv.")
2019-10-11 03:21:09 +02:00
for _, i := range cwm.ItemList {
err = insertMsgItem(cwm.Msg.ObjID64, i.ItemID64, i.Quantity)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Inserting objSubTypeMessageWithdrawRcv.")
}
2019-06-02 11:30:04 +02:00
case objSubTypeMessageStockReq:
case objSubTypeMessageMiscReq:
2019-08-08 14:39:23 +02:00
case objSubTypeMessageGStockResReq:
case objSubTypeMessageGStockAlchReq:
case objSubTypeMessageGStockMiscReq:
case objSubTypeMessageGStockRecReq:
case objSubTypeMessageGStockPartReq:
case objSubTypeMessageGStockOthReq:
2019-06-02 11:30:04 +02:00
case objSubTypeMessageGStockAnyAck:
2019-08-21 08:37:53 +02:00
_, err := parseSubTypeMessageGStockAnyAck(m, rule.re)
2019-06-02 11:30:04 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGStockAnyAck.")
2019-05-19 06:19:24 +02:00
case objSubTypeMessageReportAck:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageReportAck(m, rule.re)
2019-05-19 06:19:24 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.")
2019-05-18 09:44:25 +02:00
cwm.ObjID64 = objId
2019-05-15 10:58:25 +02:00
case objSubTypeMessagePillageInc:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessagePillageInc(m, rule.re)
2019-05-18 16:15:24 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessagePillageInc.")
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-05-18 12:59:43 +02:00
if m.Date.Add(3 * time.Minute).After(time.Now()) {
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)
2019-08-21 05:46:42 +02:00
_, err = createJob(objSubTypeJobPillage, 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
}
2019-05-27 12:56:03 +02:00
case objSubTypeMessageMeAck:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageMeAck(m, rule.re)
2019-05-28 06:14:46 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
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)")
2019-05-28 09:00:37 +02:00
case objSubTypeMessageGoQuestAck:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageGoQuestAck(m, rule.re)
2019-07-09 10:19:28 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGoQuestAck.")
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 {
err = setObjSubTypeId(objId, objSubTypeMessageUnknown)
} else {
clientMsgGoQuestAck(cwm)
2019-07-09 09:13:17 +02:00
_, err = addObjQuest(uid, cwm.QuestTypeID, cwm.Duration, m.Date.UTC())
2019-07-09 09:12:03 +02:00
}
2019-05-18 13:15:50 +02:00
case objSubTypeMessageDuelFight:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageDuelFight(m, rule.re)
2019-05-18 16:17:22 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageDuelFight.")
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")
2019-05-25 11:57:10 +02:00
case objSubTypeMessageUnionWar:
2019-08-21 08:37:53 +02:00
_, err := parseSubTypeMessageUnionWar(m, rule.re)
2019-05-25 13:12:31 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.")
2019-05-30 10:36:40 +02:00
case objSubTypeMessageGRolesAck:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageGRolesAck(m, rule.re)
2019-06-02 11:34:09 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGRolesAck.")
2019-05-30 12:57:00 +02:00
clientMsgGRolesAck(cwm)
2019-05-08 16:34:47 +02:00
case objSubTypeMessageAuctionAnnounce:
2019-08-21 08:37:53 +02:00
cwm, err := parseSubTypeMessageAuctionAnnounce(m, rule.re)
2019-06-06 07:56:28 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageAuctionAnnounce.")
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)
2019-08-21 05:46:42 +02:00
_, err = createJob(objSubTypeJobMsgRefresh, 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
}
2019-05-11 08:24:38 +02:00
case objSubTypeMessageTimeAck:
2019-08-21 08:41:30 +02:00
_, err := fromChatWarsDate(rule.re.ReplaceAllString(m.Text, "${Day}") + " " + rule.re.ReplaceAllString(m.Text, "${Month}") + " " + rule.re.ReplaceAllString(m.Text, "${Year}") + " " + rule.re.ReplaceAllString(m.Text, "${Hour}") + ":" + rule.re.ReplaceAllString(m.Text, "${Minute}"))
2019-05-10 11:23:47 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : fromChatWarsDate")
2019-08-21 08:37:53 +02:00
case objSubTypeMessageOrderbookAck:
2019-08-21 08:57:48 +02:00
_, err := parseSubTypeMessageOrderbookAck(m, rule.re)
2019-08-21 08:37:53 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageOrderbookAck.")
2019-08-22 11:31:16 +02:00
case objSubTypeMessageStockAck:
_, err := parseSubTypeMessageStockAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageStockAck.")
2019-08-30 04:47:54 +02:00
case objSubTypeMessageGDepositReq:
2019-08-30 04:48:38 +02:00
_, err := parseSubTypeMessageGDepositReq(m, rule.re)
2019-08-30 04:47:54 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGDepositReq.")
case objSubTypeMessageGDepositAck:
2019-10-10 12:14:29 +02:00
cwm, err := parseSubTypeMessageGDepositAck(m, rule.re)
2019-08-30 04:47:54 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGDepositAck.")
2019-10-10 12:14:29 +02:00
err = insertMsgItem(cwm.Msg.ObjID64, cwm.ItemID64, cwm.Quantity)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Inserting objSubTypeMessageGDepositAck.")
2019-08-30 09:11:49 +02:00
case objSubTypeMessageStockAnyAck:
_, err := parseSubTypeMessageStockAnyAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageStockAnyAck.")
2019-09-14 10:27:38 +02:00
case objSubTypeMessageExchangeAck:
_, err := parseSubTypeMessageExchangeAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageExchangeAck.")
2019-10-03 05:35:24 +02:00
case objSubTypeMessageQuestResult:
_, err := parseSubTypeMessageQuestResult(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageQuestResult.")
2019-05-08 12:31:52 +02:00
default:
2019-08-27 06:40:12 +02:00
//log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Unknwon message type in rule %d : %d (%d)\n%s\n", msgParsingRules[i].ID, msgParsingRules[i].MsgTypeID, 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-08-21 08:37:53 +02:00
if mc2, mok2 := mc1[rule.MsgTypeID]; 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-08-21 08:37:53 +02:00
mc1[rule.MsgTypeID] = 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) {
switch j.JobTypeID {
case objSubTypeJobRescanMsg:
jobRescan(j)
case objSubTypeJobSetJobDone:
jobSetDone(j)
case objSubTypeJobPillage:
jobPillage(j)
case objSubTypeJobMsgClient:
jobMsgClient(j)
case objSubTypeJobMsgRefresh:
jobMsgRefresh(j)
case objSubTypeJobBackupExport:
jobBackupExport(j)
case objSubTypeJobBackupImport:
jobBackupImport(j)
case objSubTypeJobGStock:
jobGStock(j)
case objSubTypeJobGDeposit:
jobGDeposit(j)
2019-08-30 04:16:48 +02:00
case objSubTypeJobGDepositForward:
jobGDepositForward(j)
2019-10-04 12:38:03 +02:00
case objSubTypeJobGWithdraw:
jobGWithdraw(j)
2019-10-12 07:47:40 +02:00
case objSubTypeJobVaultUserStatus:
jobVaultUserStatus(j)
case objSubTypeJobVaultItemStatus:
jobVaultItemStatus(j)
2019-08-24 07:32:55 +02:00
default:
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
}
} 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.")
}