2019-05-03 05:58:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2019-05-04 11:15:54 +02:00
|
|
|
"strconv"
|
2019-05-03 05:58:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func initDB() {
|
|
|
|
log.Println("Setting up database...")
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
_, err = tx.Exec("set foreign_key_checks = 0")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var name string
|
|
|
|
rows, err := db.Query("show tables")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = tx.Exec("drop table " + name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
_, err = tx.Exec("set foreign_key_checks = 1")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Database cleaned up")
|
2019-05-04 10:57:24 +02:00
|
|
|
|
|
|
|
_, 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 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
|
2019-05-04 11:04:52 +02:00
|
|
|
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
|
2019-05-04 10:57:24 +02:00
|
|
|
) 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)
|
2019-05-03 05:58:36 +02:00
|
|
|
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-04 10:57:24 +02:00
|
|
|
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
|
|
|
|
VALUES (1, "user", "User")
|
|
|
|
,(2, "guild", "Guild")
|
|
|
|
,(3, "msg", "Message");`)
|
2019-05-03 05:58:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Database set up")
|
|
|
|
}
|
2019-05-03 09:15:16 +02:00
|
|
|
|
2019-05-04 11:15:33 +02:00
|
|
|
func SQLCWMsgWorker(id int, msgs <-chan ChatWarsMessage) {
|
|
|
|
for m := range msgs {
|
|
|
|
err := putMsg(m)
|
|
|
|
if err != nil {
|
|
|
|
logOnError(err, "SQLCWMsgWorker["+strconv.Itoa(id)+"] : Inserting message.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("SQLCWMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:08:38 +02:00
|
|
|
func putMsg(m ChatWarsMessage) error {
|
2019-05-04 10:57:24 +02:00
|
|
|
res, err := db.Exec(`INSERT INTO obj (obj_type_id)
|
|
|
|
VALUES (3);`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
|
2019-05-04 11:18:20 +02:00
|
|
|
stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, sender_user_id, date , text)
|
|
|
|
VALUES (?, ?, ?, ?, FROM_UNIXTIME(?), ?);`)
|
2019-05-03 09:15:16 +02:00
|
|
|
if err != nil {
|
2019-05-03 18:08:38 +02:00
|
|
|
return err
|
2019-05-03 09:15:16 +02:00
|
|
|
}
|
2019-05-03 18:08:38 +02:00
|
|
|
defer stmt.Close()
|
2019-05-03 09:15:16 +02:00
|
|
|
|
2019-05-04 10:57:24 +02:00
|
|
|
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.SenderUserID64, m.Date, m.Text)
|
2019-05-03 09:15:16 +02:00
|
|
|
if err != nil {
|
2019-05-03 18:08:38 +02:00
|
|
|
return err
|
2019-05-03 09:15:16 +02:00
|
|
|
}
|
2019-05-03 13:24:32 +02:00
|
|
|
|
2019-05-03 18:08:38 +02:00
|
|
|
return nil
|
2019-05-03 09:15:16 +02:00
|
|
|
}
|