From 9806f14d618c7720b6ba9725d2de0a36b3708e5a Mon Sep 17 00:00:00 2001 From: shoopea Date: Sat, 4 May 2019 16:57:24 +0800 Subject: [PATCH] upd --- main.go | 10 +++---- mq.go | 37 ++++++++++-------------- sql.go | 90 ++++++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 90 insertions(+), 47 deletions(-) diff --git a/main.go b/main.go index fbe9367..95ef728 100644 --- a/main.go +++ b/main.go @@ -38,11 +38,11 @@ type Config struct { } type ChatWarsMessage struct { - MsgID64 int64 `json:"msg_id"` - ChatID64 int64 `json:"chat_id"` - UserID64 int64 `json:"user_id"` - MsgText string `json:"msg"` - MsgDate int32 `json:"date"` + SenderUserID64 int64 `json:"sender_user_id"` + Date int32 `json:"date"` + ID64 int64 `json:"id"` + ChatID64 int64 `json:"chat_id"` + Text string `json:"text"` } var ( diff --git a/mq.go b/mq.go index 4402078..2de4ea2 100644 --- a/mq.go +++ b/mq.go @@ -7,14 +7,14 @@ import ( "github.com/streadway/amqp" ) -func MQMainReceive() { - var m ChatWarsMessage +func MQGetMsg(id int, msgs chan<- ChatWarsMessage) { + var x ChatWarsMessage conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) - failOnError(err, "MQMainReceive : Failed to connect to RabbitMQ") + failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ") defer conn.Close() ch, err := conn.Channel() - failOnError(err, "MQMainReceive : Failed to open a channel") + failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to open a channel") defer ch.Close() q, err := ch.QueueDeclare( @@ -25,9 +25,9 @@ func MQMainReceive() { false, // no-wait nil, // arguments ) - failOnError(err, "MQMainReceive : Failed to declare a queue") + failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to declare a queue") - msgs, err := ch.Consume( + m, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack @@ -36,25 +36,18 @@ func MQMainReceive() { false, // no-wait nil, // args ) - failOnError(err, "MQMainReceive : Failed to register a consumer") + failOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Failed to register a consumer") - forever := make(chan bool) + for d := range m { + log.Printf("MQGetMsg["+strconv.Itoa(id)+"] : Received a message: %s", d.Body) - go func() { - for d := range msgs { - log.Printf("MQMainReceive : Received a message: %s", d.Body) - - if err = json.Unmarshal(d.Body, &m); err != nil { - logOnError(err, "MQMainReceive : Can't unmarshal") - } else { - err = putMsg(m) - if err != nil { - logOnError(err, "MQMainReceive") - } - } + if err = json.Unmarshal(d.Body, &x); err != nil { + logOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+d.Body) + } else { + msgs <- x } - }() + } - <-forever + log.Printf("MQGetMsg[" + strconv.Itoa(id) + "] : Closing.") } diff --git a/sql.go b/sql.go index 6070893..6e10dba 100644 --- a/sql.go +++ b/sql.go @@ -50,28 +50,71 @@ func initDB() { } log.Println("Database cleaned up") - _, err = db.Exec(`CREATE TABLE user ( - id BIGINT UNSIGNED NOT NULL, - user_id VARCHAR(32) NOT NULL, - name VARCHAR(80) NOT NULL, - guild_id SMALLINT(5), - last_msg TIMESTAMP, - busy_until TIMESTAMP, - role ENUM('commander', 'bartebder', 'squire', 'none'), - PRIMARY KEY (id) + + _, err = db.Exec(`CREATE TABLE code_obj_type ( + id SMALLINT(5) UNSIGNED NOT NULL + ,intl_id VARCHAR(32) NOT NULL + ,name VARCHAR(80) NOT NULL + ,PRIMARY KEY (id) ) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) if err != nil { log.Fatal(err) } - _, err = db.Exec(`CREATE TABLE msg ( - id BIGINT UNSIGNED NOT NULL - ,chat_id BIGINT UNSIGNED NOT NULL - ,user_id BIGINT UNSIGNED NOT NULL - ,date TIMESTAMP NOT NULL - ,text VARCHAR(4096) NOT NULL - ,PRIMARY KEY (id, chat_id) - ) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) // ,FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE + _, err = db.Exec(`CREATE TABLE obj ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT + ,obj_type_id SMALLINT UNSIGNED NOT NULL + ,PRIMARY KEY (id) + ) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8 COLLATE utf8_unicode_ci;`) + if err != nil { + log.Fatal(err) + } + + _, err = db.Exec(`CREATE TABLE obj_user ( + obj_id BIGINT UNSIGNED NOT NULL + ,tg_id BIGINT UNSIGNED NOT NULL + ,user_id VARCHAR(32) NOT NULL + ,name VARCHAR(80) NOT NULL + ,guild_id BIGINT UNSIGNED + ,last_msg TIMESTAMP + ,busy_until TIMESTAMP + ,role ENUM('commander', 'bartender', 'squire', 'none') + ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE + ) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) + if err != nil { + log.Fatal(err) + } + + _, err = db.Exec(`CREATE TABLE obj_guild ( + obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT + ,tag VARCHAR(32) NOT NULL + ,name VARCHAR(80) NOT NULL + ,chat_id BIGINT NOT NULL + ,deposit_chat_id BIGINT NOT NULL + FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE + ) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) + if err != nil { + log.Fatal(err) + } + + _, err = db.Exec(`CREATE TABLE obj_msg ( + obj_id BIGINT UNSIGNED NOT NULL + ,msg_id BIGINT NOT NULL + ,chat_id BIGINT NOT NULL + ,sender_user_id BIGINT NOT NULL + ,date TIMESTAMP NOT NULL + ,text VARCHAR(4096) 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) + VALUES (1, "user", "User") + ,(2, "guild", "Guild") + ,(3, "msg", "Message");`) if err != nil { log.Fatal(err) } @@ -80,14 +123,21 @@ func initDB() { } func putMsg(m ChatWarsMessage) error { - stmt, err := db.Prepare(`INSERT INTO msg (id, chat_id, user_id, date , text) - VALUES (?, ?, ?, FROM_UNIXTIME(?), ?);`) + res, err := db.Exec(`INSERT INTO obj (obj_type_id) + VALUES (3);`) + if err != nil { + log.Fatal(err) + } + objId, err := res.LastInsertId() + + stmt, err := db.Prepare(`INSERT INTO msg (obj_id, msg_id, chat_id, sender_user_id, date , text) + VALUES (?, ?, ?, ?, FROM_UNIXTIME(?), ?);`) if err != nil { return err } defer stmt.Close() - _, err = stmt.Exec(m.MsgID64, m.ChatID64, m.UserID64, m.MsgDate, m.MsgText) + _, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.SenderUserID64, m.Date, m.Text) if err != nil { return err }