diff --git a/bot.go b/bot.go index ca0dd2d..984283e 100644 --- a/bot.go +++ b/bot.go @@ -32,7 +32,6 @@ func BotHandlers(b *tb.Bot) { b.Handle("/timer", botTimer) - b.Handle("/g_stock", botGStock) b.Handle("/g_deposit_all", botGDepositAll) b.Handle("/g_withdraw", botGWithdraw) b.Handle("/save_res", botSaveRes) @@ -661,24 +660,14 @@ func botListParsingRule(m *tb.Message) { return } -func botGStock(m *tb.Message) { - if m.Private() { - clt, ok := getLockedClient(m.Chat.ID, false) - if !ok { - return - } - clt.Mux.Unlock() - } else if m.Chat.ID != cfg.Bot.Mainchat { - return - } - +func botGStock(m ChatWarsMessage) { clt, err := getLockedIdleClient() if err != nil { c := TGCommand{ Type: commandReplyMsg, Text: "Busy, please retry later.", - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, + FromMsgID64: m.ID64, + FromChatID64: m.ChatID64, } TGCmdQueue <- c return @@ -687,9 +676,8 @@ func botGStock(m *tb.Message) { clt.Mux.Unlock() p := JobPayloadGStock{ - MsgID64: int64(m.ID), - ChatID64: m.Chat.ID, - Progress: 0, + MsgID64: m.ID64, + ChatID64: m.ChatID64, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) @@ -699,16 +687,16 @@ func botGStock(m *tb.Message) { c := TGCommand{ Type: commandReplyMsg, Text: fmt.Sprintf("%s", err), - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, + FromMsgID64: m.ID64, + FromChatID64: m.ChatID64, } TGCmdQueue <- c } else { c := TGCommand{ Type: commandReplyMsg, Text: "Stock requested", - FromMsgID64: int64(m.ID), - FromChatID64: m.Chat.ID, + FromMsgID64: m.ID64, + FromChatID64: m.ChatID64, } TGCmdQueue <- c } diff --git a/data/code_obj_sub_type.json b/data/code_obj_sub_type.json index 81d642b..b1eb5f0 100644 --- a/data/code_obj_sub_type.json +++ b/data/code_obj_sub_type.json @@ -619,6 +619,11 @@ "name": "Craft check for all items", "obj_type": "msg" }, + { + "intl_id": "msg_bot_g_stock", + "name": "Get Stock", + "obj_type": "msg" + }, { "intl_id": "job_pillage", "name": "Pillage job", @@ -709,6 +714,11 @@ "name": "Msg Del Job", "obj_type": "job" }, + { + "intl_id": "job_get_vault", + "name": "Generic Get Vault", + "obj_type": "job" + }, { "intl_id": "job_get_hammer_time", "name": "Get Hammer Time job", diff --git a/def.go b/def.go index 2e0dacb..8072d69 100644 --- a/def.go +++ b/def.go @@ -401,10 +401,9 @@ type JobPayloadStatus struct { } type JobPayloadGStock struct { - MsgID64 int64 `json:"msg_id"` - ChatID64 int64 `json:"chat_id"` - Progress int64 `json:"progress"` - Stock []ChatWarsItems `json:"stock"` + MsgID64 int64 `json:"msg_id"` + ChatID64 int64 `json:"chat_id"` + VaultJobID64 int64 `json:"vault_job_id"` } type JobPayloadGWithdrawItem struct { @@ -488,6 +487,23 @@ type JobPayloadVaultItemStatus struct { UserID64 int64 `json:"user_id"` } +type JobPayloadCraftItem struct { + MsgID64 int64 `json:"msg_id"` + ChatID64 int64 `json:"chat_id"` + UserID64 int64 `json:"user_id"` + ObjItemID64 int64 `json:"item_id"` + Status int64 `json:"status"` + CleanupMsg []ChatWarsMessage `json:"cleanup_msg"` + VaultJobId int64 `json:"vault_job_id"` +} + +type JobPayloadGetVault struct { + JobCallbackID64 int64 `json:"job_callback_id"` + ItemTypeList []int64 `json:"item_type"` + Vault []ChatWarsItems `json:"vault"` + CleanupMsg []ChatWarsMessage `json:"cleanup_msg"` +} + const ( userID64ChtWrsBot = 408101137 diff --git a/item.go b/item.go index 8528a4a..bf8cd6d 100644 --- a/item.go +++ b/item.go @@ -11,10 +11,11 @@ import ( ) var ( - muxObjItem sync.RWMutex - objItems []ChatWarsItem - cacheObjItem map[string]int64 - cacheObjItemId map[int64]int64 + muxObjItem sync.RWMutex + objItems []ChatWarsItem + cacheObjItem map[string]int64 + cacheObjItemId map[int64]int64 + cacheObjItemCraft map[string]int64 ) func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool) (int64, error) { @@ -241,6 +242,15 @@ func addObjItemCraftItem(objID64 int64, itemID64 int64, quantity int64) error { return nil } +func getCraftItemID(cmd string) (int64, error) { + muxObjItem.RLock() + defer muxObjItem.RUnlock() + + for _, o := range objItems { + + } +} + func loadObjItem() error { var ( obj_id int64 @@ -254,6 +264,7 @@ func loadObjItem() error { defer muxObjItem.Unlock() cacheObjItem = make(map[string]int64) cacheObjItemId = make(map[int64]int64) + cacheObjItemCraft = make(map[string]int64) objItems = make([]ChatWarsItem, 0) b, err := Asset("data/obj_item.json") @@ -347,6 +358,7 @@ func loadObjItem() error { for _, i := range cacheObjItem { if objItems[i].Craft != nil { + cacheObjItemCraft[objItems[i].Craft.Command] = i setObjItemCraft(objItems[i].ObjID64, objItems[i].Craft.Command, objItems[i].Craft.Mana) emptyObjItemCraftItem(objItems[i].ObjID64) for k, o := range objItems[i].Craft.Items { diff --git a/job.go b/job.go index 40468a7..6b13747 100644 --- a/job.go +++ b/job.go @@ -144,6 +144,16 @@ func setJobTimeout(jobID64 int64, d time.Duration) error { return nil } +func setJobPayloadJSON(jobID64 int64, payload interface{}) error { + b, err := json.Marshal(payload) + logOnError(err, "setJobPayloadJSON") + if err != nil { + return err + } + + return setJobPayload(jobID64, b) +} + func setJobPayload(jobID64 int64, payload []byte) error { var ( zb bytes.Buffer @@ -191,6 +201,15 @@ func setJobPayload(jobID64 int64, payload []byte) error { return nil } +func getJobPayload(j.ID64) []byte { + var b []byte + muxObjJob.Lock() + j := cacheObjJob[jobID64] + b = j.Payload + defer muxObjJob.Unlock() + return b +} + func setJobDone(jobID64 int64) error { stmt, err := db.Prepare(`UPDATE obj_job j SET j.is_done = 1, j.in_work = 0, j.ended = ? WHERE j.obj_id = ?;`) logOnError(err, "setJobDone : prepare update obj_job") @@ -879,7 +898,10 @@ func jobBackupImport(j Job) { } func jobGStock(j Job) { - var p JobPayloadGStock + var ( + p JobPayloadGStock + p2 JobPayloadGetVault + ) var resSize, resCount, alchSize, alchCount, miscSize, miscCount, recSize, recCount, partSize, partCount, otherSize, otherCount, totalSize int64 err := setJobStart(j.ID64) logOnError(err, "jobGStock : setJobStart") @@ -890,157 +912,84 @@ func jobGStock(j Job) { fmt.Printf("jobGStock : Progress => %d\n", p.Progress) fmt.Printf("jobGStock : UserID64 => %d\n", j.UserID64) - switch p.Progress { - case 0: // send /g_stock_res - p.Progress = 1 - b, _ := json.Marshal(&p) - jobID64, err := createJob(cacheObjSubType[`job_gstock`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b) - logOnError(err, "jobGStock : createJob") - setJobCallback(jobID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) - setJobCallback(jobID64, j.UserID64, cacheObjSubType[`msg_busy`]) - setJobCallback(jobID64, j.UserID64, cacheObjSubType[`msg_battle`]) - err = setJobTimeout(jobID64, 1*time.Minute) - logOnError(err, "jobGStock : setJobTimeout") - clientSendCWMsg(j.UserID64, "/g_stock_res") - case 1: // send /g_stock_alch - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - if rule.MsgTypeID64 == cacheObjSubType[`msg_gstock_any_ack`] { - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - p.Progress = 2 - b, _ := json.Marshal(&p) - err = createJobCallback(cacheObjSubType[`job_gstock`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, 1*time.Minute) - logOnError(err, "jobGStock : createJobCallback") - clientSendCWMsg(j.UserID64, "/g_stock_alch") - } else if rule.MsgTypeID64 == cacheObjSubType[`msg_busy`] || rule.MsgTypeID64 == cacheObjSubType[`msg_battle`] { - m := TGCommand{ - Type: commandReplyMsg, - Text: "Busy, please retry later.", - FromMsgID64: p.MsgID64, - FromChatID64: p.ChatID64, - ParseMode: cmdParseModeHTML, - } - TGCmdQueue <- m - } - case 2: // send /g_stock_misc - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - p.Progress = 3 - b, _ := json.Marshal(&p) - err = createJobCallback(cacheObjSubType[`job_gstock`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, 1*time.Minute) - logOnError(err, "jobGStock : createJobCallback") - clientSendCWMsg(j.UserID64, "/g_stock_misc") - case 3: // send /g_stock_rec - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - p.Progress = 4 - b, _ := json.Marshal(&p) - err = createJobCallback(cacheObjSubType[`job_gstock`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, 1*time.Minute) - logOnError(err, "jobGStock : createJobCallback") - clientSendCWMsg(j.UserID64, "/g_stock_rec") - case 4: // send /g_stock_parts - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - p.Progress = 5 - b, _ := json.Marshal(&p) - err = createJobCallback(cacheObjSubType[`job_gstock`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, 1*time.Minute) - logOnError(err, "jobGStock : createJobCallback") - clientSendCWMsg(j.UserID64, "/g_stock_parts") - case 5: // send /g_stock_other - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - p.Progress = 6 - b, _ := json.Marshal(&p) - err = createJobCallback(cacheObjSubType[`job_gstock`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, 1*time.Minute) - logOnError(err, "jobGStock : createJobCallback") - clientSendCWMsg(j.UserID64, "/g_stock_other") - case 6: // collate everything and reply - msg, err := getObjMsg(j.Trigger) - logOnError(err, "jobGStock : getObjMsg msg") - rule, err := getMsgParsingRule(msg) - logOnError(err, "jobGStock : getMsgParsingRule") - cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re) - for _, v := range cwm.Stock { - p.Stock = append(p.Stock, v) - } - for _, v := range p.Stock { - item, err := getObjItem(v.ItemID64) - logOnError(err, "jobGStock : getObjItem") - if err == nil { - if item.Weight != -1 { - totalSize += item.Weight * v.Quantity - switch item.ItemTypeID { - case cacheObjSubType[`item_res`]: - resSize += item.Weight * v.Quantity - resCount += v.Quantity - case cacheObjSubType[`item_alch`]: - alchSize += item.Weight * v.Quantity - alchCount += v.Quantity - case cacheObjSubType[`item_misc`]: - miscSize += item.Weight * v.Quantity - miscCount += v.Quantity - case cacheObjSubType[`item_recipe`]: - recSize += item.Weight * v.Quantity - recCount += v.Quantity - case cacheObjSubType[`item_part`]: - partSize += item.Weight * v.Quantity - partCount += v.Quantity - case cacheObjSubType[`item_other`]: - otherSize += item.Weight * v.Quantity - otherCount += v.Quantity - } - } else { - w := TGCommand{ - Type: commandSendMsg, - Text: fmt.Sprintf("Unknown weight for item : %s - %s\n", item.Code, item.Names[0]), - ToUserID64: cfg.Bot.Admin, - } - TGCmdQueue <- w - } - } - } + if p.Status == 0 { + p2.JobCallbackID64 = j.ID64 + p2.ItemTypeList = make([]int64, 0) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_res`]) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_alch`]) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_misc`]) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_recipe`]) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_part`]) + p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_other`]) - txt := fmt.Sprintf("Current stock [%d/38000] :\n - Resources : %d (%d)\n - Alchemist : %d (%d)\n - Misc stuff : %d (%d)\n - Recipes : %d (%d)\n - Parts : %d (%d)\n - Other : %d (%d)\n", totalSize, resSize, resCount, alchSize, alchCount, miscSize, miscCount, recSize, recCount, partSize, partCount, otherSize, otherCount) + b2, err := json.Marshal(p2) + logOnError(err, "jobGStock : Marshal(p2)") - m := TGCommand{ - Type: commandReplyMsg, - Text: txt, - FromMsgID64: p.MsgID64, - FromChatID64: p.ChatID64, - ParseMode: cmdParseModeHTML, - } - TGCmdQueue <- m + jobID64, err := createJob(cacheObjSubType[`job_get_vault`], objJobPriority, j.UserID64, 0, time.Now().UTC(), b) + + p.Status = 1 + b, err = json.Marshal(p) + logOnError(err, "jobGStock : Marshal(p)") + + err = setJobPayloadJSON(j.ID64, p) + logOnError(err, "jobGStock : setJobPayloadJSON(p)") + + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + return } + b = getJobPayload(p.VaultJobID64) + err = json.Unmarshal(b, &p2) + logOnError(err, "jobGStock : Unmarshal(p2)") + + for _, v := range p2.Vault { + item, err := getObjItem(v.ItemID64) + logOnError(err, "jobGStock : getObjItem") + if err == nil { + if item.Weight != -1 { + totalSize += item.Weight * v.Quantity + switch item.ItemTypeID { + case cacheObjSubType[`item_res`]: + resSize += item.Weight * v.Quantity + resCount += v.Quantity + case cacheObjSubType[`item_alch`]: + alchSize += item.Weight * v.Quantity + alchCount += v.Quantity + case cacheObjSubType[`item_misc`]: + miscSize += item.Weight * v.Quantity + miscCount += v.Quantity + case cacheObjSubType[`item_recipe`]: + recSize += item.Weight * v.Quantity + recCount += v.Quantity + case cacheObjSubType[`item_part`]: + partSize += item.Weight * v.Quantity + partCount += v.Quantity + case cacheObjSubType[`item_other`]: + otherSize += item.Weight * v.Quantity + otherCount += v.Quantity + } + } else { + w := TGCommand{ + Type: commandSendMsg, + Text: fmt.Sprintf("Unknown weight for item : %s - %s\n", item.Code, item.Names[0]), + ToUserID64: cfg.Bot.Admin, + } + TGCmdQueue <- w + } + } + } + + txt := fmt.Sprintf("Current stock [%d/38000] :\n - Resources : %d (%d)\n - Alchemist : %d (%d)\n - Misc stuff : %d (%d)\n - Recipes : %d (%d)\n - Parts : %d (%d)\n - Other : %d (%d)\n", totalSize, resSize, resCount, alchSize, alchCount, miscSize, miscCount, recSize, recCount, partSize, partCount, otherSize, otherCount) + + m := TGCommand{ + Type: commandReplyMsg, + Text: txt, + FromMsgID64: p.MsgID64, + FromChatID64: p.ChatID64, + ParseMode: cmdParseModeHTML, + } + TGCmdQueue <- m + err = setJobDone(j.ID64) logOnError(err, "jobGStock : setJobDone") return @@ -1950,3 +1899,190 @@ func jobGetHammerTime(j Job) { return } + +func jobGetVault(j Job) { + var ( + p JobPayloadGetVault + reqTab map[int64]int64 + doneTab map[int64]int64 + ) + + log.Printf("jobGetVault[%d] : Starting.\n", j.ID64) + + reqTab = make(map[int64]int64) + reqTab[cacheObjSubType[`item_res`]] = 1 << 0 + reqTab[cacheObjSubType[`item_alch`]] = 1 << 1 + reqTab[cacheObjSubType[`item_misc`]] = 1 << 2 + reqTab[cacheObjSubType[`item_recipe`]] = 1 << 3 + reqTab[cacheObjSubType[`item_part`]] = 1 << 4 + reqTab[cacheObjSubType[`item_other`]] = 1 << 5 + + doneTab = make(map[int64]int64) + doneTab[cacheObjSubType[`item_res`]] = 1 << 10 + doneTab[cacheObjSubType[`item_alch`]] = 1 << 11 + doneTab[cacheObjSubType[`item_misc`]] = 1 << 12 + doneTab[cacheObjSubType[`item_recipe`]] = 1 << 13 + doneTab[cacheObjSubType[`item_part`]] = 1 << 14 + doneTab[cacheObjSubType[`item_other`]] = 1 << 15 + + err := setJobStart(j.ID64) + logOnError(err, "jobGetVault : setJobStart") + + err = json.Unmarshal(j.Payload, &p) + logOnError(err, "jobGetVault : Unmarshal payload") + + if p.Status == 0 { + for k, typeID64 := range p.ItemTypeList { + p.Status = p.Status | reqTab[typeID64] + } + if (p.Status & reqTab[cacheObjSubType[`item_res`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_res`]] + } + if (p.Status & reqTab[cacheObjSubType[`item_alch`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_alch`]] + } + if (p.Status & reqTab[cacheObjSubType[`item_misc`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_misc`]] + } + if (p.Status & reqTab[cacheObjSubType[`item_recipe`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_recipe`]] + } + if (p.Status & reqTab[cacheObjSubType[`item_part`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_part`]] + } + if (p.Status & reqTab[cacheObjSubType[`item_other`]]) == 0 { + p.Status = p.Status | doneTab[cacheObjSubType[`item_other`]] + } + + } + + if j.Trigger != 0 { + id, err := getObjSubTypeId(j.Trigger) + logOnError(err, "jobGetVault : getObjSubType("+strconv.FormatInt(j.Trigger, 10)+")") + if err == nil { + m, err := getObjMsg(j.Trigger) + logOnError(err, "jobGetVault : getObjMsg") + rule, err := getMsgParsingRule(m) + logOnError(err, "jobGetVault : getMsgParsingRule") + + switch id { + case cacheObjSubType[`msg_gstock_any_ack`]: + cwm, err := parseSubTypeMessageGStockAnyAck(m, rule.re) + logOnError(err, "jobGetVault : parseSubTypeMessageGStockAnyAck") + + for _, disp := range cwm.Stock { + item := ChatWarsItem{ + Code: disp.Code, + ItemID64: disp.ItemID64, + Name: disp.Name, + Quantity: disp.Quantity, + } + p.Vault = append(p.Vault, item) + } + + p.CleanupMsg = append(p.CleanupMsg, *m) + + case cacheObjSubType[`msg_gstock_oth_req`]: + fallthrough + case cacheObjSubType[`msg_gstock_res_req`]: + fallthrough + case cacheObjSubType[`msg_gstock_alch_req`]: + fallthrough + case cacheObjSubType[`msg_gstock_misc_req`]: + fallthrough + case cacheObjSubType[`msg_gstock_rec_req`]: + fallthrough + case cacheObjSubType[`msg_gstock_part_req`]: + p.CleanupMsg = append(p.CleanupMsg, *m) + setJobPayloadJSON(j.ID64, p) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + return + default: + } + } + } + + if (p.Status & reqTab[cacheObjSubType[`item_res`]]) == reqTab[cacheObjSubType[`item_res`]] { + log.Printf("jobGetVault[%d] : Requesting res.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_res`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_res`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_res_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_res`) + return + } else if (p.Status & reqTab[cacheObjSubType[`item_alch`]]) == reqTab[cacheObjSubType[`item_alch`]] { + log.Printf("jobGetVault[%d] : Requesting alch.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_alch`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_alch`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_alch_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_res`) + clientSendCWMsg(j.UserID64, `/g_stock_alch`) + return + } else if (p.Status & reqTab[cacheObjSubType[`item_misc`]]) == reqTab[cacheObjSubType[`item_misc`]] { + log.Printf("jobGetVault[%d] : Requesting misc.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_misc`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_misc`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_misc_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_misc`) + return + } else if (p.Status & reqTab[cacheObjSubType[`item_recipe`]]) == reqTab[cacheObjSubType[`item_recipe`]] { + log.Printf("jobGetVault[%d] : Requesting recipe.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_recipe`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_recipe`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_rec_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_rec`) + return + } else if (p.Status & reqTab[cacheObjSubType[`item_part`]]) == reqTab[cacheObjSubType[`item_part`]] { + log.Printf("jobGetVault[%d] : Requesting part.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_part`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_part`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_part_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_part`) + return + } else if (p.Status & reqTab[cacheObjSubType[`item_other`]]) == reqTab[cacheObjSubType[`item_other`]] { + log.Printf("jobGetVault[%d] : Requesting other.\n", j.ID64) + p.Status = p.Status &^ reqTab[cacheObjSubType[`item_other`]] + p.Status = p.Status | doneTab[cacheObjSubType[`item_other`]] + setJobPayloadJSON(j.ID64, p) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_oth_req`]) + setJobCallback(j.ID64, j.UserID64, cacheObjSubType[`msg_gstock_any_ack`]) + rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC()) + clientSendCWMsg(j.UserID64, `/g_stock_other`) + return + } + + /* clean up the messages */ + for _, d := range p.CleanupMsg { + delmsg := tb.StoredMessage{ + MessageID: fmt.Sprintf("%d", d.ID64), + ChatID: d.ChatID64, + } + err = bot.Delete(delmsg) + logOnError(err, "jobGetVault : Delete") + } + + /* wake up the callback */ + err = rescheduleJob(p.JobCallbackID64, 0, time.Now().UTC()) + logOnError(err, "jobGetVault : rescheduleJob") + + /* no more req to send, all ack came through, we can finish the job */ + + err = setJobDone(j.ID64) + logOnError(err, "jobGetVault : setJobDone") + + return +} diff --git a/rules.go b/rules.go index 07d1246..bb677ba 100644 --- a/rules.go +++ b/rules.go @@ -75,6 +75,16 @@ func resetMsgParsingRules() error { } } + r = MessageParsingRule{ + Priority: 9999, + Description: "Get Stock", + Rule: "^/g_stock$", + MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`], + ChatID64: cfg.Bot.Mainchat, + SenderUserID64: 0, + } + rules2 = append(rules2, r) + r = MessageParsingRule{ Priority: 1, Description: "Default Main chat", diff --git a/workers.go b/workers.go index 34ebb75..255de6c 100644 --- a/workers.go +++ b/workers.go @@ -508,7 +508,10 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) { case cacheObjSubType[`msg_job_gwithdraw_ack`]: _, err := parseSubTypeMessageJobGWithdrawAck(m, rule.re) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : Parsing cacheObjSubType[`msg_quest_res`]") + case cacheObjSubType[`msg_bot_g_stock`]: + botGStock(m) case cacheObjSubType[`msg_bot_craft_item`]: + c := TGCommand{ Type: commandReplyMsg, Text: `test`, @@ -620,6 +623,8 @@ func JobWorker(id int, jobs <-chan Job) { jobSetDef(j) case cacheObjSubType[`job_get_hammer_time`]: jobGetHammerTime(j) + case cacheObjSubType[`job_get_vault`]: + jobGetVault(j) default: log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64) }