improve orderbook ack handling

This commit is contained in:
shoopea 2019-08-21 14:37:53 +08:00
parent 2defac9ecf
commit 7cd5225a38
3 changed files with 67 additions and 23 deletions

14
def.go
View File

@ -110,6 +110,20 @@ type ChatWarsMessageReportAck struct {
ObjID64 int64 `json:"obj_id"` ObjID64 int64 `json:"obj_id"`
} }
type ChatWarsMessageOrderbookAck struct {
Msg *ChatWarsMessage `json:"msg"`
Name string `json:"name"`
Code string `json:"code"`
Qty1 int64 `json:"qty1"`
Price1 int64 `json:"price1"`
Qty2 int64 `json:"qty2"`
Price2 int64 `json:"price2"`
Qty3 int64 `json:"qty3"`
Price3 int64 `json:"price3"`
Qty int64 `json:"qty"`
Gold int64 `json:"gold"`
}
type ChatWarsMessageMeAck struct { type ChatWarsMessageMeAck struct {
Msg *ChatWarsMessage `json:"msg"` Msg *ChatWarsMessage `json:"msg"`
CWUserID64 int64 `json:"user_id"` CWUserID64 int64 `json:"user_id"`

32
msg.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"regexp" "regexp"
@ -9,6 +10,18 @@ import (
"time" "time"
) )
func getMsgParsingRule(m *ChatWarsMessage) (*MessageParsingRule, error) {
var i int
for i = 0; i < len(msgParsingRules) && msgParsingRules[i].re.FindStringSubmatch(m.Text) == nil; i++ {
}
if i == len(msgParsingRules) {
return nil, errors.New("Can't find message type in rules.")
} else {
return msgParsingRules[i], nil
}
}
func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageReportAck, error) { func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageReportAck, error) {
cwm := ChatWarsMessageReportAck{} cwm := ChatWarsMessageReportAck{}
/* /*
@ -29,6 +42,22 @@ func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWa
return &cwm, nil return &cwm, nil
} }
func parseSubTypeMessageOrderbookAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageOrderbookAck, error) {
cwm := ChatWarsMessageOrderbookAck{}
log.Printf("parseSubTypeMessageOrderbookAck : Orderbook identified\n")
log.Printf("parseSubTypeMessageOrderbookAck : Name : %s\n", r.ReplaceAllString(m.Text, "${Name}"))
log.Printf("parseSubTypeMessageOrderbookAck : Code : %s\n", r.ReplaceAllString(m.Text, "${Code}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty : %s\n", r.ReplaceAllString(m.Text, "${Qty}"))
log.Printf("parseSubTypeMessageOrderbookAck : Gold : %s\n", r.ReplaceAllString(m.Text, "${Gold}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty1 : %s\n", r.ReplaceAllString(m.Text, "${Qty1}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price1 : %s\n", r.ReplaceAllString(m.Text, "${Price1}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty2 : %s\n", r.ReplaceAllString(m.Text, "${Qty2}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price2 : %s\n", r.ReplaceAllString(m.Text, "${Price2}"))
log.Printf("parseSubTypeMessageOrderbookAck : Qty3 : %s\n", r.ReplaceAllString(m.Text, "${Qty3}"))
log.Printf("parseSubTypeMessageOrderbookAck : Price3 : %s\n", r.ReplaceAllString(m.Text, "${Price3}"))
return &cwm, nil
}
func parseSubTypeMessageAuctionAnnounce(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageAuctionAnnounce, error) { func parseSubTypeMessageAuctionAnnounce(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageAuctionAnnounce, error) {
cwm := ChatWarsMessageAuctionAnnounce{} cwm := ChatWarsMessageAuctionAnnounce{}
@ -104,6 +133,7 @@ func parseSubTypeMessageMeAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMe
log.Printf("parseSubTypeMessageReport : BagUsed : %s\n", r.ReplaceAllString(m.Text, "${BagUsed}")) log.Printf("parseSubTypeMessageReport : BagUsed : %s\n", r.ReplaceAllString(m.Text, "${BagUsed}"))
log.Printf("parseSubTypeMessageReport : BagDispo : %s\n", r.ReplaceAllString(m.Text, "${BagDispo}")) log.Printf("parseSubTypeMessageReport : BagDispo : %s\n", r.ReplaceAllString(m.Text, "${BagDispo}"))
*/ */
cwm.Msg = m
return &cwm, nil return &cwm, nil
} }
@ -148,6 +178,7 @@ func parseSubTypeMessageGRolesAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWa
log.Printf("cwm.SquireID64 : %d\n", cwm.SquireID64) log.Printf("cwm.SquireID64 : %d\n", cwm.SquireID64)
log.Printf("cwm.TreasurerID64 : %d\n", cwm.TreasurerID64) log.Printf("cwm.TreasurerID64 : %d\n", cwm.TreasurerID64)
*/ */
cwm.Msg = m
return &cwm, nil return &cwm, nil
} }
@ -171,6 +202,7 @@ func parseSubTypeMessageGoQuestAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatW
} else { } else {
cwm.Duration = 0 * time.Minute cwm.Duration = 0 * time.Minute
} }
cwm.Msg = m
return &cwm, nil return &cwm, nil
} }

View File

@ -331,16 +331,13 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving message.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving message.")
if err == nil { if err == nil {
//log.Printf("SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Message retrieved (%d)\n%s\n", objId, m.Text) //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++ { rule, err := getMsgParsingRule(m)
if err != nil {
} logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : msgRegex.")
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 { } else {
r := msgParsingRules[i].re err = setObjSubTypeId(objId, rule.MsgTypeID)
err = setObjSubTypeId(objId, msgParsingRules[i].MsgTypeID)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : setObjSubTypeId")
switch msgParsingRules[i].MsgTypeID { switch rule.MsgTypeID {
case objSubTypeMessageGRolesReq: case objSubTypeMessageGRolesReq:
case objSubTypeMessageGo: case objSubTypeMessageGo:
case objSubTypeMessageReportReq: case objSubTypeMessageReportReq:
@ -377,20 +374,20 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
case objSubTypeMessageGStockPartReq: case objSubTypeMessageGStockPartReq:
case objSubTypeMessageGStockOthReq: case objSubTypeMessageGStockOthReq:
case objSubTypeMessageGStockAnyAck: case objSubTypeMessageGStockAnyAck:
_, err := parseSubTypeMessageGStockAnyAck(m, r) _, err := parseSubTypeMessageGStockAnyAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGStockAnyAck.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGStockAnyAck.")
case objSubTypeMessageReportAck: case objSubTypeMessageReportAck:
cwm, err := parseSubTypeMessageReportAck(m, r) cwm, err := parseSubTypeMessageReportAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageReportAck.")
cwm.ObjID64 = objId cwm.ObjID64 = objId
case objSubTypeMessageMiniWar: case objSubTypeMessageMiniWar:
cwm, err := parseSubTypeMessageMiniWar(m, r) cwm, err := parseSubTypeMessageMiniWar(m, rule.re)
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")
case objSubTypeMessagePillageInc: case objSubTypeMessagePillageInc:
cwm, err := parseSubTypeMessagePillageInc(m, r) cwm, err := parseSubTypeMessagePillageInc(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessagePillageInc.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessagePillageInc.")
cwm.ObjID64 = objId cwm.ObjID64 = objId
err = insertMsgPillageInc(cwm) err = insertMsgPillageInc(cwm)
@ -412,16 +409,14 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : createJob(JobPillage)")
} }
case objSubTypeMessageMeAck: case objSubTypeMessageMeAck:
cwm, err := parseSubTypeMessageMeAck(m, r) cwm, err := parseSubTypeMessageMeAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageMeAck.")
cwm.Msg = m
clientMsgMeAck(cwm) clientMsgMeAck(cwm)
_, err = addObjXP(cwm.CWUserID64, cwm.ExpNow, cwm.ExpLvl, cwm.Level, m.Date) _, err = addObjXP(cwm.CWUserID64, cwm.ExpNow, cwm.ExpLvl, cwm.Level, m.Date)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : addObjXP(MeAck)")
case objSubTypeMessageGoQuestAck: case objSubTypeMessageGoQuestAck:
cwm, err := parseSubTypeMessageGoQuestAck(m, r) cwm, err := parseSubTypeMessageGoQuestAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGoQuestAck.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGoQuestAck.")
cwm.Msg = m
uid, err := clientGetCWUserID64(m.TGUserID64) uid, err := clientGetCWUserID64(m.TGUserID64)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving UserID64.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Retrieving UserID64.")
if err != nil { if err != nil {
@ -431,21 +426,20 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
_, err = addObjQuest(uid, cwm.QuestTypeID, cwm.Duration, m.Date.UTC()) _, err = addObjQuest(uid, cwm.QuestTypeID, cwm.Duration, m.Date.UTC())
} }
case objSubTypeMessageDuelFight: case objSubTypeMessageDuelFight:
cwm, err := parseSubTypeMessageDuelFight(m, r) cwm, err := parseSubTypeMessageDuelFight(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageDuelFight.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageDuelFight.")
cwm.ObjID64 = objId cwm.ObjID64 = objId
err = insertMsgDuelFight(cwm) err = insertMsgDuelFight(cwm)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgDuelFight") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : insertMsgDuelFight")
case objSubTypeMessageUnionWar: case objSubTypeMessageUnionWar:
_, err := parseSubTypeMessageUnionWar(m, r) _, err := parseSubTypeMessageUnionWar(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageUnionWar.")
case objSubTypeMessageGRolesAck: case objSubTypeMessageGRolesAck:
cwm, err := parseSubTypeMessageGRolesAck(m, r) cwm, err := parseSubTypeMessageGRolesAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGRolesAck.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageGRolesAck.")
cwm.Msg = m
clientMsgGRolesAck(cwm) clientMsgGRolesAck(cwm)
case objSubTypeMessageAuctionAnnounce: case objSubTypeMessageAuctionAnnounce:
cwm, err := parseSubTypeMessageAuctionAnnounce(m, r) cwm, err := parseSubTypeMessageAuctionAnnounce(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageAuctionAnnounce.") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageAuctionAnnounce.")
cwm.ObjID64 = objId cwm.ObjID64 = objId
err = insertMsgAuctionAnnounce(cwm) err = insertMsgAuctionAnnounce(cwm)
@ -465,17 +459,21 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
case objSubTypeMessageTimeAck: 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}")) _, 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") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : fromChatWarsDate")
case objSubTypeMessageOrderbookAck:
cwm, err := parseSubTypeMessageOrderbookAck(m, rule.re)
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing objSubTypeMessageOrderbookAck.")
default: 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)+"] : Unknwon message type in rule %d : %d (%d)\n%s\n", msgParsingRules[i].ID, msgParsingRules[i].MsgTypeID, objId, m.Text)
} }
muxCallbacks.Lock() muxCallbacks.Lock()
if mc1, mok1 := callbacks[m.TGUserID64]; mok1 { if mc1, mok1 := callbacks[m.TGUserID64]; mok1 {
if mc2, mok2 := mc1[msgParsingRules[i].MsgTypeID]; mok2 { if mc2, mok2 := mc1[rule.MsgTypeID]; mok2 {
for j := range mc2 { for j := range mc2 {
err := rescheduleJob(mc2[j], m.ObjID64, time.Now().UTC()) err := rescheduleJob(mc2[j], m.ObjID64, time.Now().UTC())
logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering") logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : callbacks triggering")
} }
mc1[msgParsingRules[i].MsgTypeID] = nil mc1[rule.MsgTypeID] = nil
} }
} }
muxCallbacks.Unlock() muxCallbacks.Unlock()