This commit is contained in:
shoopea 2020-01-27 16:38:11 +08:00
parent 562e721998
commit 9e24501671
2 changed files with 44 additions and 17 deletions

50
item.go
View File

@ -18,7 +18,7 @@ var (
cacheObjItemCraft map[string]int64
)
func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool) (int64, error) {
func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool, craftable bool) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjItem : start transaction")
if err != nil {
@ -41,8 +41,8 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exch
return 0, err
}
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight, exchange, auction)
VALUES (?, ?, ?, ?, ?);`)
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight, exchange, auction, craftable)
VALUES (?, ?, ?, ?, ?, ?);`)
logOnError(err, "addObjItem : prepare insert obj_item")
if err != nil {
err2 := tx.Rollback()
@ -51,19 +51,18 @@ func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exch
}
defer stmt.Close()
var e, a int
var e, a, c int = 0, 0, 0
if exchange != `` {
e = 1
} else {
e = 0
}
if auction {
a = 1
} else {
a = 0
}
if craftable {
c = 1
}
_, err = stmt.Exec(objId, code, weight, e, a)
_, err = stmt.Exec(objId, code, weight, e, a, c)
logOnError(err, "addObjItem : exec insert obj_item")
if err != nil {
err2 := tx.Rollback()
@ -173,6 +172,27 @@ func getSilentObjItemID(code string, name string) int64 {
}
func setObjItemCraftable(objID64 int64, craftable bool) error {
var c int = 0
if craftable {
c = 1
}
stmt, err := db.Prepare(`UPDATE obj_item oi SET oi.craftable = ? WHERE oi.obj_id = ?;`)
logOnError(err, "setObjItemCraftable : prepare update obj_item")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(c, objID64)
logOnError(err, fmt.Sprintf("setObjItemCraftable : update obj_item(%d)", objID64))
if err != nil {
return err
}
return nil
}
func setObjItemWeight(objID64 int64, weight int64) error {
stmt, err := db.Prepare(`UPDATE obj_item oi SET oi.weight = ? WHERE oi.obj_id = ?;`)
logOnError(err, "setObjItemWeight : prepare update obj_item")
@ -262,6 +282,7 @@ func loadObjItem() error {
intl_id string
name string
weight int64
craftable int
)
muxObjItem.Lock()
@ -300,7 +321,7 @@ func loadObjItem() error {
}
}
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;`)
objs, err := db.Query(`SELECT oi.obj_id, o.obj_sub_type_id, oi.intl_id, oi.weight, oi.craftable FROM obj o, obj_item oi WHERE o.id = oi.obj_id;`)
if err != nil {
logOnError(err, "loadObjItem : querying items")
return err
@ -308,7 +329,7 @@ func loadObjItem() error {
defer objs.Close()
for objs.Next() {
err = objs.Scan(&obj_id, &type_id, &intl_id, &weight)
err = objs.Scan(&obj_id, &type_id, &intl_id, &weight, &craftable)
if err != nil {
logOnError(err, "loadObjItem : scanning items")
return err
@ -324,6 +345,11 @@ func loadObjItem() error {
setObjItemWeight(obj_id, objItems[id].Weight)
}
if (craftable == 0 && objItems[id].Craftable) || (craftable == 1 && !objItems[id].Craftable) {
log.Printf("loadObjItem : %s - %s : craftable changed : %v => %v\n", objItems[id].Code, objItems[id].Names[0], !objItems[id].Craftable, objItems[id].Craftable)
setObjItemCraftable(obj_id, objItems[id].Craftable)
}
cacheObjItemId[obj_id] = id
}
}
@ -350,7 +376,7 @@ func loadObjItem() error {
for _, i := range cacheObjItem {
if objItems[i].ObjID64 == 0 {
id, _ := addObjItem(objItems[i].Code, objItems[i].Names[0], objItems[i].ItemTypeID, objItems[i].Weight, objItems[i].Exchange, objItems[i].Auction)
id, _ := addObjItem(objItems[i].Code, objItems[i].Names[0], objItems[i].ItemTypeID, objItems[i].Weight, objItems[i].Exchange, objItems[i].Auction, objItems[i].Craftable)
objItems[i].ObjID64 = id
cacheObjItemId[id] = i
for _, n := range objItems[i].Names {

1
sql.go
View File

@ -88,6 +88,7 @@ func initDB() {
,weight SMALLINT NOT NULL
,exchange TINYINT NOT NULL
,auction TINYINT NOT NULL
,craftable TINYINT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (intl_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)