129 lines
5.8 KiB
Go
129 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/streadway/amqp"
|
|
)
|
|
|
|
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 {
|
|
log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
|
|
|
|
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) {
|
|
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
|
for m := range msgs {
|
|
objId, err := putUnprocessedMsg(m)
|
|
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
|
|
if err == nil {
|
|
log.Printf("SQLCWMsgWorker["+strconv.Itoa(id)+"] : Message inserted (%d).\n", objId)
|
|
objIds <- objId
|
|
}
|
|
}
|
|
|
|
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
}
|
|
|
|
func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
|
|
var i int
|
|
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
|
for objId := range objIds {
|
|
m, err := getMsg(objId)
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving message.")
|
|
if err == nil {
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message retrieved (%d)\n%s\n", objId, m.Text)
|
|
for i = 0; i < len(msgParsingRules) && msgParsingRules[i].re.FindStringSubmatch(m.Text) == nil; i++ {
|
|
|
|
}
|
|
if i == len(msgParsingRules) {
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Can't find message type in rules(%d)\n", objId)
|
|
} else {
|
|
r := msgParsingRules[i].re
|
|
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)
|
|
case objSubTypeMessageAuctionAnnounce:
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Auction announce (%d)\n", objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Lot : %s (%d)\n", r.ReplaceAllString(m.Text, "${Lot}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Item : %s (%d)\n", r.ReplaceAllString(m.Text, "${Item}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Seller : %s (%d)\n", r.ReplaceAllString(m.Text, "${Seller}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Buyer : %s (%d)\n", r.ReplaceAllString(m.Text, "${Buyer}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Price : %s (%d)\n", r.ReplaceAllString(m.Text, "${Price}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Status : %s (%d)\n", r.ReplaceAllString(m.Text, "${Status}"), objId)
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : End : %s (%d)\n", r.ReplaceAllString(m.Text, "${End}"), objId)
|
|
default:
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Unknwon message type in rule %d : %d (%d)\n", msgParsingRules[i].ID, msgParsingRules[i].MsgTypeID, objId)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
}
|
|
|
|
func SQLJobWorker(id int) {
|
|
// var tick time.Time
|
|
log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Starting.")
|
|
for true {
|
|
time.Sleep(100 * time.Millisecond)
|
|
// tick = time.Now()
|
|
}
|
|
log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
}
|