From e8f26b67a09d41a7aa880332455ccf5851ad93aa Mon Sep 17 00:00:00 2001 From: syspro86 Date: Mon, 27 Dec 2021 01:38:10 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B0=81=EC=9D=B8=EA=B3=84=EC=82=B0=EC=98=A4?= =?UTF-8?q?=EB=A5=98=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/loa/accessory.go | 80 ++++++++++++++++++++++---------------------- cmd/loa/cache.go | 48 ++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 cmd/loa/cache.go diff --git a/cmd/loa/accessory.go b/cmd/loa/accessory.go index fa016ad..d43fb56 100644 --- a/cmd/loa/accessory.go +++ b/cmd/loa/accessory.go @@ -1,10 +1,7 @@ package main import ( - "encoding/json" - "errors" "fmt" - "os" "runtime" "sort" "strings" @@ -111,24 +108,27 @@ func (job *AccessoryJob) Start() { } targetStats := job.Ctx.TargetStats.ToInts() - minGold := map[int]int{} + minGold := map[int][]int{} minGoldLock := sync.RWMutex{} getMinGold := func(level int) int { minGoldLock.Lock() defer minGoldLock.Unlock() if v, ok := minGold[level]; ok { - return v + return v[len(v)-1] } - minGold[level] = 1_000_000 - return minGold[level] + minGold[level] = []int{1_000_000} + return minGold[level][0] } setMinGold := func(level int, value int) { minGoldLock.Lock() defer minGoldLock.Unlock() for i := level; i <= job.Ctx.MaxDebuffLevel; i++ { - minGold[i] = value + minGold[i] = append(minGold[i], value) + if len(minGold[i]) > 100 { + minGold[i] = minGold[i][0:100] + } } } @@ -170,9 +170,9 @@ func (job *AccessoryJob) Start() { } if remainStep == 0 { debuffLevel := getDebuffLevel(itemSet.curDebuffs) - // if itemSet.curPrice >= getMinGold(debuffLevel) { - // return - // } + if itemSet.curPrice >= getMinGold(debuffLevel) { + return + } setMinGold(debuffLevel, itemSet.curPrice) reportResult(itemSet) log.WithField("itemSet", itemSet).Info("New Item Set") @@ -186,9 +186,9 @@ func (job *AccessoryJob) Start() { continue } } - // if itemSet.curPrice+item.Price > getMinGold(job.Ctx.MaxDebuffLevel) { - // continue - // } + if itemSet.curPrice+item.Price > getMinGold(job.Ctx.MaxDebuffLevel) { + continue + } hasUniqueName := false if item.UniqueName { for step2, index2 := range itemSet.itemIndexList { @@ -203,7 +203,18 @@ func (job *AccessoryJob) Start() { } nextBuffs := sumArray(itemSet.curBuffs, item.Buffs) - getInsufficientPoint := func(arr []int) int { + getMaxInsufficientPoint := func(arr []int) int { + pt := 0 + for i, num := range arr { + if num < job.TargetLevels[i] { + if pt < job.TargetLevels[i]-num { + pt = job.TargetLevels[i] - num + } + } + } + return pt + } + getTotalInsufficientPoint := func(arr []int) int { pt := 0 for i, num := range arr { if num < job.TargetLevels[i] { @@ -213,7 +224,10 @@ func (job *AccessoryJob) Start() { return pt } - if itemSet.step >= 3 && getInsufficientPoint(nextBuffs) > loa.Const.MaxBuffPointPerGrade[job.Ctx.Grade]*(remainStep-1) { + if itemSet.step >= 3 && getMaxInsufficientPoint(nextBuffs) > 3*(remainStep-1) { + continue + } + if itemSet.step >= 3 && getTotalInsufficientPoint(nextBuffs) > loa.Const.MaxBuffPointPerGrade[job.Ctx.Grade]*(remainStep-1) { continue } nextDebuffs := sumArray(itemSet.curDebuffs, item.Debuffs) @@ -455,32 +469,22 @@ func (job *AccessoryJob) searchAccessory() ([][]AccessoryItem, []bool) { } func (job *AccessoryJob) readOrSearchItem(category string, characterClass string, stepName string, grade string, buff1 string, buff2 string, buffLevel1 int, buffLevel2 int, stat1 string, stat2 string, quality string) ([][]string, string, bool) { - // filename - fileName := fmt.Sprintf("%s_%s_%d", stepName, buff1, buffLevel1) + cacheKey := fmt.Sprintf("%s_%s_%d", stepName, buff1, buffLevel1) if buff2 != "" { - fileName += fmt.Sprintf("_%s_%d", buff2, buffLevel2) + cacheKey += fmt.Sprintf("_%s_%d", buff2, buffLevel2) } if stat1 != "" { - fileName += fmt.Sprintf("_%s", stat1) + cacheKey += fmt.Sprintf("_%s", stat1) } if stat2 != "" { - fileName += fmt.Sprintf("_%s", stat2) + cacheKey += fmt.Sprintf("_%s", stat2) } - fileName += ".json" - // 캐시 데이터가 한시간 이상 지났으면 삭제 - if stat, err := os.Stat(toolConfig.CachePath + fileName); err == nil { - if stat.ModTime().Add(time.Hour * 24).Before(time.Now()) { - os.Remove(toolConfig.FileBase + fileName) - } - } - - if data, err := os.ReadFile(toolConfig.CachePath + fileName); err == nil { - tmp := new([][]string) - json.Unmarshal(data, tmp) - return *tmp, fileName, true + ret := loadCacheResult(cacheKey) + if len(ret) > 0 { + return ret, cacheKey, true } else if runtime.GOOS != "windows" { - return [][]string{}, fileName, true + return [][]string{}, cacheKey, true } else { job.Web.loginStove() job.Web.openAuction() @@ -538,12 +542,8 @@ func (job *AccessoryJob) readOrSearchItem(category string, characterClass string } job.LogWriter("log", fmt.Sprintf("검색 결과 [%d]건", len(ret))) if toolConfig.CacheSearchResult { - data, _ := json.MarshalIndent(ret, "", " ") - if _, err := os.Stat(toolConfig.CachePath); errors.Is(err, os.ErrNotExist) { - os.Mkdir(toolConfig.CachePath, 0755) - } - os.WriteFile(toolConfig.CachePath+fileName, data, 0644) + saveCacheResult(cacheKey, ret) } - return ret, fileName, true + return ret, cacheKey, true } } diff --git a/cmd/loa/cache.go b/cmd/loa/cache.go new file mode 100644 index 0000000..5619ca6 --- /dev/null +++ b/cmd/loa/cache.go @@ -0,0 +1,48 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "time" + + _ "github.com/go-sql-driver/mysql" +) + +func getCachePath(searchKey string) string { + return fmt.Sprintf("%s%s.json", toolConfig.CachePath, searchKey) +} + +func saveCacheResult(searchKey string, result [][]string) { + saveCacheResultFile(searchKey, result) + // saveCacheResultDB(searchKey+".json", result) +} + +func saveCacheResultFile(searchKey string, result [][]string) { + data, _ := json.MarshalIndent(result, "", " ") + if _, err := os.Stat(toolConfig.CachePath); errors.Is(err, os.ErrNotExist) { + os.Mkdir(toolConfig.CachePath, 0755) + } + os.WriteFile(getCachePath(searchKey), data, 0644) +} + +func loadCacheResult(searchKey string) [][]string { + return loadCacheResultFile(searchKey) + // return loadCacheResultDB(searchKey + ".json") +} + +func loadCacheResultFile(searchKey string) [][]string { + // 캐시 데이터가 한시간 이상 지났으면 삭제 + if stat, err := os.Stat(getCachePath(searchKey)); err == nil { + if stat.ModTime().Add(time.Hour * 24).Before(time.Now()) { + os.Remove(toolConfig.FileBase + searchKey) + } + } + if data, err := os.ReadFile(getCachePath(searchKey)); err == nil { + tmp := new([][]string) + json.Unmarshal(data, tmp) + return *tmp + } + return [][]string{} +}