create function unhex(text) returns text language sql immutable strict as $$
  select encode(decode($1, 'hex'), 'escape')
$$;

CREATE TABLE t_sdk_account (
    uid int primary key generated always as identity,
    token varchar(64) NOT NULL,
    username varchar(40) NOT NULL,
    password varchar(256) NOT NULL,
    UNIQUE(username)
);

CREATE TABLE t_combo_token (
    account_uid varchar(32) primary key,
    token varchar(64) NOT NULL,
    device_id varchar(128) NOT NULL,
    UNIQUE(account_uid, device_id)
);

CREATE TABLE t_client_config (
	id bigint primary key,
	version varchar(64) NOT NULL DEFAULT '',
	name varchar(64) NOT NULL DEFAULT '',
	stop_server_config_str varchar(512) NOT NULL DEFAULT '',
	client_custom_config_str varchar(512) NOT NULL DEFAULT '',
	dispatch_seed varchar(512) NOT NULL DEFAULT '',
	UNIQUE(version)
);

INSERT INTO t_client_config VALUES
(1, 'CNCBWin5.0.50',  '', '', '{\"codeSwitch\":[4334],\"sdkenv\":2,\"checkdevice\":false,\"loadPatch\":false,\"showexception\":false,\"regionConfig\":\"pm|fk|add\",\"downloadMode\":0}', '');

CREATE TABLE t_region_config (
	id bigint primary key,
	name varchar(64) NOT NULL DEFAULT '',
	title varchar(128) NOT NULL DEFAULT '',
	region_type varchar(64) NOT NULL DEFAULT '',
	area_type varchar(64) NOT NULL DEFAULT '',
	dispatch_url varchar(64) NOT NULL DEFAULT '',
	sdk_env int NOT NULL,
	client_secret_key bytea NOT NULL DEFAULT '',
	rsa_key_list varchar(128) NOT NULL DEFAULT '',
	UNIQUE(name)
);

INSERT INTO t_region_config VALUES
(1, 'dev_xilonen', 'XilonenImpact', 'DEV_PUBLIC', 'global', 'http://127.0.0.1:21041/query_cur_region', 2, decode('456332621000000083FD4642C3298A7CBB33E0A04332058300080000BBFA992CD5433217198CB083995F29C7DD0CF0E30414DD89E70A3716B9EACB3935ADD70A8C6777E6BA31CEB50BDDDA259E8E104A8BBB0EE900DA167628EA277BE0365354C5F211051301202DA77ADBF26B792B3601E2E191E26B2EE31860EF8C7230373628A0CD0BA3AAA5FA6BA5BC78C46A7AD671287D64BDE6098CE62108D74D3C12C27F2F03495D60A6F7A55179FF0ED710EC708C225F0BB15F17C0D16FA990BEB3D08C35DE2E457685ACBE2FE1E32A73872DE59345E31623B3192AD0EC365D8A6F111EBC7374DA167FD7374952952738677F00D2E7D622A6718AAF647F461C27636F21E46D932C27CC148B1206E3A306A882A3B66288F96C5B3DBDB78A0699CEA20153CD494EB617C5FB04521E389338B6075EAE179368502EFF0FAF7FC57A5832266151D68129295E2C4B6476A8744BD8F066BB825DE45301DA8189CA956F89CA166D14DD3A6C2F75B91990B3201CDADA86BDE390632DA8633BCC301E28F60F60C71C0397CD4093DDDA51F0AD6C666B5EE7E7CD789260CED2941CD701296003E2926281472EAA56EA9256387A365A9171DBDA10BBB428DE396371E24F620A2656C0026B96E49C872DB31A9FDA82FA5082A3B9B1B94BEA2DBD6A4ECC94821A8C63D6181FFD733261A8A73902BF68EB8A5E9F87DEDD7C65EA3E521E04008B2C40FE138C9D1E1C61E376E9051DC4651ED94B2B91C330DDA6F816CAABE12E0A87F9748DBDCDA66E6DA7307CB8894EBF77B346D5F9543D45F5A21CD5966E6426B2DCB20F2D796079C86F881C5EB5A612068C1C04AEB87775D2A33F4862AA94CD26DBB66846972F3C2B41552D46ED18514FE0F0FA83473A024D0B2561417D4A6FDBB68F6F09FA130057A3048ADDCB745FB108AF6A9091F5A6872E668FF83F5C68445EB99B28F1AF515F8BD9365683AB819B275151F969BC15BA33C16B0714F15B19EE08540C971979A122BF2D598BDC5A8CAADDEEBFE9DA4EB15B16940213E38D79E1AB952E76A627647DD5ECED8C638D717BD05858DD377FFD62558DE398E2975286E73873AB32E94E65231B3C42DEF1D661D466E162DE5CA2FA82FB13379AA2587D723005C7FAF9DE64CE1D9ADC705BB62603591B1C3ACA8DE407827D1AC61B0414BBC660B13E8AC948623B061E154AA8FBC7EDAAC00FDB99289F6F000B50C70F4E4969AE40FA0EBDF202427EBB55119A3131DB0758DF3BE956CE7838DB638CA4E1224546EFA34466017652E96E20DD76EEE8C96793EE96426785E9D219096CB91F2521DDF125007A2DB3DD2B784BBA1653B0F590A29F12C467A7595D203971F80C1BF92E517EA648383D34468FD40CCC5A207FB33A03869E884F878C997BDEC8DA2805C823D2F56505B70A7813F48AC177D780D70CD810938F9B41346F8379DF9073B23342CE38940730D5EAB1E7C43464B387EFAB49BE5AD6FFA4C44014D4128DCB74E4A2A6F1C13C5F3C4F79767203C8B8AED90AF270BA64A4BF83A5B172C3E01C7BC00126F0B19DA32D159602D77019B51959ABB7A588822896BD9AC54D7E4EE00601792FDF2D4AE0F18EDF99D45BC73400CB541D70690776EB676ED14F88228FB908059D45EC75F203AEC210B6E8A529874387CED25F384C2A9489FAAF0DB23813799EBC2DFA8EDEE42C70AD36D7919FBD5299927264EF36973C3FCCBBFF26B28C51F872E6C28BC4BA5B6DC0BD907C3E3A4F07F4201612E696144A19A9FF329A15390DD975D5C6723EF4EE0F7627BD5DDEE7AF819DB1D4A65D3233D6DF7D55F2A7952FF2DC4761CAD105047B41DB7845BBCFA7A291190F528409028A78F813E96C80E943CD4A6036513171D0ACEADE80C1A68E68112F8841F94D1CB01D0E6678EBD844E73D3066B9D53AD66F0B2381CB25A8F8676C7443C8A729C668861E62B6BB130B574853F2B4B2B7D73521C1D0FF16C1DAF6A4C42B143F4A3C3AF5FD819D49A70ED918C159C572A9A1C56E23F7945FD76FEAF95EA882A438C5197934BAF26DD5E2C702C14AA3556181B61240CF6001F37D62A40FC97721BC777A73E79FAAB902B29337E6A97DD60DFD402DAEC9C0D109A5985DA86DE967F35BDF2A6D35487D7557EA70C08403009D4E3C70C6266D6D909C7DD63A64D3FABC8FA067CB768746A90FC1078EE26F94BEFC1FA8639CF9CA02A171D4FAA8FFA3E1FB41727045DC6AC36DD28C1816B376C18D9FC2D766A2997F2A4802B7557D5228A89BA830F251C01B16E072F60F8E48CE613F495B130C322A692F22C1A653C6CA8650A96384F53D86427F47A84C56BC88F69812BFB23B332DD97E7B9F200FCAFFA4FC8E56B047239B5510679C8A51ACB95A2165CECFD18883868710A68D1CA7B07BC221534D356986F383C094582C8FC4326FA84DB89989A15F4320232D6F2936F8F06DC9D512993888D818EC4EFD30D77D27721572BD53082BFCD20DDCAEC50B8AA66457F0A00F283835F8D732DA4E3FEF7AB5204E8241A6FBE176E655456AEEAC5ACEDF24BC98FE84BBB13D224B407A44137A2F5D0832A46202D390A840BC89B16D282E8B0B4485F2E02CF62F82D71FAE36BE921C1D8DFEFB60BA3D774985D23D8F6476A8B4E92AD15E1E9054BA139D7B3C0B8C2DCE59F13E4A134F0BAFAE8EDBDC4613C22F95C5AC54811D54DF44717B29F64204229BA73BB9E8703ADF8363688B24F45BEDD266DE6EE71597B79A1A644E57C5CA4BBB4A04AA78817566E790875AF6C171A164ED955581329006AAEC6EE08B3172F193C8BDDC14743F538320AAAC74EF8AFE92B1FB117A581758F01EC0A9F050885820B50C88DEB4A8037ADA2D64B48E09F0C785BDAB98DF6D5193C08110508D2C6353A6617B31084BEC5D026459CF747B245A27FD77EB7E8C5FB89876A9EF58C452D3AB90A411B2E8239AFCEB56B3', 'hex'), '4,5');

CREATE TABLE t_bind_config (
	id bigint primary key,
	client_version varchar(64) NOT NULL,
	region_name varchar(64) NOT NULL,
	channel_id int NOT NULL
);

INSERT INTO t_bind_config VALUES
(1, 'CNCBWin5.0.50', 'dev_xilonen', 1);

CREATE TABLE t_channel_id_config (
	id bigint primary key,
	channel_id int NOT NULL,
	enable_login_pc bool
);

INSERT INTO t_channel_id_config VALUES
(1, 1, TRUE);

CREATE TABLE t_client_region_config (
	id bigint primary key,
	client_version varchar(64) NOT NULL,
	region_name varchar(64) NOT NULL,
	client_region_custom_config varchar(512) NOT NULL
);

INSERT INTO t_client_region_config VALUES
(1, 'CNCBWin5.0.50', 'dev_xilonen', '');

CREATE TABLE t_rsakey_config (
	id bigint primary key,
	client_public_key varchar(4096) NOT NULL,
	server_private_key varchar(4096) NOT NULL
);

INSERT INTO t_rsakey_config VALUES
(4, E'-----BEGIN PUBLIC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F9YhEizJhOm\ndzTqW4PgW2yUaHYd3q5QjiILy7AC+oOYoTZln3RfjPOxl+bYjeMOWlqkgtpPQkAE\n4I64w8RZAoGBAPLR44pEkmTdfIIF8ZtzBiVfDZ29bT96J0CWXGVzp8x6bSu5J5jl\ns7sP8DEcjGZ6vHsLGOvkcNxzcnR3l/5HOz6TIuvVuUm36b1jHltq1xZStjGeKZs1\nihhJSu2lIA+TrK8FCRnKARJ0ughXGNZFItgeM230Sgjp2RL4ISXJ724XAoGBANBy\nS2RwNpUYvkCSZHSFnQM/jq1jldxw+0p4jAGpWLilEaA/8xWUnZrnCrPFF/t9llpb\ndTR/dCI8ntIMAy2dH4IUHyYKUahyHSzCAUNKpS0s433kn5hy9tGvn7jyuOJ4dk9F\no1PIZM7qfzmkdCBbX3NF2TGpzOvbYGJHHC3ssVr3AoGBANHJDopN9iDYzpJTaktA\nVEYDWnM2zmUyNylw/sDT7FwYRaup2xEZG2/5NC5qGM8NKTww+UYMZom/4FnJXXLd\nvcyxOFGCpAORtoreUMLwioWJzkkN+apT1kxnPioVKJ7smhvYAOXcBZMZcAR2o0m0\nD4eiiBJuJWyQBPCDmbfZQFffAoGBAKpcr4ewOrwS0/O8cgPV7CTqfjbyDFp1sLwF\n2A/Hk66dotFBUvBRXZpruJCCxn4R/59r3lgAzy7oMrnjfXl7UHQk8+xIRMMSOQwK\np7OSv3szk96hy1pyo41vJ3CmWDsoTzGs7bcdMl72wvKemRaU92ckMEZpzAT8cEMC\ncWKLb8yzAoGAMibG8IyHSo7CJz82+7UHm98jNOlg6s73CEjp0W/+FL45Ka7MF/lp\nxtR3eSmxltvwvjQoti3V4Qboqtc2IPCt+EtapTM7Wo41wlLCWCNx4u25pZPH/c8g\n1yQ+OvH+xOYG+SeO98Phw/8d3IRfR83aqisQHv5upo2Rozzo0Kh3OsE=\n-----END RSA PRIVATE KEY-----');

CREATE TABLE t_user_uid (
	uid int primary key NOT NULL,
	account_uid varchar(128) NOT NULL,
	ext varchar(512) NOT NULL DEFAULT '',
	tag int NOT NULL DEFAULT 0,
	UNIQUE(account_uid)
);

CREATE TABLE t_player_data (
	uid int primary key,
	nickname varchar(128) NOT NULL DEFAULT '0',
	level int NOT NULL DEFAULT 0,
	exp int NOT NULL DEFAULT 0,
	vip_point int NOT NULL DEFAULT 0,
	json_data varchar(512) NOT NULL DEFAULT '',
	bin_data bytea NOT NULL,
	extra_bin_data bytea DEFAULT NULL,
	tag_list varchar(128) NOT NULL DEFAULT '',
	is_delete bool,
	reserved_1 int,
	reserved_2 int,
	before_login_bin_data bytea DEFAULT NULL
);

