-- 创建数据库

drop database if exists anhei_game;
create database anhei_game DEFAULT CHARACTER SET utf8mb4;
use anhei_game;


-- 兑换券兑换实物
drop table if exists tbexchange;
create table tbexchange (
	transactionId 		varchar(100) not null,	-- 流水号,服务器生成
	uid				bigint not null,				-- 玩家角色uid
	nick		varchar(64) not null default '', -- 昵称
	itemId 		int not null,					-- 道具Id，就是兑换卷id
	itemUniqueId bigint not null,				-- 道具唯一id，如果有的话
	phoneCardMoney int not null, -- 如果是电话卡，电话卡的金额
	trueName 	varchar(64) not null,	-- 真实名字
	phoneNum varchar(64) not null,	-- 电话号码
	telcom	varchar(64) not null,	-- 电信运营商
	email varchar(64) not null,	-- email
	opTime 	bigint not null, 		-- 兑换时间
	status 			int not null,  -- 订单状态 0=建立订单  1=成功

	primary key (transactionId),
	index (uid),
    index (itemId)
) engine=InnoDB default charset=utf8mb4;



-- ----------------------------
-- Table structure for tb_worldglobaldata
-- ----------------------------
-- world 全局数据
DROP TABLE IF EXISTS `tb_worldglobaldata`;
CREATE TABLE `tb_worldglobaldata` (
  `dataType` int(11)  NOT NULL COMMENT '数据类型',
  `globalId` int(11) NOT NULL COMMENT '全局id,按world分就是worldid,按大区分就是大区id',
  `dataVer` int(11) NOT NULL,
  `data` mediumblob NOT NULL  COMMENT 'zipdata',
  PRIMARY KEY (`dataType`, `globalId`,`dataVer`)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;


-- ----------------------------
-- 关卡战斗记录
-- ----------------------------
DROP TABLE IF EXISTS `tb_mainland_recording`;
CREATE TABLE `tb_mainland_recording` (
  `logicWorldId` int(11) NOT NULL,
  `recordId` bigint(20) NOT NULL,
  `mainlandId` int(11) NOT NULL,
  `data` mediumblob NOT NULL,
  PRIMARY KEY (`logicWorldId`,`recordId`)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

-- ----------------------------
-- 全局机器人领地表
-- ----------------------------
drop table if exists tbworld_robot_home;
create table tbworld_robot_home (
	`worldId` 			int(11) NOT NULL,
	`uid`				bigint not null,
	`resource` 			blob,
	`working`			mediumblob,
	`playerbase`		blob,
	`resourceReward`	mediumblob,
	`updatetime`		bigint default 0,
	primary key (`worldId`,`uid`)
) ENGINE=InnoDB default charset=utf8mb4;

-- 结束符
delimiter $$

-- 用户表，字段含义参见协议定义
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 100 DO
		SET @sqlstr = CONCAT(
		"create table tbuser_",
		`@i`,
		"(
			`uid`				bigint not null,
			`realm`				int not null,
			`nick`				varchar(64) not null default '',
			`gender`			int not null default 0,
			`icon`				varchar(200) not null default '',
			`exp`				int not null default 0,
			`level`				int not null default 0,
			`vipLevel`			int not null default 0,
			`vipExp` 			int not null default 0,
			`chip`				bigint not null default 0,
			`diamond`			bigint not null default 0,
			`lastLoginTime`		bigint not null default 0,
			`createTime`		bigint not null default 0,
			`onlineTime`		bigint not null default 0,
			`firebasePushId`	varchar(256) not null default '',
			`freezeTime`  		bigint not null default 0,
			`freezeReason`  	int not null default 0,
			`enableMsgTime`  	int not null default 0,
			`freezeReasonStr` 	varchar(256) not null default '',
			`lang`				varchar(16) not null default '',
			`heroExpPool` 		int(11) NOT NULL DEFAULT 0,
			`roleCreateComplete` int(11) NOT NULL DEFAULT 0,
			`power` bigint NOT NULL DEFAULT 0,
			`gameData` 			mediumblob,
			`unimportanceData` 	mediumblob,
	        `dropData` 	        mediumblob,
			
			primary key (`uid`),
			index `idx_realm_level`(`realm`, `level`),
			index (`nick`)
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$



-- 角色邮件表,op也放到这里，用邮件逻辑处理
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 100 DO
		SET @sqlstr = CONCAT(
		"create table tbmail_",
		`@i`,
		"(
			`uid`				bigint not null,
			`data` 			mediumblob,

			primary key (`uid`)
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$

-- 排行榜
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 10 DO
		SET @sqlstr = CONCAT(
		"create table tbrank_",
		`@i`,
		"(
			`rankId` int(11) NOT NULL, 					-- id
			`realmId` int(11) NOT NULL,					-- realmId
			`groupId` int(11) NOT NULL,					-- 组id
			`beginTime` int(11) NOT NULL,				-- 组id
			`rankData` mediumblob,						-- bolb数据
			PRIMARY KEY (`rankId`,`realmId`,`groupId`,`beginTime`)
			) ENGINE=InnoDB DEFAULT charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$

-- 好友表
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 100 DO
		SET @sqlstr = CONCAT(
		"create table tbfriend_",
		`@i`,
		"(
			`uid`			bigint not null,
			`self` 			blob,
			`list` 			mediumblob,
			`op`			mediumblob,

			primary key (`uid`)
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$

-- 关卡战斗记录(已经废弃后面再删除)
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 10 DO
		SET @sqlstr = CONCAT(
		"create table tbmainland_recording_",
		`@i`,
		"(
			`mainlandId` int(11) NOT NULL,
			`saveIndex` int(11) NOT NUll,
			`recording` mediumblob,
			PRIMARY KEY (`mainlandId`),
			index (`saveIndex`)
		) ENGINE=InnoDB DEFAULT charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$

-- 战斗回放记录表
CREATE procedure create_table()
BEGIN
	DECLARE `@i` int(11);
	DECLARE `@sqlstr` varchar(2560);
	SET `@i`= 1;
	WHILE `@i` <= 100 DO
		SET @sqlstr = CONCAT(
		"create table tbbattle_replay_",
		`@i`,
		"(
			`battleId`		bigint not null,
			`mainlandId`	int not null,
			`battleVersion`	bigint not null,
			`data` 			mediumblob,
			`createDay` int default 0,
			
			primary key (`battleId`),
			index (`battleVersion`),
			index (`createDay`)
		) engine=InnoDB default charset=utf8mb4;"
		);
		prepare stmt from @sqlstr;
		execute stmt;

		SET `@i` = `@i` + 1;
	END WHILE;
END;
call create_table();
drop procedure create_table;
$$

drop table if exists tbrace_user_signup;
CREATE TABLE `tbrace_user_signup` (
	`uid` bigint not null,
	`season` bigint not null,
	`seasonNo`	int not null,
	`signUpInfo`	mediumblob,
	primary key (`uid`),
	index (season),
	index (seasonNo)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

drop table if exists tbpeakarena;
CREATE TABLE `tbpeakarena` (
  `worldId` int NOT NULL,
  `data` blob,
  PRIMARY KEY (`worldId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;

drop table if exists tbactRank;
CREATE TABLE `tbactRank` (
    `actRemRankId` bigint not null,
	`endClear`	int not null,
	`beginTime`	bigint not null,
	`endTime`	bigint not null,
	`rankData`	mediumblob,
	 primary key (`actRemRankId`)
) ENGINE=InnoDB DEFAULT charset=utf8mb4;

drop table if exists tbpeakarena_world;
CREATE TABLE `tbpeakarena_world` (
  `bigWorldId` int NOT NULL,
  `logicWorlds` varchar(4096) CHARACTER SET utf8mb4 DEFAULT NULL,
  `service1` int DEFAULT NULL,
  `service2` int DEFAULT NULL,
  PRIMARY KEY (`bigWorldId`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;