2019-05-05 13:29:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-06 04:54:24 +02:00
|
|
|
"encoding/json"
|
2019-05-16 14:41:21 +02:00
|
|
|
"fmt"
|
2019-05-05 13:29:28 +02:00
|
|
|
"log"
|
2019-05-16 14:41:21 +02:00
|
|
|
"math/rand"
|
2019-05-05 13:29:28 +02:00
|
|
|
"strconv"
|
2019-06-10 11:14:03 +02:00
|
|
|
"strings"
|
2019-05-08 12:17:07 +02:00
|
|
|
"time"
|
2019-05-06 04:54:24 +02:00
|
|
|
|
2019-06-28 09:56:58 +02:00
|
|
|
"github.com/streadway/amqp"
|
2019-05-16 04:21:36 +02:00
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2019-05-05 13:29:28 +02:00
|
|
|
)
|
|
|
|
|
2019-05-06 04:54:24 +02:00
|
|
|
func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
|
2019-05-25 13:27:08 +02:00
|
|
|
//log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
2019-06-28 09:54:39 +02:00
|
|
|
var x ChatWarsMessage
|
2019-06-25 11:37:28 +02:00
|
|
|
for true {
|
2019-06-28 09:59:58 +02:00
|
|
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Path)
|
2019-06-28 09:57:06 +02:00
|
|
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
} else {
|
|
|
|
ch, err := conn.Channel()
|
|
|
|
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel")
|
|
|
|
if err != nil {
|
|
|
|
ch.Close()
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
} else {
|
|
|
|
q, err := ch.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 {
|
|
|
|
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, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
|
|
|
|
if err != nil {
|
|
|
|
ch.Close()
|
|
|
|
conn.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ch.Close()
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 09:56:29 +02:00
|
|
|
|
2019-06-25 11:37:28 +02:00
|
|
|
}
|
2019-06-28 09:54:42 +02:00
|
|
|
}
|
2019-05-06 04:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("MQGetMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage, objIds chan<- int64) {
|
2019-05-25 13:27:08 +02:00
|
|
|
//log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
2019-05-05 13:29:28 +02:00
|
|
|
for m := range msgs {
|
2019-05-25 09:25:11 +02:00
|
|
|
objId, err := addObjMsg(m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
|
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-25 13:27:08 +02:00
|
|
|
//log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
|
2019-05-05 13:29:28 +02:00
|
|
|
for objId := range objIds {
|
2019-06-09 14:17:05 +02:00
|
|
|
m, err := getObjMsg(objId)
|
2019-05-06 04:54:24 +02:00
|
|
|
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-13 04:27:03 +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-06-02 08:48:16 +02:00
|
|
|
err = setObjSubTypeId(objId, msgParsingRules[i].MsgTypeID)
|
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId")
|
2019-05-08 12:31:52 +02:00
|
|
|
switch msgParsingRules[i].MsgTypeID {
|
2019-06-02 11:30:04 +02:00
|
|
|
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:
|
2019-06-02 11:34:09 +02:00
|
|
|
_, err := parseSubTypeMessageGStockAnyAck(m, r)
|
2019-06-02 11:30:04 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGStockAnyAck.")
|
2019-05-19 06:19:24 +02:00
|
|
|
case objSubTypeMessageReportAck:
|
2019-05-19 06:20:20 +02:00
|
|
|
cwm, err := parseSubTypeMessageReportAck(m, r)
|
2019-05-19 06:19:24 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.")
|
2019-05-18 09:44:25 +02:00
|
|
|
cwm.ObjID64 = objId
|
2019-05-11 12:40:58 +02:00
|
|
|
case objSubTypeMessageMiniWar:
|
2019-05-13 08:12:31 +02:00
|
|
|
cwm, err := parseSubTypeMessageMiniWar(m, r)
|
2019-05-13 08:18:26 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.")
|
2019-05-14 04:28:26 +02:00
|
|
|
cwm.ObjID64 = objId
|
2019-05-14 04:29:47 +02:00
|
|
|
err = insertMsgMiniWar(cwm)
|
2019-05-14 04:28:26 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
|
2019-05-15 10:58:25 +02:00
|
|
|
case objSubTypeMessagePillageInc:
|
2019-05-16 14:39:12 +02:00
|
|
|
cwm, err := parseSubTypeMessagePillageInc(m, r)
|
2019-05-18 16:15:24 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessagePillageInc.")
|
2019-05-16 14:39:12 +02:00
|
|
|
cwm.ObjID64 = objId
|
|
|
|
err = insertMsgPillageInc(cwm)
|
2019-05-18 10:33:58 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgPillageInc")
|
2019-05-27 06:03:04 +02:00
|
|
|
// only catch live pillages
|
2019-05-18 12:59:43 +02:00
|
|
|
if m.Date.Add(3 * time.Minute).After(time.Now()) {
|
2019-05-18 12:38:24 +02:00
|
|
|
s := TGCommand{
|
|
|
|
Type: commandSendMsg,
|
|
|
|
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)),
|
|
|
|
ToUserID64: m.UserID64,
|
|
|
|
}
|
|
|
|
TGCmdQueue <- s
|
2019-05-27 06:03:04 +02:00
|
|
|
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)")
|
2019-05-18 12:59:43 +02:00
|
|
|
}
|
2019-05-27 12:56:03 +02:00
|
|
|
case objSubTypeMessageMeAck:
|
2019-05-28 06:14:46 +02:00
|
|
|
cwm, err := parseSubTypeMessageMeAck(m, r)
|
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
|
2019-05-30 08:08:39 +02:00
|
|
|
cwm.Msg = m
|
2019-05-30 07:56:57 +02:00
|
|
|
clientMsgMeAck(cwm)
|
2019-05-31 04:05:58 +02:00
|
|
|
_, err = addObjXP(cwm.UserID64, cwm.ExpNow, cwm.ExpLvl, cwm.Level, m.Date)
|
2019-05-31 04:02:21 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)")
|
2019-05-28 09:00:37 +02:00
|
|
|
case objSubTypeMessageGoQuestAck:
|
|
|
|
cwm, err := parseSubTypeMessageGoQuestAck(m, r)
|
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
|
|
|
|
cwm.ObjID64 = objId
|
2019-05-18 13:15:50 +02:00
|
|
|
case objSubTypeMessageDuelFight:
|
2019-05-18 16:17:22 +02:00
|
|
|
cwm, err := parseSubTypeMessageDuelFight(m, r)
|
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageDuelFight.")
|
|
|
|
cwm.ObjID64 = objId
|
2019-05-19 14:02:06 +02:00
|
|
|
err = insertMsgDuelFight(cwm)
|
2019-06-06 07:52:22 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgDuelFight")
|
2019-05-25 11:57:10 +02:00
|
|
|
case objSubTypeMessageUnionWar:
|
2019-05-25 13:13:38 +02:00
|
|
|
_, err := parseSubTypeMessageUnionWar(m, r)
|
2019-05-25 13:12:31 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.")
|
2019-05-30 10:36:40 +02:00
|
|
|
case objSubTypeMessageGRolesAck:
|
|
|
|
cwm, err := parseSubTypeMessageGRolesAck(m, r)
|
2019-06-02 11:34:09 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGRolesAck.")
|
2019-05-30 10:36:40 +02:00
|
|
|
cwm.Msg = m
|
2019-05-30 12:57:00 +02:00
|
|
|
clientMsgGRolesAck(cwm)
|
2019-05-08 16:34:47 +02:00
|
|
|
case objSubTypeMessageAuctionAnnounce:
|
2019-06-06 07:52:22 +02:00
|
|
|
cwm, err := parseSubTypeMessageAuctionAnnounce(m, r)
|
2019-06-06 07:56:28 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageAuctionAnnounce.")
|
2019-06-06 08:18:42 +02:00
|
|
|
cwm.ObjID64 = objId
|
2019-06-06 07:55:22 +02:00
|
|
|
err = insertMsgAuctionAnnounce(cwm)
|
2019-05-14 04:28:26 +02:00
|
|
|
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgAuctionAnnounce")
|
2019-06-11 08:36:08 +02:00
|
|
|
if cwm.End.After(time.Now().UTC()) || strings.Compare(cwm.Status, `#active`) == 0 {
|
2019-06-08 17:28:07 +02:00
|
|
|
p := JobPayloadMsgRefresh{
|
|
|
|
ObjID64: m.ObjID64,
|
|
|
|
}
|
|
|
|
b, _ := json.Marshal(&p)
|
2019-06-11 08:36:08 +02:00
|
|
|
_, err = createJob(objSubTypeJobMsgRefresh, objJobPriority, m.UserID64, cwm.End.Add(5*time.Minute).UTC(), b)
|
2019-06-08 17:28:07 +02:00
|
|
|
}
|
2019-05-11 08:24:38 +02:00
|
|
|
case objSubTypeMessageTimeAck:
|
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-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
|
|
|
|
2019-05-31 14:24:27 +02:00
|
|
|
func SQLJobWorker() {
|
|
|
|
//log.Printf("SQLJobWorker : Starting.")
|
2019-05-07 13:15:25 +02:00
|
|
|
for true {
|
2019-05-31 14:24:27 +02:00
|
|
|
jobs, err := loadCurrentJobs()
|
|
|
|
logOnError(err, "SQLJobWorker : loadCurrentJobs")
|
2019-06-11 08:03:58 +02:00
|
|
|
/*
|
|
|
|
if len(jobs) > 0 {
|
|
|
|
log.Printf("SQLJobWorker : %d jobs.\n", len(jobs))
|
|
|
|
}
|
|
|
|
*/
|
2019-05-11 06:54:12 +02:00
|
|
|
for _, j := range jobs {
|
2019-05-31 14:24:27 +02:00
|
|
|
JobQueue <- j
|
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
|
|
|
}
|
2019-05-31 14:24:27 +02:00
|
|
|
log.Printf("SQLJobWorker : Closing.")
|
|
|
|
}
|
|
|
|
|
2019-05-31 14:30:08 +02:00
|
|
|
func JobWorker(id int, jobs <-chan Job) {
|
2019-05-31 14:24:27 +02:00
|
|
|
//log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Starting.")
|
|
|
|
for j := range jobs {
|
2019-06-11 08:22:04 +02:00
|
|
|
//log.Printf("JobWorker[%d] : Starting job %d.\n", id, j.ID64)
|
2019-05-31 14:24:27 +02:00
|
|
|
switch j.JobTypeID {
|
|
|
|
case objSubTypeJobRescanMsg:
|
|
|
|
jobRescan(j)
|
|
|
|
case objSubTypeJobSetJobDone:
|
|
|
|
jobSetDone(j)
|
|
|
|
case objSubTypeJobPillage:
|
|
|
|
jobPillage(j)
|
|
|
|
case objSubTypeJobMsgClient:
|
|
|
|
jobMsgClient(j)
|
2019-06-10 06:03:47 +02:00
|
|
|
case objSubTypeJobMsgRefresh:
|
|
|
|
jobMsgRefresh(j)
|
2019-06-11 03:59:20 +02:00
|
|
|
case objSubTypeJobBackupExport:
|
|
|
|
jobBackupExport(j)
|
|
|
|
case objSubTypeJobBackupImport:
|
|
|
|
jobBackupImport(j)
|
|
|
|
|
2019-05-31 14:24:27 +02:00
|
|
|
default:
|
|
|
|
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("jobWorker[" + strconv.Itoa(id) + "] : Closing.")
|
2019-05-07 13:15:25 +02:00
|
|
|
}
|
2019-05-11 05:15:42 +02:00
|
|
|
|
2019-05-16 04:16:35 +02:00
|
|
|
func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
|
2019-05-25 13:27:08 +02:00
|
|
|
//log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
|
2019-05-16 04:16:35 +02:00
|
|
|
for c := range cmds {
|
2019-06-11 06:03:24 +02:00
|
|
|
//j, err := json.Marshal(c)
|
|
|
|
//logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
|
2019-06-11 06:02:54 +02:00
|
|
|
//log.Printf("TGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
|
2019-06-10 11:14:03 +02:00
|
|
|
opt := tb.SendOptions{}
|
2019-06-10 11:05:59 +02:00
|
|
|
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
|
|
|
|
}
|
2019-05-16 13:34:26 +02:00
|
|
|
switch c.Type {
|
2019-05-17 09:20:50 +02:00
|
|
|
case commandSendMsg:
|
|
|
|
if c.ToChatID64 != 0 {
|
|
|
|
ch := tb.Chat{
|
|
|
|
ID: c.ToChatID64,
|
|
|
|
}
|
2019-06-10 11:20:20 +02:00
|
|
|
_, err := b.Send(&ch, c.Text, &opt)
|
2019-06-08 17:41:50 +02:00
|
|
|
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg Chat")
|
2019-05-17 09:20:50 +02:00
|
|
|
} else if c.ToUserID64 != 0 {
|
2019-06-09 10:02:10 +02:00
|
|
|
u := tb.User{
|
|
|
|
ID: int(c.ToUserID64),
|
2019-05-17 09:20:50 +02:00
|
|
|
}
|
2019-06-10 11:20:20 +02:00
|
|
|
_, err := b.Send(&u, c.Text, &opt)
|
2019-06-08 17:41:50 +02:00
|
|
|
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg User")
|
2019-05-17 09:20:50 +02:00
|
|
|
}
|
2019-06-09 09:34:08 +02:00
|
|
|
case commandReplyMsg:
|
|
|
|
ch := tb.Chat{
|
|
|
|
ID: c.FromChatID64,
|
|
|
|
}
|
|
|
|
m := tb.Message{
|
|
|
|
ID: int(c.FromMsgID64),
|
|
|
|
Chat: &ch,
|
|
|
|
}
|
2019-06-10 11:20:20 +02:00
|
|
|
_, err := b.Reply(&m, c.Text, &opt)
|
2019-06-09 09:34:08 +02:00
|
|
|
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : ReplyMsg")
|
2019-06-09 08:50:04 +02:00
|
|
|
case commandSendDocument:
|
|
|
|
if c.ToChatID64 != 0 {
|
|
|
|
ch := tb.Chat{
|
2019-06-09 09:58:08 +02:00
|
|
|
ID: c.ToChatID64,
|
2019-06-09 08:50:04 +02:00
|
|
|
}
|
2019-06-09 09:46:12 +02:00
|
|
|
d := c.Document
|
|
|
|
_, err := b.Send(&ch, &d)
|
2019-06-09 09:35:01 +02:00
|
|
|
//_, err := c.Document.Send(b, ch, nil)
|
2019-06-09 08:50:04 +02:00
|
|
|
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
|
|
|
|
} else if c.ToUserID64 != 0 {
|
2019-06-09 09:58:08 +02:00
|
|
|
u := tb.User{
|
2019-06-09 10:02:10 +02:00
|
|
|
ID: int(c.ToUserID64),
|
2019-06-09 08:50:04 +02:00
|
|
|
}
|
2019-06-09 09:46:12 +02:00
|
|
|
d := c.Document
|
2019-06-09 09:58:08 +02:00
|
|
|
_, err := b.Send(&u, &d)
|
2019-06-09 09:35:01 +02:00
|
|
|
//_, err := c.Document.Send(b, &ch, nil)
|
2019-06-09 08:50:04 +02:00
|
|
|
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
|
|
|
|
}
|
2019-05-16 13:34:26 +02:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-16 04:16:35 +02:00
|
|
|
}
|
|
|
|
log.Printf("TGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
}
|
2019-05-16 14:39:12 +02:00
|
|
|
|
2019-05-16 14:41:21 +02:00
|
|
|
func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
|
2019-05-25 13:27:08 +02:00
|
|
|
//log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Starting.")
|
2019-05-16 14:39:12 +02:00
|
|
|
for c := range cmds {
|
2019-06-28 09:57:06 +02:00
|
|
|
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),
|
|
|
|
})
|
2019-05-17 10:30:01 +02:00
|
|
|
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Publishing message.")
|
2019-06-11 05:37:06 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : client %d offline.\n", c.FromUserID64)
|
2019-05-17 10:30:01 +02:00
|
|
|
}
|
2019-05-16 14:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("MQTGCmdWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
|
|
|
|
}
|
2019-05-17 09:02:35 +02:00
|
|
|
|
2019-06-28 09:55:29 +02:00
|
|
|
func MQKeepAliveWorker() {
|
|
|
|
//log.Printf("MQKeepAliveWorker : Starting.")
|
|
|
|
for true {
|
2019-06-28 09:59:58 +02:00
|
|
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Path)
|
2019-06-28 09:57:06 +02:00
|
|
|
logOnError(err, "MQKeepAliveWorker : Failed to connect to RabbitMQ")
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2019-06-28 09:55:29 +02:00
|
|
|
time.Sleep(15 * time.Second)
|
2019-06-28 09:57:06 +02:00
|
|
|
} 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)
|
2019-06-28 09:55:29 +02:00
|
|
|
} else {
|
2019-06-28 09:57:06 +02:00
|
|
|
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
|
2019-06-28 09:55:29 +02:00
|
|
|
|
2019-06-28 09:57:06 +02:00
|
|
|
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
|
2019-06-28 09:55:29 +02:00
|
|
|
|
2019-06-28 09:57:06 +02:00
|
|
|
clientSendCWMsg(x.UserID64, `🏅Me`)
|
|
|
|
/*
|
|
|
|
c = TGCommand{
|
|
|
|
Type: commandSendMsg,
|
|
|
|
FromUserID64: x.UserID64,
|
|
|
|
ToChatID64: userID64ChtWrsBot,
|
|
|
|
Text: `/hero`,
|
|
|
|
}
|
|
|
|
MQTGCmdQueue <- c
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 09:55:29 +02:00
|
|
|
}
|
2019-06-28 09:57:06 +02:00
|
|
|
ch.Close()
|
|
|
|
conn.Close()
|
|
|
|
}
|
2019-06-28 09:55:29 +02:00
|
|
|
}
|
2019-06-28 09:57:06 +02:00
|
|
|
|
2019-06-28 09:55:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("MQKeepAliveWorker : Closing.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:02:35 +02:00
|
|
|
func MQTidyKeepAliveWorker() {
|
2019-05-25 13:27:08 +02:00
|
|
|
//log.Printf("MQTidyKeepAliveWorker : Starting.")
|
2019-05-17 09:02:35 +02:00
|
|
|
for true {
|
|
|
|
t := time.Now()
|
2019-06-14 05:24:43 +02:00
|
|
|
clientsKeepAlive.Range(clientKeepAlive)
|
2019-05-17 09:07:47 +02:00
|
|
|
time.Sleep(time.Until(t.Add(time.Second)))
|
2019-05-17 09:02:35 +02:00
|
|
|
}
|
|
|
|
log.Printf("MQTidyKeepAliveWorker : Closing.")
|
|
|
|
}
|