234 lines
8.2 KiB
Go
234 lines
8.2 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
)
|
|
|
|
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")
|
|
failOnError(err, "initDB : set foreign_key_checks = 0")
|
|
|
|
var name string
|
|
rows, err := db.Query("show tables")
|
|
failOnError(err, "initDB : show tables")
|
|
|
|
for rows.Next() {
|
|
err = rows.Scan(&name)
|
|
failOnError(err, "initDB : show tables listing")
|
|
|
|
_, err = tx.Exec("drop table " + name)
|
|
failOnError(err, "initDB : drop table "+name)
|
|
}
|
|
err = rows.Err()
|
|
failOnError(err, "initDB : show tables listing end")
|
|
rows.Close()
|
|
|
|
_, err = tx.Exec("set foreign_key_checks = 1")
|
|
failOnError(err, "initDB : set foreign_key_checks = 1")
|
|
|
|
err = tx.Commit()
|
|
failOnError(err, "initDB : commit cleanup")
|
|
|
|
log.Println("Database cleaned up")
|
|
|
|
_, 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=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table code_obj_type")
|
|
|
|
_, 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=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table code_obj_sub_type")
|
|
|
|
_, err = db.Exec(`CREATE TABLE obj (
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
|
|
,obj_type_id SMALLINT UNSIGNED NOT NULL
|
|
,obj_sub_type_id SMALLINT UNSIGNED NOT NULL
|
|
,PRIMARY KEY (id)
|
|
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table obj")
|
|
|
|
_, err = db.Exec(`CREATE TABLE obj_user (
|
|
obj_id BIGINT UNSIGNED NOT NULL
|
|
,telegram_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=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table obj_user")
|
|
|
|
_, 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=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table obj_guild")
|
|
|
|
_, err = db.Exec(`CREATE TABLE obj_msg (
|
|
obj_id BIGINT UNSIGNED NOT NULL
|
|
,msg_id BIGINT NOT NULL
|
|
,chat_id BIGINT NOT NULL
|
|
,user_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=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table obj_msg")
|
|
|
|
_, 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 TINYINT NOT NULL
|
|
,crit TINYINT NOT NULL
|
|
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
|
|
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
|
failOnError(err, "initDB : create table obj_msg_report")
|
|
|
|
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
|
|
VALUES (` + strconv.Itoa(objTypeUser) + `, "user", "User")
|
|
,(` + strconv.Itoa(objTypeGuild) + `, "guild", "Guild")
|
|
,(` + strconv.Itoa(objTypeMessage) + `, "msg", "Message");`)
|
|
failOnError(err, "initDB : populate table code_obj_type")
|
|
|
|
_, err = db.Exec(`INSERT INTO code_obj_sub_type (id, intl_id, name, obj_type_id)
|
|
VALUES (` + strconv.Itoa(objSubTypeMessageUnprocess) + `, "unprocessed", "Unprocessed", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageUnknown) + `, "unknown", "Unknown", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageReport) + `, "report", "War report", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageGReport) + `, "g_report", "Guild War report", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageQuest) + `, "quest", "Quest result", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageFight) + `, "fight", "Fight result", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageHero) + `, "hero", "Hero summary", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageMe) + `, "me", "Hero short summary", ` + strconv.Itoa(objTypeMessage) + `)
|
|
,(` + strconv.Itoa(objSubTypeMessageInventory) + `, "inv", "Inventory", ` + strconv.Itoa(objTypeMessage) + `);`)
|
|
failOnError(err, "initDB : populate table code_obj_sub_type")
|
|
|
|
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 0, err
|
|
}
|
|
|
|
objId, err := res.LastInsertId()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date , text)
|
|
VALUES (?, ?, ?, ?, ?, FROM_UNIXTIME(?), ?);`)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
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.obj_id = ?`)
|
|
if err != nil {
|
|
return m, err
|
|
}
|
|
defer stmt.Close()
|
|
|
|
err = stmt.QueryRow(objId).Scan(&m.ID64, &m.ChatID64, &m.SenderUserID64, &m.Date, &m.Text)
|
|
if err != nil {
|
|
return m, err
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func getObjSubTypeId(objId int64) (int64, error) {
|
|
var objSubTypeId int64
|
|
stmt, err := db.Prepare(`SELECT o.sub_type_id FROM obj o WHERE o.id = ?`)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer stmt.Close()
|
|
|
|
err = stmt.QueryRow(1).Scan(&objSubTypeId)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return objSubTypeId, nil
|
|
}
|
|
|
|
func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int32, gold int32, stock int32, crit bool, stamina bool) error {
|
|
/*
|
|
objSubTypeId, err := getObjSubTypeId(objId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if objSubTypeId != objSubTypeMessageUnprocess {
|
|
return errors.New("Message is not of type Unknown")
|
|
}
|
|
|
|
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 MALLINT UNSIGNED NOT NULL
|
|
,exp SMALLINT UNSIGNED NOT NULL
|
|
,stamina TINYINT NOT NULL
|
|
,crit TINYINT NOT NULL
|
|
|
|
stmt, err := db.Prepare(`INSERT INTO obj_msg_report (obj_id, war_date, attack, defense, gold, stock, exp, stamina, crit)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?FROM_UNIXTIME(?), ?);`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*/
|
|
return nil
|
|
}
|