test craft

This commit is contained in:
shoopea 2020-01-27 12:05:38 +08:00
parent 2fac84340c
commit 8a1d244d08
4 changed files with 260 additions and 279 deletions

49
bot.go
View File

@ -672,8 +672,7 @@ func botGStock(m *ChatWarsMessage) {
TGCmdQueue <- c TGCmdQueue <- c
return return
} }
userID64 := cfg.Bot.Admin userID64 := clt.TGUserID64
//userID64 := clt.TGUserID64
clt.Mux.Unlock() clt.Mux.Unlock()
p := JobPayloadGStock{ p := JobPayloadGStock{
@ -706,6 +705,52 @@ func botGStock(m *ChatWarsMessage) {
return return
} }
func botCraftItem(m *ChatWarsMessage) {
clt, err := getLockedIdleClient()
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: "Busy, please retry later.",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
return
}
userID64 := clt.TGUserID64
clt.Mux.Unlock()
p := JobPayloadCraftItem{
MsgID64: m.ID64,
ChatID64: m.ChatID64,
ObjItemID64: getCraftItemID(m.Text),
Status: 0,
}
b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second)
_, err = createJob(cacheObjSubType[`job_craft_item`], objJobPriority, userID64, 0, t, b)
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: fmt.Sprintf("%s", err),
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
} else {
c := TGCommand{
Type: commandReplyMsg,
Text: "Craft summary coming",
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
}
TGCmdQueue <- c
}
return
}
func botSaveRes(m *tb.Message) { func botSaveRes(m *tb.Message) {
if !m.Private() { if !m.Private() {
return return

13
def.go
View File

@ -489,13 +489,12 @@ type JobPayloadVaultItemStatus struct {
} }
type JobPayloadCraftItem struct { type JobPayloadCraftItem struct {
MsgID64 int64 `json:"msg_id"` MsgID64 int64 `json:"msg_id"`
ChatID64 int64 `json:"chat_id"` ChatID64 int64 `json:"chat_id"`
UserID64 int64 `json:"user_id"` UserID64 int64 `json:"user_id"`
ObjItemID64 int64 `json:"item_id"` ObjItemID64 int64 `json:"item_id"`
Status int64 `json:"status"` Status int64 `json:"status"`
CleanupMsg []ChatWarsMessage `json:"cleanup_msg"` VaultJobID64 int64 `json:"vault_job_id"`
VaultJobId int64 `json:"vault_job_id"`
} }
type JobPayloadGetVault struct { type JobPayloadGetVault struct {

465
job.go
View File

@ -902,11 +902,18 @@ func jobBackupImport(j Job) {
func jobGStock(j Job) { func jobGStock(j Job) {
var ( var (
p JobPayloadGStock p JobPayloadGStock
p2 JobPayloadGetVault p2 JobPayloadGetVault
b []byte b []byte
resSize, resCount int64
alchSize, alchCount int64
miscSize, miscCount int64
recSize, recCount int64
partSize, partCount int64
otherSize, otherCount int64
totalSize int64
) )
var resSize, resCount, alchSize, alchCount, miscSize, miscCount, recSize, recCount, partSize, partCount, otherSize, otherCount, totalSize int64
err := setJobStart(j.ID64) err := setJobStart(j.ID64)
logOnError(err, "jobGStock : setJobStart") logOnError(err, "jobGStock : setJobStart")
@ -1395,29 +1402,13 @@ func jobVaultUserStatus(j Job) {
func jobGWithdraw(j Job) { func jobGWithdraw(j Job) {
var ( var (
p JobPayloadGWithdraw p JobPayloadGWithdraw
reqTab map[int64]int64 p2 JobPayloadGetVault
doneTab map[int64]int64 b []byte
) )
log.Printf("jobGWithdraw[%d] : Starting.\n", j.ID64) log.Printf("jobGWithdraw[%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) err := setJobStart(j.ID64)
logOnError(err, "jobGWithdraw : setJobStart") logOnError(err, "jobGWithdraw : setJobStart")
@ -1425,92 +1416,45 @@ func jobGWithdraw(j Job) {
logOnError(err, "jobGWithdraw : Unmarshal payload") logOnError(err, "jobGWithdraw : Unmarshal payload")
if p.Status == 0 { if p.Status == 0 {
p2.JobCallbackID64 = j.ID64
p2.ItemTypeList = make([]int64, 0)
for k, item := range p.Items { for k, item := range p.Items {
id := getSilentObjItemID(item.Code, ``) id := getSilentObjItemID(item.Code, ``)
if id != 0 { if id != 0 {
obj, _ := getObjItem(id) obj, _ := getObjItem(id)
p.Status = p.Status | reqTab[obj.ItemTypeID] p2.ItemTypeList = append(p2.ItemTypeList, obj.ItemTypeID)
} else if ok, _ := regexp.MatchString(`^u[0-9]+$`, item.Code); ok { } else if ok, _ := regexp.MatchString(`^u[0-9]+$`, item.Code); ok {
p.Items[k].Inspect = true p.Items[k].Inspect = true
p.Status = p.Status | reqTab[cacheObjSubType[`item_other`]] p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_other`])
} }
} }
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`]]
}
b, err = json.Marshal(p2)
logOnError(err, "jobGWithdraw : Marshal(p2)")
jobID64, err := createJob(cacheObjSubType[`job_get_vault`], objJobPriority, j.UserID64, 0, time.Now().UTC(), b)
p.Status = 1
p.VaultJobID64 = jobID64
b, err = json.Marshal(p)
logOnError(err, "jobGWithdraw : Marshal(p)")
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobGWithdraw : setJobPayloadJSON(p)")
rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
return
} else if p.Status == 1 {
/* loop through items and get unique/inspect */
} }
if j.Trigger != 0 { if j.Trigger != 0 {
id, err := getObjSubTypeId(j.Trigger) id, err := getObjSubTypeId(j.Trigger)
logOnError(err, "jobGWithdraw : getObjSubType("+strconv.FormatInt(j.Trigger, 10)+")") logOnError(err, "jobGWithdraw : getObjSubType("+strconv.FormatInt(j.Trigger, 10)+")")
if err == nil { if err == nil {
if id == cacheObjSubType[`msg_gstock_any_ack`] { if id == cacheObjSubType[`msg_inspect_ack`] {
var isUnique bool
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
rule, err := getMsgParsingRule(m)
logOnError(err, "jobGWithdraw : getMsgParsingRule")
cwm, err := parseSubTypeMessageGStockAnyAck(m, rule.re)
logOnError(err, "jobGWithdraw : parseSubTypeMessageGStockAnyAck")
for k, req := range p.Items {
for _, disp := range cwm.Stock {
if req.Code == disp.Code {
p.Items[k].Available = disp.Quantity
p.Items[k].Name = disp.Name
p.Items[k].Inspect = false
log.Printf("jobGWithdraw[%d] : Found %s - %s : %d.\n", j.ID64, disp.Code, disp.Name, disp.Quantity)
}
if ok, _ := regexp.MatchString(`^u[0-9]+$`, disp.Code); ok {
isUnique = true
}
}
}
p.CleanupMsg = append(p.CleanupMsg, *m)
if isUnique {
for _, req := range p.Items {
if req.Inspect {
p2 := JobPayloadMsgDel{
MsgTypeID64: cacheObjSubType[`msg_g_inspect_req`],
Delay: (10 * time.Second),
ObjMsgID64: 0,
}
b2, _ := json.Marshal(p2)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
p.Inspecting = req.Code
b, _ := json.Marshal(p)
id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
logOnError(err, "jobGWithdraw : createJob")
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_invalid_action`])
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_inspect_ack`])
clientSendCWMsg(j.UserID64, fmt.Sprintf("/g_inspect_%s", p.Inspecting))
err = setJobDone(j.ID64)
logOnError(err, "jobGWithdraw : setJobDone")
return
}
}
}
} else if id == cacheObjSubType[`msg_inspect_ack`] {
m, err := getObjMsg(j.Trigger) m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg") logOnError(err, "jobGWithdraw : getObjMsg")
rule, err := getMsgParsingRule(m) rule, err := getMsgParsingRule(m)
@ -1660,170 +1604,78 @@ func jobGWithdraw(j Job) {
} }
} }
if (p.Status & reqTab[cacheObjSubType[`item_res`]]) == reqTab[cacheObjSubType[`item_res`]] { /*
log.Printf("jobGWithdraw[%d] : Requesting res.\n", j.ID64) c, err := getLockedRoleClient(`commander`)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_res`]] logOnError(err, "jobGWithdraw: getLockedRoleClient(commander)")
p.Status = p.Status | doneTab[cacheObjSubType[`item_res`]] if err == nil {
c.Mux.Unlock()
b, _ := json.Marshal(p)
p2 := JobPayloadMsgDel{
MsgTypeID64: cacheObjSubType[`msg_gstock_res_req`],
Delay: (10 * time.Second),
ObjMsgID64: 0,
} }
b2, _ := json.Marshal(p2) */
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_res`)
} else if (p.Status & reqTab[cacheObjSubType[`item_alch`]]) == reqTab[cacheObjSubType[`item_alch`]] {
log.Printf("jobGWithdraw[%d] : Requesting alch.\n", j.ID64)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_alch`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_alch`]]
b, _ := json.Marshal(p) var stock string
p2 := JobPayloadMsgDel{ for _, i := range p.Items {
MsgTypeID64: cacheObjSubType[`msg_gstock_alch_req`], if i.Available > i.Required {
Delay: (10 * time.Second), stock = fmt.Sprintf("%s\n%d x %s", stock, i.Required, i.Name)
ObjMsgID64: 0, } else if i.Available > 0 {
stock = fmt.Sprintf("%s\n%d x %s", stock, i.Available, i.Name)
} }
b2, _ := json.Marshal(p2) }
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_alch`)
} else if (p.Status & reqTab[cacheObjSubType[`item_misc`]]) == reqTab[cacheObjSubType[`item_misc`]] {
log.Printf("jobGWithdraw[%d] : Requesting misc.\n", j.ID64)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_misc`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_misc`]]
if len(stock) > 0 {
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
p2 := JobPayloadMsgDel{ id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
MsgTypeID64: cacheObjSubType[`msg_gstock_misc_req`], logOnError(err, "jobGWithdraw : createJob")
Delay: (10 * time.Second), setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`])
ObjMsgID64: 0,
}
b2, _ := json.Marshal(p2)
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_misc`)
} else if (p.Status & reqTab[cacheObjSubType[`item_recipe`]]) == reqTab[cacheObjSubType[`item_recipe`]] {
log.Printf("jobGWithdraw[%d] : Requesting recipe.\n", j.ID64)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_recipe`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_recipe`]]
b, _ := json.Marshal(p) sha256 := sha256.Sum256([]byte(cfg.Telegram.Token))
p2 := JobPayloadMsgDel{ sha128 := sha256[:aes.BlockSize]
MsgTypeID64: cacheObjSubType[`msg_gstock_rec_req`], c, err := aes.NewCipher(sha128)
Delay: (10 * time.Second),
ObjMsgID64: 0,
}
b2, _ := json.Marshal(p2)
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_rec`)
} else if (p.Status & reqTab[cacheObjSubType[`item_part`]]) == reqTab[cacheObjSubType[`item_part`]] {
log.Printf("jobGWithdraw[%d] : Requesting part.\n", j.ID64)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_part`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_part`]]
b, _ := json.Marshal(p) in := make([]byte, 0)
p2 := JobPayloadMsgDel{ buf := make([]byte, 8)
MsgTypeID64: cacheObjSubType[`msg_gstock_part_req`], binary.LittleEndian.PutUint64(buf, uint64(id))
Delay: (10 * time.Second), in = append(in, buf...)
ObjMsgID64: 0, binary.LittleEndian.PutUint64(buf, uint64(p.UserID64))
} in = append(in, buf...)
b2, _ := json.Marshal(p2) out := make([]byte, len(in))
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_parts`)
} else if (p.Status & reqTab[cacheObjSubType[`item_other`]]) == reqTab[cacheObjSubType[`item_other`]] {
log.Printf("jobGWithdraw[%d] : Requesting other.\n", j.ID64)
p.Status = p.Status &^ reqTab[cacheObjSubType[`item_other`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_other`]]
b, _ := json.Marshal(p) ref := hex.EncodeToString(in)
p2 := JobPayloadMsgDel{ log.Printf("jobGWithdraw[%d] : in string : %s.\n", j.ID64, ref)
MsgTypeID64: cacheObjSubType[`msg_gstock_oth_req`], c.Encrypt(out, in)
Delay: (10 * time.Second), ref = hex.EncodeToString(out)
ObjMsgID64: 0, log.Printf("jobGWithdraw[%d] : out string : %s.\n", j.ID64, ref)
ref = hex.EncodeToString(out)
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
p.CleanupMsg = append(p.CleanupMsg, *m)
b, err = json.Marshal(p)
log.Printf("jobGWithdraw[%d] : %s\n", j.ID64, string(b))
u, err := bot.ChatByID(fmt.Sprintf("%d", p.UserID64))
logOnError(err, "jobGWithdraw : ChatByID")
msg := fmt.Sprintf("Click to validate @%s's withdrawal of<code>%s</code>\n/withdraw_%s", u.Username, stock, string(ref))
cmd := TGCommand{
Type: commandSendMsg,
Text: msg,
ToChatID64: cfg.Bot.Mainchat,
ParseMode: cmdParseModeHTML,
} }
b2, _ := json.Marshal(p2) TGCmdQueue <- cmd
createJobCallback(cacheObjSubType[`job_gwithdraw`], j.UserID64, cacheObjSubType[`msg_gstock_any_ack`], b, time.Minute)
createJobCallback(cacheObjSubType[`job_msg_del`], j.UserID64, p2.MsgTypeID64, b2, time.Minute)
clientSendCWMsg(j.UserID64, `/g_stock_other`)
} else { } else {
/* cmd := TGCommand{
c, err := getLockedRoleClient(`commander`) Type: commandReplyMsg,
logOnError(err, "jobGWithdraw: getLockedRoleClient(commander)") Text: "No stock available whatsoever",
if err == nil { FromMsgID64: p.MsgID64,
c.Mux.Unlock() FromChatID64: p.ChatID64,
} ParseMode: cmdParseModeHTML,
*/
var stock string
for _, i := range p.Items {
if i.Available > i.Required {
stock = fmt.Sprintf("%s\n%d x %s", stock, i.Required, i.Name)
} else if i.Available > 0 {
stock = fmt.Sprintf("%s\n%d x %s", stock, i.Available, i.Name)
}
}
if len(stock) > 0 {
b, _ := json.Marshal(p)
id, err := createJob(cacheObjSubType[`job_gwithdraw`], objJobPriority, j.UserID64, 0, time.Unix(maxUnixTimestamp, 0).UTC(), b)
logOnError(err, "jobGWithdraw : createJob")
setJobCallback(id, int64(bot.Me.ID), cacheObjSubType[`msg_job_gwithdraw_ack`])
sha256 := sha256.Sum256([]byte(cfg.Telegram.Token))
sha128 := sha256[:aes.BlockSize]
c, err := aes.NewCipher(sha128)
in := make([]byte, 0)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(id))
in = append(in, buf...)
binary.LittleEndian.PutUint64(buf, uint64(p.UserID64))
in = append(in, buf...)
out := make([]byte, len(in))
ref := hex.EncodeToString(in)
log.Printf("jobGWithdraw[%d] : in string : %s.\n", j.ID64, ref)
c.Encrypt(out, in)
ref = hex.EncodeToString(out)
log.Printf("jobGWithdraw[%d] : out string : %s.\n", j.ID64, ref)
ref = hex.EncodeToString(out)
m, err := getObjMsg(j.Trigger)
logOnError(err, "jobGWithdraw : getObjMsg")
p.CleanupMsg = append(p.CleanupMsg, *m)
b, err = json.Marshal(p)
log.Printf("jobGWithdraw[%d] : %s\n", j.ID64, string(b))
u, err := bot.ChatByID(fmt.Sprintf("%d", p.UserID64))
logOnError(err, "jobGWithdraw : ChatByID")
msg := fmt.Sprintf("Click to validate @%s's withdrawal of<code>%s</code>\n/withdraw_%s", u.Username, stock, string(ref))
cmd := TGCommand{
Type: commandSendMsg,
Text: msg,
ToChatID64: cfg.Bot.Mainchat,
ParseMode: cmdParseModeHTML,
}
TGCmdQueue <- cmd
} else {
cmd := TGCommand{
Type: commandReplyMsg,
Text: "No stock available whatsoever",
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
ParseMode: cmdParseModeHTML,
}
TGCmdQueue <- cmd
} }
TGCmdQueue <- cmd
} }
err = setJobDone(j.ID64) err = setJobDone(j.ID64)
@ -1903,6 +1755,111 @@ func jobGetHammerTime(j Job) {
return return
} }
func jobCraftItem(j Job) {
var (
p JobPayloadCraftItem
p2 JobPayloadGetVault
b []byte
item *ChatWarsItem
totalMana int64
requiredItems map[string]int64
missingItems map[string]int64
availableItems map[string]int64
craftItems map[string]int64
)
err := setJobStart(j.ID64)
logOnError(err, "jobCraftItem : setJobStart")
err = json.Unmarshal(j.Payload, &p)
logOnError(err, "jobCraftItem : Unmarshal payload")
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_recipe`])
p2.ItemTypeList = append(p2.ItemTypeList, cacheObjSubType[`item_part`])
b, err = json.Marshal(p2)
logOnError(err, "jobCraftItem : Marshal(p2)")
jobID64, err := createJob(cacheObjSubType[`job_get_vault`], objJobPriority, j.UserID64, 0, time.Now().UTC(), b)
p.Status = 1
p.VaultJobID64 = jobID64
b, err = json.Marshal(p)
logOnError(err, "jobCraftItem : Marshal(p)")
err = setJobPayloadJSON(j.ID64, p)
logOnError(err, "jobCraftItem : setJobPayloadJSON(p)")
rescheduleJob(j.ID64, 0, time.Unix(maxUnixTimestamp, 0).UTC())
return
}
b = getJobPayload(p.VaultJobID64)
err = json.Unmarshal(b, &p2)
logOnError(err, "jobCraftItem : Unmarshal(p2)")
item = getObjItem(p.ObjItemID64)
availableItems = make(map[string]int64)
requiredItems = make(map[string]int64)
missingItems = make(map[string]int64)
totalMana = item.Craft.Mana
for _, v := range item.Craft.Items {
log.Printf("jobCraftItem[%s] : %s - %d\n", item.Code, v.Code, v.Quantity)
requiredItems[v.Code] = v.Quantity
missingItems[v.Code] = 0
craftItems[v.Code] = 0
}
for _, v := range p2.Vault {
availableItems[v.Code] = v.Quantity
}
update := true
for update {
update = false
for code, req := range requiredItems {
ava, _ := availableItems[code]
craft, _ := craftItems[code]
missing, _ := missingItems[code]
if (ava + craft + missing) < req {
update = true
obj := getObjItem(getObjItemID(code, ``))
if obj.Craft != nil {
craftItems[code] = req - ava
totalMana = (req - ava) * obj.Craft.Mana
for _, v := range obj.Craft.Items {
req2, _ := requiredItems[v.Code]
requiredItems[v.Code] = req2 + v.Quantity*(req-ava)
}
} else {
missingItems[code] = req - ava
}
}
}
}
for code, req := range requiredItems {
ava, _ := availableItems[code]
craft, _ := craftItems[code]
missing, _ := missingItems[code]
log.Printf("jobCraftItem[%s] : %s - Req : %d / Ava : %d / Craft : %d / Missing : %d\n", item.Code, v.Code, req, ava, craft, missing)
}
/* we can finish the job */
err = setJobDone(j.ID64)
logOnError(err, "jobCraftItem : setJobDone")
return
}
func jobGetVault(j Job) { func jobGetVault(j Job) {
var ( var (
p JobPayloadGetVault p JobPayloadGetVault
@ -1911,8 +1868,6 @@ func jobGetVault(j Job) {
) )
items := []string{`item_res`, `item_alch`, `item_misc`, `item_recipe`, `item_part`, `item_other`} items := []string{`item_res`, `item_alch`, `item_misc`, `item_recipe`, `item_part`, `item_other`}
log.Printf("jobGetVault[%d] : Starting.\n", j.ID64)
reqTab = make(map[int64]int64) reqTab = make(map[int64]int64)
for k, v := range items { for k, v := range items {
reqTab[cacheObjSubType[v]] = 1 << uint(k) reqTab[cacheObjSubType[v]] = 1 << uint(k)
@ -1987,7 +1942,6 @@ func jobGetVault(j Job) {
} }
if (p.Status & reqTab[cacheObjSubType[`item_res`]]) == reqTab[cacheObjSubType[`item_res`]] { 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 &^ reqTab[cacheObjSubType[`item_res`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_res`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_res`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -1997,7 +1951,6 @@ func jobGetVault(j Job) {
clientSendCWMsg(j.UserID64, `/g_stock_res`) clientSendCWMsg(j.UserID64, `/g_stock_res`)
return return
} else if (p.Status & reqTab[cacheObjSubType[`item_alch`]]) == reqTab[cacheObjSubType[`item_alch`]] { } 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 &^ reqTab[cacheObjSubType[`item_alch`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_alch`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_alch`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -2007,7 +1960,6 @@ func jobGetVault(j Job) {
clientSendCWMsg(j.UserID64, `/g_stock_alch`) clientSendCWMsg(j.UserID64, `/g_stock_alch`)
return return
} else if (p.Status & reqTab[cacheObjSubType[`item_misc`]]) == reqTab[cacheObjSubType[`item_misc`]] { } 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 &^ reqTab[cacheObjSubType[`item_misc`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_misc`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_misc`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -2017,7 +1969,6 @@ func jobGetVault(j Job) {
clientSendCWMsg(j.UserID64, `/g_stock_misc`) clientSendCWMsg(j.UserID64, `/g_stock_misc`)
return return
} else if (p.Status & reqTab[cacheObjSubType[`item_recipe`]]) == reqTab[cacheObjSubType[`item_recipe`]] { } 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 &^ reqTab[cacheObjSubType[`item_recipe`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_recipe`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_recipe`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -2027,7 +1978,6 @@ func jobGetVault(j Job) {
clientSendCWMsg(j.UserID64, `/g_stock_rec`) clientSendCWMsg(j.UserID64, `/g_stock_rec`)
return return
} else if (p.Status & reqTab[cacheObjSubType[`item_part`]]) == reqTab[cacheObjSubType[`item_part`]] { } 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 &^ reqTab[cacheObjSubType[`item_part`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_part`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_part`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -2037,7 +1987,6 @@ func jobGetVault(j Job) {
clientSendCWMsg(j.UserID64, `/g_stock_parts`) clientSendCWMsg(j.UserID64, `/g_stock_parts`)
return return
} else if (p.Status & reqTab[cacheObjSubType[`item_other`]]) == reqTab[cacheObjSubType[`item_other`]] { } 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 &^ reqTab[cacheObjSubType[`item_other`]]
p.Status = p.Status | doneTab[cacheObjSubType[`item_other`]] p.Status = p.Status | doneTab[cacheObjSubType[`item_other`]]
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
@ -2050,23 +1999,17 @@ func jobGetVault(j Job) {
setJobPayloadJSON(j.ID64, p) setJobPayloadJSON(j.ID64, p)
} }
log.Printf("jobGetVault[%d] : Cleaning up.\n", j.ID64)
/* clean up the messages */ /* clean up the messages */
for _, d := range p.CleanupMsg { for _, d := range p.CleanupMsg {
clientDelTGMsg(j.UserID64, d.ID64, d.ChatID64) clientDelTGMsg(j.UserID64, d.ID64, d.ChatID64)
} }
log.Printf("jobGetVault[%d] : Rescheduling callback.\n", j.ID64)
/* wake up the callback */ /* wake up the callback */
err = rescheduleJob(p.JobCallbackID64, 0, time.Now().UTC()) err = rescheduleJob(p.JobCallbackID64, 0, time.Now().UTC())
logOnError(err, "jobGetVault : rescheduleJob") logOnError(err, "jobGetVault : rescheduleJob")
/* no more req to send, all ack came through, we can finish the job */ /* no more req to send, all ack came through, we can finish the job */
log.Printf("jobGetVault[%d] : All done.\n", j.ID64)
err = setJobDone(j.ID64) err = setJobDone(j.ID64)
logOnError(err, "jobGetVault : setJobDone") logOnError(err, "jobGetVault : setJobDone")

View File

@ -511,15 +511,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
case cacheObjSubType[`msg_bot_g_stock`]: case cacheObjSubType[`msg_bot_g_stock`]:
botGStock(m) botGStock(m)
case cacheObjSubType[`msg_bot_craft_item`]: case cacheObjSubType[`msg_bot_craft_item`]:
botCraftItem(m)
c := TGCommand{
Type: commandReplyMsg,
Text: `<b>test</b>`,
FromMsgID64: m.ID64,
FromChatID64: m.ChatID64,
ParseMode: cmdParseModeHTML,
}
TGCmdQueue <- c
case cacheObjSubType[`msg_bot_craft_all`]: case cacheObjSubType[`msg_bot_craft_all`]:
c := TGCommand{ c := TGCommand{
Type: commandReplyMsg, Type: commandReplyMsg,
@ -625,6 +617,8 @@ func JobWorker(id int, jobs <-chan Job) {
jobGetHammerTime(j) jobGetHammerTime(j)
case cacheObjSubType[`job_get_vault`]: case cacheObjSubType[`job_get_vault`]:
jobGetVault(j) jobGetVault(j)
case cacheObjSubType[`job_craft_item`]:
jobCraftItem(j)
default: default:
log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64) log.Printf("jobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID64)
} }