chirpnest/workers.go

305 lines
12 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-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-05-06 04:54:24 +02:00
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
2019-05-17 09:02:35 +02:00
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
2019-05-06 04:54:24 +02:00
var x ChatWarsMessage
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
m, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
for d := range m {
2019-05-11 07:46:14 +02:00
// log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
2019-05-06 04:54:24 +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
}
}
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage, objIds chan<- int64) {
2019-05-05 13:29:28 +02:00
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
for m := range msgs {
objId, err := putUnprocessedMsg(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-05-05 13:29:28 +02:00
}
}
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
2019-05-08 12:32:48 +02:00
var i int
2019-05-05 13:29:28 +02:00
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
for objId := range objIds {
2019-05-06 04:54:24 +02:00
m, err := getMsg(objId)
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-05-08 15:28:10 +02:00
for i = 0; i < len(msgParsingRules) && msgParsingRules[i].re.FindStringSubmatch(m.Text) == nil; i++ {
2019-05-08 12:31:52 +02:00
}
if i == len(msgParsingRules) {
2019-05-13 04:27:03 +02:00
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Can't find message type in rules(%d)\n%s\n", objId, m.Text)
2019-05-06 09:28:34 +02:00
} else {
2019-05-08 12:34:51 +02:00
r := msgParsingRules[i].re
2019-05-08 12:31:52 +02:00
switch msgParsingRules[i].MsgTypeID {
case objSubTypeMessageReport:
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War report identified (%d)\n", objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Castle : %s (%d)\n", r.ReplaceAllString(m.Text, "${Castle}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Guild : %s (%d)\n", r.ReplaceAllString(m.Text, "${Guild}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : User : %s (%d)\n", r.ReplaceAllString(m.Text, "${User}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Attack : %s (%d)\n", r.ReplaceAllString(m.Text, "${Attack}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AttackMod : %s (%d)\n", r.ReplaceAllString(m.Text, "${AttackMod}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Defense : %s (%d)\n", r.ReplaceAllString(m.Text, "${Defense}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Level : %s (%d)\n", r.ReplaceAllString(m.Text, "${Level}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Exp : %s (%d)\n", r.ReplaceAllString(m.Text, "${Exp}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Gold : %s (%d)\n", r.ReplaceAllString(m.Text, "${Gold}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stock : %s (%d)\n", r.ReplaceAllString(m.Text, "${Stock}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stamina : %s (%d)\n", r.ReplaceAllString(m.Text, "${Stamina}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Crit : %s (%d)\n", r.ReplaceAllString(m.Text, "${Crit}"), objId)
2019-05-11 12:40:58 +02:00
case objSubTypeMessageMiniWar:
2019-05-15 12:02:17 +02:00
2019-05-13 08:12:31 +02:00
cwm, err := parseSubTypeMessageMiniWar(m, r)
2019-05-13 08:18:26 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.")
2019-05-14 04:28:26 +02:00
cwm.ObjID64 = objId
2019-05-14 04:29:47 +02:00
err = insertMsgMiniWar(cwm)
2019-05-14 04:28:26 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
2019-05-15 12:02:17 +02:00
2019-05-14 04:28:26 +02:00
err = setObjSubTypeId(objId, objSubTypeMessageMiniWar)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MiniWar)")
2019-05-15 10:58:25 +02:00
case objSubTypeMessagePillageInc:
2019-05-16 14:39:12 +02:00
cwm, err := parseSubTypeMessagePillageInc(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.")
cwm.ObjID64 = objId
err = insertMsgPillageInc(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
2019-05-15 10:58:25 +02:00
err = setObjSubTypeId(objId, objSubTypeMessagePillageInc)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageInc)")
2019-05-16 14:44:51 +02:00
p := JobPayloadPillage{}
b, _ := json.Marshal(&p)
err = createJob(objSubTypeJobPillage, objJobPriority, m.UserID64, time.Now().Add(time.Duration(25+rand.Intn(35))*time.Second), b)
2019-05-16 14:39:12 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
s := TGCommand{
Type: commandSendMsg,
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)),
ToChatID64: m.UserID64,
}
TGCmdQueue <- s
case objSubTypeMessageGo:
2019-05-08 16:34:47 +02:00
case objSubTypeMessageAuctionAnnounce:
2019-05-09 11:21:12 +02:00
cwm := ChatWarsMessageAuctionAnnounce{
2019-05-11 07:41:05 +02:00
ObjID64: objId,
2019-05-09 11:19:02 +02:00
}
2019-05-09 11:28:24 +02:00
l, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Lot}"), 10, 32)
cwm.LotID = int32(l)
2019-05-09 11:27:02 +02:00
cwm.Item = r.ReplaceAllString(m.Text, "${Item}")
2019-05-09 11:21:12 +02:00
cwm.Cond = r.ReplaceAllString(m.Text, "${Cond}")
cwm.Quality = r.ReplaceAllString(m.Text, "${Quality}")
cwm.Seller = r.ReplaceAllString(m.Text, "${Seller}")
cwm.Buyer = r.ReplaceAllString(m.Text, "${Buyer}")
2019-05-09 11:28:24 +02:00
p, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Price}"), 10, 32)
cwm.Price = int32(p)
2019-05-09 11:21:12 +02:00
cwm.Status = r.ReplaceAllString(m.Text, "${Status}")
2019-05-09 12:37:12 +02:00
cwm.End, _ = fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}"))
2019-05-09 11:32:34 +02:00
err = insertMsgAuctionAnnounce(&cwm)
2019-05-14 04:28:26 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce")
2019-05-11 08:27:32 +02:00
err = setObjSubTypeId(objId, objSubTypeMessageAuctionAnnounce)
2019-05-11 08:24:38 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(AuctionAnnounce)")
case objSubTypeMessageTimeAck:
2019-05-11 08:27:59 +02:00
_, err := fromChatWarsDate(r.ReplaceAllString(m.Text, "${Day}") + " " + r.ReplaceAllString(m.Text, "${Month}") + " " + r.ReplaceAllString(m.Text, "${Year}") + " " + r.ReplaceAllString(m.Text, "${Hour}") + ":" + r.ReplaceAllString(m.Text, "${Minute}"))
2019-05-10 11:23:47 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : fromChatWarsDate")
2019-05-11 08:27:32 +02:00
err = setObjSubTypeId(objId, objSubTypeMessageTimeAck)
2019-05-11 08:24:38 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(objSubTypeMessageTimeAck)")
2019-05-08 12:31:52 +02:00
default:
2019-05-11 07:46:14 +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-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
func SQLJobWorker(id int) {
log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Starting.")
for true {
2019-05-10 11:23:47 +02:00
jobs, err := loadCurrentJobs()
logOnError(err, "SQLJobWorker["+strconv.Itoa(id)+"] : loadCurrentJobs")
2019-05-10 12:21:02 +02:00
if len(jobs) > 0 {
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : %d jobs.\n", len(jobs))
2019-05-11 05:15:42 +02:00
}
2019-05-11 06:54:12 +02:00
for _, j := range jobs {
switch j.JobTypeID {
case objSubTypeJobRescanMsg:
jobRescan(j)
case objSubTypeJobSetJobDone:
jobSetDone(j)
default:
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
2019-05-11 05:24:11 +02:00
}
2019-05-11 06:54:12 +02:00
}
2019-05-10 11:27:55 +02:00
if len(jobs) < SQLJobSliceSize {
time.Sleep(100 * time.Millisecond)
}
2019-05-07 13:15:25 +02:00
}
log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Closing.")
}
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) {
log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
for c := range cmds {
2019-05-16 13:34:26 +02:00
j, err := json.Marshal(c)
2019-05-16 04:54:52 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
2019-05-16 13:34:26 +02:00
log.Printf("TGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
switch c.Type {
case commandReplyMsg:
2019-05-16 13:35:05 +02:00
ch := tb.Chat{
2019-05-16 13:34:26 +02:00
ID: c.FromChatID64,
}
2019-05-16 13:35:05 +02:00
m := tb.Message{
2019-05-16 13:34:26 +02:00
ID: int(c.FromMsgID64),
Chat: &ch,
}
2019-05-16 13:35:24 +02:00
_, err := b.Reply(&m, c.Text)
2019-05-16 13:34:26 +02:00
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Reply")
default:
}
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-16 14:41:21 +02:00
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
2019-05-16 14:39:12 +02:00
log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
for c := range cmds {
j, err := json.Marshal(c)
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
}
log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
}
2019-05-17 09:02:35 +02:00
func MQKeepAliveWorker() {
log.Printf("MQKeepAliveWorker : Starting.")
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
failOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "MQKeepAliveWorker : Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
"keepalive", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "MQKeepAliveWorker : Failed to declare a queue")
m, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "MQKeepAliveWorker : Failed to register a consumer")
for d := range m {
// log.Printf("MQKeepAliveWorker : Received a message: %s", string(d.Body))
x := MQKeepAlive{}
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
if err == nil {
if x.Date.Add(time.Minute).Before(time.Now()) {
// outdate keep-alive
} else if _, ok := clientsQueues[x.UserID64]; ok {
clientsQueues[x.UserID64].Date = x.Date
} else {
2019-05-17 09:15:14 +02:00
clientsQueues[x.UserID64] = &x
2019-05-17 09:02:35 +02:00
c := TGCommand{
Type: commandSendMsg,
ToChatID64: x.UserID64,
Text: "You are connected.",
}
TGCmdQueue <- c
}
}
}
log.Printf("MQKeepAliveWorker : Closing.")
}
func MQTidyKeepAliveWorker() {
log.Printf("MQTidyKeepAliveWorker : Starting.")
for true {
t := time.Now()
2019-05-17 09:07:47 +02:00
for _, v := range clientsQueues {
2019-05-17 09:02:35 +02:00
if v.Date.Add(90 * time.Second).Before(time.Now()) {
c := TGCommand{
Type: commandSendMsg,
2019-05-17 09:07:47 +02:00
ToChatID64: v.UserID64,
2019-05-17 09:02:35 +02:00
Text: "Timeout.",
}
TGCmdQueue <- c
2019-05-17 09:07:47 +02:00
delete(clientsQueues, v.UserID64)
2019-05-17 09:02:35 +02:00
}
}
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.")
}