This commit is contained in:
shoopea 2019-05-05 19:29:28 +08:00
parent bca3777f2e
commit c3cb74a705
3 changed files with 108 additions and 23 deletions

View File

@ -53,6 +53,7 @@ var (
cfg Config cfg Config
MQCWMsgQueue chan ChatWarsMessage MQCWMsgQueue chan ChatWarsMessage
SQLMsgIdQueue chan int64
) )
func PrintText(m *tb.Message) { func PrintText(m *tb.Message) {
@ -103,6 +104,9 @@ func main() {
for w := 1; w <= 6; w++ { for w := 1; w <= 6; w++ {
go SQLCWMsgWorker(w, MQCWMsgQueue) go SQLCWMsgWorker(w, MQCWMsgQueue)
} }
for w := 1; w <= 6; w++ {
go SQLIdentifyMsgWorker(w, SQLMsgIdQueue)
}
fmt.Println("Started !") fmt.Println("Started !")

94
sql.go
View File

@ -62,6 +62,18 @@ func initDB() {
log.Fatal(err) log.Fatal(err)
} }
_, err = db.Exec(`CREATE TABLE code_obj_sub_type (
id SMALLINT(5) UNSIGNED NOT NULL
,intl_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,obj_type_id SMALLINT UNSIGNED NOT NULL
,PRIMARY KEY (id)
,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(`CREATE TABLE obj ( _, err = db.Exec(`CREATE TABLE obj (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
,obj_type_id SMALLINT UNSIGNED NOT NULL ,obj_type_id SMALLINT UNSIGNED NOT NULL
@ -112,6 +124,23 @@ func initDB() {
log.Fatal(err) log.Fatal(err)
} }
_, err = db.Exec(`CREATE TABLE obj_msg_report (
obj_id BIGINT UNSIGNED NOT NULL
,war_date TIMESTAMP NOT NULL
,attack SMALLINT UNSIGNED NOT NULL
,defense SMALLINT UNSIGNED NOT NULL
,gold SMALLINT UNSIGNED NOT NULL
,stock SMALLINT UNSIGNED NOT NULL
,exp SMALLINT UNSIGNED NOT NULL
,stamina BOOLEAN NOT NULL
,crit BOOLEAN NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (msg_id, chat_id, sender_user_id)
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name) _, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
VALUES (1, "user", "User") VALUES (1, "user", "User")
,(2, "guild", "Guild") ,(2, "guild", "Guild")
@ -120,40 +149,63 @@ func initDB() {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Database set up") _, err = db.Exec(`INSERT INTO code_obj_sub_type (id, intl_id, name, obj_type_id)
} VALUES (1, "unprocessed", "Unprocessed", 3)
,(2, "unknown", "Unknown", 3)
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage) { ,(3, "report", "War report", 3)
for m := range msgs { ,(4, "g_report", "Guild War report", 3)
err := putMsg(m) ,(5, "quest", "Quest result", 3)
if err != nil { ,(6, "fight", "Fight result", 3)
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.") ,(7, "hero", "Hero summary", 3)
} ,(8, "me", "Hero short summary", 3)
} ,(9, "inv", "Inventory", 3);`)
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func putMsg(m ChatWarsMessage) error {
res, err := db.Exec(`INSERT INTO obj (obj_type_id)
VALUES (3);`)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Database set up")
}
func putUnprocessedMsg(m ChatWarsMessage) (int64, error) {
res, err := db.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
VALUES (3 , 1);`)
if err != nil {
return nil, err
}
objId, err := res.LastInsertId() objId, err := res.LastInsertId()
if err != nil {
return nil, err
}
stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, sender_user_id, date , text) stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, sender_user_id, date , text)
VALUES (?, ?, ?, ?, FROM_UNIXTIME(?), ?);`) VALUES (?, ?, ?, ?, FROM_UNIXTIME(?), ?);`)
if err != nil { if err != nil {
return err return nil, err
} }
defer stmt.Close() defer stmt.Close()
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.SenderUserID64, m.Date, m.Text) _, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.SenderUserID64, m.Date, m.Text)
if err != nil { if err != nil {
return err return nil, err
} }
return nil return objId, nil
}
func getMsg(objId int64) (ChatWarsMessage, error) {
var m ChatWarsMessage
stmt, err := db.Prepare(`SELECT om.msg_id, om.chat_id, om.sender_user_id, UNIX_TIMESTAMP(om.date), om.text FROM obj_msg om WHERE om.id = ?`)
if err != nil {
return nil, err
}
defer stmt.Close()
err = stmtOut.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.SenderUserID64, &m.Date, &m.Text)
if err != nil {
return nil, err
}
return m, nil
} }

29
workers.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"log"
"strconv"
)
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage) {
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
for m := range msgs {
objId, err := putUnprocessedMsg(m)
if err != nil {
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
} else {
}
}
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func SQLIdentifyMsgWorker(id int, objIds <-chan int64) {
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Starting.")
for objId := range objIds {
}
log.Printf("SQLIdentifyMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}