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"
|
2019-05-06 09:27:05 +02:00
|
|
|
"regexp"
|
2019-05-05 13:29:28 +02:00
|
|
|
"strconv"
|
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 {
|
|
|
|
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) {
|
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.")
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("SQLCWMsgWorker["+strconv.Itoa(id)+"] : Message inserted (%d).\n", objId)
|
|
|
|
objIds <- objId
|
2019-05-05 13:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
|
|
|
|
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-06 09:25:57 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message retrieved (%d)\n%s\n", objId, m.Text)
|
|
|
|
// War reports
|
2019-05-06 10:57:04 +02:00
|
|
|
// r := regexp.MustCompile(`^(?P<Castle>.)(?P<Guild>(\[[A-Z]{3}\]){0,1})(?P<User>([A-Za-z0-9 ]*)) ⚔:(?P<Attack>[0-9]+) .:(?P<Defense>[0-9]+) Lvl: (?P<Level>[0-9]+)\nYour result on the battlefield:\n.Exp: (?P<Exp>[0-9]+)\n.Gold: (?P<Gold>[0-9]+)\n.Stock: (?P<Stock>[0-9]+)\n(?s:.*)$`) // FIXME defense/exp/gold/stock characters !
|
2019-05-06 16:02:03 +02:00
|
|
|
r := regexp.MustCompile(`^(?P<Castle>[🐉🦅🐺🦈🦌🥔🌑])(?P<Guild>(\[[A-Z]{3}\]){0,1})(?P<User>([A-Za-z0-9 ]*)) ⚔:(?P<Attack>[0-9]+) 🛡:(?P<Defense>[0-9]+) Lvl: (?P<Level>[0-9]+)\nYour result on the battlefield:\n🔥Exp: (?P<Exp>[0-9]+)\n💰Gold: (?P<Gold>-{0,1}[0-9]+)\n📦Stock: (?P<Stock>-{0,1}[0-9]+)\n\n(?P<Stamina>(🔋Stamina restored)){0,1}(\n)*(?P<Crit>(⚡Critical strike)){0,1}(\n)*(?s:.*)$`) // FIXME defense/exp/gold/stock characters !
|
2019-05-06 09:27:05 +02:00
|
|
|
if r.FindStringSubmatch(m.Text) != nil {
|
2019-05-06 09:25:57 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War report identified (%d)\n", objId)
|
2019-05-06 09:34:55 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Castle : %s (%d)\n", r.ReplaceAllString(m.Text, "${Castle}"), objId)
|
2019-05-06 09:35:54 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Guild : %s (%d)\n", r.ReplaceAllString(m.Text, "${Guild}"), objId)
|
2019-05-06 09:37:13 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : User : %s (%d)\n", r.ReplaceAllString(m.Text, "${User}"), objId)
|
2019-05-06 09:38:28 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Attack : %s (%d)\n", r.ReplaceAllString(m.Text, "${Attack}"), objId)
|
2019-05-06 09:40:45 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Defense : %s (%d)\n", r.ReplaceAllString(m.Text, "${Defense}"), objId)
|
2019-05-06 09:47:41 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Level : %s (%d)\n", r.ReplaceAllString(m.Text, "${Level}"), objId)
|
2019-05-06 10:39:58 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Exp : %s (%d)\n", r.ReplaceAllString(m.Text, "${Exp}"), objId)
|
2019-05-06 10:46:05 +02:00
|
|
|
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)
|
2019-05-06 10:57:04 +02:00
|
|
|
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-07 05:12:03 +02:00
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Test : %s (%d)\n", r.ReplaceAllString(m.Text, "${Test}"), objId)
|
2019-05-06 09:28:34 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Unknown message type (%d)\n", objId)
|
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.")
|
|
|
|
}
|