chirpnest/sql.go

745 lines
29 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
package main
import (
2019-05-06 16:05:43 +02:00
"errors"
2019-05-10 16:13:14 +02:00
"fmt"
2019-05-03 05:58:36 +02:00
"log"
2019-05-08 12:47:32 +02:00
"regexp"
2019-05-06 06:01:01 +02:00
"strconv"
2020-02-03 09:45:00 +01:00
"github.com/go-sql-driver/mysql"
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")
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
2019-05-10 12:19:38 +02:00
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-07-08 05:49:56 +02:00
if ok, _ := regexp.MatchString(`^.*_v$`, name); ok {
2019-07-08 05:49:03 +02:00
_, err = tx.Exec("drop view " + name)
failOnError(err, "initDB : drop view "+name)
} else {
_, err = tx.Exec("drop table " + name)
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
2019-05-11 09:48:39 +02:00
log.Println("initDB : Database cleaned up")
2019-05-04 10:57:24 +02:00
_, err = db.Exec(`CREATE TABLE code_obj_type (
2020-01-02 11:22:08 +01:00
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
2019-05-04 10:57:24 +02:00
,intl_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,PRIMARY KEY (id)
2019-05-18 13:11:53 +02:00
,UNIQUE KEY (intl_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-06 05:03:12 +02:00
failOnError(err, "initDB : create table code_obj_type")
2019-05-11 09:48:39 +02:00
log.Println("initDB : code_obj_type created ...")
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 (
2020-01-02 11:22:08 +01:00
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
2019-05-07 13:15:25 +02:00
,intl_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
2019-05-05 13:29:28 +02:00
,obj_type_id SMALLINT UNSIGNED NOT NULL
,PRIMARY KEY (id)
2019-05-18 13:11:53 +02:00
,UNIQUE KEY (intl_id)
2019-05-05 13:29:28 +02:00
,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-06 05:03:12 +02:00
failOnError(err, "initDB : create table code_obj_sub_type")
2019-05-11 09:48:39 +02:00
log.Println("initDB : code_obj_sub_type created ...")
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-10 11:23:47 +02:00
,timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
2019-05-04 10:57:24 +02:00
,PRIMARY KEY (id)
2019-05-08 12:36:29 +02:00
,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE
,FOREIGN KEY (obj_sub_type_id) REFERENCES code_obj_sub_type(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-06 05:03:12 +02:00
failOnError(err, "initDB : create table obj")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj created ...")
2019-05-04 10:57:24 +02:00
2019-10-10 11:18:52 +02:00
_, err = db.Exec(`CREATE TABLE obj_item (
obj_id BIGINT UNSIGNED NOT NULL
,intl_id VARCHAR(32)
,weight SMALLINT NOT NULL
,exchange TINYINT NOT NULL
,auction TINYINT NOT NULL
2020-01-27 09:38:11 +01:00
,craftable TINYINT NOT NULL
2019-10-10 11:18:52 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (intl_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_item")
log.Println("initDB : obj_item created ...")
2020-01-26 07:35:38 +01:00
_, err = db.Exec(`CREATE TABLE obj_craft (
obj_id BIGINT UNSIGNED NOT NULL
,cmd VARCHAR(32)
,mana INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (cmd)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_craft")
log.Println("initDB : obj_craft created ...")
_, err = db.Exec(`CREATE TABLE obj_craft_item (
obj_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (obj_id, item_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_craft_item")
log.Println("initDB : obj_craft_item created ...")
2019-05-25 09:25:11 +02:00
_, err = db.Exec(`CREATE TABLE obj_castle (
obj_id BIGINT UNSIGNED NOT NULL
2019-05-25 09:50:17 +02:00
,logo VARCHAR(32) NOT NULL
2019-05-25 09:25:11 +02:00
,name VARCHAR(80) NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-25 09:25:11 +02:00
failOnError(err, "initDB : create table obj_castle")
log.Println("initDB : obj_guild created ...")
_, err = db.Exec(`CREATE TABLE obj_guild (
obj_id BIGINT UNSIGNED NOT NULL
,tag VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,chat_id BIGINT
,deposit_chat_id BIGINT
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-25 09:25:11 +02:00
failOnError(err, "initDB : create table obj_guild")
log.Println("initDB : obj_guild created ...")
2019-05-04 10:57:24 +02:00
_, err = db.Exec(`CREATE TABLE obj_user (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(80) NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-06 05:03:12 +02:00
failOnError(err, "initDB : create table obj_user")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_user created ...")
2019-05-04 10:57:24 +02:00
2019-05-07 05:12:03 +02:00
_, err = db.Exec(`CREATE TABLE obj_war (
obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
2019-05-09 12:36:45 +02:00
,start_time DATETIME NOT NULL
,end_time DATETIME NOT NULL
2019-05-07 05:12:03 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-07 05:12:03 +02:00
failOnError(err, "initDB : create table obj_war")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_war created ...")
2019-05-07 05:12:03 +02:00
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
2019-08-13 05:09:44 +02:00
,user_id BIGINT NOT NULL CHECK (user_id > 0)
,sender_user_id BIGINT NOT NULL CHECK (sender_user_id > 0)
2019-05-09 12:36:45 +02:00
,date DATETIME NOT NULL
2019-05-04 10:57:24 +02:00
,text VARCHAR(4096) NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-08-13 05:09:44 +02:00
,UNIQUE KEY (msg_id, chat_id, user_id, sender_user_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-06 05:03:12 +02:00
failOnError(err, "initDB : create table obj_msg")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_msg created ...")
2019-05-03 05:58:36 +02:00
2019-05-15 12:02:17 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_pillage_inc (
obj_id BIGINT UNSIGNED NOT NULL
,attacker VARCHAR(32)
,guild VARCHAR(32)
2019-05-25 09:41:16 +02:00
,castle_id BIGINT UNSIGNED NOT NULL
2019-05-15 12:02:17 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-05-25 09:41:16 +02:00
,FOREIGN KEY (castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-15 12:02:17 +02:00
failOnError(err, "initDB : create table obj_msg_report")
log.Println("initDB : obj_msg_report created ...")
2019-05-05 13:29:28 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_report (
2019-05-06 12:43:42 +02:00
obj_id BIGINT UNSIGNED NOT NULL
2019-05-07 05:12:03 +02:00
,war_id BIGINT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (war_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-07 05:12:03 +02:00
failOnError(err, "initDB : create table obj_msg_report")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_msg_report created ...")
2019-05-07 05:12:03 +02:00
_, err = db.Exec(`CREATE TABLE obj_war_report (
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,war_id BIGINT UNSIGNED NOT NULL
2019-05-06 12:43:42 +02:00
,attack SMALLINT UNSIGNED NOT NULL
,defense SMALLINT UNSIGNED NOT NULL
,gold SMALLINT UNSIGNED NOT NULL
2019-05-06 15:50:57 +02:00
,stock SMALLINT UNSIGNED NOT NULL
2019-05-05 13:29:28 +02:00
,exp SMALLINT UNSIGNED NOT NULL
2019-05-06 12:43:42 +02:00
,stamina TINYINT NOT NULL
,crit TINYINT NOT NULL
2019-05-05 13:29:28 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-05-07 05:12:03 +02:00
,FOREIGN KEY (war_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (user_id, war_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-11 09:48:39 +02:00
failOnError(err, "initDB : create table obj_war_report")
log.Println("initDB : obj_war_report created ...")
2019-05-05 13:29:28 +02:00
2019-05-09 11:19:02 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_auction_announce (
2019-06-06 07:52:22 +02:00
obj_id BIGINT UNSIGNED NOT NULL
,lot_id BIGINT UNSIGNED NOT NULL
2019-06-06 08:08:25 +02:00
,item_id BIGINT UNSIGNED
2019-06-06 07:52:22 +02:00
,cond VARCHAR(32)
,quality VARCHAR(32)
,seller_castle_id BIGINT UNSIGNED
,seller_guild_id BIGINT UNSIGNED
,seller_id BIGINT UNSIGNED
,buyer_castle_id BIGINT UNSIGNED
,buyer_guild_id BIGINT UNSIGNED
,buyer_id BIGINT UNSIGNED
,price SMALLINT NOT NULL
,status VARCHAR(32)
,end DATETIME NOT NULL
2019-05-08 15:39:04 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-10 12:45:33 +02:00
,FOREIGN KEY (seller_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,FOREIGN KEY (buyer_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,FOREIGN KEY (seller_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE
,FOREIGN KEY (buyer_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE
,FOREIGN KEY (seller_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
,FOREIGN KEY (buyer_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
2019-05-11 09:55:42 +02:00
,KEY (lot_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-09 11:19:02 +02:00
failOnError(err, "initDB : create table obj_msg_auction_announce")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_msg_auction_announce created ...")
2019-05-08 15:39:04 +02:00
2019-05-19 14:00:18 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_duel_fight (
obj_id BIGINT UNSIGNED NOT NULL
2019-05-25 09:41:16 +02:00
,win_castle_id BIGINT UNSIGNED NOT NULL
2019-05-25 10:24:59 +02:00
,win_guild_id BIGINT UNSIGNED
2019-05-25 10:56:13 +02:00
,win_user_id BIGINT UNSIGNED NOT NULL
2019-05-19 14:00:18 +02:00
,win_life SMALLINT NOT NULL
2019-05-25 09:41:16 +02:00
,loss_castle_id BIGINT UNSIGNED NOT NULL
2019-05-25 10:24:59 +02:00
,loss_guild_id BIGINT UNSIGNED
2019-05-25 10:56:13 +02:00
,loss_user_id BIGINT UNSIGNED NOT NULL
2019-05-19 14:00:18 +02:00
,loss_life SMALLINT NOT NULL
,exp INT UNSIGNED NOT NULL
,weapon VARCHAR(80)
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-05-25 09:41:16 +02:00
,FOREIGN KEY (win_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
,FOREIGN KEY (loss_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
2019-05-25 10:24:59 +02:00
,FOREIGN KEY (win_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE
,FOREIGN KEY (loss_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE
2019-05-25 10:56:13 +02:00
,FOREIGN KEY (win_user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,FOREIGN KEY (loss_user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-19 14:00:18 +02:00
failOnError(err, "initDB : create table obj_msg_duel_fight")
log.Println("initDB : obj_msg_duel_fight created ...")
2019-05-14 04:28:26 +02:00
2019-05-10 04:26:37 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_auction_upd_req (
obj_id BIGINT UNSIGNED NOT NULL
,lot_id BIGINT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-05-11 09:45:20 +02:00
,KEY (lot_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-10 04:26:37 +02:00
failOnError(err, "initDB : create table obj_msg_auction_upd_req")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_msg_auction_upd_req created ...")
2019-05-10 04:26:37 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_auction_upd_ack (
obj_id BIGINT UNSIGNED NOT NULL
,seller VARCHAR(32)
,buyer VARCHAR(32)
,item VARCHAR(80)
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-11 09:48:39 +02:00
failOnError(err, "initDB : create table obj_msg_auction_upd_ack")
log.Println("initDB : obj_msg_auction_upd_ack created ...")
2019-05-10 04:26:37 +02:00
2019-10-10 11:15:58 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_exp (
obj_id BIGINT UNSIGNED NOT NULL
,exp INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_msg_exp")
log.Println("initDB : obj_msg_exp created ...")
_, err = db.Exec(`CREATE TABLE obj_msg_gold (
obj_id BIGINT UNSIGNED NOT NULL
,gold INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_msg_gold")
log.Println("initDB : obj_msg_gold created ...")
_, err = db.Exec(`CREATE TABLE obj_msg_item (
obj_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity INT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE
2020-02-03 07:18:17 +01:00
,UNIQUE KEY (obj_id, item_id)
2019-10-10 11:15:58 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2020-02-03 07:18:17 +01:00
failOnError(err, "initDB : create table obj_msg_item")
2019-10-10 11:15:58 +02:00
log.Println("initDB : obj_msg_item created ...")
2020-02-03 07:18:17 +01:00
_, err = db.Exec(`CREATE TABLE obj_msg_tribute (
2019-05-31 04:02:21 +02:00
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity SMALLINT NOT NULL
,xp SMALLINT NOT NULL
,date DATETIME NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE
,UNIQUE KEY (user_id, date)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2020-02-03 07:18:17 +01:00
failOnError(err, "initDB : create table obj_msg_tribute")
log.Println("initDB : obj_msg_tribute created ...")
_, err = db.Exec(`CREATE TABLE obj_tribute (
tg_user_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity INT NOT NULL
,exp INT NOT NULL
,date DATETIME NOT NULL
,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE
,UNIQUE KEY (tg_user_id, item_id, quantity, exp, date)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-31 04:02:21 +02:00
failOnError(err, "initDB : create table obj_tribute")
log.Println("initDB : obj_tribute created ...")
2020-02-03 07:18:17 +01:00
_, err = db.Exec(`CREATE TABLE msg_rules (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
,prio SMALLINT NOT NULL
,descn VARCHAR(32) NOT NULL
,rule VARCHAR(4096) NOT NULL
,msg_type_id SMALLINT UNSIGNED NOT NULL
,chat_id BIGINT NOT NULL
,user_id BIGINT NOT NULL
,UNIQUE KEY (id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table msg_rules")
log.Println("initDB : msg_rules created ...")
2019-05-31 04:02:21 +02:00
_, err = db.Exec(`CREATE TABLE obj_xp (
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,val BIGINT UNSIGNED NOT NULL
,target BIGINT UNSIGNED NOT NULL
,level SMALLINT UNSIGNED NOT NULL
,date DATETIME NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,UNIQUE KEY (user_id, date)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-31 04:02:21 +02:00
failOnError(err, "initDB : create table obj_xp")
log.Println("initDB : obj_xp created ...")
2019-07-09 06:56:41 +02:00
_, err = db.Exec(`CREATE TABLE obj_quest (
obj_id BIGINT UNSIGNED NOT NULL
,user_id BIGINT UNSIGNED NOT NULL
,duration SMALLINT UNSIGNED NOT NULL
,date DATETIME NOT NULL
,exp INT UNSIGNED NOT NULL
,gold INT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE
,UNIQUE KEY (user_id, date)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
failOnError(err, "initDB : create table obj_quest")
log.Println("initDB : obj_xp obj_quest ...")
2019-05-10 11:23:47 +02:00
_, err = db.Exec(`CREATE TABLE obj_job (
obj_id BIGINT UNSIGNED NOT NULL
,priority SMALLINT NOT NULL
2019-05-15 12:31:03 +02:00
,user_id BIGINT UNSIGNED NOT NULL
2019-08-21 05:59:53 +02:00
,trigger_id BIGINT UNSIGNED NOT NULL
2019-05-10 11:23:47 +02:00
,schedule DATETIME NOT NULL
2019-05-11 06:54:12 +02:00
,is_done TINYINT NOT NULL
,in_work TINYINT NOT NULL
2019-06-03 03:19:06 +02:00
,seq_nr BIGINT UNSIGNED
2019-05-11 07:00:20 +02:00
,inserted TIMESTAMP
2019-05-11 06:54:12 +02:00
,pulled TIMESTAMP
,started TIMESTAMP
,ended TIMESTAMP
2019-08-23 14:09:08 +02:00
,timeout TIMESTAMP
2020-01-21 07:14:58 +01:00
,zipped TINYINT NOT NULL
2020-01-12 10:37:46 +01:00
,payload VARBINARY(20000)
2019-05-10 11:23:47 +02:00
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
2019-05-18 16:36:04 +02:00
,KEY (is_done)
,KEY (in_work)
2019-05-15 12:31:03 +02:00
,KEY (user_id)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-05-10 11:23:47 +02:00
failOnError(err, "initDB : create table obj_job")
2019-05-11 09:48:39 +02:00
log.Println("initDB : obj_job created ...")
2019-05-10 11:23:47 +02:00
2020-01-12 12:43:45 +01:00
_, err = db.Exec(`CREATE TABLE obj_item_craft (
obj_id BIGINT UNSIGNED NOT NULL
,item_id BIGINT UNSIGNED NOT NULL
,quantity INT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (item_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_item_craft")
log.Println("initDB : obj_item_craft created ...")
2019-08-29 11:14:15 +02:00
2019-06-14 12:13:10 +02:00
_, err = db.Exec(`CREATE TABLE obj_name (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(80)
,priority INT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY(name)
2019-06-25 11:54:46 +02:00
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
2019-06-14 12:13:10 +02:00
failOnError(err, "initDB : create table obj_name")
log.Println("initDB : obj_name created ...")
2020-02-01 12:03:22 +01:00
log.Println("initDB : Database set up")
}
func initDBViews() {
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Rollback()
_, err = tx.Exec("set foreign_key_checks = 0")
failOnError(err, "initDBViews : set foreign_key_checks = 0")
var name string
rows, err := db.Query("show tables")
failOnError(err, "initDBViews : show tables")
for rows.Next() {
err = rows.Scan(&name)
failOnError(err, "initDBViews : show tables listing")
if ok, _ := regexp.MatchString(`^.*_v$`, name); ok {
_, err = tx.Exec("drop view " + name)
failOnError(err, "initDBViews : drop view "+name)
}
}
err = rows.Err()
failOnError(err, "initDBViews : show tables listing end")
rows.Close()
_, err = tx.Exec("set foreign_key_checks = 1")
failOnError(err, "initDBViews : set foreign_key_checks = 1")
err = tx.Commit()
failOnError(err, "initDBViews : commit cleanup")
log.Println("initDBViews : Views cleaned up")
_, err = db.Exec(`CREATE VIEW obj_user_v AS
SELECT ou.obj_id
,ou.name COLLATE utf8mb4_unicode_ci AS name
FROM obj_user ou;`)
failOnError(err, "initDBViews : create view obj_user_v")
log.Println("initDBViews : obj_user_v created ...")
_, err = db.Exec(`CREATE VIEW obj_msg_v AS
SELECT om.obj_id
,o.obj_sub_type_id AS msg_type_id
,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type
,om.msg_id
,om.chat_id
,om.user_id
,om.sender_user_id
,om.date
,om.text COLLATE utf8mb4_unicode_ci AS text
FROM obj_msg om
,obj o
,code_obj_sub_type cost
WHERE om.obj_id = o.id
AND cost.id = o.obj_sub_type_id;`)
failOnError(err, "initDBViews : create view obj_msg_v")
log.Println("initDBViews : obj_msg_v created ...")
_, err = db.Exec(`CREATE VIEW msg_rules_v AS
SELECT r.id
,r.prio
,r.descn COLLATE utf8mb4_unicode_ci AS descn
,r.rule COLLATE utf8mb4_unicode_ci AS rule
,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type
FROM msg_rules r
,code_obj_sub_type cost
WHERE cost.id = r.msg_type_id;`)
failOnError(err, "initDBViews : create table msg_rules_v")
log.Println("initDBViews : msg_rules_v created ...")
2019-07-08 05:44:23 +02:00
_, err = db.Exec(`CREATE VIEW obj_name_v AS
2019-07-09 06:56:41 +02:00
SELECT obn.obj_id
,obn.name COLLATE utf8mb4_unicode_ci AS name
,obn.priority
2019-07-08 05:44:23 +02:00
FROM obj_name obn;`)
2020-02-01 12:03:22 +01:00
failOnError(err, "initDBViews : create view obj_name_v")
log.Println("initDBViews : obj_name_v created ...")
2019-07-08 05:44:23 +02:00
2019-08-12 10:55:15 +02:00
_, err = db.Exec(`CREATE VIEW obj_item_name_v AS
SELECT obi.obj_id
2019-08-21 06:23:59 +02:00
,obi.intl_id COLLATE utf8mb4_unicode_ci AS intl_id
2019-08-12 10:55:15 +02:00
,obn.name COLLATE utf8mb4_unicode_ci AS name
,obn.priority
FROM obj_name obn
,obj_item obi
WHERE obi.obj_id = obn.obj_id
ORDER BY obi.intl_id ASC;`)
2020-02-01 12:03:22 +01:00
failOnError(err, "initDBViews : create view obj_item_name_v")
log.Println("initDBViews : obj_item_name_v created ...")
2019-05-04 11:15:33 +02:00
2019-10-11 06:00:25 +02:00
_, err = db.Exec(`CREATE VIEW obj_msg_vault_v AS
2019-10-11 09:04:42 +02:00
SELECT obm.sender_user_id user_id
,obm.chat_id
2019-10-11 08:53:49 +02:00
,om.obj_sub_type_id msg_type_id
2019-10-11 06:03:42 +02:00
,omi.item_id
2019-10-11 08:53:49 +02:00
,oi.obj_sub_type_id item_type_id
2019-10-11 06:03:42 +02:00
,omi.quantity
2019-10-11 08:53:49 +02:00
FROM obj om
2019-10-11 09:04:42 +02:00
,obj_msg obm
2019-10-11 08:53:49 +02:00
,obj oi
2019-10-11 06:03:42 +02:00
,obj_msg_item omi
2020-01-02 16:41:00 +01:00
WHERE om.obj_type_id = ` + strconv.FormatInt(cacheObjType[`msg`], 10) + `
2020-01-02 12:45:56 +01:00
AND om.obj_sub_type_id in (` + strconv.FormatInt(cacheObjSubType[`msg_withdraw_rcv`], 10) + `, ` + strconv.FormatInt(cacheObjSubType[`msg_g_deposit_ack`], 10) + `)
2019-10-11 09:04:42 +02:00
AND obm.obj_id = om.id
2019-10-11 09:05:34 +02:00
AND omi.obj_id = om.id
2019-10-11 08:53:49 +02:00
AND oi.id = omi.item_id
2019-10-11 09:06:19 +02:00
ORDER BY obm.sender_user_id ASC
2019-10-11 06:03:42 +02:00
,omi.item_id ASC;`)
2020-02-01 12:03:22 +01:00
failOnError(err, "initDBViews : create view obj_msg_vault_v")
log.Println("initDBViews : obj_msg_vault_v created ...")
2019-10-11 06:00:25 +02:00
2020-02-01 12:03:22 +01:00
log.Println("initDBViews : Views set up")
2019-05-04 11:15:33 +02:00
}
2020-01-02 11:04:48 +01:00
func insertObjType(intlId string, name string) error {
stmt, err := db.Prepare(`INSERT INTO code_obj_type (intl_id, name)
VALUES (?, ?);`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(intlId, name)
if err != nil {
return err
}
return nil
}
2020-01-02 11:22:08 +01:00
func insertObjSubType(intlId string, name string, objType string) error {
2020-01-02 11:35:41 +01:00
stmt, err := db.Prepare(`INSERT INTO code_obj_sub_type
SELECT null id, ? intl_id, ? name, cot.id
FROM code_obj_type cot
WHERE cot.intl_id = ?;`)
2020-01-02 11:22:08 +01:00
if err != nil {
return err
}
defer stmt.Close()
2020-01-02 11:35:41 +01:00
_, err = stmt.Exec(intlId, name, objType)
2020-01-02 11:22:08 +01:00
if err != nil {
return err
}
return nil
}
2019-10-10 12:14:29 +02:00
func insertMsgItem(objId int64, itemId int64, quantity int64) error {
stmt, err := db.Prepare(`INSERT INTO obj_msg_item (obj_id, item_id, quantity)
VALUES (?, ?, ?);`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(objId, itemId, quantity)
if err != nil {
return err
}
return nil
}
2019-05-08 17:18:17 +02:00
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
}
2020-01-02 12:35:03 +01:00
if objSubTypeId != cacheObjSubType[`msg`] {
2019-05-08 17:18:17 +02:00
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
}
2019-05-19 14:00:18 +02:00
func insertMsgDuelFight(m *ChatWarsMessageDuelFight) error {
objSubTypeId, err := getObjSubTypeId(m.ObjID64)
if err != nil {
return err
}
2020-01-02 12:35:03 +01:00
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_duel_fight`] {
2019-05-19 14:00:18 +02:00
return errors.New("Message type mismatch")
}
2019-05-25 10:56:13 +02:00
stmt, err := db.Prepare(`INSERT INTO obj_msg_duel_fight (obj_id, win_castle_id, win_guild_id, win_user_id, win_life, loss_castle_id, loss_guild_id, loss_user_id, loss_life, weapon)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`)
2019-05-19 14:00:18 +02:00
if err != nil {
return err
}
defer stmt.Close()
2019-05-25 10:56:13 +02:00
_, err = stmt.Exec(m.ObjID64, getObjCastleID(m.WinCastle), getObjGuildID(m.WinGuild), getObjUserID(m.WinUser), m.WinLife, getObjCastleID(m.LossCastle), getObjGuildID(m.LossGuild), getObjUserID(m.LossUser), m.LossLife, m.Weapon)
2019-05-19 14:00:18 +02:00
if err != nil {
return err
}
2020-02-03 07:18:17 +01:00
return nil
}
func insertMsgTributesStats(m *ChatWarsMessageTributesStatsAck) error {
2020-02-03 07:19:39 +01:00
objSubTypeId, err := getObjSubTypeId(m.Msg.ObjID64)
2020-02-03 07:18:17 +01:00
if err != nil {
return err
}
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_tributes_stats_ack`] {
return errors.New("Message type mismatch")
}
stmt, err := db.Prepare(`INSERT INTO obj_tribute (tg_user_id, item_id, quantity, exp, date)
VALUES (?, ?, ?, ?, ?);`)
logOnError(err, "insertMsgTributesStats : Prepare")
if err != nil {
return err
}
defer stmt.Close()
for _, t := range m.Tributes {
2020-02-03 07:21:16 +01:00
_, err = stmt.Exec(m.Msg.TGUserID64, t.ItemID64, t.Quantity, t.Exp, t.Date)
2020-02-03 09:45:00 +01:00
mysqlerr, ok := err.(*mysql.MySQLError)
if ok && mysqlerr.Number == 1062 {
// duplicate, expected
} else {
logOnError(err, "insertMsgTributesStats Exec")
}
2020-02-03 07:18:17 +01:00
}
2019-05-19 14:00:18 +02:00
return nil
}
2019-05-09 11:19:02 +02:00
func insertMsgAuctionAnnounce(m *ChatWarsMessageAuctionAnnounce) error {
2019-05-11 07:42:46 +02:00
objSubTypeId, err := getObjSubTypeId(m.ObjID64)
2019-05-06 16:05:43 +02:00
if err != nil {
return err
}
2020-01-02 12:35:03 +01:00
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_auction_announce`] {
2019-05-09 11:19:02 +02:00
return errors.New("Message type mismatch")
2019-05-06 16:05:43 +02:00
}
2019-05-06 15:49:55 +02:00
2019-06-06 08:06:29 +02:00
stmt, err := db.Prepare(`INSERT INTO obj_msg_auction_announce (obj_id, lot_id, item_id, cond, quality, seller_id, seller_guild_id, seller_castle_id, buyer_id, buyer_guild_id, buyer_castle_id, price, status, end)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`)
2019-05-09 11:19:02 +02:00
if err != nil {
return err
}
defer stmt.Close()
2019-05-06 15:49:55 +02:00
2019-06-06 07:56:28 +02:00
_, err = stmt.Exec(m.ObjID64, m.LotID, m.ItemID64, m.Cond, m.Quality, m.SellerUserID64, m.SellerGuildID64, m.SellerCastleID64, m.BuyerUserID64, m.BuyerGuildID64, m.BuyerCastleID64, m.Price, m.Status, m.End)
2019-05-09 11:19:02 +02:00
if err != nil {
return err
}
2019-05-15 10:58:25 +02:00
2019-05-06 12:43:42 +02:00
return nil
}
2019-05-08 12:13:47 +02:00
2019-05-15 12:02:17 +02:00
func insertMsgPillageInc(m *ChatWarsMessagePillageInc) error {
objSubTypeId, err := getObjSubTypeId(m.ObjID64)
if err != nil {
return err
}
2020-01-02 12:35:03 +01:00
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_pillage_inc`] {
2019-05-15 12:02:17 +02:00
return errors.New("Message type mismatch")
}
stmt, err := db.Prepare(`INSERT INTO obj_msg_pillage_inc (obj_id, attacker, guild, castle_id)
VALUES (?, ?, ?, ?);`)
if err != nil {
return err
}
defer stmt.Close()
2019-05-25 09:39:34 +02:00
_, err = stmt.Exec(m.ObjID64, m.Attacker, m.Guild, getObjCastleID(m.Castle))
2019-05-19 14:00:18 +02:00
2019-05-15 12:02:17 +02:00
if err != nil {
return err
}
return nil
}
2019-05-11 06:54:12 +02:00
func getSQLListID64(q string) []int64 {
2019-05-11 06:56:48 +02:00
var (
id int64
ids []int64
)
2019-05-11 06:54:12 +02:00
rows, err := db.Query(q)
s := fmt.Sprintf("getSQLListID64 : Query(%s)", q)
logOnError(err, s)
for rows.Next() {
err = rows.Scan(&id)
logOnError(err, "getSQLListID64 : scan next val")
ids = append(ids, id)
}
err = rows.Err()
2019-05-11 06:56:58 +02:00
logOnError(err, "getSQLListID64 : query end")
2019-05-11 06:54:12 +02:00
rows.Close()
2019-05-16 05:09:51 +02:00
2019-05-11 06:54:12 +02:00
return ids
}