test craft all

This commit is contained in:
shoopea 2020-02-01 15:00:33 +08:00
parent 87278e293d
commit b70dff2700
2 changed files with 77 additions and 45 deletions

118
job.go
View File

@ -1935,12 +1935,16 @@ func jobCraftItem(j Job) {
func jobCraftAll(j Job) {
var (
p JobPayloadCraftAll
p2 JobPayloadGetVault
b []byte
partsItems map[int64]string
recipesItems map[int64]string
ratioItems map[string]int64
p JobPayloadCraftAll
p2 JobPayloadGetVault
b []byte
itemParts map[int64]string
itemRecipes map[int64]string
ratioItems map[string]int64
totalParts map[string]int64
totalRecipes map[string]int64
completeItems map[string]float64
maxItems int64
)
err := setJobStart(j.ID64)
@ -1977,9 +1981,12 @@ func jobCraftAll(j Job) {
err = json.Unmarshal(b, &p2)
logOnError(err, "jobCraftAll : Unmarshal(p2)")
partsItems = make(map[string]int64)
recipesItems = make(map[string]int64)
itemParts = make(map[int64]string)
itemRecipes = make(map[int64]string)
ratioItems = make(map[string]int64)
totalParts = make(map[string]int64)
totalRecipes = make(map[string]int64)
completeItems = make(map[string]float64)
muxObjItem.RLock()
for _, o := range objItems {
@ -1989,9 +1996,9 @@ func jobCraftAll(j Job) {
p, _ := getObjItem(i.ItemID64)
if p.ItemTypeID == cacheObjSubType[`item_part`] {
ratioItems[o.Code] = i.Quantity
partsItems[p.ObjID64] = o.Code
itemParts[p.ObjID64] = o.Code
} else if p.ItemTypeID == cacheObjSubType[`item_recipe`] {
recipesItems[p.ObjID64] = o.Code
itemRecipes[p.ObjID64] = o.Code
}
}
}
@ -2000,46 +2007,71 @@ func jobCraftAll(j Job) {
muxObjItem.RUnlock()
for _, i := range p2.Vault {
if item, ok := itemsParts[i.ItemID64]; ok {
totalParts[item] = i.Quantity
} else if item, ok := itemsRecipes[i.ItemID64]; ok {
totalRecipes[item] = i.Quantity
}
}
for k, i := range ratioItems {
recipes, _ := totalRecipes[k]
parts, _ := totalParts[k]
if (recipes > 0 && parts > (i-1)) || (parts >= i) {
completeItems[k] = MinInt64(recipes*i, parts) / i
maxItems = MaxInt64(maxItems, int64(completeItems[k]))
}
}
/* we can finish the job */
/*
out := fmt.Sprintf("<code>Summary for %d %s\n", p.Quantity, item.Names[0])
out = fmt.Sprintf("%s Mana : %d\n", out, totalMana)
out = fmt.Sprintf("%s Items :\n", out)
for k, v := range requiredItems {
obj, _ := getObjItem(getObjItemID(k, ``))
ava, _ := availableItems[k]
out = fmt.Sprintf("%s [%s] %s : %d (%d)\n", out, obj.Code, obj.Names[0], v, ava)
}
out = fmt.Sprintf("%s Missing :\n", out)
for k, v := range missingItems {
if v > 0 {
obj, _ := getObjItem(getObjItemID(k, ``))
out = fmt.Sprintf("%s [%s] %s : %d\n", out, obj.Code, obj.Names[0], v)
}
}
out = fmt.Sprintf("%s To craft :\n", out)
for k, v := range craftItems {
if v > 0 {
obj, _ := getObjItem(getObjItemID(k, ``))
out = fmt.Sprintf("%s [%s] %s : %d\n", out, obj.Code, obj.Names[0], v)
}
}
out = fmt.Sprintf("%s</code>", out)
out := fmt.Sprintf("<code>Vault crafting summary\n")
c := TGCommand{
Type: commandReplyMsg,
Text: out,
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
ParseMode: cmdParseModeHTML,
for maxItems > 0 {
out = fmt.Sprintf("%s%d Items :\n", out, maxItems)
for k, v := range completeItems {
if maxItems == int64(v) {
o, _ := getObjItem(getSilentObjItemID(k, ``))
out = fmt.Sprintf("%s %s - %s\n", out, k, o.Names[0])
}
}
TGCmdQueue <- c
*/
maxItems -= 1
}
out = fmt.Sprintf("%s1 Part missing :\n", out)
for k, v := range completeItems {
if int64(v) == 0 {
parts, _ := totalParts[k]
if parts == ratioItems[k]-1 {
o, _ := getObjItem(getSilentObjItemID(k, ``))
out = fmt.Sprintf("%s %s - %s\n", out, k, o.Names[0])
}
}
}
out = fmt.Sprintf("%sRecipe missing :\n", out)
for k, v := range completeItems {
if int64(v) == 0 {
recipe, _ := totalRecipes[k]
if recipe == 0 {
o, _ := getObjItem(getSilentObjItemID(k, ``))
out = fmt.Sprintf("%s %s - %s\n", out, k, o.Names[0])
}
}
}
out = fmt.Sprintf("%s</code>", out)
c := TGCommand{
Type: commandReplyMsg,
Text: out,
FromMsgID64: p.MsgID64,
FromChatID64: p.ChatID64,
ParseMode: cmdParseModeHTML,
}
TGCmdQueue <- c
err = setJobDone(j.ID64)
logOnError(err, "jobCraftItem : setJobDone")
logOnError(err, "jobCraftAll : setJobDone")
return

View File

@ -22,7 +22,7 @@ func logOnError(err error, msg string) {
}
}
func MinInt(a int, b int) int {
func MinInt64(a int64, b int64) int64 {
if a < b {
return a
} else {
@ -30,7 +30,7 @@ func MinInt(a int, b int) int {
}
}
func MaxInt(a int, b int) int {
func MaxInt(a int64, b int64) int64 {
if a > b {
return a
} else {