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

86
job.go
View File

@ -1938,9 +1938,13 @@ func jobCraftAll(j Job) {
p JobPayloadCraftAll p JobPayloadCraftAll
p2 JobPayloadGetVault p2 JobPayloadGetVault
b []byte b []byte
partsItems map[int64]string itemParts map[int64]string
recipesItems map[int64]string itemRecipes map[int64]string
ratioItems map[string]int64 ratioItems map[string]int64
totalParts map[string]int64
totalRecipes map[string]int64
completeItems map[string]float64
maxItems int64
) )
err := setJobStart(j.ID64) err := setJobStart(j.ID64)
@ -1977,9 +1981,12 @@ func jobCraftAll(j Job) {
err = json.Unmarshal(b, &p2) err = json.Unmarshal(b, &p2)
logOnError(err, "jobCraftAll : Unmarshal(p2)") logOnError(err, "jobCraftAll : Unmarshal(p2)")
partsItems = make(map[string]int64) itemParts = make(map[int64]string)
recipesItems = make(map[string]int64) itemRecipes = make(map[int64]string)
ratioItems = make(map[string]int64) ratioItems = make(map[string]int64)
totalParts = make(map[string]int64)
totalRecipes = make(map[string]int64)
completeItems = make(map[string]float64)
muxObjItem.RLock() muxObjItem.RLock()
for _, o := range objItems { for _, o := range objItems {
@ -1989,9 +1996,9 @@ func jobCraftAll(j Job) {
p, _ := getObjItem(i.ItemID64) p, _ := getObjItem(i.ItemID64)
if p.ItemTypeID == cacheObjSubType[`item_part`] { if p.ItemTypeID == cacheObjSubType[`item_part`] {
ratioItems[o.Code] = i.Quantity ratioItems[o.Code] = i.Quantity
partsItems[p.ObjID64] = o.Code itemParts[p.ObjID64] = o.Code
} else if p.ItemTypeID == cacheObjSubType[`item_recipe`] { } else if p.ItemTypeID == cacheObjSubType[`item_recipe`] {
recipesItems[p.ObjID64] = o.Code itemRecipes[p.ObjID64] = o.Code
} }
} }
} }
@ -2000,33 +2007,58 @@ func jobCraftAll(j Job) {
muxObjItem.RUnlock() muxObjItem.RUnlock()
for _, i := range p2.Vault { 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 */ /* we can finish the job */
/* out := fmt.Sprintf("<code>Vault crafting summary\n")
out := fmt.Sprintf("<code>Summary for %d %s\n", p.Quantity, item.Names[0])
out = fmt.Sprintf("%s Mana : %d\n", out, totalMana) for maxItems > 0 {
out = fmt.Sprintf("%s Items :\n", out) out = fmt.Sprintf("%s%d Items :\n", out, maxItems)
for k, v := range requiredItems { for k, v := range completeItems {
obj, _ := getObjItem(getObjItemID(k, ``)) if maxItems == int64(v) {
ava, _ := availableItems[k] o, _ := getObjItem(getSilentObjItemID(k, ``))
out = fmt.Sprintf("%s [%s] %s : %d (%d)\n", out, obj.Code, obj.Names[0], v, ava) out = fmt.Sprintf("%s %s - %s\n", out, k, o.Names[0])
}
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) maxItems -= 1
for k, v := range craftItems { }
if v > 0 {
obj, _ := getObjItem(getObjItemID(k, ``)) out = fmt.Sprintf("%s1 Part missing :\n", out)
out = fmt.Sprintf("%s [%s] %s : %d\n", out, obj.Code, obj.Names[0], v) 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) out = fmt.Sprintf("%s</code>", out)
c := TGCommand{ c := TGCommand{
@ -2037,9 +2069,9 @@ func jobCraftAll(j Job) {
ParseMode: cmdParseModeHTML, ParseMode: cmdParseModeHTML,
} }
TGCmdQueue <- c TGCmdQueue <- c
*/
err = setJobDone(j.ID64) err = setJobDone(j.ID64)
logOnError(err, "jobCraftItem : setJobDone") logOnError(err, "jobCraftAll : setJobDone")
return 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 { if a < b {
return a return a
} else { } 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 { if a > b {
return a return a
} else { } else {