This commit is contained in:
shoopea 2019-05-18 15:44:25 +08:00
parent da1d3ca910
commit bade7a4f78
4 changed files with 77 additions and 27 deletions

29
bot.go
View File

@ -21,6 +21,7 @@ func BotHandlers(b *tb.Bot) {
b.Handle("/test", botTest) b.Handle("/test", botTest)
b.Handle("/msg_rescan", botMsgRescan) b.Handle("/msg_rescan", botMsgRescan)
b.Handle("/msg_rescan_all", botMsgRescanAll) b.Handle("/msg_rescan_all", botMsgRescanAll)
b.Handle("/msg_dump", botMsgDump)
b.Handle(tb.OnPhoto, botPhoto) b.Handle(tb.OnPhoto, botPhoto)
b.Handle(tb.OnChannelPost, botChannelPost) b.Handle(tb.OnChannelPost, botChannelPost)
@ -168,3 +169,31 @@ func botMsgRescanAll(m *tb.Message) {
} }
return return
} }
func botMsgDump(m *tb.Message) {
var res string
r := regexp.MustCompile("^[0-9]+$")
if r.MatchString(m.Payload) {
objId, _ := strconv.ParseInt(m.Payload, 10, 64)
objSubTypeId, err := getObjSubTypeId(objId)
if err != nil {
res = `This is not a message reference`
} else {
cwm, err := getMsg(objId)
b, _ := json.Marshal(cwm)
res = string(b)
}
} else {
res = `/msg_dump <msg_id>`
}
c := TGCommand{
Type: commandReplyMsg,
Text: res,
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
return
}

5
def.go
View File

@ -40,6 +40,10 @@ type ChatWarsMessage struct {
Text string `json:"text"` Text string `json:"text"`
} }
type ChatWarsMessageReport struct {
ObjID64 int64 `json:"obj_id"`
}
type ChatWarsMessageAuctionAnnounce struct { type ChatWarsMessageAuctionAnnounce struct {
ObjID64 int64 `json:"obj_id"` ObjID64 int64 `json:"obj_id"`
LotID int32 `json:"lot_id"` LotID int32 `json:"lot_id"`
@ -97,6 +101,7 @@ type Job struct {
} }
type JobPayloadPillage struct { type JobPayloadPillage struct {
Date time.Time `json:"date"`
} }
type JobPayloadTribute struct { type JobPayloadTribute struct {

19
msg.go
View File

@ -7,6 +7,25 @@ import (
"time" "time"
) )
func parseSubTypeMessageReport(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageReport, error) {
cwm := ChatWarsMessageReport{}
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : War report identified\n")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Castle : %s\n", r.ReplaceAllString(m.Text, "${Castle}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Guild : %s\n", r.ReplaceAllString(m.Text, "${Guild}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : User : %s\n", r.ReplaceAllString(m.Text, "${User}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Attack : %s\n", r.ReplaceAllString(m.Text, "${Attack}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AttackMod : %s\n", r.ReplaceAllString(m.Text, "${AttackMod}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Defense : %s\n", r.ReplaceAllString(m.Text, "${Defense}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Level : %s\n", r.ReplaceAllString(m.Text, "${Level}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Exp : %s\n", r.ReplaceAllString(m.Text, "${Exp}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stock : %s\n", r.ReplaceAllString(m.Text, "${Stock}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Stamina : %s\n", r.ReplaceAllString(m.Text, "${Stamina}"))
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Crit : %s\n", r.ReplaceAllString(m.Text, "${Crit}"))
return &cwm, err
}
func parseSubTypeMessageMiniWar(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageMiniWar, error) { func parseSubTypeMessageMiniWar(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageMiniWar, error) {
t, err := time.Parse("02/01/06 15:04", r.ReplaceAllString(m.Text, "${Time}")) t, err := time.Parse("02/01/06 15:04", r.ReplaceAllString(m.Text, "${Time}"))
logOnError(err, "parseSubTypeMessageMiniWar : parsing time : "+r.ReplaceAllString(m.Text, "${Time}")) logOnError(err, "parseSubTypeMessageMiniWar : parsing time : "+r.ReplaceAllString(m.Text, "${Time}"))

View File

@ -89,27 +89,15 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
r := msgParsingRules[i].re r := msgParsingRules[i].re
switch msgParsingRules[i].MsgTypeID { switch msgParsingRules[i].MsgTypeID {
case objSubTypeMessageReport: case objSubTypeMessageReport:
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : War report identified (%d)\n", objId) cwm, err := parseSubTypeMessageReport(m, r)
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Castle : %s (%d)\n", r.ReplaceAllString(m.Text, "${Castle}"), objId) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReport.")
log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Guild : %s (%d)\n", r.ReplaceAllString(m.Text, "${Guild}"), objId) cwm.ObjID64 = 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 objSubTypeMessageMiniWar: case objSubTypeMessageMiniWar:
cwm, err := parseSubTypeMessageMiniWar(m, r) cwm, err := parseSubTypeMessageMiniWar(m, r)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMiniWar.")
cwm.ObjID64 = objId cwm.ObjID64 = objId
err = insertMsgMiniWar(cwm) err = insertMsgMiniWar(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
err = setObjSubTypeId(objId, objSubTypeMessageMiniWar) err = setObjSubTypeId(objId, objSubTypeMessageMiniWar)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MiniWar)") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(MiniWar)")
case objSubTypeMessagePillageInc: case objSubTypeMessagePillageInc:
@ -120,17 +108,24 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgMiniWar")
err = setObjSubTypeId(objId, objSubTypeMessagePillageInc) err = setObjSubTypeId(objId, objSubTypeMessagePillageInc)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageInc)") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(PillageInc)")
p := JobPayloadPillage{} if m.Date.Add(3 * time.Minute).After(time.Now()) {
b, _ := json.Marshal(&p) p := JobPayloadPillage{
err = createJob(objSubTypeJobPillage, objJobPriority, m.UserID64, time.Now().Add(time.Duration(25+rand.Intn(35))*time.Second), b) Date: m.Date,
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)") }
s := TGCommand{ b, _ := json.Marshal(&p)
Type: commandSendMsg, err = createJob(objSubTypeJobPillage, objJobPriority, m.UserID64, time.Now().Add(time.Duration(25+rand.Intn(35))*time.Second), b)
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)), logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
ToUserID64: m.UserID64, s := TGCommand{
Type: commandSendMsg,
Text: fmt.Sprintf("Catching pillage (%s)", m.Date.Format(time.RFC3339)),
ToUserID64: m.UserID64,
}
TGCmdQueue <- s
} }
TGCmdQueue <- s
case objSubTypeMessageGo: case objSubTypeMessageGo:
err = setObjSubTypeId(objId, objSubTypeMessageGo)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId(Go)")
case objSubTypeMessageAuctionAnnounce: case objSubTypeMessageAuctionAnnounce:
cwm := ChatWarsMessageAuctionAnnounce{ cwm := ChatWarsMessageAuctionAnnounce{
ObjID64: objId, ObjID64: objId,
@ -178,6 +173,8 @@ func SQLJobWorker(id int) {
jobRescan(j) jobRescan(j)
case objSubTypeJobSetJobDone: case objSubTypeJobSetJobDone:
jobSetDone(j) jobSetDone(j)
case objSubTypeJobPillage:
jobPillage(j)
default: default:
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID) log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
} }
@ -236,10 +233,10 @@ func MQTGCmdWorker(id int, cmds <-chan TGCommand) {
logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)") logOnError(err, "MQTGCmdWorker["+strconv.Itoa(id)+"] : Marshal(c)")
log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j)) log.Printf("MQTGCmdWorker["+strconv.Itoa(id)+"] : new command.\n%s\n", string(j))
err = clientsQueue[c.FromUserID64].Channel.Publish( err = clientsQueue[c.FromUserID64].Channel.Publish(
"", // exchange "", // exchange
clientsQueue[c.FromUserID64].Queue.Name, // routing key clientsQueue[c.FromUserID64].Queue.Name, // routing key
false, // mandatory false, // mandatory
false, // immediate false, // immediate
amqp.Publishing{ amqp.Publishing{
ContentType: "application/json", ContentType: "application/json",
Body: []byte(j), Body: []byte(j),