This commit is contained in:
shoopea 2020-02-05 10:37:50 +08:00
parent d7647d171f
commit 14c4657c6c
2 changed files with 51 additions and 10 deletions

51
obj.go
View File

@ -255,7 +255,7 @@ func objAddName(objID64 int64, name string) error {
return err
}
func addObjMsg(msgID64 int64, msgChatID64 int64, msgTGUserID64 int64, msgTGSenderUserID64 int64, msgDate time.Time, msgText string) (int64, error) {
func addObjMsg(m ChatWarsMessage) (int64, error) {
tx, err := db.Begin()
logOnError(err, "addObjMsg : start transaction")
if err != nil {
@ -286,26 +286,57 @@ func addObjMsg(msgID64 int64, msgChatID64 int64, msgTGUserID64 int64, msgTGSende
}
defer stmt.Close()
_, err = stmt.Exec(objId, msgID64, msgChatID64, msgTGUserID64, msgTGSenderUserID64, msgDate, msgText)
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.TGUserID64, m.TGSenderUserID64, m.Date, m.Text)
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjMsg : rollback exec insert obj_msg")
return 0, err
}
if len(m.Callbacks) > 0 {
stmt2, err := tx.Prepare(`INSERT INTO obj_msg_callback (obj_id, name, data)
VALUES (?, ?, ?);`)
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjMsg : rollback prepare insert obj_msg_callback")
return 0, err
}
defer stmt2.Close()
for _, c := range m.Callbacks {
_, err = stmt2.Exec(objId, c.Name, c.Data)
if err != nil {
err2 := tx.Rollback()
logOnError(err2, "addObjMsg : rollback exec insert obj_msg_callback")
return 0, err
}
}
}
err = tx.Commit()
if err != nil {
return 0, err
}
m := new(ChatWarsMessage)
m.ObjID64 = objId
m.TGUserID64 = msgTGUserID64
m.TGSenderUserID64 = msgTGSenderUserID64
m.Date = msgDate
m.ID64 = msgID64
m.ChatID64 = msgChatID64
m.Text = msgText
msg := new(ChatWarsMessage)
msg.ObjID64 = objId
msg.TGUserID64 = m.TGUserID64
msg.TGSenderUserID64 = m.TGSenderUserID64
msg.Date = m.Date
msg.ID64 = m.ID64
msg.ChatID64 = m.ChatID64
msg.Text = m.Text
msg.Callbacks = []ChatWarsCallback{}
for _, c := range m.Callbacks {
cb := ChatWarsCallback{
Name: c.Name,
Data: c.Data,
}
msg.Callbacks = append(msg.Callbacks, cb)
}
cacheObjMsg.Store(objId, *m)
return objId, nil

10
sql.go
View File

@ -169,6 +169,16 @@ func initDB() {
failOnError(err, "initDB : create table obj_msg")
log.Println("initDB : obj_msg created ...")
_, err = db.Exec(`CREATE TABLE obj_msg_callback (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(128) NOT NULL
,data VARBINARY(256) NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (obj_id, name)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_msg_callback")
log.Println("initDB : obj_msg_callback created ...")
_, err = db.Exec(`CREATE TABLE obj_msg_pillage_inc (
obj_id BIGINT UNSIGNED NOT NULL
,attacker VARCHAR(32)