chirpnest/workers.go
2019-06-28 16:34:15 +08:00

507 lines
16 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"
"github.com/streadway/amqp"
tb "gopkg.in/tucnak/telebot.v2"
)
func (c *MQClient) Open() error {
var err error
c.Connection, err = amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path)
if err != nil {
c.Connection.Close()
return err
}
c.Channel, err = c.Connection.Channel()
if err != nil {
c.Channel.Close()
c.Connection.Close()
return err
}
return err
}
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
//log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
var x ChatWarsMessage
for true {
c := new(MQClient)
c.User = cfg.Rabbit.User
c.Password = cfg.Rabbit.Password
c.Host = cfg.Rabbit.Host
c.Path = cfg.Rabbit.Path
c.SSL = false
err := c.Open()
if err != nil {
q, 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 {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
} else {
m, err := c.Channel.Consume(
q.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)
} else {
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
}
}
c.Channel.Close()
c.Connection.Close()
}
}
}
}
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 := addObjMsg(m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
if err == nil && objId != 0 {
// 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 := getObjMsg(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%s\n", objId, m.Text)
} else {
r := msgParsingRules[i].re
err = setObjSubTypeId(objId, msgParsingRules[i].MsgTypeID)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId")
switch msgParsingRules[i].MsgTypeID {
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:
case objSubTypeMessageGoQuest:
case objSubTypeMessageGoFastFight:
case objSubTypeMessageGoArena:
case objSubTypeMessageTop:
case objSubTypeMessageMenu:
case objSubTypeMessageBuyReq:
case objSubTypeMessageSellReq:
case objSubTypeMessageOrderbookReq:
case objSubTypeMessageOrderbookAck:
case objSubTypeMessageWithdrawReq:
case objSubTypeMessageWithdrawCode:
case objSubTypeMessageWithdrawRcv:
case objSubTypeMessageDepositReq:
case objSubTypeMessageDepositAck:
case objSubTypeMessageStockReq:
case objSubTypeMessageMiscReq:
case objSubTypeMessageGStockAnyAck:
_, err := parseSubTypeMessageGStockAnyAck(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGStockAnyAck.")
case objSubTypeMessageReportAck:
cwm, err := parseSubTypeMessageReportAck(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.")
cwm.ObjID64 = objId
case objSubTypeMessageMiniWar:
cwm, err := parseSubTypeMessageMiniWar(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.")
cwm.ObjID64 = objId
err = insertMsgMiniWar(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
case objSubTypeMessagePillageInc:
cwm, err := parseSubTypeMessagePillageInc(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessagePillageInc.")
cwm.ObjID64 = objId
err = insertMsgPillageInc(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgPillageInc")
// only catch live pillages
if m.Date.Add(3 * time.Minute).After(time.Now()) {
s := TGCommand{
Type: commandSendMsg,
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)),
ToUserID64: m.UserID64,
}
TGCmdQueue <- s
p := JobPayloadPillage{
ObjID64: objId,
}
b, _ := json.Marshal(&p)
_, err = createJob(objSubTypeJobPillage, objJobPriority, m.UserID64, m.Date.Add(time.Duration(25+rand.Intn(35))*time.Second), b)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
}
case objSubTypeMessageMeAck:
cwm, err := parseSubTypeMessageMeAck(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
cwm.Msg = m
clientMsgMeAck(cwm)
_, err = addObjXP(cwm.UserID64, cwm.ExpNow, cwm.ExpLvl, cwm.Level, m.Date)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)")
case objSubTypeMessageGoQuestAck:
cwm, err := parseSubTypeMessageGoQuestAck(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
cwm.ObjID64 = objId
case objSubTypeMessageDuelFight:
cwm, err := parseSubTypeMessageDuelFight(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageDuelFight.")
cwm.ObjID64 = objId
err = insertMsgDuelFight(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgDuelFight")
case objSubTypeMessageUnionWar:
_, err := parseSubTypeMessageUnionWar(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.")
case objSubTypeMessageGRolesAck:
cwm, err := parseSubTypeMessageGRolesAck(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGRolesAck.")
cwm.Msg = m
clientMsgGRolesAck(cwm)
case objSubTypeMessageAuctionAnnounce:
cwm, err := parseSubTypeMessageAuctionAnnounce(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageAuctionAnnounce.")
cwm.ObjID64 = objId
err = insertMsgAuctionAnnounce(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce")
if cwm.End.After(time.Now().UTC()) || strings.Compare(cwm.Status, `#active`) == 0 {
p := JobPayloadMsgRefresh{
ObjID64: m.ObjID64,
}
b, _ := json.Marshal(&p)
_, err = createJob(objSubTypeJobMsgRefresh, objJobPriority, m.UserID64, cwm.End.Add(5*time.Minute).UTC(), b)
}
case objSubTypeMessageTimeAck:
_, 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}"))
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : fromChatWarsDate")
default:
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)
}
}
}
}
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func SQLJobWorker() {
//log.Printf("SQLJobWorker : Starting.")
for true {
jobs, err := loadCurrentJobs()
logOnError(err, "SQLJobWorker : loadCurrentJobs")
/*
if len(jobs) > 0 {
log.Printf("SQLJobWorker : %d jobs.\n", len(jobs))
}
*/
for _, j := range jobs {
JobQueue <- j
}
if len(jobs) < SQLJobSliceSize {
time.Sleep(100 * time.Millisecond)
}
}
log.Printf("SQLJobWorker : Closing.")
}
func JobWorker(id int, jobs <-chan Job) {
//log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Starting.")
for j := range jobs {
//log.Printf("JobWorker[%d] : Starting job %d.\n", id, j.ID64)
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)
default:
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
}
}
log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
//log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
for c := range cmds {
//j, err := json.Marshal(c)
//logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
//log.Printf("TGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
opt := tb.SendOptions{}
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
}
switch c.Type {
case commandSendMsg:
if c.ToChatID64 != 0 {
ch := tb.Chat{
ID: c.ToChatID64,
}
_, err := b.Send(&ch, c.Text, &opt)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg Chat")
} else if c.ToUserID64 != 0 {
u := tb.User{
ID: int(c.ToUserID64),
}
_, err := b.Send(&u, c.Text, &opt)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg User")
}
case commandReplyMsg:
ch := tb.Chat{
ID: c.FromChatID64,
}
m := tb.Message{
ID: int(c.FromMsgID64),
Chat: &ch,
}
_, err := b.Reply(&m, c.Text, &opt)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : ReplyMsg")
case commandSendDocument:
if c.ToChatID64 != 0 {
ch := tb.Chat{
ID: c.ToChatID64,
}
d := c.Document
_, err := b.Send(&ch, &d)
//_, err := c.Document.Send(b, ch, nil)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
} else if c.ToUserID64 != 0 {
u := tb.User{
ID: int(c.ToUserID64),
}
d := c.Document
_, err := b.Send(&u, &d)
//_, err := c.Document.Send(b, &ch, nil)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
}
default:
}
}
log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
for c := range cmds {
if _, ok := clientsKeepAlive.Load(c.FromUserID64); ok {
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))
err = clientsQueue[c.FromUserID64].Channel.Publish(
"", // exchange
clientsQueue[c.FromUserID64].Queue.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(j),
})
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Publishing message.")
} else {
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : client %d offline.\n", c.FromUserID64)
}
}
log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func MQKeepAliveWorker() {
//log.Printf("MQKeepAliveWorker : Starting.")
for true {
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Path)
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
if err != nil {
conn.Close()
time.Sleep(15 * time.Second)
} else {
ch, err := conn.Channel()
logOnError(err, "MQKeepAliveWorker : Failed to open a channel")
if err != nil {
ch.Close()
conn.Close()
time.Sleep(15 * time.Second)
} else {
q, err := ch.QueueDeclare(
"keepalive", // 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 {
ch.Close()
conn.Close()
time.Sleep(15 * time.Second)
} else {
m, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
logOnError(err, "MQKeepAliveWorker : Failed to register a consumer")
if err != nil {
ch.Close()
conn.Close()
time.Sleep(15 * time.Second)
} else {
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(10 * time.Second).Before(time.Now()) {
// outdated keep-alive coming from client
} else if v, ok := clientsKeepAlive.Load(x.UserID64); ok {
k := v.(*MQKeepAlive)
k.Date = x.Date
} else {
clt := MQClient{}
clt.Connection, err = amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + x.Queue)
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
clt.Channel, err = clt.Connection.Channel()
logOnError(err, "MQKeepAliveWorker : Failed to open a channel")
clt.Queue, err = clt.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")
clientsKeepAlive.Store(x.UserID64, &x)
clientsQueue[x.UserID64] = &clt
c := TGCommand{
Type: commandSendMsg,
ToUserID64: x.UserID64,
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
clientSendCWMsg(x.UserID64, `🏅Me`)
/*
c = TGCommand{
Type: commandSendMsg,
FromUserID64: x.UserID64,
ToChatID64: userID64ChtWrsBot,
Text: `/hero`,
}
MQTGCmdQueue <- c
*/
}
}
}
ch.Close()
conn.Close()
}
}
}
}
}
log.Printf("MQKeepAliveWorker : Closing.")
}
func MQTidyKeepAliveWorker() {
//log.Printf("MQTidyKeepAliveWorker : Starting.")
for true {
t := time.Now()
clientsKeepAlive.Range(clientKeepAlive)
time.Sleep(time.Until(t.Add(time.Second)))
}
log.Printf("MQTidyKeepAliveWorker : Closing.")
}