package main import ( "errors" "fmt" "log" "regexp" "strconv" "time" ) 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 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 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 ,timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,PRIMARY KEY (id) ,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 ) 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 ,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_war ( obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,start_time DATETIME NOT NULL ,end_time DATETIME 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_war") _, 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 DATETIME 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_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 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_msg_report") _, 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 ,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 ,FOREIGN KEY (war_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (user_id, war_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_msg_report") _, err = db.Exec(`CREATE TABLE obj_msg_auction_announce ( obj_id BIGINT UNSIGNED NOT NULL ,lot_id BIGINT UNSIGNED NOT NULL ,item VARCHAR(80) ,cond VARCHAR(32) ,quality VARCHAR(32) ,seller VARCHAR(32) ,buyer VARCHAR(32) ,price SMALLINT NOT NULL ,status VARCHAR(32) ,end DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (lot_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_msg_auction_announce") _, 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 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_msg_auction_upd_req") _, 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 ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_msg_auction_upd_req") _, 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 ,UNIQUE KEY (id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table msg_rules") _, err = db.Exec(`CREATE TABLE obj_item ( obj_id BIGINT UNSIGNED NOT NULL ,intl_id VARCHAR(32) ,name VARCHAR(80) ,weight SMALLINT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (intl_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_auction_announce") _, err = db.Exec(`CREATE TABLE obj_job ( obj_id BIGINT UNSIGNED NOT NULL ,priority SMALLINT NOT NULL ,status SMALLINT NOT NULL ,schedule DATETIME NOT NULL ,start TIMESTAMP ,end TIMESTAMP ,payload VARCHAR(4000) ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_job") _, 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") ,(` + strconv.Itoa(objTypeWar) + `, "war", "War") ,(` + strconv.Itoa(objTypeWarReport) + `, "war_report", "War Report") ,(` + strconv.Itoa(objTypeJob) + `, "job", "Job") ,(` + strconv.Itoa(objTypeItem) + `, "item", "Item") ;`) 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(objSubTypeMessageUnknown) + `, "unknown", "Unknown", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageWar) + `, "war", "War report", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageMiniWar) + `, "mini_war", "Mini war reprot", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageGuildWar) + `, "guild_war", "Guilds war report", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageReport) + `, "report", "Player war report", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageGReport) + `, "g_report", "Player guilds 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) + `) ,(` + strconv.Itoa(objSubTypeMessagePillageInc) + `, "pillage_inc", "Pillage incoming", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageTributeInc) + `, "tribute_inc", "Request incoming", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessagePillageAck) + `, "pillage_ack", "Pillage acknowledged", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageTributeAck) + `, "tribute_ack", "Request acknowledged", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageAuctionAnnounce) + `, "auction_announce", "Auction announce", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeMessageTime) + `, "time", "Time", ` + strconv.Itoa(objTypeMessage) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobPillage) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobTribute) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobWithdrawal) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobGStock) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobRescanMsg) + `) ,(` + strconv.Itoa(objSubTypeJob) + `, "job_pillage", "Pillage job", ` + strconv.Itoa(objSubTypeJobRescanAllMsg) + `) ,(` + strconv.Itoa(objSubTypeItemResource) + `, "item_res", "Time", ` + strconv.Itoa(objTypeItem) + `) ,(` + strconv.Itoa(objSubTypeItemAlch) + `, "item_alch", "Time", ` + strconv.Itoa(objTypeItem) + `) ,(` + strconv.Itoa(objSubTypeItemMisc) + `, "item_misc", "Time", ` + strconv.Itoa(objTypeItem) + `) ,(` + strconv.Itoa(objSubTypeItemRecipe) + `, "item_recipe", "Time", ` + strconv.Itoa(objTypeItem) + `) ,(` + strconv.Itoa(objSubTypeItemPart) + `, "item_part", "Time", ` + strconv.Itoa(objTypeItem) + `) ,(` + strconv.Itoa(objSubTypeItemOther) + `, "item_other", "Time", ` + strconv.Itoa(objTypeItem) + `) ;`) failOnError(err, "initDB : populate table code_obj_sub_type") _, err = db.Exec(`INSERT INTO msg_rules (prio, msg_type_id, descn, rule) VALUES (5000, ` + strconv.Itoa(objSubTypeMessageReport) + `, "Player war report", "^(?P[🐉🦅🐺🦈🦌🥔🌑])(?P(\\[[A-Z]{3}\\]){0,1})(?P([A-Za-z0-9 ]*)) ⚔:(?P[0-9]+)(?P\\((-|\\+)[0-9]+\\)){0,1} 🛡:(?P[0-9]+) Lvl: (?P[0-9]+)\\nYour result on the battlefield:\\n🔥Exp: (?P[0-9]+)\\n💰Gold: (?P-{0,1}[0-9]+)\\n📦Stock: (?P-{0,1}[0-9]+)(\\n)*(?P(🔋Stamina restored)){0,1}(\\n)*(?P(⚡Critical strike)){0,1}(\\n)*(?s:.*)$") ,(5000, ` + strconv.Itoa(objSubTypeMessageAuctionAnnounce) + `, "Auction annouce", "^Lot #(?P[0-9]+) : (?P.*)\\nSeller: (?P.*)\\nCurrent price: (?P[0-9]+) pouch\\(es\\)\\nBuyer: (?P.*)\\nEnd At: (?P.*)\\nStatus: (?P.*)(\\n)*(?s:.*)") ,(5000, ` + strconv.Itoa(objSubTypeMessageTime) + `, "Time", "^In Chat Wars world now\\n(?P