chirpnest/sql.go

1035 lines
39 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-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
2020-02-17 11:05:30 +01:00
_, err = db.Exec(`CREATE TABLE code_obj_classif (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
,intl_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,obj_type_id SMALLINT UNSIGNED NOT NULL
,PRIMARY KEY (id)
,UNIQUE KEY (intl_id)
,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table code_obj_type")
log.Println("initDB : code_obj_classif created ...")
_, err = db.Exec(`CREATE TABLE code_obj_class (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT
,intl_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,obj_classif_id SMALLINT UNSIGNED NOT NULL
,PRIMARY KEY (id)
,UNIQUE KEY (intl_id)
,FOREIGN KEY (obj_classif_id) REFERENCES code_obj_classif(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table code_obj_class")
log.Println("initDB : code_obj_class created ...")
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
2020-02-17 11:05:30 +01:00
_, err = db.Exec(`CREATE TABLE obj_class (
obj_id BIGINT UNSIGNED NOT NULL
,obj_classif_id SMALLINT UNSIGNED NOT NULL
,obj_class_id SMALLINT UNSIGNED NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (obj_classif_id) REFERENCES code_obj_classif(id) ON DELETE CASCADE
,FOREIGN KEY (obj_class_id) REFERENCES code_obj_class(id) ON DELETE CASCADE
,UNIQUE KEY (obj_id, obj_classif_id)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_class")
log.Println("initDB : obj_class created ...")
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
2020-02-05 03:37:50 +01:00
_, err = db.Exec(`CREATE TABLE obj_msg_callback (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(128) NOT NULL
,data VARBINARY(256) NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (obj_id, name)
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_msg_callback")
log.Println("initDB : obj_msg_callback created ...")
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)
2020-06-28 11:24:57 +02:00
,modifiers VARCHAR(400)
2019-06-06 07:52:22 +02:00
,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
2020-06-28 11:24:57 +02:00
,UNIQUE 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 ...")
2020-02-06 09:48:06 +01:00
_, err = db.Exec(`CREATE TABLE obj_msg_shop_main (
obj_id BIGINT UNSIGNED NOT NULL
,name VARCHAR(80)
2020-02-06 11:25:49 +01:00
,link VARCHAR(80)
2020-02-06 09:48:06 +01:00
,number INT UNSIGNED NOT NULL
,user VARCHAR(80)
,mana INT UNSIGNED NOT NULL
,mana_total INT UNSIGNED NOT NULL
,class VARCHAR(80)
,castle_id BIGINT UNSIGNED NOT NULL
,fees VARCHAR(80)
,guru VARCHAR(80)
,open TINYINT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,FOREIGN KEY (castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`)
failOnError(err, "initDB : create table obj_msg_shop_main")
log.Println("initDB : obj_msg_shop_main created ...")
2020-06-27 11:54:13 +02:00
_, err = db.Exec(`CREATE TABLE obj_msg_time (
obj_id BIGINT UNSIGNED NOT NULL
,date DATETIME 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_time")
log.Println("initDB : obj_msg_time created ...")
2020-02-03 07:18:17 +01:00
_, 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
2020-06-25 17:22:54 +02:00
,bot_cmd TINYINT NOT NULL
2020-02-03 07:18:17 +01:00
,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
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-05 04:05:34 +01:00
_, err = db.Exec(`CREATE VIEW obj_msg_callback_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
,omc.name COLLATE utf8mb4_unicode_ci AS name
,CAST(omc.data as CHAR) data
FROM obj_msg om
,obj o
,code_obj_sub_type cost
,obj_msg_callback omc
WHERE om.obj_id = o.id
AND cost.id = o.obj_sub_type_id
AND omc.obj_id = o.id;`)
failOnError(err, "initDBViews : create view obj_msg_callback_v")
log.Println("initDBViews : obj_msg_callback_v created ...")
2020-02-06 11:57:46 +01:00
_, err = db.Exec(`CREATE VIEW obj_shop_main_v AS
SELECT omsm.obj_id
,omsm.name COLLATE utf8mb4_unicode_ci AS name
,omsm.link COLLATE utf8mb4_unicode_ci AS link
,omsm.number
,omsm.user COLLATE utf8mb4_unicode_ci AS user
2020-02-07 04:14:02 +01:00
,oc.name COLLATE utf8mb4_unicode_ci AS castle
2020-02-06 11:57:46 +01:00
,omsm.mana
,omsm.mana_total
,omsm.class COLLATE utf8mb4_unicode_ci AS class
,omsm.fees COLLATE utf8mb4_unicode_ci AS fees
,omsm.guru COLLATE utf8mb4_unicode_ci AS guru
,omsm.open
2020-02-07 04:14:02 +01:00
,omsm.castle_id
2020-02-06 11:57:46 +01:00
FROM obj_msg_shop_main omsm
2020-02-07 04:14:02 +01:00
,obj_castle oc
WHERE oc.obj_id = omsm.castle_id;`)
2020-02-06 11:57:46 +01:00
failOnError(err, "initDBViews : create view obj_shop_main_v")
log.Println("initDBViews : obj_shop_main_v created ...")
2020-02-06 11:25:49 +01:00
2020-05-08 16:02:18 +02:00
_, err = db.Exec(`CREATE VIEW obj_job_v AS
SELECT oj.obj_id
2020-06-27 09:25:18 +02:00
,oj.user_id
2020-05-08 16:02:18 +02:00
,o.obj_sub_type_id
,cost.intl_id COLLATE utf8mb4_unicode_ci AS obj_sub_type
,oj.is_done
,oj.in_work
,oj.trigger_id
,(SELECT costx.intl_id FROM code_obj_sub_type costx, obj ox WHERE ox.id = oj.trigger_id AND costx.id = ox.obj_sub_type_id) COLLATE utf8mb4_unicode_ci AS trigger_sub_type
,oj.inserted
2020-06-27 09:29:46 +02:00
,oj.schedule
2020-05-08 16:02:18 +02:00
,oj.ended
2020-06-17 11:48:24 +02:00
,oj.zipped
,CASE oj.zipped WHEN 0 THEN CAST(oj.payload AS CHAR(20000)) ELSE null END COLLATE utf8mb4_unicode_ci AS payload
2020-05-08 16:02:18 +02:00
FROM obj o
,obj_job oj
,code_obj_sub_type cost
WHERE o.id = oj.obj_id
AND cost.id = o.obj_sub_type_id;`)
failOnError(err, "initDBViews : create view obj_job_v")
2020-06-27 09:29:46 +02:00
log.Println("initDBViews : obj_job_v created ...")
2020-05-08 16:02:18 +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
}
2020-02-17 11:05:30 +01:00
func insertObjClassif(intlId string, name string, objType string) error {
stmt, err := db.Prepare(`INSERT INTO code_obj_classif
SELECT null id, ? intl_id, ? name, cot.id obj_type_id
FROM code_obj_type cot
WHERE cot.intl_id = ?;`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(intlId, name, objType)
if err != nil {
return err
}
return nil
}
func insertObjClass(intlId string, name string, objClassif string) error {
stmt, err := db.Prepare(`INSERT INTO code_obj_class
SELECT null id, ? intl_id, ? name, coc.id obj_classif_id
FROM code_obj_classif coc
WHERE coc.intl_id = ?;`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(intlId, name, objClassif)
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)
2020-10-11 12:53:15 +02:00
VALUES (?, ?, ?, ?, ?);`)
2020-02-03 07:18:17 +01:00
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
}
2020-02-06 09:48:06 +01:00
func insertMsgShopMainAck(m *ChatWarsMessageShopMainAck) error {
objSubTypeId, err := getObjSubTypeId(m.Msg.ObjID64)
if err != nil {
return err
}
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_shop_main_ack`] {
return errors.New("Message type mismatch")
}
2020-02-06 11:25:49 +01:00
stmt, err := db.Prepare(`INSERT INTO obj_msg_shop_main (obj_id, name, link, number, user, mana, mana_total, class, castle_id, fees, guru, open)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`)
2020-02-06 09:52:18 +01:00
logOnError(err, "insertMsgShopMainAck : Prepare")
if err != nil {
return err
}
defer stmt.Close()
2020-02-06 09:48:06 +01:00
2020-02-06 09:52:18 +01:00
var open int
2020-02-06 09:53:01 +01:00
if m.Open {
2020-02-06 09:52:18 +01:00
open = 1
} else {
open = 0
}
2020-02-06 09:48:06 +01:00
2020-02-06 11:25:49 +01:00
_, err = stmt.Exec(m.Msg.ObjID64, m.Name, m.Link, m.ShopNumber, m.User, m.Mana, m.ManaTotal, m.Class, m.CastleID64, m.Fees, m.Guru, open)
2020-02-06 09:52:18 +01:00
logOnError(err, "insertMsgShopMainAck Exec")
2020-02-06 09:48:06 +01:00
2020-02-06 09:52:18 +01:00
return nil
2020-02-06 09:48:06 +01:00
}
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
2020-06-25 19:28:25 +02:00
stmt, err := db.Prepare(`INSERT INTO obj_msg_auction_announce (obj_id, lot_id, item_id, cond, quality, modifiers, 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
2020-06-25 19:24:52 +02:00
_, err = stmt.Exec(m.ObjID64, m.LotID, m.ItemID64, m.Cond, m.Quality, m.Mod, 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
2020-06-27 11:54:13 +02:00
func insertMsgTimeAck(m *ChatWarsMessageTimeAck) error {
objSubTypeId, err := getObjSubTypeId(m.ObjID64)
if err != nil {
return err
}
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_time_ack`] {
return errors.New("Message type mismatch")
}
stmt, err := db.Prepare(`INSERT INTO obj_msg_time (obj_id, date)
VALUES (?, ?);`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(m.ObjID64, m.RealTime)
if err != nil {
return err
}
return nil
}
2019-05-15 12:02:17 +02:00
func insertMsgPillageInc(m *ChatWarsMessagePillageInc) error {
objSubTypeId, err := getObjSubTypeId(m.ObjID64)
if err != nil {
return err
}
2020-02-06 11:27:53 +01:00
if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_pillage_inc`] && objSubTypeId != cacheObjSubType[`msg_pillage_inc2`] {
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 (
2020-06-25 19:04:51 +02:00
id int64
ids []int64
count int64
2019-05-11 06:56:48 +02:00
)
2019-05-11 06:54:12 +02:00
rows, err := db.Query(q)
2020-02-05 04:05:34 +01:00
logOnError(err, "getSQLListID64 : "+q)
2019-05-11 06:54:12 +02:00
2020-06-25 19:04:51 +02:00
count = 0
2019-05-11 06:54:12 +02:00
for rows.Next() {
2020-06-25 19:04:51 +02:00
count++
2019-05-11 06:54:12 +02:00
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
}
2020-10-11 12:53:15 +02:00
func cleanupJobData() {
2020-10-11 12:58:32 +02:00
var (
jobID int64
count int64
)
2020-10-11 12:57:07 +02:00
jobStmt, err := db.Prepare(`SELECT o.id FROM obj o WHERE o.obj_type_id = ` + strconv.FormatInt(cacheObjType[`job`], 10) + `;`)
2020-10-11 12:53:15 +02:00
logOnError(err, "cleanupJobData : prepare jobStmt")
if err != nil {
jobStmt.Close()
return
}
2020-10-17 11:12:01 +02:00
defer jobStmt.Close()
2020-10-11 12:53:15 +02:00
2020-10-11 12:57:07 +02:00
jobs, err := jobStmt.Query()
2020-10-11 12:53:15 +02:00
logOnError(err, "cleanupJobData : query jobStmt")
if err != nil {
return
}
2020-10-17 11:12:01 +02:00
doneStmt, err := db.Prepare(`SELECT oj.is_done FROM obj_job oj WHERE oj.obj_id = ?;`)
logOnError(err, "cleanupJobData : prepare doneStmt")
if err != nil {
doneStmt.Close()
return
}
defer doneStmt.Close()
delJobStmt, err := db.Prepare(`DELETE FROM obj_job WHERE obj_id = ?;`)
logOnError(err, "cleanupJobData : prepare delJobStmt")
if err != nil {
delJobStmt.Close()
return
}
defer delJobStmt.Close()
delObjStmt, err := db.Prepare(`DELETE FROM obj WHERE id = ?;`)
logOnError(err, "cleanupJobData : prepare delObjStmt")
if err != nil {
delObjStmt.Close()
return
}
defer delObjStmt.Close()
2020-10-11 12:53:15 +02:00
for jobs.Next() {
2020-10-11 12:57:07 +02:00
err = jobs.Scan(&jobID)
2020-10-11 12:53:15 +02:00
logOnError(err, "cleanupJobData : scan jobStmt")
if err != nil {
return
}
log.Printf("cleanupJobData : JobID : %d\n", jobID)
2020-10-17 11:12:01 +02:00
2020-10-11 12:58:32 +02:00
count += 1
2020-10-11 12:53:15 +02:00
}
2020-10-11 12:58:32 +02:00
log.Printf("cleanupJobData : %d jobs\n", count)
2020-10-11 12:53:15 +02:00
/*
for _, id := range ids {
job, err := db.Query(`SELECT oj.is_done FROM obj_job oj WHERE oj.obj_id = ?;`)
if err != nil {
return err
}
defer jobs.Close()
stmt, err := db.Prepare(`DELETE FROM obj WHERE id = ?`)
if err != nil {
return err
}
defer stmt.Close()
}
*/
}