Blame view

src/services/dbseed.lua 4.7 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
  require "shared.init"
  require "utils.init"
  require "GlobalVar"
  require "RedisKeys"
  require "ProtocolCode"
  require "skynet.manager"
913e070e   测试   添加订单表,全局id定时回写数据库
7
  require "utils.MysqlUtil"
314bc5df   zhengshouren   提交服务器初始代码
8
9
10
11
  
  skynet = require "skynet"
  
  redisproxy = require("shared.redisproxy")
0de80321   测试   创建游戏数据库,role对应mys...
12
  mysqlproxy = require "shared.mysqlproxy"
314bc5df   zhengshouren   提交服务器初始代码
13
14
15
  
  SendPacket = function ( ... ) end
  
913e070e   测试   添加订单表,全局id定时回写数据库
16
  
314bc5df   zhengshouren   提交服务器初始代码
17
  local function initRedisDb( ... )
913e070e   测试   添加订单表,全局id定时回写数据库
18
19
20
21
22
23
24
25
26
27
28
  	local function initAutoIncrementUid(tbName, keyName, fieldName)
  		if not fieldName then fieldName = "value" end
  		local mysqlVal = getDbCfgVal(tbName, keyName, fieldName)
  		if not mysqlVal then 
  			skynet.error(string.format("get db cfg fail, table %s, key %s, field %s", tbName, keyName, fieldName))
  			return
  		end
  		local redisVal = tonum(redisproxy:hget(tbName, keyName))
  		if redisVal < mysqlVal then
  			redisproxy:hset(tbName, keyName, mysqlVal)
  		end
314bc5df   zhengshouren   提交服务器初始代码
29
  	end
913e070e   测试   添加订单表,全局id定时回写数据库
30
31
32
33
34
35
36
37
38
39
40
  
  	initAutoIncrementUid("autoincrement_set", "role")
  	initAutoIncrementUid("autoincrement_set", "union")
  	initAutoIncrementUid("autoincrement_set", "order")
  	initAutoIncrementUid("autoincrement_set", "email")
  	initAutoIncrementUid("autoincrement_set", "emailTimestamp")
  	initAutoIncrementUid("autoincrement_set", "delay_email")
  
  	--redisproxy:hsetnx("adv_season", "idx", 0)
  	--redisproxy:hsetnx("adv_season", "chapter", globalCsv.adv_endless_default_chapter)
  	--redisproxy:hsetnx("adv_season", "overTime", 0)
314bc5df   zhengshouren   提交服务器初始代码
41
42
  end
  
0de80321   测试   创建游戏数据库,role对应mys...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  -- 初始化服务器数据库以及服务器信息表
  local function initServerDatabase()
  	local servId = skynet.getenv("servId")
  	mysqlproxy:query(string.format("CREATE DATABASE IF NOT EXISTS server_%s DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_general_ci;", servId))
  	mysqlproxy:query(string.format("use server_%s", servId))
  
  	-- 服务器信息表 开服时间
  	mysqlproxy:query [[
  		CREATE TABLE IF NOT EXISTS `server_info` (
  			`key` varchar(45) NOT NULL,
  			`int_value` int(11) DEFAULT NULL,
  			`str_value` varchar(128) DEFAULT NULL,
  			PRIMARY KEY (`key`)
  			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
  	]]
  
  	local res = mysqlproxy:query("SELECT * FROM `server_info` where `key` = 'server_start';")
  	if not next(res) then
  		mysqlproxy:query(string.format("INSERT INTO `server_info`(`key`, `str_value`) VALUES('server_start', '%s');",
  			os.date("%Y%m%d", skynet.timex())))
  	end
  end
  
  local function initAutoIncreUidTable()
  	mysqlproxy:query [[
913e070e   测试   添加订单表,全局id定时回写数据库
68
  		CREATE TABLE IF NOT EXISTS `autoincrement_set` (
0de80321   测试   创建游戏数据库,role对应mys...
69
70
71
72
73
74
75
  			`key` varchar(45) NOT NULL,
  			`value` int(11) DEFAULT NULL,
  			PRIMARY KEY (`key`)
  			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
  	]]
  	local servId = tonumber(skynet.getenv("servId"))
  	if servId then
913e070e   测试   添加订单表,全局id定时回写数据库
76
  		local tpl = "INSERT INTO `autoincrement_set`(`key`, `value`) values('%s', %d)"
0de80321   测试   创建游戏数据库,role对应mys...
77
78
  		mysqlproxy:query(string.format(tpl, "role", servId * MAX_ROLE_NUM))
  		mysqlproxy:query(string.format(tpl, "union", servId * MAX_ROLE_NUM))
913e070e   测试   添加订单表,全局id定时回写数据库
79
  		mysqlproxy:query(string.format(tpl, "order", 0))
0de80321   测试   创建游戏数据库,role对应mys...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  		mysqlproxy:query(string.format(tpl, "email", 0))
  		mysqlproxy:query(string.format(tpl, "emailTimestamp", 0))
  		mysqlproxy:query(string.format(tpl, "delay_email", 0))
  	end
  end
  
  local function initAdvSeasonTable()
  	mysqlproxy:query [[
  		CREATE TABLE IF NOT EXISTS `adv_season` (
  			`key` varchar(45) NOT NULL,
  			`value` int(11) DEFAULT NULL,
  			PRIMARY KEY (`key`)
  			) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
  	]]
  	local servId = tonumber(skynet.getenv("servId"))
  	if servId then
  		local tpl = "INSERT INTO `adv_season`(`key`, `value`) values('%s', %d)"
  
  		mysqlproxy:query(string.format(tpl, "idx", 0))
  		mysqlproxy:query(string.format(tpl, "chapter", globalCsv.adv_endless_default_chapter))
  		mysqlproxy:query(string.format(tpl, "overTime", 0))
  	end
  end
  
  local function checkRoleTables()
2ca93972   测试   添加邮件表
105
  	local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order", "Email"}
fa992c94   测试   添加daily,diner,act...
106
107
108
109
110
111
  	for _, name in ipairs(list) do
  		local obj = require("models."..name).new({key = "key"})
  		print("check table [" .. name .. "] begin.")
  		obj:checkTableSchema()
  		print("check table [" .. name .. "] end.")
  	end
0de80321   测试   创建游戏数据库,role对应mys...
112
113
  end
  
314bc5df   zhengshouren   提交服务器初始代码
114
115
  local steps = {
  	[1] = {
0de80321   测试   创建游戏数据库,role对应mys...
116
117
118
  		handler = initServerDatabase,
  		desc = "initialize server database "
  	},
913e070e   测试   添加订单表,全局id定时回写数据库
119
  	[2] = {
0de80321   测试   创建游戏数据库,role对应mys...
120
121
122
  		handler = initAutoIncreUidTable,
  		desc = "initialize auto_increment_uid table "
  	},
913e070e   测试   添加订单表,全局id定时回写数据库
123
  	[3] = {
0de80321   测试   创建游戏数据库,role对应mys...
124
125
126
  		handler = initAdvSeasonTable,
  		desc = "initialize adv_season table "
  	},
913e070e   测试   添加订单表,全局id定时回写数据库
127
  	[4] = {
0de80321   测试   创建游戏数据库,role对应mys...
128
129
  		handler = checkRoleTables,
  		desc = "check role tables "
314bc5df   zhengshouren   提交服务器初始代码
130
131
132
133
  	}
  }
  
  skynet.start(function ()
0de80321   测试   创建游戏数据库,role对应mys...
134
135
136
137
138
139
  	--local new = redisproxy:hsetnx("autoincrement_set", "server_start", os.date("%Y%m%d", skynet.timex())) == 1
  	--if not new then
  	--	print("server has been initialized...")
  	--	skynet.exit()
  	--	return
  	--end
a5486ede   zhouhaihai   csvdata 修改为 share...
140
141
  	csvdb = require "shared.csvdata"
  	globalCsv = csvdb["GlobalDefineCsv"]
314bc5df   zhengshouren   提交服务器初始代码
142
143
144
145
146
147
  
  	for _, action in ipairs(steps) do
  		print(action.desc .. "start ...")
  		action.handler()
  		print(action.desc .. "finished ...")
  	end
913e070e   测试   添加订单表,全局id定时回写数据库
148
  	initRedisDb()
314bc5df   zhengshouren   提交服务器初始代码
149
150
  	skynet.exit()
  end)