-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuancheng Zhang
committed
May 18, 2021
1 parent
8557cbb
commit 9172e71
Showing
12 changed files
with
812 additions
and
67 deletions.
There are no files selected for viewing
9 changes: 0 additions & 9 deletions
9
Code/['World']['Global']['Define']['GlobalDataModule'].ModuleScript.lua
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Code/['World']['Global']['Framework']['Client']['ClientDataSyncModule'].ModuleScript.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- 游戏客户端数据同步 | ||
--- @module Client Sync Data, Client-side | ||
--- @copyright Lilith Games, Avatar Team | ||
--- @author Yuancheng Zhang | ||
local ClientDataSync = {} | ||
|
||
-- Localize global vars | ||
local FrameworkConfig, MetaData = FrameworkConfig, MetaData | ||
|
||
-- 客户端私有数据 | ||
local rawDataGlobal = {} | ||
local rawDataPlayer = {} | ||
|
||
--- 打印数据同步日志 | ||
local PrintLog = FrameworkConfig.DebugMode and FrameworkConfig.Debug.ShowDataSyncLog and function(...) | ||
--print('[DataSync][Client]', ...) | ||
end or function() | ||
end | ||
|
||
--! 初始化 | ||
|
||
--- 数据初始化 | ||
function ClientDataSync.Init() | ||
--print('[DataSync][Client] Init()') | ||
InitEventsAndListeners() | ||
InitDataDefines() | ||
end | ||
|
||
--- 初始化事件和绑定Handler | ||
function InitEventsAndListeners() | ||
if localPlayer.C_Event == nil then | ||
world:CreateObject('FolderObject', 'S_Event', localPlayer) | ||
end | ||
|
||
-- 数据同步事件 | ||
world:CreateObject('CustomEvent', 'DataSyncS2CEvent', localPlayer.C_Event) | ||
localPlayer.C_Event.DataSyncS2CEvent:Connect(DataSyncS2CEventHandler) | ||
|
||
-- 长期存储成功事件 | ||
if not localPlayer.C_Event.LoadPlayerDataSuccessEvent then | ||
world:CreateObject('CustomEvent', 'LoadPlayerDataSuccessEvent', localPlayer.C_Event) | ||
end | ||
end | ||
|
||
--- 校验数据定义 | ||
function InitDataDefines() | ||
--* 客户端全局数据 | ||
Data.Global = Data.Global or MetaData.New(rawDataGlobal, MetaData.Enum.GLOBAL, MetaData.Enum.CLIENT) | ||
-- 默认赋值 | ||
for k, v in pairs(Data.Default.Global) do | ||
Data.Global[k] = v | ||
end | ||
|
||
--* 客户端玩家数据 | ||
local uid = localPlayer.UserId | ||
local path = MetaData.Enum.PLAYER .. uid | ||
Data.Player = Data.Player or MetaData.New(rawDataPlayer, path, uid) | ||
-- 默认赋值 | ||
for k, v in pairs(Data.Default.Player) do | ||
Data.Player[k] = v | ||
end | ||
end | ||
|
||
--- 开始同步 | ||
function ClientDataSync.Start() | ||
--print('[DataSync][Client] 客户端数据同步开启') | ||
MetaData.ClientSync = true | ||
end | ||
|
||
--! Event handler | ||
|
||
--- 数据同步事件Handler | ||
function DataSyncS2CEventHandler(_path, _value) | ||
if not MetaData.ClientSync then | ||
return | ||
end | ||
|
||
PrintLog(string.format('收到 _path = %s, _value = %s', _path, table.dump(_value))) | ||
|
||
local uid = localPlayer.UserId | ||
|
||
--* 收到服务器数据 | ||
if string.startswith(_path, MetaData.Enum.GLOBAL) then | ||
--* Data.Global 全局数据 | ||
MetaData.Set(rawDataGlobal, _path, _value, uid, false) | ||
elseif string.startswith(_path, MetaData.Enum.PLAYER .. uid) then | ||
--* Data.Player 玩家数据 | ||
MetaData.Set(rawDataPlayer, _path, _value, uid, false) | ||
else | ||
error( | ||
string.format( | ||
'[DataSync][Client] _path错误 _player = %s, _path = %s, _value = %s', | ||
localPlayer, | ||
_path, | ||
table.dump(_value) | ||
) | ||
) | ||
end | ||
end | ||
|
||
return ClientDataSync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Code/['World']['Global']['Framework']['DataModule'].ModuleScript.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- 游戏数据 | ||
--- @module Game Data, Both-side | ||
--- @copyright Lilith Games, Avatar Team | ||
--- @author Yuancheng Zhang | ||
local Data = {} | ||
|
||
-- 客户端 | ||
-- 1. Data.Global | ||
-- 2. Data.Player | ||
|
||
-- 服务器 | ||
-- 1. Data.Global | ||
-- 2. Data.Players | ||
|
||
--! 这个Module为空 | ||
--! 数据定义在:ClientDataSyncModule、ServerDataSyncModule | ||
|
||
return Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.