chirpnest/workers.go

201 lines
9.0 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-05 13:29:28 +02:00
"log"
"strconv"
2019-05-08 12:17:07 +02:00
"time"
2019-05-06 04:54:24 +02:00
"github.com/streadway/amqp"
2019-05-05 13:29:28 +02:00
)
2019-05-06 04:54:24 +02:00
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
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-11 07:56:51 +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-11 13:22:50 +02:00
t, err := time.Parse("02/01/06 15:04", r.ReplaceAllString(m.Text, "${Time}"))
2019-05-11 12:40:58 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : objSubTypeMessageMiniWar parsing time : "+r.ReplaceAllString(m.Text, "${Time}"))
2019-05-11 13:22:50 +02:00
cwm := ChatWarsMessageMiniWar{
Time: t,
}
g, err := strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold1}"))
s, err := strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock1}"))
2019-05-11 13:24:01 +02:00
rep := ChatWarsMessageMiniWarCastle{
2019-05-11 13:22:50 +02:00
Gardian: r.ReplaceAllString(m.Text, "${Gardian1}"),
Result: r.ReplaceAllString(m.Text, "${Result1}"),
Gold: g,
Stock: s,
}
cwm.Report[r.ReplaceAllString(m.Text, "${Loot1}")] = rep
2019-05-11 13:24:01 +02:00
g, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Gold2}"))
s, err = strconv.Atoi(r.ReplaceAllString(m.Text, "${Stock2}"))
rep = ChatWarsMessageMiniWarCastle{
2019-05-11 13:22:50 +02:00
Gardian: r.ReplaceAllString(m.Text, "${Gardian2}"),
Result: r.ReplaceAllString(m.Text, "${Result2}"),
Gold: g,
Stock: s,
}
cwm.Report[r.ReplaceAllString(m.Text, "${Loot2}")] = rep
2019-05-11 13:02:08 +02:00
/*
if err == nil {
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War time : %s (%d)\n", r.ReplaceAllString(m.Text, "${Time}"), objId)
}
*/
2019-05-11 12:40:58 +02:00
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-09 11:40:36 +02:00
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AuctionAnnounce")
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-09 09:17:59 +02:00
/*
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time (%d)\n", objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Time : %s (%d)\n", r.ReplaceAllString(m.Text, "${Time}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Hour : %s (%d)\n", r.ReplaceAllString(m.Text, "${Hour}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Minute : %s (%d)\n", r.ReplaceAllString(m.Text, "${Minute}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Day : %s (%d)\n", r.ReplaceAllString(m.Text, "${Day}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Month : %s (%d)\n", r.ReplaceAllString(m.Text, "${Month}"), objId)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Year : %s (%d)\n", r.ReplaceAllString(m.Text, "${Year}"), objId)
*/
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
/*
func BotMsgWorker(id int, botMsg <-chan
*/