This commit is contained in:
shoopea 2019-05-08 18:13:47 +08:00
parent d2db3b9d1d
commit 5fdb6747e9
2 changed files with 47 additions and 1 deletions

View File

@ -46,6 +46,7 @@ var (
cfg Config cfg Config
MQCWMsgQueue chan ChatWarsMessage MQCWMsgQueue chan ChatWarsMessage
SQLMsgIdentifyQueue chan int64 SQLMsgIdentifyQueue chan int64
msgParsingRules map[int]MessageParsingRule
) )
func PrintText(m *tb.Message) { func PrintText(m *tb.Message) {
@ -87,6 +88,9 @@ func main() {
initDB() initDB()
} }
msgParsingRules, err := msgParsingRules()
failOnError(err, "Message parsing rules")
go StartBot() go StartBot()
MQCWMsgQueue = make(chan ChatWarsMessage, 100) MQCWMsgQueue = make(chan ChatWarsMessage, 100)

44
sql.go
View File

@ -143,7 +143,7 @@ func initDB() {
,prio SMALLINT NOT NULL ,prio SMALLINT NOT NULL
,descn VARCHAR(32) NOT NULL ,descn VARCHAR(32) NOT NULL
,rule VARCHAR(4096) NOT NULL ,rule VARCHAR(4096) NOT NULL
,msg_type_id ,msg_type_id SMALLINT UNSIGNED NOT NULL
,UNIQUE KEY (id) ,UNIQUE KEY (id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table msg_rules") failOnError(err, "initDB : create table msg_rules")
@ -176,6 +176,10 @@ func initDB() {
,(` + strconv.Itoa(objSubTypeMessageTributeInc) + `, "tribute_ack", "Request acknoledged", ` + strconv.Itoa(objTypeMessage) + `);`) ,(` + strconv.Itoa(objSubTypeMessageTributeInc) + `, "tribute_ack", "Request acknoledged", ` + strconv.Itoa(objTypeMessage) + `);`)
failOnError(err, "initDB : populate table code_obj_sub_type") failOnError(err, "initDB : populate table code_obj_sub_type")
_, err = db.Exec(`INSERT INTO msg_rule (prio, descn, rule, msg_type_id)
VALUES (5000, "Player war report", "^(?P<Castle>[🐉🦅🐺🦈🦌🥔🌑])(?P<Guild>(\[[A-Z]{3}\]){0,1})(?P<User>([A-Za-z0-9 ]*)) ⚔:(?P<Attack>[0-9]+)(?P<AttackMod>\((-|\+)[0-9]+\)){0,1} 🛡:(?P<Defense>[0-9]+) Lvl: (?P<Level>[0-9]+)\nYour result on the battlefield:\n🔥Exp: (?P<Exp>[0-9]+)\n💰Gold: (?P<Gold>-{0,1}[0-9]+)\n📦Stock: (?P<Stock>-{0,1}[0-9]+)\n\n(?P<Stamina>(🔋Stamina restored)){0,1}(\n)*(?P<Crit>(⚡Critical strike)){0,1}(\n)*(?s:.*)$", ` + strconv.Itoa(objSubTypeMessageReport) + `);`)
failOnError(err, "initDB : populate table msg_rule")
log.Println("Database set up") log.Println("Database set up")
} }
@ -225,6 +229,7 @@ func getMsg(objId int64) (ChatWarsMessage, error) {
func getObjSubTypeId(objId int64) (int64, error) { func getObjSubTypeId(objId int64) (int64, error) {
var objSubTypeId int64 var objSubTypeId int64
stmt, err := db.Prepare(`SELECT o.sub_type_id FROM obj o WHERE o.id = ?`) stmt, err := db.Prepare(`SELECT o.sub_type_id FROM obj o WHERE o.id = ?`)
if err != nil { if err != nil {
return 0, err return 0, err
@ -272,3 +277,40 @@ func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int3
*/ */
return nil return nil
} }
func loadMsgParsingRules() (map[int]MessageParsingRule, error) {
var (
id int32
priority int32
descn string
rule string
msgTypeID int32
)
m := make(map[int]MessageParsingRule)
count := int(0)
rules, err := db.Query(`SELECT r.id, r.prio, r.descn, r.rule, r.msg_type_id FROM msg_rule r ORDER BY r.prio DESC;`)
if err != nil {
return m, err
}
defer rules.Close()
for rules.Next() {
err = classifs.Scan(&id, &priority, &descn, &rule, &msgTypeID)
if err != nil {
return m, err
}
rule := new(MessageParsingRule)
rule.ID = id
rule.Priority = priority
rule.Description = descn
rule.Rule = rule
rule.MsgTypeID = msgTypeID
m[count] = *rule
count++
}
return m, nil
}