introduce code table caching

This commit is contained in:
shoopea 2020-01-02 18:04:48 +08:00
parent 5aae81b0f7
commit fcb90c267a
7 changed files with 163 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,66 +1,53 @@
[
{
"id": "1",
"intl_id": "user",
"name": "User"
},
{
"id": "2",
"intl_id": "guild",
"name": "Guild"
},
{
"id": "3",
"intl_id": "msg",
"name": "Message"
},
{
"id": "4",
"intl_id": "war",
"name": "War"
},
{
"id": "5",
"intl_id": "war_report",
"name": "War Report"
},
{
"id": "6",
"intl_id": "job",
"name": "Job"
},
{
"id": "7",
"intl_id": "item",
"name": "Item"
},
{
"id": "8",
"intl_id": "castle",
"name": "Castle"
},
{
"id": "9",
"intl_id": "fair",
"name": "Fair"
},
{
"id": "10",
"intl_id": "union",
"name": "Trade Union"
},
{
"id": "11",
"intl_id": "tribute",
"name": "Tribute"
},
{
"id": "12",
"intl_id": "xp",
"name": "Experience"
},
{
"id": "13",
"intl_id": "quest",
"name": "Quest"
}

0
data/config.json Normal file
View File

14
def.go
View File

@ -17,6 +17,20 @@ type DataBackup struct {
Messages []ChatWarsMessage `json:"messages"`
}
type ObjType struct {
ID64 int64
IntlId string `json:"intl_id"`
Name string `json:"name"`
}
type ObjSubType struct {
ID64 int64
IntlId string `json:"intl_id"`
Name string `json:"name"`
ObjType string `json:"obj_type"`
ObjTypeID64 int64
}
type MQClient struct {
User string
Password string

19
main.go
View File

@ -131,24 +131,7 @@ func main() {
_, _ = addObjGuild(``, `No Guild`)
}
resetMsgParsingRules()
msgParsingRules, err = loadMsgParsingRules()
logOnError(err, "Message parsing rules")
err = loadObjCastle()
logOnError(err, "Caching castles")
err = loadObjGuild()
logOnError(err, "Caching guilds")
err = loadObjUser()
logOnError(err, "Caching user")
err = loadObjItem()
logOnError(err, "Caching items")
err = loadObjMsg()
logOnError(err, "Caching msgs")
initCache()
// Registering bot
bot, err := tb.NewBot(tb.Settings{

101
obj.go
View File

@ -16,12 +16,113 @@ var (
cacheObjUser *sync.Map
cacheObjMsg *sync.Map
cacheObjType map[string]int64
cacheObjSubType map[string]int64
cacheObjItem map[string]ChatWarsItem
cacheObjItemId map[int64]ChatWarsItem
muxObjItem sync.Mutex
muxObjItemId sync.Mutex
)
func initCache() {
resetMsgParsingRules()
msgParsingRules, err = loadMsgParsingRules()
logOnError(err, "initCache : message parsing rules")
err = loadObjCastle()
logOnError(err, "initCache : caching castles")
err = loadObjGuild()
logOnError(err, "initCache : caching guilds")
err = loadObjUser()
logOnError(err, "initCache : caching user")
err = loadObjItem()
logOnError(err, "initCache : caching items")
err = loadObjMsg()
logOnError(err, "initCache : caching msgs")
}
func loadObjType() error {
var obj []ObjType
b, err := Asset("data/code_obj_type.json")
logOnError(err, "loadObjType : load data/code_obj_type.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &obj)
logOnError(err, "loadObjType : Unmarshal")
if err != nil {
return err
}
cacheObjType = make(map[string]int64)
for _, v := range obj {
id := codeObjTypeId(v.IntlId)
if id > 0 {
cacheObjType[v.IntlId] = id
} else {
err = insertObjType(v.IntlId, v.Name)
logOnError(err, "loadObjType : insertObjType")
if err == nil {
id = codeObjTypeId(v.IntlId)
if id > 0 {
cacheObjType[v.IntlId] = id
} else {
// issue inserting
}
}
}
}
return nil
}
func loadObjSubType() error {
var obj []ObjSubType
b, err := Asset("data/code_obj_sub_type.json")
logOnError(err, "loadObjSubType : load data/code_obj_sub_type.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &obj)
logOnError(err, "loadObjSubType : Unmarshal")
if err != nil {
return err
}
cacheObjSubType = make(map[string]int64)
for _, v := range obj {
id := codeObjSubTypeId(v.IntlId)
if id > 0 {
cacheObjSubType[v.IntlId] = id
} else {
err = insertObjSubType(v.IntlId, v.Name, v.ObjType)
logOnError(err, "loadObjSubType : insertObjSubType")
if err == nil {
id = codeObjSubTypeId(v.IntlId)
if id > 0 {
cacheObjSubType[v.IntlId] = id
} else {
// issue inserting
}
}
}
}
return nil
}
func codeObjTypeId(intlId string) (int64, error) {
var objTypeId int64

16
sql.go
View File

@ -620,6 +620,22 @@ func initDB() {
log.Println("initDB : Database set up")
}
func insertObjType(intlId string, name string) error {
stmt, err := db.Prepare(`INSERT INTO code_obj_type (intl_id, name)
VALUES (?, ?);`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(intlId, name)
if err != nil {
return err
}
return nil
}
func insertMsgItem(objId int64, itemId int64, quantity int64) error {
stmt, err := db.Prepare(`INSERT INTO obj_msg_item (obj_id, item_id, quantity)
VALUES (?, ?, ?);`)