From 5fdb6747e9c9803eb9c70b6e885491bcbe1eeda9 Mon Sep 17 00:00:00 2001 From: shoopea Date: Wed, 8 May 2019 18:13:47 +0800 Subject: [PATCH] test --- main.go | 4 ++++ sql.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6948145..d7e2a98 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,7 @@ var ( cfg Config MQCWMsgQueue chan ChatWarsMessage SQLMsgIdentifyQueue chan int64 + msgParsingRules map[int]MessageParsingRule ) func PrintText(m *tb.Message) { @@ -87,6 +88,9 @@ func main() { initDB() } + msgParsingRules, err := msgParsingRules() + failOnError(err, "Message parsing rules") + go StartBot() MQCWMsgQueue = make(chan ChatWarsMessage, 100) diff --git a/sql.go b/sql.go index e8b5be7..b3f0d57 100644 --- a/sql.go +++ b/sql.go @@ -143,7 +143,7 @@ func initDB() { ,prio SMALLINT NOT NULL ,descn VARCHAR(32) NOT NULL ,rule VARCHAR(4096) NOT NULL - ,msg_type_id + ,msg_type_id SMALLINT UNSIGNED NOT NULL ,UNIQUE KEY (id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table msg_rules") @@ -176,6 +176,10 @@ func initDB() { ,(` + strconv.Itoa(objSubTypeMessageTributeInc) + `, "tribute_ack", "Request acknoledged", ` + strconv.Itoa(objTypeMessage) + `);`) 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[🐉🦅🐺🦈🦌🥔🌑])(?P(\[[A-Z]{3}\]){0,1})(?P([A-Za-z0-9 ]*)) ⚔:(?P[0-9]+)(?P\((-|\+)[0-9]+\)){0,1} 🛡:(?P[0-9]+) Lvl: (?P[0-9]+)\nYour result on the battlefield:\n🔥Exp: (?P[0-9]+)\n💰Gold: (?P-{0,1}[0-9]+)\n📦Stock: (?P-{0,1}[0-9]+)\n\n(?P(🔋Stamina restored)){0,1}(\n)*(?P(⚡Critical strike)){0,1}(\n)*(?s:.*)$", ` + strconv.Itoa(objSubTypeMessageReport) + `);`) + failOnError(err, "initDB : populate table msg_rule") + log.Println("Database set up") } @@ -225,6 +229,7 @@ func getMsg(objId int64) (ChatWarsMessage, error) { func getObjSubTypeId(objId int64) (int64, error) { var objSubTypeId int64 + stmt, err := db.Prepare(`SELECT o.sub_type_id FROM obj o WHERE o.id = ?`) if err != nil { return 0, err @@ -272,3 +277,40 @@ func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int3 */ 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 +}