test mux for obj

This commit is contained in:
shoopea 2019-07-22 15:40:52 +08:00
parent b83a8c2787
commit 57000e33af
3 changed files with 58 additions and 51 deletions

View File

@ -6,13 +6,13 @@ import (
func reloadObjItem(code string, name string, itemTypeID64 int64, weight int) {
var objID64 int64
objID64 = silentGetObjItemID(code, ``)
objID64 = getSilentObjItemID(code, ``)
if objID64 == 0 {
id, err := addObjItem(code, name, itemTypeID64, weight)
objID64 = id
logOnError(err, "reloadObjItem : addObjItem")
}
if silentGetObjItemID(``, name) == 0 {
if getSilentObjItemID(``, name) == 0 {
log.Printf("Adding %s to %d.\n", name, objID64)
objAddName(objID64, name)
c := new(ChatWarsItem)
@ -20,12 +20,16 @@ func reloadObjItem(code string, name string, itemTypeID64 int64, weight int) {
c.Code = code
c.Name = name
c.Weight = weight
cacheObjItem.Store(name, *c)
muxObjItem.Lock()
cacheObjItem[name] = c
muxObjItem.Unlock()
}
}
func resetObjItem() {
log.Print("Reloading items")
reloadObjItem(`01`, `Thread`, objSubTypeItemResource, 1)
reloadObjItem(`02`, `Stick`, objSubTypeItemResource, 1)
reloadObjItem(`03`, `Pelt`, objSubTypeItemResource, 1)

View File

@ -125,7 +125,6 @@ func main() {
err = loadObjItem()
logOnError(err, "Caching items")
resetObjItem()
err = loadObjMsg()
logOnError(err, "Caching msgs")

94
obj.go
View File

@ -14,8 +14,10 @@ var (
cacheObjCastle *sync.Map
cacheObjGuild *sync.Map
cacheObjUser *sync.Map
cacheObjItem *sync.Map
cacheObjMsg *sync.Map
cacheObjItem map[string]ChatWarsItem
muxObjItem sync.Mutex
)
func getObjTypeId(objId int64) (int64, error) {
@ -586,21 +588,25 @@ func addObjQuest(userID64 int64, questTypeID int, duration time.Duration, date t
}
func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64, error) {
if v, ok := cacheObjItem.Load(code); ok {
item := v.(ChatWarsItem)
if v, ok = cacheObjItem.Load(name); ok {
muxObjItem.Lock()
defer muxObjItem.Unlock()
if obj1, ok := cacheObjItem[code]; ok {
if obj2, ok := cacheObjItem[name]; ok {
// obj is already added ?
return item.ObjID64, nil
return obj1.ObjID64, nil
} else {
err := objAddName(item.ObjID64, name)
err := objAddName(obj1.ObjID64, name)
if err != nil {
return 0, nil
} else {
return item.ObjID64, nil
cacheObjItem[name] = obj1
return obj1.ObjID64, nil
}
}
}
tx, err := db.Begin()
logOnError(err, "addObjItem : start transaction")
if err != nil {
@ -655,14 +661,14 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
c.Code = code
c.Name = name
c.Weight = weight
cacheObjItem.Store(code, *c)
cacheObjItem.Store(name, *c)
cacheObjItem[code] = c
cacheObjItem[name] = c
return objId, nil
}
func getObjItemID(c string, n string) int64 {
i := silentGetObjItemID(c, n)
i := getSilentObjItemID(c, n)
if i == 0 {
w := TGCommand{
Type: commandSendMsg,
@ -674,12 +680,13 @@ func getObjItemID(c string, n string) int64 {
return i
}
func silentGetObjItemID(code string, name string) int64 {
func getSilentObjItemID(code string, name string) int64 {
muxObjItem.Lock()
defer muxObjItem.Unlock()
if len(code) > 0 {
if v, ok := cacheObjItem.Load(code); ok {
item := v.(ChatWarsItem)
//log.Printf("Matching item code %s with %s.\n", code, item.Code)
return item.ObjID64
if obj, ok := cacheObjItem[code]; ok {
//log.Printf("Matching item code %s with %s.\n", code, obj.Code)
return obj.ObjID64
}
if ok, _ := regexp.MatchString(`^u[0-9]+`, code); !ok {
return 0
@ -688,45 +695,34 @@ func silentGetObjItemID(code string, name string) int64 {
if len(name) == 0 {
return 0
}
if v, ok := cacheObjItem.Load(name); ok {
item := v.(ChatWarsItem)
//log.Printf("Matching item name %s with %s.\n", name, item.Name)
return item.ObjID64
if obj, ok := cacheObjItem[name]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
return obj.ObjID64
}
if ok, _ := regexp.MatchString(`^((u|e)[0-9]+|(a|w)[0-9]+[a-e]{0,1})$`, code); ok || len(code) == 0 {
r := regexp.MustCompile(`^((?P<Modifier>⚡\+[0-9]+) ){0,1}(?P<BaseName>.+?)( \+(?P<Atk>[0-9]+)⚔){0,1}( \+(?P<Def>[0-9]+)🛡){0,1}( \+(?P<Mana>[0-9]+)💧){0,1}$`)
basename := r.ReplaceAllString(name, "${BaseName}")
if v, ok := cacheObjItem.Load(basename); ok && len(basename) > 0 {
item := v.(ChatWarsItem)
//log.Printf("Matching item full basename %s with %s.\n", basename, item.Name)
return item.ObjID64
if obj, ok := cacheObjItem[basename]; ok && len(basename) > 0 {
//log.Printf("Matching item full basename %s with %s.\n", basename, obj.Name)
return obj.ObjID64
}
item := ChatWarsItem{
i := ChatWarsItem{
ObjID64: 0,
}
match := false
cacheObjItem.Range(func(k, v interface{}) bool {
item = v.(ChatWarsItem)
if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, item.Code); !ok { //only gear can be custom named
return true
}
m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(item.Name), regexp.QuoteMeta(item.Name))
for k, obj := range items {
if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, obj.Code); ok { //only gear can be custom named
m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(obj.Name), regexp.QuoteMeta(obj.Name))
if ok, _ := regexp.MatchString(m, basename); ok {
//log.Printf("LOOP : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
match = true
return false
} else {
//log.Printf("Not matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
item = ChatWarsItem{
ObjID64: 0,
i = obj
break
}
}
return true
}
})
if item.ObjID64 != 0 && match {
if i.ObjID64 != 0 {
//log.Printf("RETURN : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
return item.ObjID64
return i.ObjID64
} else {
/*
fmt.Printf("silentGetObjItemID : Modifier : `%s`\n", r.ReplaceAllString(name, "${Modifier}"))
@ -750,7 +746,9 @@ func loadObjItem() error {
weight int
)
cacheObjItem = new(sync.Map)
muxObjItem.Lock()
cacheObjItem = make(map[string]ChatWarsItem)
muxObjItem.Unlock()
items, err := db.Query(`SELECT oi.obj_id, oi.intl_id, obn.name, oi.weight FROM obj_item oi, obj_name obn WHERE obn.obj_id = oi.obj_id AND obn.priority = 0;`)
if err != nil {
@ -768,8 +766,10 @@ func loadObjItem() error {
c.Code = intl_id
c.Name = name
c.Weight = weight
cacheObjItem.Store(intl_id, *c)
cacheObjItem.Store(name, *c)
muxObjItem.Lock()
cacheObjItem[intl_id] = c
cacheObjItem[name] = c
muxObjItem.Unlock()
}
items2, err := db.Query(`SELECT oi.obj_id, oi.intl_id, obn.name, oi.weight FROM obj_item oi, obj_name obn WHERE obn.obj_id = oi.obj_id AND obn.priority > 0;`)
@ -790,11 +790,15 @@ func loadObjItem() error {
c.Code = intl_id
c.Name = name
c.Weight = weight
cacheObjItem.Store(name, *c)
muxObjItem.Lock()
cacheObjItem[name] = c
muxObjItem.Unlock()
} else {
log.Printf("loadObjItem : orphaned obj_name for `%s` : %s.\n", intl_id, name)
}
}
resetObjItem()
return nil
}