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"`
}
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 {
Msg *ChatWarsMessage `json:"msg"`
CWUserID64 int64 `json:"user_id"`

32
msg.go
View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"log"
"regexp"
@ -9,6 +10,18 @@ import (
"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) {
cwm := ChatWarsMessageReportAck{}
/*
@ -29,6 +42,22 @@ func parseSubTypeMessageReportAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWa
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) {
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 : BagDispo : %s\n", r.ReplaceAllString(m.Text, "${BagDispo}"))
*/
cwm.Msg = m
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.TreasurerID64 : %d\n", cwm.TreasurerID64)
*/
cwm.Msg = m
return &cwm, nil
}
@ -171,6 +202,7 @@ func parseSubTypeMessageGoQuestAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatW
} else {
cwm.Duration = 0 * time.Minute
}
cwm.Msg = m
return &cwm, nil
}

View File

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