2019-05-03 05:58:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : set foreign_key_checks = 0")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
|
|
|
var name string
|
|
|
|
rows, err := db.Query("show tables")
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : show tables")
|
|
|
|
|
2019-05-03 05:58:36 +02:00
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&name)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : show tables listing")
|
|
|
|
|
2019-05-03 05:58:36 +02:00
|
|
|
_, err = tx.Exec("drop table " + name)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : drop table "+name)
|
2019-05-03 05:58:36 +02:00
|
|
|
}
|
|
|
|
err = rows.Err()
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : show tables listing end")
|
2019-05-03 05:58:36 +02:00
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
_, err = tx.Exec("set foreign_key_checks = 1")
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : set foreign_key_checks = 1")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
|
|
|
err = tx.Commit()
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : commit cleanup")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
|
|
|
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;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table code_obj_type")
|
2019-05-04 10:57:24 +02:00
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
_, 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;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table code_obj_sub_type")
|
2019-05-05 13:29:28 +02:00
|
|
|
|
2019-05-04 10:57:24 +02:00
|
|
|
_, err = db.Exec(`CREATE TABLE obj (
|
2019-05-06 05:09:31 +02:00
|
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
|
|
|
|
,obj_type_id SMALLINT UNSIGNED NOT NULL
|
|
|
|
,obj_sub_type_id SMALLINT UNSIGNED NOT NULL
|
2019-05-04 10:57:24 +02:00
|
|
|
,PRIMARY KEY (id)
|
|
|
|
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8 COLLATE utf8_unicode_ci;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table obj")
|
2019-05-04 10:57:24 +02:00
|
|
|
|
|
|
|
_, err = db.Exec(`CREATE TABLE obj_user (
|
|
|
|
obj_id BIGINT UNSIGNED NOT NULL
|
2019-05-06 05:03:12 +02:00
|
|
|
,telegram_id BIGINT UNSIGNED NOT NULL
|
2019-05-04 10:57:24 +02:00
|
|
|
,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;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table obj_user")
|
2019-05-04 10:57:24 +02:00
|
|
|
|
|
|
|
_, 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;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table obj_guild")
|
2019-05-04 10:57:24 +02:00
|
|
|
|
|
|
|
_, 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;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table obj_msg")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
_, 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
|
|
|
|
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : create table obj_msg_report")
|
2019-05-05 13:29:28 +02:00
|
|
|
|
2019-05-04 10:57:24 +02:00
|
|
|
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
|
2019-05-06 05:59:19 +02:00
|
|
|
VALUES (` + objTypeUser + `, "user", "User")
|
|
|
|
,(` + objTypeGuild + `, "guild", "Guild")
|
|
|
|
,(` + objTypeMessage + `, "msg", "Message");`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : populate table code_obj_type")
|
2019-05-03 05:58:36 +02:00
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
_, 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)
|
|
|
|
,(3, "report", "War report", 3)
|
|
|
|
,(4, "g_report", "Guild War report", 3)
|
|
|
|
,(5, "quest", "Quest result", 3)
|
|
|
|
,(6, "fight", "Fight result", 3)
|
|
|
|
,(7, "hero", "Hero summary", 3)
|
|
|
|
,(8, "me", "Hero short summary", 3)
|
|
|
|
,(9, "inv", "Inventory", 3);`)
|
2019-05-06 05:03:12 +02:00
|
|
|
failOnError(err, "initDB : populate table code_obj_sub_type")
|
2019-05-04 11:15:33 +02:00
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
log.Println("Database set up")
|
2019-05-04 11:15:33 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
func putUnprocessedMsg(m ChatWarsMessage) (int64, error) {
|
|
|
|
res, err := db.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (3 , 1);`)
|
2019-05-04 10:57:24 +02:00
|
|
|
if err != nil {
|
2019-05-06 04:56:59 +02:00
|
|
|
return 0, err
|
2019-05-04 10:57:24 +02:00
|
|
|
}
|
2019-05-05 13:29:28 +02:00
|
|
|
|
2019-05-04 10:57:24 +02:00
|
|
|
objId, err := res.LastInsertId()
|
2019-05-05 13:29:28 +02:00
|
|
|
if err != nil {
|
2019-05-06 04:56:59 +02:00
|
|
|
return 0, err
|
2019-05-05 13:29:28 +02:00
|
|
|
}
|
2019-05-04 10:57:24 +02:00
|
|
|
|
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-06 04:56:59 +02:00
|
|
|
return 0, 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-06 04:56:59 +02:00
|
|
|
return 0, err
|
2019-05-05 13:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return objId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMsg(objId int64) (ChatWarsMessage, error) {
|
|
|
|
var m ChatWarsMessage
|
|
|
|
|
2019-05-06 05:13:11 +02:00
|
|
|
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.obj_id = ?`)
|
2019-05-05 13:29:28 +02:00
|
|
|
if err != nil {
|
2019-05-06 04:56:59 +02:00
|
|
|
return m, err
|
2019-05-05 13:29:28 +02:00
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2019-05-06 04:57:20 +02:00
|
|
|
err = stmt.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.SenderUserID64, &m.Date, &m.Text)
|
2019-05-05 13:29:28 +02:00
|
|
|
if err != nil {
|
2019-05-06 04:56:59 +02:00
|
|
|
return m, err
|
2019-05-03 09:15:16 +02:00
|
|
|
}
|
2019-05-03 13:24:32 +02:00
|
|
|
|
2019-05-05 13:29:28 +02:00
|
|
|
return m, nil
|
2019-05-03 09:15:16 +02:00
|
|
|
}
|