This commit is contained in:
shoopea 2019-05-04 16:57:24 +08:00
parent f6d3bef2cd
commit 9806f14d61
3 changed files with 90 additions and 47 deletions

View File

@ -38,11 +38,11 @@ type Config struct {
} }
type ChatWarsMessage struct { type ChatWarsMessage struct {
MsgID64 int64 `json:"msg_id"` SenderUserID64 int64 `json:"sender_user_id"`
Date int32 `json:"date"`
ID64 int64 `json:"id"`
ChatID64 int64 `json:"chat_id"` ChatID64 int64 `json:"chat_id"`
UserID64 int64 `json:"user_id"` Text string `json:"text"`
MsgText string `json:"msg"`
MsgDate int32 `json:"date"`
} }
var ( var (

33
mq.go
View File

@ -7,14 +7,14 @@ import (
"github.com/streadway/amqp" "github.com/streadway/amqp"
) )
func MQMainReceive() { func MQGetMsg(id int, msgs chan<- ChatWarsMessage) {
var m ChatWarsMessage var x ChatWarsMessage
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue) 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() defer conn.Close()
ch, err := conn.Channel() 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() defer ch.Close()
q, err := ch.QueueDeclare( q, err := ch.QueueDeclare(
@ -25,9 +25,9 @@ func MQMainReceive() {
false, // no-wait false, // no-wait
nil, // arguments 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 q.Name, // queue
"", // consumer "", // consumer
true, // auto-ack true, // auto-ack
@ -36,25 +36,18 @@ func MQMainReceive() {
false, // no-wait false, // no-wait
nil, // args 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() { if err = json.Unmarshal(d.Body, &x); err != nil {
for d := range msgs { logOnError(err, "MQGetMsg["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+d.Body)
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 { } else {
err = putMsg(m) msgs <- x
if err != nil {
logOnError(err, "MQMainReceive")
} }
} }
}
}()
<-forever log.Printf("MQGetMsg[" + strconv.Itoa(id) + "] : Closing.")
} }

86
sql.go
View File

@ -50,28 +50,71 @@ func initDB() {
} }
log.Println("Database cleaned up") log.Println("Database cleaned up")
_, err = db.Exec(`CREATE TABLE user (
id BIGINT UNSIGNED NOT NULL, _, err = db.Exec(`CREATE TABLE code_obj_type (
user_id VARCHAR(32) NOT NULL, id SMALLINT(5) UNSIGNED NOT NULL
name VARCHAR(80) NOT NULL, ,intl_id VARCHAR(32) NOT NULL
guild_id SMALLINT(5), ,name VARCHAR(80) NOT NULL
last_msg TIMESTAMP, ,PRIMARY KEY (id)
busy_until TIMESTAMP,
role ENUM('commander', 'bartebder', 'squire', 'none'),
PRIMARY KEY (id)
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) ) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
_, err = db.Exec(`CREATE TABLE msg ( _, err = db.Exec(`CREATE TABLE obj (
id BIGINT UNSIGNED NOT NULL id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
,chat_id BIGINT UNSIGNED NOT NULL ,obj_type_id SMALLINT UNSIGNED NOT NULL
,user_id BIGINT 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 ,date TIMESTAMP NOT NULL
,text VARCHAR(4096) NOT NULL ,text VARCHAR(4096) NOT NULL
,PRIMARY KEY (id, chat_id) ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`) // ,FOREIGN KEY (user_id) REFERENCES user(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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -80,14 +123,21 @@ func initDB() {
} }
func putMsg(m ChatWarsMessage) error { func putMsg(m ChatWarsMessage) error {
stmt, err := db.Prepare(`INSERT INTO msg (id, chat_id, user_id, date , text) res, err := db.Exec(`INSERT INTO obj (obj_type_id)
VALUES (?, ?, ?, FROM_UNIXTIME(?), ?);`) 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 { if err != nil {
return err return err
} }
defer stmt.Close() 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 { if err != nil {
return err return err
} }