package main import ( "encoding/json" "fmt" "log" "math/rand" "strconv" "time" "github.com/streadway/amqp" tb "gopkg.in/tucnak/telebot.v2" ) func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) { //log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.") 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 := 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 := 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%s\n", objId, m.Text) } else { r := msgParsingRules[i].re switch msgParsingRules[i].MsgTypeID { case objSubTypeMessageReportAck: cwm, err := parseSubTypeMessageReportAck(m, r) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.") cwm.ObjID64 = objId err = setObjSubTypeId(objId, objSubTypeMessageReportAck) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(ReportAck)") 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") err = setObjSubTypeId(objId, objSubTypeMessageMiniWar) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MiniWar)") 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") err = setObjSubTypeId(objId, objSubTypeMessagePillageInc) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageInc)") // 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 objSubTypeMessageGo: err = setObjSubTypeId(objId, objSubTypeMessageGo) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(Go)") case objSubTypeMessageReportReq: err = setObjSubTypeId(objId, objSubTypeMessageReportReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(ReportReq)") case objSubTypeMessageGReportReq: err = setObjSubTypeId(objId, objSubTypeMessageGReportReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(GReportReq)") case objSubTypeMessageHeroReq: err = setObjSubTypeId(objId, objSubTypeMessageHeroReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(HeroReq)") case objSubTypeMessageMeReq: err = setObjSubTypeId(objId, objSubTypeMessageMeReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MeReq)") case objSubTypeMessageInventoryReq: err = setObjSubTypeId(objId, objSubTypeMessageInventoryReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(InventoryReq)") case objSubTypeMessageTimeReq: err = setObjSubTypeId(objId, objSubTypeMessageTimeReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(TimeReq)") case objSubTypeMessagePledge: err = setObjSubTypeId(objId, objSubTypeMessagePledge) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(Pledge)") case objSubTypeMessagePillageGo: err = setObjSubTypeId(objId, objSubTypeMessagePillageGo) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageGo)") case objSubTypeMessagePillageTimeout: err = setObjSubTypeId(objId, objSubTypeMessagePillageTimeout) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageTimeout)") case objSubTypeMessagePillageWin: err = setObjSubTypeId(objId, objSubTypeMessagePillageWin) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageWin)") case objSubTypeMessagePillageLoss: err = setObjSubTypeId(objId, objSubTypeMessagePillageLoss) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageLoss)") 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)+"] : insertMsgAuctionAnnounce") err = setObjSubTypeId(objId, objSubTypeMessageDuelFight) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(DuelFight)") case objSubTypeMessageGoQuest: err = setObjSubTypeId(objId, objSubTypeMessageGoQuest) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(GoQuest)") case objSubTypeMessageGoFastFight: err = setObjSubTypeId(objId, objSubTypeMessageGoFastFight) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(GoFastFight)") case objSubTypeMessageGoArena: err = setObjSubTypeId(objId, objSubTypeMessageGoArena) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(GoArena)") case objSubTypeMessageTop: err = setObjSubTypeId(objId, objSubTypeMessageTop) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(Top)") case objSubTypeMessageMenu: err = setObjSubTypeId(objId, objSubTypeMessageMenu) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(Menu)") case objSubTypeMessageBuyReq: err = setObjSubTypeId(objId, objSubTypeMessageBuyReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(BuyReq)") case objSubTypeMessageSellReq: err = setObjSubTypeId(objId, objSubTypeMessageSellReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(SellReq)") case objSubTypeMessageOrderbookReq: err = setObjSubTypeId(objId, objSubTypeMessageOrderbookReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(OrderbookReq)") case objSubTypeMessageOrderbookAck: err = setObjSubTypeId(objId, objSubTypeMessageSellReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(SellReq)") case objSubTypeMessageWithdrawReq: err = setObjSubTypeId(objId, objSubTypeMessageWithdrawReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(WithdrawReq)") case objSubTypeMessageWithdrawCode: err = setObjSubTypeId(objId, objSubTypeMessageWithdrawCode) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(WithdrawCode)") case objSubTypeMessageWithdrawRcv: err = setObjSubTypeId(objId, objSubTypeMessageWithdrawRcv) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(WithdrawRcv)") case objSubTypeMessageDepositReq: err = setObjSubTypeId(objId, objSubTypeMessageDepositReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(DepositReq)") case objSubTypeMessageDepositAck: err = setObjSubTypeId(objId, objSubTypeMessageDepositAck) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(DepositAck)") case objSubTypeMessageStockReq: err = setObjSubTypeId(objId, objSubTypeMessageStockReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(StockReq)") case objSubTypeMessageMiscReq: err = setObjSubTypeId(objId, objSubTypeMessageMiscReq) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MiscReq)") case objSubTypeMessageUnionWar: _, err := parseSubTypeMessageUnionWar(m, r) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.") err = setObjSubTypeId(objId, objSubTypeMessageUnionWar) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(UnionWar)") case objSubTypeMessageAuctionAnnounce: cwm := ChatWarsMessageAuctionAnnounce{ ObjID64: objId, } l, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Lot}"), 10, 32) cwm.LotID = int32(l) cwm.Item = r.ReplaceAllString(m.Text, "${Item}") 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}") p, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Price}"), 10, 32) cwm.Price = int32(p) cwm.Status = r.ReplaceAllString(m.Text, "${Status}") cwm.End, _ = fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}")) err = insertMsgAuctionAnnounce(&cwm) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce") err = setObjSubTypeId(objId, objSubTypeMessageAuctionAnnounce) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(AuctionAnnounce)") 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") err = setObjSubTypeId(objId, objSubTypeMessageTimeAck) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(objSubTypeMessageTimeAck)") 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(id int) { //log.Printf("SQLJobWorker[" + strconv.Itoa(id) + "] : Starting.") for true { jobs, err := loadCurrentJobs(id) logOnError(err, "SQLJobWorker["+strconv.Itoa(id)+"] : loadCurrentJobs") if len(jobs) > 0 { log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : %d jobs.\n", len(jobs)) } for _, j := range jobs { switch j.JobTypeID { case objSubTypeJobRescanMsg: jobRescan(j) case objSubTypeJobSetJobDone: jobSetDone(j) case objSubTypeJobPillage: jobPillage(j) case objSubTypeJobMsgClient: jobMsgClient(j) default: log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID) } } if len(jobs) < SQLJobSliceSize { time.Sleep(100 * time.Millisecond) } } log.Printf("SQLJobWorker[" + 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)) switch c.Type { case commandReplyMsg: ch := tb.Chat{ ID: c.FromChatID64, } m := tb.Message{ ID: int(c.FromMsgID64), Chat: &ch, } _, err := b.Reply(&m, c.Text) logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Reply") case commandSendMsg: if c.ToChatID64 != 0 { ch := tb.Chat{ ID: c.ToChatID64, } _, err := b.Send(&ch, c.Text) logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Send") } else if c.ToUserID64 != 0 { ch := tb.Chat{ ID: c.ToUserID64, } _, err := b.Send(&ch, c.Text) logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Send") } 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[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.") } } log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Closing.") } 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()) { // outdated keep-alive } else if _, ok := clientsKeepAlive[x.UserID64]; ok { clientsKeepAlive[x.UserID64].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[x.UserID64] = &x clientsQueue[x.UserID64] = &clt c := TGCommand{ Type: commandSendMsg, ToUserID64: x.UserID64, Text: "You are connected.", } TGCmdQueue <- c c = TGCommand{ Type: commandSendMsg, ToUserID64: cfg.Bot.Admin, Text: fmt.Sprintf("%s is connected.", x.Nickname), } TGCmdQueue <- c c = TGCommand{ Type: commandSendMsg, FromUserID64: x.UserID64, ToChatID64: userID64ChtWrsBot, Text: `🏅Me`, } MQTGCmdQueue <- c c = TGCommand{ Type: commandSendMsg, FromUserID64: x.UserID64, ToChatID64: userID64ChtWrsBot, Text: `/hero`, } MQTGCmdQueue <- c } } } log.Printf("MQKeepAliveWorker : Closing.") } func MQTidyKeepAliveWorker() { //log.Printf("MQTidyKeepAliveWorker : Starting.") for true { t := time.Now() for _, v := range clientsKeepAlive { if v.Date.Add(90 * time.Second).Before(time.Now()) { msgs, err := clientsQueue[v.UserID64].Channel.QueuePurge(clientsQueue[v.UserID64].Queue.Name, false) logOnError(err, "MQTidyKeepAliveWorker : Channel.QueuePurge()") err = clientsQueue[v.UserID64].Channel.Close() logOnError(err, "MQTidyKeepAliveWorker : Channel.Close()") err = clientsQueue[v.UserID64].Connection.Close() logOnError(err, "MQTidyKeepAliveWorker : Connection.Close()") c := TGCommand{ Type: commandSendMsg, ToUserID64: v.UserID64, Text: "Timeout, purging and closing command queue.", } TGCmdQueue <- c c = TGCommand{ Type: commandSendMsg, ToUserID64: cfg.Bot.Admin, Text: fmt.Sprintf("Client %s timed out (%d messages purged).", v.Nickname, msgs), } TGCmdQueue <- c delete(clientsKeepAlive, v.UserID64) } } time.Sleep(time.Until(t.Add(time.Second))) } log.Printf("MQTidyKeepAliveWorker : Closing.") }