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

View File

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

98
obj.go
View File

@ -14,8 +14,10 @@ var (
cacheObjCastle *sync.Map cacheObjCastle *sync.Map
cacheObjGuild *sync.Map cacheObjGuild *sync.Map
cacheObjUser *sync.Map cacheObjUser *sync.Map
cacheObjItem *sync.Map
cacheObjMsg *sync.Map cacheObjMsg *sync.Map
cacheObjItem map[string]ChatWarsItem
muxObjItem sync.Mutex
) )
func getObjTypeId(objId int64) (int64, error) { 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) { func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64, error) {
if v, ok := cacheObjItem.Load(code); ok { muxObjItem.Lock()
item := v.(ChatWarsItem) defer muxObjItem.Unlock()
if v, ok = cacheObjItem.Load(name); ok {
if obj1, ok := cacheObjItem[code]; ok {
if obj2, ok := cacheObjItem[name]; ok {
// obj is already added ? // obj is already added ?
return item.ObjID64, nil return obj1.ObjID64, nil
} else { } else {
err := objAddName(item.ObjID64, name) err := objAddName(obj1.ObjID64, name)
if err != nil { if err != nil {
return 0, nil return 0, nil
} else { } else {
return item.ObjID64, nil cacheObjItem[name] = obj1
return obj1.ObjID64, nil
} }
} }
} }
tx, err := db.Begin() tx, err := db.Begin()
logOnError(err, "addObjItem : start transaction") logOnError(err, "addObjItem : start transaction")
if err != nil { if err != nil {
@ -655,14 +661,14 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
c.Code = code c.Code = code
c.Name = name c.Name = name
c.Weight = weight c.Weight = weight
cacheObjItem.Store(code, *c) cacheObjItem[code] = c
cacheObjItem.Store(name, *c) cacheObjItem[name] = c
return objId, nil return objId, nil
} }
func getObjItemID(c string, n string) int64 { func getObjItemID(c string, n string) int64 {
i := silentGetObjItemID(c, n) i := getSilentObjItemID(c, n)
if i == 0 { if i == 0 {
w := TGCommand{ w := TGCommand{
Type: commandSendMsg, Type: commandSendMsg,
@ -674,12 +680,13 @@ func getObjItemID(c string, n string) int64 {
return i 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 len(code) > 0 {
if v, ok := cacheObjItem.Load(code); ok { if obj, ok := cacheObjItem[code]; ok {
item := v.(ChatWarsItem) //log.Printf("Matching item code %s with %s.\n", code, obj.Code)
//log.Printf("Matching item code %s with %s.\n", code, item.Code) return obj.ObjID64
return item.ObjID64
} }
if ok, _ := regexp.MatchString(`^u[0-9]+`, code); !ok { if ok, _ := regexp.MatchString(`^u[0-9]+`, code); !ok {
return 0 return 0
@ -688,45 +695,34 @@ func silentGetObjItemID(code string, name string) int64 {
if len(name) == 0 { if len(name) == 0 {
return 0 return 0
} }
if v, ok := cacheObjItem.Load(name); ok { if obj, ok := cacheObjItem[name]; ok {
item := v.(ChatWarsItem) //log.Printf("Matching item name %s with %s.\n", name, obj.Name)
//log.Printf("Matching item name %s with %s.\n", name, item.Name) return obj.ObjID64
return item.ObjID64
} }
if ok, _ := regexp.MatchString(`^((u|e)[0-9]+|(a|w)[0-9]+[a-e]{0,1})$`, code); ok || len(code) == 0 { 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}$`) 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}") basename := r.ReplaceAllString(name, "${BaseName}")
if v, ok := cacheObjItem.Load(basename); ok && len(basename) > 0 { if obj, ok := cacheObjItem[basename]; ok && len(basename) > 0 {
item := v.(ChatWarsItem) //log.Printf("Matching item full basename %s with %s.\n", basename, obj.Name)
//log.Printf("Matching item full basename %s with %s.\n", basename, item.Name) return obj.ObjID64
return item.ObjID64
} }
item := ChatWarsItem{ i := ChatWarsItem{
ObjID64: 0, ObjID64: 0,
} }
match := false for k, obj := range items {
cacheObjItem.Range(func(k, v interface{}) bool { if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, obj.Code); ok { //only gear can be custom named
item = v.(ChatWarsItem) m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(obj.Name), regexp.QuoteMeta(obj.Name))
if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, item.Code); !ok { //only gear can be custom named if ok, _ := regexp.MatchString(m, basename); ok {
return true //log.Printf("LOOP : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
} i = obj
m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(item.Name), regexp.QuoteMeta(item.Name)) break
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,
} }
return true
} }
}
}) if i.ObjID64 != 0 {
if item.ObjID64 != 0 && match {
//log.Printf("RETURN : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64) //log.Printf("RETURN : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
return item.ObjID64 return i.ObjID64
} else { } else {
/* /*
fmt.Printf("silentGetObjItemID : Modifier : `%s`\n", r.ReplaceAllString(name, "${Modifier}")) fmt.Printf("silentGetObjItemID : Modifier : `%s`\n", r.ReplaceAllString(name, "${Modifier}"))
@ -750,7 +746,9 @@ func loadObjItem() error {
weight int 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;`) 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 { if err != nil {
@ -768,8 +766,10 @@ func loadObjItem() error {
c.Code = intl_id c.Code = intl_id
c.Name = name c.Name = name
c.Weight = weight c.Weight = weight
cacheObjItem.Store(intl_id, *c) muxObjItem.Lock()
cacheObjItem.Store(name, *c) 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;`) 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.Code = intl_id
c.Name = name c.Name = name
c.Weight = weight c.Weight = weight
cacheObjItem.Store(name, *c) muxObjItem.Lock()
cacheObjItem[name] = c
muxObjItem.Unlock()
} else { } else {
log.Printf("loadObjItem : orphaned obj_name for `%s` : %s.\n", intl_id, name) log.Printf("loadObjItem : orphaned obj_name for `%s` : %s.\n", intl_id, name)
} }
} }
resetObjItem()
return nil return nil
} }