This commit is contained in:
shoopea 2019-06-14 18:13:10 +08:00
parent bf05f656b3
commit c5bbc2403f
2 changed files with 65 additions and 5 deletions

59
obj.go
View File

@ -65,6 +65,15 @@ func setObjSubTypeId(objId int64, objSubTypeID64 int64) error {
return err
}
func objAddName(objID64 int64, name string) error {
stmt, err := db.Prepare(`INSERT INTO obj_name (obj_id, name, priority)
SELECT ? obj_id, ? name, (SELECT MAX(priority) + 1 FROM obj_name WHERE obj_id = ?) priority FROM DUAL;`)
_, err := stmt.Exec(objID64, name, objID64)
return err
}
func addObjMsg(msgID64 int64, msgChatID64 int64, msgUserID64 int64, msgSenderUserID64 int64, msgDate time.Time, msgText string) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjMsg : start transaction")
@ -528,6 +537,21 @@ func addObjXP(userID64 int64, expNow int64, expLvl int64, level int64, date time
}
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 {
// obj is already added ?
return item.ObjID64, nil
} else {
err := objAddName(item.ObjID64, name)
if err != nil {
return 0, nil
} else {
return item.ObjID64, nil
}
}
}
tx, err := db.Begin()
logOnError(err, "addObjItem : start transaction")
if err != nil {
@ -550,8 +574,8 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
return 0, err
}
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, name, weight)
VALUES (?, ?, ?, ?);`)
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight)
VALUES (?, ?, ?);`)
logOnError(err, "addObjItem : prepare insert obj_item")
if err != nil {
err2 := tx.Rollback()
@ -560,7 +584,7 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
}
defer stmt.Close()
_, err = stmt.Exec(objId, code, name, weight)
_, err = stmt.Exec(objId, code, weight)
logOnError(err, "addObjItem : exec insert obj_item")
if err != nil {
err2 := tx.Rollback()
@ -574,6 +598,9 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int) (int64
return 0, err
}
err = objAddName(objId, name)
logOnError(err, "addObjItem : add name")
c := new(ChatWarsItem)
c.ObjID64 = objId
c.Code = code
@ -670,7 +697,7 @@ func loadObjItem() error {
cacheObjItem = new(sync.Map)
items, err := db.Query(`SELECT oi.obj_id, oi.intl_id, oi.name, oi.weight FROM obj_item oi;`)
items, err := db.Query(`SELECT oi.obj_id, oi.intl_id, on.name, oi.weight FROM obj_item oi, obj_name on WHERE on.obj_id = oi.obj_id AND on.priority = 0;`)
if err != nil {
return err
}
@ -690,5 +717,29 @@ func loadObjItem() error {
cacheObjItem.Store(name, *c)
}
items2, err := db.Query(`SELECT oi.obj_id, oi.intl_id, on.name, oi.weight FROM obj_item oi, obj_name on WHERE on.obj_id = oi.obj_id AND on.priority > 0;`)
if err != nil {
return err
}
defer items2.Close()
for items2.Next() {
err = items2.Scan(&id, &intl_id, &name, &weight)
if err != nil {
return err
}
if v, ok := cacheObjItem.Load(intl_id); ok {
c := new(ChatWarsItem)
c.ObjID64 = id
c.Code = intl_id
c.Name = name
c.Weight = weight
cacheObjItem.Store(name, *c)
} else {
log.Printf("loadObjItem : orphaned obj_name for `%s` : %s.\n", intl_id, name)
}
}
return nil
}

11
sql.go
View File

@ -273,7 +273,6 @@ func initDB() {
_, err = db.Exec(`CREATE TABLE obj_item (
obj_id BIGINT UNSIGNED NOT NULL
,intl_id VARCHAR(32)
,name VARCHAR(80)
,weight SMALLINT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (intl_id)
@ -332,6 +331,16 @@ func initDB() {
failOnError(err, "initDB : create table obj_job")
log.Println("initDB : obj_job created ...")
_, err = db.Exec(`CREATE TABLE obj_name (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(80)
,priority INT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY(name)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_name")
log.Println("initDB : obj_name created ...")
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
VALUES (` + strconv.Itoa(objTypeUser) + `, "user", "User")
,(` + strconv.Itoa(objTypeGuild) + `, "guild", "Guild")