From 574834b2b91628a661dbfffa3749cd2d23764db6 Mon Sep 17 00:00:00 2001 From: shoopea Date: Fri, 30 Aug 2019 10:14:30 +0800 Subject: [PATCH] update g deposit --- bot.go | 49 +++++++++++++++++++++++++------------------------ def.go | 5 ----- job.go | 11 +++++++---- main.go | 3 +++ msg.go | 30 ++++++++++++++++++++++++++++++ workers.go | 2 -- 6 files changed, 65 insertions(+), 35 deletions(-) diff --git a/bot.go b/bot.go index 7cb0019..4d84a10 100644 --- a/bot.go +++ b/bot.go @@ -639,31 +639,32 @@ func botGDepositAll(m *tb.Message) { } p.ResObjID64 = append(p.ResObjID64, getObjItemID(`01`, `Thread`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`03`, `Pelt`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`04`, `Bone`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`07`, `Powder`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`08`, `Iron Ore`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`09`, `Cloth`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`10`, `Silver Ore`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`11`, `Bauxite`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`13`, `Magic Stone`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`14`, `Wooden Shaft`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`15`, `Sapphire`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`17`, `Ruby`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`18`, `Hardener`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`19`, `Steel`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`21`, `Bone Powder`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`22`, `String`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`23`, `Coke`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`24`, `Purified Powder`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`31`, `Rope`)) /* - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`39`, `Stinky Sumac`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`s01`, `📕Scroll of Rage`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`r01`, `Champion Sword recipe`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`k01`, `Champion blade`)) - p.ResObjID64 = append(p.ResObjID64, getObjItemID(`w07`, `Rapier`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`02`, `Stick`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`03`, `Pelt`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`04`, `Bone`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`07`, `Powder`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`08`, `Iron Ore`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`09`, `Cloth`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`10`, `Silver Ore`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`11`, `Bauxite`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`13`, `Magic Stone`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`14`, `Wooden Shaft`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`15`, `Sapphire`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`17`, `Ruby`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`18`, `Hardener`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`19`, `Steel`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`21`, `Bone Powder`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`22`, `String`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`23`, `Coke`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`24`, `Purified Powder`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`31`, `Rope`)) + /* + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`39`, `Stinky Sumac`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`s01`, `📕Scroll of Rage`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`r01`, `Champion Sword recipe`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`k01`, `Champion blade`)) + p.ResObjID64 = append(p.ResObjID64, getObjItemID(`w07`, `Rapier`)) */ b, _ := json.Marshal(p) diff --git a/def.go b/def.go index 5b389c3..6cfaacc 100644 --- a/def.go +++ b/def.go @@ -274,11 +274,6 @@ type JobPayloadGDeposit struct { Status int `json:"status"` } -type JobPayloadGDepositForward struct { - ItemID64 int64 `json:"item_id"` - Quantity int64 `json:"quantity"` -} - type JobPayloadSaveRes struct { MsgID64 int64 `json:"msg_id"` ChatID64 int64 `json:"chat_id"` diff --git a/job.go b/job.go index 3534310..57e344c 100644 --- a/job.go +++ b/job.go @@ -737,18 +737,20 @@ func jobGDepositForward(j Job) { logOnError(err, "jobGDepositForward : getObjMsg") rule, err := getMsgParsingRule(msg) logOnError(err, "jobGDepositForward : getMsgParsingRule") - cwm, err := parseSubTypeMessageGDepositAck(msg, rule.re) + cwm, err := parseSubTypeMessageGDepositReq(msg, rule.re) if cwm.ItemID64 == p.ItemID64 && cwm.Quantity == p.Quantity { //log.Printf("jobGDepositForward : match (%d / %d).\n", cwm.ItemID64, cwm.Quantity) - clientFwdCWMsg(j.UserID64, cwm.Msg.ID64, cwm.Msg.ChatID64, cfg.Bot.Depositchat) + gDepositForwardMux.Lock() + gDepositForwardMsg = append(gDepositForwardMsg, j.Trigger) + gDepositForwardMux.Unlock() err = setJobDone(j.ID64) logOnError(err, "jobGDeposit : setJobDone") } else { //log.Printf("jobGDepositForward : found (%d / %d), expected (%d / %d).\n", cwm.ItemID64, cwm.Quantity, p.ItemID64, p.Quantity) err = rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) logOnError(err, "jobGDepositForward : rescheduleJob") - setJobCallback(j.ID64, j.UserID64, objSubTypeMessageGDepositAck) + setJobCallback(j.ID64, j.UserID64, objSubTypeMessageGDepositReq) } return @@ -850,13 +852,14 @@ func jobGDeposit(j Job) { if cwm.Stock[stockIdx].ItemID64 == p.ResObjID64[resIdx] { //log.Printf("jobGDeposit : objSubTypeMessageStockAck : Matching ItemID %d (%d).\n", p.ResObjID64[resIdx], cwm.Stock[stockIdx].Quantity) item, _ := getObjItem(p.ResObjID64[resIdx]) + cwm.Stock[stockIdx].Quantity = 1 // FIXME REMOVE ONCE DEBUG IS OK clientSendCWMsg(p.ChatID64, fmt.Sprintf("/g_deposit %s %d", item.Code, cwm.Stock[stockIdx].Quantity)) p2 := JobPayloadGDepositForward{ ItemID64: p.ResObjID64[resIdx], Quantity: cwm.Stock[stockIdx].Quantity, } b2, _ := json.Marshal(p2) - err = createJobCallback(objSubTypeJobGDepositForward, j.UserID64, objSubTypeMessageGDepositAck, b2, time.Duration(1*time.Minute)) + err = createJobCallback(objSubTypeJobGDepositForward, j.UserID64, objSubTypeMessageGDepositReq, b2, time.Duration(1*time.Minute)) } } } diff --git a/main.go b/main.go index b2a3c10..e7cfac2 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,9 @@ var ( callbacks map[int64]map[int64][]int64 muxCallbacks sync.Mutex + + gDepositForwardMux sync.Mutex + gDepositForwardMsg []int64 ) func PrintText(m *tb.Message) { diff --git a/msg.go b/msg.go index c182907..6d4286f 100644 --- a/msg.go +++ b/msg.go @@ -23,12 +23,42 @@ func getMsgParsingRule(m *ChatWarsMessage) (*MessageParsingRule, error) { } } +func parseSubTypeMessageGDepositReq(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGDepositReq, error) { + cwm := ChatWarsMessageGDepositReq{} + + cwm.ItemID64 = getObjItemID(r.ReplaceAllString(m.Text, "${Item}"), ``) // FIXME, handle uXXX items ? + cwm.Quantity, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Quantity}"), 10, 64) + + cwm.Msg = m + return &cwm, nil +} + func parseSubTypeMessageGDepositAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGDepositAck, error) { cwm := ChatWarsMessageGDepositAck{} cwm.ItemID64 = getObjItemID(``, r.ReplaceAllString(m.Text, "${Item}")) cwm.Quantity, _ = strconv.ParseInt(r.ReplaceAllString(m.Text, "${Quantity}"), 10, 64) + gDepositForwardMux.Lock() + for i := range gDepositForwardMsg { + msg, err := getObjMsg(gDepositForwardMsg[i]) + logOnError(err, "parseSubTypeMessageGDepositAck : gDepositForwardMsg") + if err == nil && m.TGUserID64 == msg.TGUserID64 { + rule, err := getMsgParsingRule(msg) + logOnError(err, "parseSubTypeMessageGDepositAck : getMsgParsingRule") + cwmreq, err := parseSubTypeMessageGDepositReq(msg, rule.re) + logOnError(err, "parseSubTypeMessageGDepositAck : parseSubTypeMessageGDepositReq") + if err == nil && cwmreq.ItemID64 == cwm.ItemID64 && cwmreq.Quantity == cwm.Quantity { + clientFwdCWMsg(msg.TGUserID64, m.ID64, m.ChatID64, cfg.Bot.Depositchat) + gDepositForwardMsg[i] = gDepositForwardMsg[len(gDepositForwardMsg)-1] + gDepositForwardMsg = gDepositForwardMsg[:len(gDepositForwardMsg)-1] + break + } + + } + } + gDepositForwardMux.Unlock() + cwm.Msg = m return &cwm, nil } diff --git a/workers.go b/workers.go index b3e1ed6..567993a 100644 --- a/workers.go +++ b/workers.go @@ -531,8 +531,6 @@ func JobWorker(id int, jobs <-chan Job) { jobGStock(j) case objSubTypeJobGDeposit: jobGDeposit(j) - case objSubTypeJobGDepositForward: - jobGDepositForward(j) default: log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID) }