update g_stock

This commit is contained in:
shoopea 2019-12-13 19:19:52 +08:00
parent 9ebce1d76a
commit 72604ae70d
5 changed files with 127 additions and 17 deletions

1
bot.go
View File

@ -646,6 +646,7 @@ func botGStock(m *tb.Message) {
p := JobPayloadGStock{
MsgID64: int64(m.ID),
ChatID64: m.Chat.ID,
Progress: 0,
}
b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second)

View File

@ -31,6 +31,16 @@ func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) {
}
}
func clientDelCWMsg(userID64 int64, fromMsgID64 int64, fromChatID64 int64) {
c := TGCommand{
Type: commandDeleteMsg,
FromUserID64: userID64,
FromMsgID64: fromMsgID64,
FromChatID64: fromChatID64,
}
MQTGCmdQueue <- c
}
func clientFwdCWMsg(userID64 int64, fromMsgID64 int64, fromChatID64 int64, toChatID64 int64) {
c := TGCommand{
Type: commandForwardMsg,

11
def.go
View File

@ -130,7 +130,7 @@ type ChatWarsMessageExchangeAck struct {
type ChatWarsMessageGStock struct {
Msg *ChatWarsMessage `json:"msg"`
OwnerID64 int64 `json:"owner_id"`
ItemList []ChatWarsItems `json:"item_list"`
Stock []ChatWarsItems `json:"stock"`
}
type ChatWarsMessageWithdrawRcv struct {
@ -313,8 +313,10 @@ type JobPayloadStatus struct {
}
type JobPayloadGStock struct {
MsgID64 int64 `json:"msg_id"`
ChatID64 int64 `json:"chat_id"`
MsgID64 int64 `json:"msg_id"`
ChatID64 int64 `json:"chat_id"`
Progress int64 `json:"progress"`
Stock []ChatWarsItems `json:"stock"`
}
type JobPayloadGWithdraw struct {
@ -537,8 +539,9 @@ const (
objSubTypeJobSaveRes = 614
objSubTypeJobVaultUserStatus = 615
objSubTypeJobVaultItemStatus = 616
objSubTypeJobFwdMsg = 617
objSubTypeJobFwdMsg = 617 // rename to objSubTypeJobMsgFwd
objSubTypeJobSetDef = 618
objSubTypeJobMsgDel = 619
objSubTypeItemResource = 701
objSubTypeItemAlch = 702
objSubTypeItemMisc = 703

116
job.go
View File

@ -698,23 +698,119 @@ func jobBackupImport(j Job) {
func jobGStock(j Job) {
var p JobPayloadGStock
var resSize, resCount, alchSize, alchCount, miscSize, miscCount, recSize, recCount, partSize, partCount, otherSize, otherCount, totalSize int64
err := setJobStart(j.ID64)
logOnError(err, "jobGStock : setJobStart")
err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobGStock : Unmarshal payload")
/*
clientSendCWMsg(m.Chat.ID, "/g_stock_res")
clientSendCWMsg(m.Chat.ID, "/g_stock_alch")
clientSendCWMsg(m.Chat.ID, "/g_stock_misc")
clientSendCWMsg(m.Chat.ID, "/g_stock_rec")
clientSendCWMsg(m.Chat.ID, "/g_stock_parts")
clientSendCWMsg(m.Chat.ID, "/g_stock_other")
*/
if err == nil {
switch p.Progress {
case 0: // send /g_stock_res
p.Progress = 1
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/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")
cwm, err := parseSubTypeMessageGStockAnyAck(msg, rule.re)
p.Stock = append(p.Stock, cwm.Stock)
p.Progress = 2
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/g_stock_alch")
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)
p.Stock = append(p.Stock, cwm.Stock)
p.Progress = 3
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/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)
p.Stock = append(p.Stock, cwm.Stock)
p.Progress = 4
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/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)
p.Stock = append(p.Stock, cwm.Stock)
p.Progress = 5
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/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)
p.Stock = append(p.Stock, cwm.Stock)
p.Progress = 6
b, _ := json.Marshal(&p)
err = createJobCallback(objSubTypeJobGStock, c.TGUserID64, objSubTypeMessageGStockAnyAck, b, 1*time.Minute)
logOnError(err, "jobGStock : createJobCallback")
clientSendCWMsg(cfg.Bot.Admin, "/g_stock_other")
case 6: // collate everything and reply
for k, v := range p.Stock {
item, err := getObjItem(v.ItemID64)
logOnError(err, "jobGStock : getObjItem")
if err == nil {
totalSize = totalSize + item.Weight*v.Quantity
switch item.ItemTypeID {
case objSubTypeItemResource:
resSize += item.Weight * v.Quantity
resCount += 1
case objSubTypeItemAlch:
alchSize += item.Weight * v.Quantity
alchCount += 1
case objSubTypeItemMisc:
miscSize += item.Weight * v.Quantity
miscCount += 1
case objSubTypeItemRecipe:
recSize += item.Weight * v.Quantity
recCount += 1
case objSubTypeItemPart:
partSize += item.Weight * v.Quantity
partCount += 1
case objSubTypeItemOther:
otherSize += item.Weight * v.Quantity
otherCount += 1
}
}
}
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, partsSize, partsCount, otherSize, otherCount)
m := TGCommand{
Type: commandReplyMsg,
Text: "Message sent.",
Text: txt,
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
}

6
msg.go
View File

@ -243,7 +243,7 @@ func parseSubTypeMessageWithdrawRcv(m *ChatWarsMessage, r *regexp.Regexp) (*Chat
func parseSubTypeMessageGStockAnyAck(m *ChatWarsMessage, r *regexp.Regexp) (*ChatWarsMessageGStock, error) {
cwm := ChatWarsMessageGStock{}
items := []ChatWarsItems{}
stock := []ChatWarsItems{}
re := regexp.MustCompile(`\n([0-9a-z]+) (.*) x ([0-9]+)`)
for _, l := range re.FindAllStringSubmatch(r.ReplaceAllString(m.Text, "${Stock}"), -1) {
i := getObjItemID(l[1], l[2])
@ -253,10 +253,10 @@ func parseSubTypeMessageGStockAnyAck(m *ChatWarsMessage, r *regexp.Regexp) (*Cha
ItemID64: i,
Quantity: q,
}
items = append(items, item)
stock = append(stock, item)
}
}
cwm.ItemList = items
cwm.Stock = stock
cwm.Msg = m
return &cwm, nil
}