Skip to content

Commit

Permalink
更新框架代码:优化心跳事件;显示默认打印
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuancheng Zhang committed May 18, 2021
1 parent e385f33 commit ea47bb9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end

--- 加载的时候运行的代码
function ClientBase:InitDefault(_module)
-- --print(string.format('[ClientBase][%s] InitDefault()', self.name))
-- print(string.format('[ClientBase][%s] InitDefault()', self.name))
-- 初始化默认监听事件
EventUtil.LinkConnects(localPlayer.C_Event, _module, self)
end
Expand All @@ -20,7 +20,7 @@ end
-- FrameworkConfig.DebugMode 框架中的全局debug开关
function ClientBase:Log(...)
if self.debug and FrameworkConfig.DebugMode then
--print(string.format('[%s]', self.name), ...)
print(string.format('[%s]', self.name), ...)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ local rawDataPlayer = {}

--- 打印数据同步日志
local PrintLog = FrameworkConfig.DebugMode and FrameworkConfig.Debug.ShowDataSyncLog and function(...)
--print('[DataSync][Client]', ...)
print('[DataSync][Client]', ...)
end or function()
end

--! 初始化

--- 数据初始化
function ClientDataSync.Init()
--print('[DataSync][Client] Init()')
print('[DataSync][Client] Init()')
InitEventsAndListeners()
InitDataDefines()
end
Expand Down Expand Up @@ -63,7 +63,7 @@ end

--- 开始同步
function ClientDataSync.Start()
--print('[DataSync][Client] 客户端数据同步开启')
print('[DataSync][Client] 客户端数据同步开启')
MetaData.ClientSync = true
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ local sTmpTs, cTmpTs -- 时间戳缓存

--- 打印心跳日志
local PrintHb = FrameworkConfig.DebugMode and FrameworkConfig.Debug.ShowHeartbeatLog and function(...)
--print('[Heartbeat][Client]', ...)
print('[Heartbeat][Client]', ...)
end or function()
end

--! 外部接口

--- 初始化心跳包
function ClientHeartbeat.Init()
--print('[Heartbeat][Client] Init()')
print('[Heartbeat][Client] Init()')
CheckSetting()
InitEventsAndListeners()
end

--- 开始发出心跳
function ClientHeartbeat.Start()
--print('[Heartbeat][Client] Start()')
print('[Heartbeat][Client] Start()')
local cTimestamp
running = true
while (running) do
Expand All @@ -64,7 +64,7 @@ end

-- 停止心跳
function ClientHeartbeat.Stop()
--print('[Heartbeat][Client] Stop()')
print('[Heartbeat][Client] Stop()')
running = false
end

Expand Down Expand Up @@ -127,13 +127,13 @@ end
function CheckPlayerJoin(_player, _sTimestamp)
if not cache.sTimestamp then
--* 玩家新加入 OnPlayerJoinEvent
--print('[Heartbeat][Client] OnPlayerJoinEvent, 新玩家加入,', localPlayer, localPlayer.UserId)
NetUtil.Fire_C('OnPlayerJoinEvent', localPlayer)
print('[Heartbeat][Client] OnPlayerJoinEvent, 新玩家加入,', localPlayer, localPlayer.UserId)
NetUtil.Fire_C('OnPlayerJoinEvent', localPlayer, localPlayer.UserId)
cache.state = HeartbeatEnum.CONNECT
elseif cache.state == HeartbeatEnum.DISCONNECT then
--* 玩家断线重连 OnPlayerReconnectEvent
--print('[Heartbeat][Client] OnPlayerReconnectEvent, 玩家断线重连,', localPlayer, localPlayer.UserId)
NetUtil.Fire_C('OnPlayerReconnectEvent', localPlayer)
print('[Heartbeat][Client] OnPlayerReconnectEvent, 玩家断线重连,', localPlayer, localPlayer.UserId)
NetUtil.Fire_C('OnPlayerReconnectEvent', localPlayer, localPlayer.UserId)
cache.state = HeartbeatEnum.CONNECT
end
end
Expand All @@ -150,19 +150,19 @@ function CheckPlayerState(_player, _cTimestamp)
cache.state = HeartbeatEnum.CONNECT
elseif cache.state == HeartbeatEnum.CONNECT and diff >= HEARTBEAT_THRESHOLD_1 then
--* 玩家断线,弱网环境
--print('[Heartbeat][Client] OnPlayerDisconnectEvent, 玩家离线, 弱网环境,', localPlayer)
NetUtil.Fire_C('OnPlayerDisconnectEvent', localPlayer)
print('[Heartbeat][Client] OnPlayerDisconnectEvent, 玩家离线, 弱网环境,', localPlayer)
NetUtil.Fire_C('OnPlayerDisconnectEvent', localPlayer, localPlayer.UserId)
cache.state = HeartbeatEnum.DISCONNECT
elseif cache.state == HeartbeatEnum.DISCONNECT and diff >= HEARTBEAT_THRESHOLD_2 then
--* 玩家断线, 退出游戏
-- QuitGame()
NetUtil.Fire_C('OnPlayerLeaveEvent', localPlayer)
NetUtil.Fire_C('OnPlayerLeaveEvent', localPlayer, localPlayer.UserId)
end
end

--- 退出游戏
function QuitGame()
--print('[Heartbeat][Client] Game.Quit(), 玩家退出游戏', localPlayer, localPlayer.UserId)
print('[Heartbeat][Client] Game.Quit(), 玩家退出游戏', localPlayer, localPlayer.UserId)
Game.Quit()
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ local initDefaultList, initList, updateList = {}, {}, {}

--- 运行客户端
function Client:Run()
--print('[Client] Run()')
print('[Client] Run()')
InitClient()
StartUpdate()
end

--- 停止Update
function Client:Stop()
--print('[Client] Stop()')
print('[Client] Stop()')
running = false
ClientHeartbeat.Stop()
end
Expand All @@ -33,7 +33,7 @@ function InitClient()
if initialized then
return
end
--print('[Client] InitClient()')
print('[Client] InitClient()')
InitRandomSeed()
InitHeartbeat()
InitDataSync()
Expand Down Expand Up @@ -85,14 +85,11 @@ end
function GenInitAndUpdateList()
-- TODO: 改成在FrameworkConfig中配置
-- Init Default
ModuleUtil.GetModuleListWithFunc(Module.UI_Module, 'InitDefault', initDefaultList)
ModuleUtil.GetModuleListWithFunc(Module.C_Module, 'InitDefault', initDefaultList)
-- Init
ModuleUtil.GetModuleListWithFunc(Define, 'Init', initList)
ModuleUtil.GetModuleListWithFunc(Module.UI_Module, 'Init', initList)
ModuleUtil.GetModuleListWithFunc(Module.C_Module, 'Init', initList)
-- Update
ModuleUtil.GetModuleListWithFunc(Module.UI_Module, 'Update', updateList)
ModuleUtil.GetModuleListWithFunc(Module.C_Module, 'Update', updateList)
-- Plugin
for _, m in pairs(Config.PluginModules) do
Expand Down Expand Up @@ -123,7 +120,7 @@ end

--- 开始Update
function StartUpdate()
--print('[Client] StartUpdate()')
print('[Client] StartUpdate()')
assert(not running, '[Client] StartUpdate() 正在运行')

running = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ end

--- 打印数据同步日志
PrintLog = FrameworkConfig.DebugMode and debugMode and function(...)
--print('[MetaData]', ...)
print('[MetaData]', ...)
end or function()
end

Expand Down Expand Up @@ -263,12 +263,12 @@ Data.Global.b = {22, 33}
Data.Global.c = {c1 = {44, 55}, c2 = 66}
Data.Global.c.c3 = {c4 = 77}
Data.Global.d = {'88', Vector3(9,9,9)}
--print(table.dump(Data.Global))
--print(table.dump(MetaData.Get(Data.Global)))
print(table.dump(Data.Global))
print(table.dump(MetaData.Get(Data.Global)))
--print(table.dump(Data.Player))
print(table.dump(Data.Player))
--print(table.dump(Data.Players))
print(table.dump(Data.Players))
--print(table.dump(Data.Players['pid:local_1']))
print(table.dump(Data.Players['pid:local_1']))
]]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end

--- 加载的时候运行的代码
function ServerBase:InitDefault(_module)
-- --print(string.format('[ServerBase][%s] InitDefault()', self.name))
-- print(string.format('[ServerBase][%s] InitDefault()', self.name))
-- 初始化默认监听事件
EventUtil.LinkConnects(world.S_Event, _module, self)
end
Expand All @@ -20,7 +20,7 @@ end
-- FrameworkConfig.DebugMode 框架中的全局debug开关
function ServerBase:Log(...)
if self.debug and FrameworkConfig.DebugMode then
--print(string.format('[%s]', self.name), ...)
print(string.format('[%s]', self.name), ...)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ local sheet

--- 打印数据同步日志
local PrintLog = FrameworkConfig.DebugMode and FrameworkConfig.Debug.ShowDataSyncLog and function(...)
--print('[DataSync][Server]', ...)
print('[DataSync][Server]', ...)
end or function()
end

--! 初始化

--- 数据初始化
function ServerDataSync.Init()
--print('[DataSync][Server] Init()')
print('[DataSync][Server] Init()')
InitEventsAndListeners()
InitDefines()
sheet = DataStore:GetSheet('PlayerData')
Expand Down Expand Up @@ -99,7 +99,7 @@ end

--- 开始同步
function ServerDataSync.Start()
--print('[DataSync][Server] 服务器数据同步开启')
print('[DataSync][Server] 服务器数据同步开启')
MetaData.ServerSync = true

-- 启动定时器
Expand Down Expand Up @@ -129,10 +129,10 @@ function LoadGameDataAsyncCb(_val, _msg, _uid)
local player = world:GetPlayerByUserId(_uid)
assert(player, string.format('[DataSync][Server] 玩家不存在, uid = %s', _uid))
if _msg == 0 or _msg == 101 then
--print('[DataSync][Server] 获取玩家数据成功', player.Name)
print('[DataSync][Server] 获取玩家数据成功', player.Name)
local hasData = _val ~= nil
if hasData then
--print('[DataSync][Server] 玩家数据,存在', player.Name)
print('[DataSync][Server] 玩家数据,存在', player.Name)
--若以前的数据存在,更新
-- TODO: 数据兼容的处理
local data = _val
Expand All @@ -143,7 +143,7 @@ function LoadGameDataAsyncCb(_val, _msg, _uid)
end
else
-- 不存在数据,用之前生成的默认数据
--print('[DataSync][Server] 玩家数据,不存在', player.Name)
print('[DataSync][Server] 玩家数据,不存在', player.Name)
end
NetUtil.Fire_S('LoadPlayerDataSuccessEvent', player, hasData)
NetUtil.Fire_C('LoadPlayerDataSuccessEvent', player, hasData)
Expand Down Expand Up @@ -196,9 +196,9 @@ end
function SaveGameDataAsyncCb(_val, _msg, _uid, _delete)
-- 保存成功
if _msg == 0 then
--print('[DataSync][Server] 保存玩家数据,成功', _uid)
print('[DataSync][Server] 保存玩家数据,成功', _uid)
if _delete == true then
--print('[DataSync][Server] 删除服务器玩家数据', _uid)
print('[DataSync][Server] 删除服务器玩家数据', _uid)
rawDataPlayers[_uid] = nil
--* 删除玩家端数据
Data.Players[_uid] = nil
Expand All @@ -207,7 +207,7 @@ function SaveGameDataAsyncCb(_val, _msg, _uid, _delete)
end

-- 保存失败
--print(string.format('[DataSync][Server] 保存玩家数据失败,%s秒后重试, uid = %s, msg = %s', RELOAD_TIME, _uid, _msg))
print(string.format('[DataSync][Server] 保存玩家数据失败,%s秒后重试, uid = %s, msg = %s', RELOAD_TIME, _uid, _msg))
--若失败,则1秒后重新再读取一次
invoke(
function()
Expand All @@ -220,10 +220,10 @@ end
--- 存储全部玩家数据
function SaveAllGameDataAsync()
if not MetaData.ServerSync then
--print('[DataSync][Server] ServerSync未开始')
print('[DataSync][Server] ServerSync未开始')
return
end
--print('[DataSync][Server] 尝试保存全部玩家数据……')
print('[DataSync][Server] 尝试保存全部玩家数据……')
for uid, data in pairs(Data.Players) do
if not string.isnilorempty(uid) and data then
SaveGameDataAsync(uid, false)
Expand Down Expand Up @@ -262,21 +262,22 @@ function DataSyncC2SEventHandler(_player, _path, _value)
end

--- 新玩家加入事件Handler
function OnPlayerJoinEventHandler(_player)
--print('[DataSync][Server] OnPlayerJoinEventHandler', _player, _player.UserId)
function OnPlayerJoinEventHandler(_player, _uid)
print('[DataSync][Server] OnPlayerJoinEventHandler', _player, _player.UserId, _uid)

--* 向客户端同步Data.Global
NetUtil.Fire_C('DataSyncS2CEvent', _player, MetaData.Enum.GLOBAL, MetaData.Get(Data.Global))

local uid = _player.UserId
InitDataPlayer(uid)
-- 初始化玩家数据
InitDataPlayer(_uid)

--* 获取长期存储,成功后向客户端同步
LoadGameDataAsync(uid)
LoadGameDataAsync(_uid)
end

--- 玩家离开事件Handler
function OnPlayerLeaveEventHandler(_player, _uid)
--print('[DataSync][Server] OnPlayerLeaveEventHandler', _player, _uid)
print('[DataSync][Server] OnPlayerLeaveEventHandler', _player, _uid)
assert(not string.isnilorempty(_uid), '[ServerDataSync] OnPlayerLeaveEventHandler() uid不存在')
--* 保存长期存储:rawDataPlayers[_uid] 保存成功后删掉
SaveGameDataAsync(_uid, true)
Expand Down
Loading

0 comments on commit ea47bb9

Please sign in to comment.