chirpnest/item.go

422 lines
12 KiB
Go
Raw Normal View History

2020-01-26 07:38:41 +01:00
package main
2020-01-26 07:48:22 +01:00
import (
2020-01-26 07:50:27 +01:00
"encoding/json"
2020-01-26 07:48:22 +01:00
"errors"
"fmt"
2020-01-26 07:49:27 +01:00
"log"
2020-01-26 07:48:22 +01:00
"regexp"
"strconv"
"sync"
)
2020-01-26 07:38:41 +01:00
var (
2020-01-26 14:40:20 +01:00
muxObjItem sync.RWMutex
objItems []ChatWarsItem
cacheObjItem map[string]int64
cacheObjItemId map[int64]int64
cacheObjItemCraft map[string]int64
2020-01-26 07:38:41 +01:00
)
2020-01-27 09:38:11 +01:00
func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool, craftable bool) (int64, error) {
2020-01-26 07:38:41 +01:00
tx, err := db.Begin()
logOnError(err, "addObjItem : start transaction")
if err != nil {
return 0, err
}
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
VALUES (` + strconv.FormatInt(cacheObjType[`item`], 10) + `,` + fmt.Sprintf("%d", itemTypeID64) + `);`)
logOnError(err, "addObjItem : exec insert obj ("+code+", "+name+")")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjItem : rollback insert obj")
return 0, err
}
objId, err := res.LastInsertId()
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjItem : rollback get lastInsertId")
return 0, err
}
2020-01-27 09:38:11 +01:00
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight, exchange, auction, craftable)
VALUES (?, ?, ?, ?, ?, ?);`)
2020-01-26 07:38:41 +01:00
logOnError(err, "addObjItem : prepare insert obj_item")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjItem : rollback prepare insert obj_item")
return 0, err
}
defer stmt.Close()
2020-01-27 09:38:11 +01:00
var e, a, c int = 0, 0, 0
2020-01-26 07:38:41 +01:00
if exchange != `` {
e = 1
}
if auction {
a = 1
2020-01-27 09:38:11 +01:00
}
if craftable {
c = 1
2020-01-26 07:38:41 +01:00
}
2020-01-27 09:38:11 +01:00
_, err = stmt.Exec(objId, code, weight, e, a, c)
2020-01-26 07:38:41 +01:00
logOnError(err, "addObjItem : exec insert obj_item")
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjItem : rollback exec insert obj_item")
return 0, err
}
err = tx.Commit()
logOnError(err, "addObjItem : commit")
if err != nil {
return 0, err
}
err = objAddName(objId, name)
logOnError(err, "addObjItem : add name")
return objId, nil
}
func getObjItem(objID64 int64) (*ChatWarsItem, error) {
muxObjItem.RLock()
defer muxObjItem.RUnlock()
if id, ok := cacheObjItemId[objID64]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
2020-01-26 08:08:18 +01:00
return &objItems[id], nil
2020-01-26 07:38:41 +01:00
} else {
return nil, errors.New("Item not found.")
}
}
func getObjItemID(c string, n string) int64 {
return getVerboseObjItemID(c, n)
}
func getVerboseObjItemID(c string, n string) int64 {
2020-01-26 08:12:32 +01:00
i := getSilentObjItemID(c, n)
2020-01-26 07:38:41 +01:00
if i == 0 {
w := TGCommand{
Type: commandSendMsg,
Text: fmt.Sprintf("Object unknown : %s - %s\n", c, n),
ToUserID64: cfg.Bot.Admin,
}
TGCmdQueue <- w
}
return i
}
func getSilentObjItemID(code string, name string) int64 {
muxObjItem.RLock()
defer muxObjItem.RUnlock()
if len(code) > 0 {
if id, ok := cacheObjItem[code]; ok {
//log.Printf("Matching item code %s with %s.\n", code, obj.Code)
return objItems[id].ObjID64
}
if ok, _ := regexp.MatchString(`^(a|w)[0-9]+[a-e]$`, code); ok {
// log.Printf("Matching quality item code %s with %s.\n", code, code[:len(code)-1])
if id, ok := cacheObjItem[code[:len(code)-1]]; ok {
return objItems[id].ObjID64
}
}
if ok, _ := regexp.MatchString(`^u[0-9]+`, code); !ok {
return 0
}
}
if len(name) == 0 {
return 0
}
if id, ok := cacheObjItem[name]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
return objItems[id].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 id, ok := cacheObjItem[basename]; ok && len(basename) > 0 {
//log.Printf("Matching item full basename %s with %s.\n", basename, obj.Name)
return objItems[id].ObjID64
}
2020-01-26 07:49:27 +01:00
i := int64(-1)
2020-01-26 07:38:41 +01:00
for _, id := range cacheObjItem {
if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, objItems[id].Code); ok { //only gear can be custom named
m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(objItems[id].Names[0]), regexp.QuoteMeta(objItems[id].Names[0]))
if ok, _ := regexp.MatchString(m, basename); ok {
//log.Printf("LOOP : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
i = id
break
}
}
}
if i != -1 {
//log.Printf("RETURN : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
return objItems[i].ObjID64
} else {
/*
fmt.Printf("silentGetObjItemID : Modifier : `%s`\n", r.ReplaceAllString(name, "${Modifier}"))
fmt.Printf("silentGetObjItemID : BaseName : `%s`\n", r.ReplaceAllString(name, "${BaseName}"))
fmt.Printf("silentGetObjItemID : Atk : `%s`\n", r.ReplaceAllString(name, "${Atk}"))
fmt.Printf("silentGetObjItemID : Def : `%s`\n", r.ReplaceAllString(name, "${Def}"))
fmt.Printf("silentGetObjItemID : Mana : `%s`\n", r.ReplaceAllString(name, "${Mana}"))
*/
}
}
return 0
}
2020-01-27 09:38:11 +01:00
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
}
2020-01-26 07:38:41 +01:00
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")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(weight, objID64)
2020-01-26 07:49:59 +01:00
logOnError(err, fmt.Sprintf("setObjItemWeight : update obj_item(%d)", objID64))
2020-01-26 07:38:41 +01:00
if err != nil {
return err
}
return nil
}
2020-01-26 09:39:32 +01:00
func setObjItemCraft(objID64 int64, cmd string, mana int64) error {
2020-01-26 09:40:59 +01:00
stmt, err := db.Prepare(`INSERT INTO obj_craft (obj_id, cmd, mana)
VALUES (?, ?, ?)
2020-01-26 09:39:32 +01:00
ON DUPLICATE KEY UPDATE cmd = ?, mana = ?;`)
2020-01-26 09:40:59 +01:00
logOnError(err, "setObjItemCraft : prepare update obj_craft")
2020-01-26 09:39:32 +01:00
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(objID64, cmd, mana, cmd, mana)
2020-01-26 09:40:59 +01:00
logOnError(err, fmt.Sprintf("setObjItemCraft : update obj_craft(%d)", objID64))
2020-01-26 09:39:32 +01:00
if err != nil {
return err
}
return nil
}
2020-01-26 09:52:50 +01:00
func emptyObjItemCraftItem(objID64 int64) error {
stmt, err := db.Prepare(`DELETE FROM obj_craft_item WHERE obj_id = ?;`)
logOnError(err, "emptyObjItemCraftItem : prepare delete obj_craft_item")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(objID64)
logOnError(err, fmt.Sprintf("setObjItemWeight : delete obj_craft_item(%d)", objID64))
if err != nil {
return err
}
return nil
}
func addObjItemCraftItem(objID64 int64, itemID64 int64, quantity int64) error {
2020-01-26 10:05:47 +01:00
stmt, err := db.Prepare(`INSERT INTO obj_craft_item (obj_id, item_id, quantity)
VALUES (?, ?, ?);`)
2020-01-26 09:52:50 +01:00
logOnError(err, "addObjItemCraftItem : prepare update obj_craft_item")
if err != nil {
return err
}
defer stmt.Close()
2020-01-26 09:54:06 +01:00
_, err = stmt.Exec(objID64, itemID64, quantity)
2020-01-26 09:52:50 +01:00
logOnError(err, fmt.Sprintf("addObjItemCraftItem : update obj_craft_item(%d)", objID64))
if err != nil {
return err
}
return nil
}
2020-01-27 05:10:45 +01:00
func getCraftItemID(cmd string) (int64, error) {
2020-01-26 14:40:20 +01:00
muxObjItem.RLock()
defer muxObjItem.RUnlock()
2020-01-26 14:47:13 +01:00
if id, ok := cacheObjItemCraft[cmd]; ok {
return objItems[id].ObjID64, nil
} else {
return 0, nil
2020-01-26 14:40:20 +01:00
}
2020-01-26 14:47:13 +01:00
return 0, nil
2020-01-26 14:40:20 +01:00
}
2020-01-26 07:38:41 +01:00
func loadObjItem() error {
var (
2020-01-27 09:38:11 +01:00
obj_id int64
type_id int64
intl_id string
name string
weight int64
craftable int
2020-01-26 07:38:41 +01:00
)
muxObjItem.Lock()
defer muxObjItem.Unlock()
cacheObjItem = make(map[string]int64)
cacheObjItemId = make(map[int64]int64)
2020-01-26 14:40:20 +01:00
cacheObjItemCraft = make(map[string]int64)
2020-01-26 07:38:41 +01:00
objItems = make([]ChatWarsItem, 0)
b, err := Asset("data/obj_item.json")
logOnError(err, "loadObjItem : load data/obj_item.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &objItems)
var k int64
2020-01-26 07:51:12 +01:00
for k = 0; k < int64(len(objItems)); k++ {
2020-01-26 07:38:41 +01:00
if len(objItems[k].Names) == 0 {
log.Printf("loadObjItems : %s : name missing.\n", objItems[k].Code)
} else {
if id, ok := cacheObjItem[objItems[k].Code]; ok {
2020-01-26 07:51:12 +01:00
log.Printf("loadObjItem : %s : duplicate code found. Belong to %s\n", objItems[id].Code, objItems[id].Names[0])
2020-01-26 07:38:41 +01:00
} else {
cacheObjItem[objItems[k].Code] = k
2020-01-26 07:52:04 +01:00
objItems[k].ItemTypeID = cacheObjSubType[objItems[k].ItemType]
2020-01-26 07:38:41 +01:00
for _, n := range objItems[k].Names {
if id, ok := cacheObjItem[n]; ok {
log.Printf("loadObjItem : %s - %s : duplicate name found. Belongs to %s\n", objItems[k].Code, n, objItems[id].Code)
} else {
cacheObjItem[n] = k
}
}
}
}
}
2020-01-27 09:38:11 +01:00
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;`)
2020-01-26 07:38:41 +01:00
if err != nil {
logOnError(err, "loadObjItem : querying items")
return err
}
defer objs.Close()
for objs.Next() {
2020-01-27 09:38:11 +01:00
err = objs.Scan(&obj_id, &type_id, &intl_id, &weight, &craftable)
2020-01-26 07:38:41 +01:00
if err != nil {
logOnError(err, "loadObjItem : scanning items")
return err
}
if id, ok := cacheObjItem[intl_id]; !ok {
log.Printf("loadObjItem : %s : orphaned item in database (id : %d)\n", intl_id, obj_id)
} else {
objItems[id].ObjID64 = obj_id
2020-01-26 07:52:31 +01:00
if weight != objItems[id].Weight {
2020-01-26 07:38:41 +01:00
log.Printf("loadObjItem : %s - %s : weight changed : %d => %d\n", objItems[id].Code, objItems[id].Names[0], weight, objItems[id].Weight)
2020-01-26 07:52:04 +01:00
setObjItemWeight(obj_id, objItems[id].Weight)
2020-01-26 07:38:41 +01:00
}
2020-01-27 09:38:11 +01:00
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)
}
2020-01-26 07:38:41 +01:00
cacheObjItemId[obj_id] = id
}
}
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(err, "loadObjItem : querying names")
return err
}
defer names.Close()
for names.Next() {
err = names.Scan(&obj_id, &name)
if err != nil {
logOnError(err, "loadObjItem : scanning names")
return err
}
if _, ok := cacheObjItem[name]; !ok {
if id, ok := cacheObjItemId[obj_id]; ok {
log.Printf("loadObjItem : %s : orphaned name in database for item %s\n", name, objItems[id].Code)
}
}
}
for _, i := range cacheObjItem {
if objItems[i].ObjID64 == 0 {
2020-01-27 09:38:11 +01:00
id, _ := addObjItem(objItems[i].Code, objItems[i].Names[0], objItems[i].ItemTypeID, objItems[i].Weight, objItems[i].Exchange, objItems[i].Auction, objItems[i].Craftable)
2020-01-26 07:38:41 +01:00
objItems[i].ObjID64 = id
cacheObjItemId[id] = i
for _, n := range objItems[i].Names {
objAddName(id, n)
cacheObjItem[n] = id
}
}
}
2020-01-26 09:31:43 +01:00
for _, i := range cacheObjItem {
if objItems[i].Craft != nil {
2020-01-26 14:40:20 +01:00
cacheObjItemCraft[objItems[i].Craft.Command] = i
2020-01-26 09:39:32 +01:00
setObjItemCraft(objItems[i].ObjID64, objItems[i].Craft.Command, objItems[i].Craft.Mana)
2020-01-26 09:54:06 +01:00
emptyObjItemCraftItem(objItems[i].ObjID64)
2020-01-26 09:52:50 +01:00
for k, o := range objItems[i].Craft.Items {
2020-01-26 10:08:10 +01:00
if id, ok := cacheObjItem[o.Code]; !ok {
2020-01-26 10:04:50 +01:00
log.Printf("loadObjItem : %s : unknown item %s for recipe.\n", objItems[i].Code, o.Code)
} else {
objItems[i].Craft.Items[k].ItemID64 = objItems[id].ObjID64
}
2020-01-26 09:52:50 +01:00
}
for _, o := range objItems[i].Craft.Items {
addObjItemCraftItem(objItems[i].ObjID64, o.ItemID64, o.Quantity)
}
2020-01-26 09:31:43 +01:00
}
}
2020-01-26 07:52:45 +01:00
log.Printf("%d items loaded.\n", len(objItems))
2020-01-26 07:38:41 +01:00
/*
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)
}
}
*/
return nil
}