This commit is contained in:
shoopea 2020-01-26 14:35:38 +08:00
parent 5e2ce7e747
commit 7ef54702b2
6 changed files with 130 additions and 324 deletions

File diff suppressed because one or more lines are too long

View File

@ -23,6 +23,13 @@
"guild": "GLD",
"main_chat_id": 0,
"deposit_chat_id": 0,
"report_chat_id": 0
"report_chat_id": 0,
"vault_limit": [
{
"item" : "01",
"min_qty": 0,
"max_qty": 10000
}
]
}
}

View File

@ -5520,7 +5520,49 @@
"auction": "true",
"names": [
"Black Morningstar"
]
],
"craft": {
"cmd": "/craft_w98",
"mana": 800,
"items": [
{
"item": "r98",
"quantity": 1
},
{
"item": "k98",
"quantity": 6
},
{
"item": "23",
"quantity": 107
},
{
"item": "33",
"quantity": 79
},
{
"item": "32",
"quantity": 17
},
{
"item": "18",
"quantity": 6
},
{
"item": "38",
"quantity": 4
},
{
"item": "17",
"quantity": 3
},
{
"item": "36",
"quantity": 3
}
]
}
},
{
"item_type": "item_other",
@ -5530,6 +5572,52 @@
"auction": "true",
"names": [
"Maiming Bulawa"
]
],
"craft": {
"cmd": "/craft_w99",
"mana": 800,
"items": [
{
"item": "r99",
"quantity": 1
},
{
"item": "k99",
"quantity": 6
},
{
"item": "23",
"quantity": 107
},
{
"item": "33",
"quantity": 79
},
{
"item": "27",
"quantity": 39
},
{
"item": "32",
"quantity": 7
},
{
"item": "18",
"quantity": 6
},
{
"item": "38",
"quantity": 4
},
{
"item": "15",
"quantity": 3
},
{
"item": "36",
"quantity": 1
}
]
}
}
]

6
job.go
View File

@ -119,7 +119,7 @@ func setJobTimeout(jobID64 int64, d time.Duration) error {
t := time.Now().UTC().Add(d)
_, err = stmt.Exec(t, jobID64)
logOnError(err, fmt.Sprintf("setJobTimeout, update obj_job(%d)", jobID64))
logOnError(err, fmt.Sprintf("setJobTimeout : update obj_job(%d)", jobID64))
if err != nil {
return err
}
@ -146,14 +146,14 @@ func setJobPayload(jobID64 int64, payload []byte) error {
}
stmt, err := db.Prepare(`UPDATE obj_job j SET j.payload = ? WHERE j.obj_id = ?;`)
logOnError(err, "setJobTimeout : prepare update obj_job")
logOnError(err, "setJobPayload : prepare update obj_job")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(zpayload, jobID64)
logOnError(err, fmt.Sprintf("setJobTimeout, update obj_job(%d)", jobID64))
logOnError(err, fmt.Sprintf("setJobPayload : update obj_job(%d)", jobID64))
if err != nil {
return err
}

314
obj.go
View File

@ -21,13 +21,8 @@ var (
cacheObjType map[string]int64
cacheObjSubType map[string]int64
cacheObjItem map[string]ChatWarsItem
muxObjItem sync.Mutex
cacheObjItemId map[int64]ChatWarsItem
muxObjItemId sync.Mutex
cacheObjJob map[int64]Job
muxObjJob sync.Mutex
cacheObjJob map[int64]Job
muxObjJob sync.Mutex
)
func initCache(initDB bool) {
@ -772,311 +767,6 @@ func addObjQuest(userID64 int64, questTypeID64 int64, duration time.Duration, da
return objId, nil
}
func addObjItem(code string, name string, itemTypeID64 int64, weight int64, exchange string, auction bool) (int64, error) {
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
}
stmt, err := tx.Prepare(`INSERT INTO obj_item (obj_id, intl_id, weight, exchange, auction)
VALUES (?, ?, ?, ?, ?);`)
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()
var e, a int
if exchange != `` {
e = 1
} else {
e = 0
}
if auction {
a = 1
} else {
a = 0
}
_, err = stmt.Exec(objId, code, weight, e, a)
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 getObjItemID(c string, n string) int64 {
i := getSilentObjItemID(c, n)
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 getObjItem(objItemID64 int64) (*ChatWarsItem, error) {
muxObjItemId.Lock()
defer muxObjItemId.Unlock()
muxObjItem.Lock()
defer muxObjItem.Unlock()
if obj, ok := cacheObjItemId[objItemID64]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
return &obj, nil
} else {
return nil, errors.New("Item not found.")
}
}
func getSilentObjItemID(code string, name string) int64 {
muxObjItem.Lock()
defer muxObjItem.Unlock()
if len(code) > 0 {
if obj, ok := cacheObjItem[code]; ok {
//log.Printf("Matching item code %s with %s.\n", code, obj.Code)
return obj.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 obj, ok := cacheObjItem[code[:len(code)-1]]; ok {
return obj.ObjID64
}
}
if ok, _ := regexp.MatchString(`^u[0-9]+`, code); !ok {
return 0
}
}
if len(name) == 0 {
return 0
}
if obj, ok := cacheObjItem[name]; ok {
//log.Printf("Matching item name %s with %s.\n", name, obj.Name)
return obj.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 obj, ok := cacheObjItem[basename]; ok && len(basename) > 0 {
//log.Printf("Matching item full basename %s with %s.\n", basename, obj.Name)
return obj.ObjID64
}
i := ChatWarsItem{
ObjID64: 0,
}
for _, obj := range cacheObjItem {
if ok, _ := regexp.MatchString(`^(a|e|w)[0-9]+$`, obj.Code); ok { //only gear can be custom named
m := fmt.Sprintf("^((%s.*)|(.*%s))$", regexp.QuoteMeta(obj.Names[0]), regexp.QuoteMeta(obj.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 = obj
break
}
}
}
if i.ObjID64 != 0 {
//log.Printf("RETURN : Matching item modified basename %s with %s (%d).\n", basename, item.Name, item.ObjID64)
return 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
}
func loadObjItem() error {
var (
id int64
type_id int64
intl_id string
name string
weight int64
items []ChatWarsItem
)
muxObjItem.Lock()
defer muxObjItem.Unlock()
cacheObjItem = make(map[string]ChatWarsItem)
muxObjItemId.Lock()
defer muxObjItemId.Unlock()
cacheObjItemId = make(map[int64]ChatWarsItem)
b, err := Asset("data/obj_item.json")
logOnError(err, "loadObjItem2 : load data/obj_item.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &items)
for _, i := range items {
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", i.Code, obj.Names[0])
} else {
i2 := ChatWarsItem{
Auction: i.Auction,
Code: i.Code,
Exchange: i.Exchange,
ItemType: i.ItemType,
ItemTypeID: cacheObjSubType[i.ItemType],
Weight: i.Weight,
Craft: ChatWarsItemCraft{},
Names: make([]string, 0),
}
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(err, "loadObjItem2 : querying items")
return err
}
defer objs.Close()
for objs.Next() {
err = objs.Scan(&id, &type_id, &intl_id, &weight)
if err != nil {
logOnError(err, "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
delete(cacheObjItem, intl_id)
cacheObjItem[intl_id] = obj
for _, n := range obj.Names {
delete(cacheObjItem, n)
cacheObjItem[n] = obj
}
if weight != obj.Weight {
log.Printf("loadObjItem2 : %s - %s : weight changed : %d => %d\n", obj.Code, obj.Names[0], 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(err, "loadObjItem2 : querying names")
return err
}
defer names.Close()
for names.Next() {
err = names.Scan(&id, &name)
if err != nil {
logOnError(err, "loadObjItem2 : scanning names")
return err
}
if _, ok := cacheObjItem[name]; !ok {
if obj, ok := cacheObjItemId[id]; ok {
log.Printf("loadObjItem2 : %s : orphaned name in database for item %s\n", name, obj.Code)
}
}
}
for _, i := range cacheObjItem {
if i.ObjID64 == 0 {
id, _ := addObjItem(i.Code, i.Names[0], i.ItemTypeID, i.Weight, i.Exchange, i.Auction)
i.ObjID64 = id
cacheObjItemId[id] = i
count := 0
for _, n := range i.Names {
if count == 0 {
delete(cacheObjItem, n)
cacheObjItem[n] = i
continue
} else {
objAddName(id, n)
delete(cacheObjItem, n)
cacheObjItem[n] = i
}
}
}
}
log.Printf("%d items loaded.\n", len(items))
/*
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
}
func loadObjJob() error {
var (
id int64

21
sql.go
View File

@ -94,6 +94,27 @@ func initDB() {
failOnError(err, "initDB : create table obj_item")
log.Println("initDB : obj_item created ...")
_, err = db.Exec(`CREATE TABLE obj_craft (
obj_id BIGINT UNSIGNED NOT NULL
,cmd VARCHAR(32)
,mana INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (cmd)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_craft")
log.Println("initDB : obj_craft created ...")
_, err = db.Exec(`CREATE TABLE obj_craft_item (
obj_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (obj_id, item_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_craft_item")
log.Println("initDB : obj_craft_item created ...")
_, err = db.Exec(`CREATE TABLE obj_castle (
obj_id BIGINT UNSIGNED NOT NULL
,logo VARCHAR(32) NOT NULL