From 266aebd401237980608a47b03bf617f2b3690621 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 12 Jan 2020 21:21:34 +0800 Subject: [PATCH] test --- obj.go | 154 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 57 deletions(-) diff --git a/obj.go b/obj.go index 7c8e5ab..081f1a6 100644 --- a/obj.go +++ b/obj.go @@ -750,29 +750,6 @@ func addObjQuest(userID64 int64, questTypeID64 int64, duration time.Duration, da } func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool) (int64, error) { - muxObjItem.Lock() - defer muxObjItem.Unlock() - - if obj, ok := cacheObjItem[code]; ok { - if _, ok := cacheObjItem[name]; ok { - // obj is already added ? - return obj.ObjID64, nil - } else { - err := objAddName(obj.ObjID64, name) - if err != nil { - return 0, nil - } else { - obj.Names = append(obj.Names, name) - cacheObjItem[code] = obj - for _, n := range obj.Names { - cacheObjItem[n] = obj - } - return obj.ObjID64, nil - } - - } - } - tx, err := db.Begin() logOnError(err, "addObjItem : start transaction") if err != nil { @@ -834,23 +811,6 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exch err = objAddName(objId, name) logOnError(err, "addObjItem : add name") - c := new(ChatWarsItem) - c.ObjID64 = objId - c.ItemTypeID = itemTypeID64 - c.Code = code - n := make([]string, 1) - n = append(n, name) - c.Names = n - c.Weight = weight - c.Exchange = exchange - c.Auction = auction - cacheObjItem[code] = *c - cacheObjItem[name] = *c - - muxObjItemId.Lock() - cacheObjItemId[c.ObjID64] = *c - muxObjItemId.Unlock() - return objId, nil } @@ -948,12 +908,12 @@ func loadObjItem2() error { var items []ChatWarsItem muxObjItem.Lock() + defer muxObjItem.Unlock() cacheObjItem = make(map[string]ChatWarsItem) - muxObjItem.Unlock() muxObjItemId.Lock() + defer muxObjItemId.Unlock() cacheObjItemId = make(map[int64]ChatWarsItem) - muxObjItemId.Unlock() b, err := Asset("data/obj_item.json") logOnError(err, "loadObjItem2 : load data/obj_item.json") @@ -964,33 +924,113 @@ func loadObjItem2() error { err = json.Unmarshal(b, &items) for _, i := range items { - var id int64 = 0 - log.Printf("loadObjItem2[%s] : testing\n", i.Code) - for _, n := range i.Names { - log.Printf("loadObjItem2[%s] : testing %s\n", i.Code, n) - if id == 0 { - id = getSilentObjItemID(i.Code, ``) - log.Printf("loadObjItem2[%s] : %d\n", i.Code, id) - if id == 0 { - id, err = addObjItem(i.Code, n, i.ItemTypeID, i.Weight, i.Exchange, i.Auction) - logOnError(err, "loadObjItem2 : addObjItem") - } + if len(i.Names) == 0 { + log.Printf("loadObjItems2 : %s : name missing.\n", i.Code) + } else { + if obj, ok := cacheObjItem[i.Code]; ok { + log.Printf("loadObjItem2 : %s : duplicate code found. Belong to %s\n", i2.Code, n, obj.Code) } else { - objAddName(id, n) + i2 := ChatWarsItem{ + Auction: i.Auction, + Code: i.Code, + Exchange: i.Exchange, + ItemTypeID: i.ItemTypeID, + Weight: i.Weight, + Craft: ChatWarsItemCraft{}, + Names: make([]string, 1), + } + + for _, n := range i.Names { + if obj, ok := cacheObjItem[n]; ok { + log.Printf("loadObjItem2 : %s - %s : duplicate name found. Belongs to %s\n", i2.Code, n, obj.Code) + } else { + i2.Names = append(i2.Names, n) + } + } + + cacheObjItem[i.Code] = *i2 + for _, n := range i.Names { + cacheObjItem[n] = *i2 + } } } } + + objs, err := db.Query(`SELECT oi.obj_id, o.obj_sub_type_id, oi.intl_id, oi.weight FROM obj o, obj_item oi WHERE o.id = oi.obj_id;`) + if err != nil { + logOnError("loadObjItem2 : querying items") + return err + } + defer objs.Close() + + for objs.Next() { + err = objs.Scan(&id, &type_id, &intl_id, &weight) + if err != nil { + logOnError("loadObjItem2 : scanning items") + return err + } + + if obj, ok := cacheObjItem[intl_id]; !ok { + log.Printf("loadObjItem2 : %s : orphaned item in database (id : %d)\n", intl_id, id) + } else { + obj.ObjID64 = id + cacheObjItem[intl_id] = obj + for _, n := range obj.Names { + cacheObjItem[n] = obj + } + if weight != obj.Weight { + log.Printf("loadObjItem2 : %s : weight changed : %d => %d\n", weight, obj.Weight) + } + cacheObjItemID[id] = obj + } + } + + names, err := db.Query(`SELECT oi.obj_id, obn.name FROM obj_item oi, obj_name obn WHERE oi.obj_id = obn.obj_id;`) + if err != nil { + logOnError("loadObjItem2 : querying names") + return err + } + defer names.Close() + + for names.Next() { + err = names.Scan(&id, &name) + if err != nil { + logOnError("loadObjItem2 : scanning names") + return err + } + if obj, ok := cacheObjItem[name]; !ok { + if obj2, ok := cacheObjItemId[id]; ok { + log.Printf("loadObjItem2 : %s : orphaned name in database for item %s\n", name, obj2.Code) + } + } + } + + for _, i := range cacheObjItem { + if i.ObjID64 == 0 { + id, err := addObjItem(i.Code, i.Names[0], i.ItemTypeID, i.Weight, i.Exchange, i.Auction) + i.ObjID64 = id + cacheObjItemID[id] = obj + count := 0 + for _, n := range i.Names { + if count == 0 { + cacheObjItemID[n] = obj + continue + } else { + objAddName(id, n) + cacheObjItem[n] = obj + } + } + } + } + log.Printf("%d items loaded.\n", len(items)) - muxObjItemId.Lock() - log.Printf("Start loop for cached items\n") for _, v := range cacheObjItemId { log.Printf("Item cached : %d\n", v.ObjID64) for _, n := range v.Names { log.Printf("cacheObjItemId[%d] : %s : %s.\n", v.ObjID64, v.Names[0], n) } } - muxObjItemId.Unlock() return nil