rbxIdleWeapon/src/Server/ServerMain/init.server.luau

101 lines
3.2 KiB
Plaintext
Raw Normal View History

2025-06-29 23:59:43 +08:00
--[[
Evercyan @ March 2023
ServerMain
ServerMain requires many important modules for the game to work, as well as housing some miscellaneous code.
The modules it requires is under ServerStorage.Modules and the script itself, which makes core features of the game work,
such as armor, mobs, and the leveling system.
]]
--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
--> References
local PlayerData = ReplicatedStorage:WaitForChild("PlayerData")
--> Dependencies
local HumanoidAttributes = require(script.HumanoidAttributes)
local PlayerLeveling = require(script.PlayerLeveling)
local GameConfig = require(ReplicatedStorage.Data.GameConfig)
local ContentLibrary = require(ReplicatedStorage.Modules.ContentLibrary)
local ToolLib = require(ServerStorage.Modules.ToolLib)
local ArmorLib = require(ServerStorage.Modules.ArmorLib)
--------------------------------------------------------------------------------
-- Create initial folders
local function CreateFolder(Name: string, Parent: Instance)
local Folder = Instance.new("Folder")
Folder.Name = Name
Folder.Parent = Parent
return Folder
end
2025-07-02 00:21:32 +08:00
-- 初始化workspace目录
2025-06-29 23:59:43 +08:00
local Temporary = CreateFolder("Temporary", workspace)
local ProjectileCache = CreateFolder("ProjectileCache", Temporary)
local Characters = CreateFolder("Characters", workspace)
2025-07-02 00:21:32 +08:00
-- 初始化玩家信息存储目录(沟通作用,具体数据还得后端处理)
local PlayerDataFolder = Instance.new("Configuration")
PlayerDataFolder.Name = "PlayerData"
PlayerDataFolder.Parent = ReplicatedStorage
-- 加载Proxy目录下的所有代理
local Proxies = {}
local ProxyFolder = script.Parent.Parent:FindFirstChild("Proxy")
if ProxyFolder then
for _, proxyModule in ipairs(ProxyFolder:GetChildren()) do
if proxyModule:IsA("ModuleScript") then
local success, result = pcall(require, proxyModule)
if success then
-- 去掉文件名后缀
local name = proxyModule.Name
Proxies[name] = result
else
warn("加载代理模块失败: " .. proxyModule.Name, result)
2025-06-29 23:59:43 +08:00
end
end
end
end
-- Initially require all server-sided & shared modules
2025-06-30 00:06:14 +08:00
-- 加载模块 ReplicatedStorage.Modules, ServerStorage.Modules
2025-06-29 23:59:43 +08:00
for _, Location in {ReplicatedStorage.Modules, ServerStorage.Modules} do
for _, Library in Location:GetChildren() do
if Library:IsA("ModuleScript") then
task.spawn(require, Library)
end
end
end
2025-07-02 00:21:32 +08:00
--------------------------------------------------------------------------------
2025-06-29 23:59:43 +08:00
2025-07-02 00:21:32 +08:00
local function OnPlayerAdded(Player: Player)
if not Proxies.ArchiveProxy:IsPlayerDataLoaded(Player) then
warn("玩家数据未加载: " .. Player.Name)
return
2025-06-29 23:59:43 +08:00
end
2025-07-02 00:21:32 +08:00
local pData = Instance.new("Configuration")
pData.Name = Player.UserId
pData.Parent = PlayerDataFolder
-- 加载对应玩家的其他系统代理
Proxies.EquipmentProxy:InitPlayer(Player)
Proxies.PlayerInfoProxy:InitPlayer(Player)
end
2025-06-29 23:59:43 +08:00
2025-07-02 00:21:32 +08:00
local function OnPlayerRemoving(Player: Player)
local pData = PlayerDataFolder:FindFirstChild(Player.UserId)
if pData then pData:Destroy() end
2025-06-29 23:59:43 +08:00
end
2025-07-02 00:21:32 +08:00
Players.PlayerAdded:Connect(OnPlayerAdded)
for _, Player in Players:GetPlayers() do
task.spawn(OnPlayerAdded, Player)
end
Players.PlayerRemoving:Connect(OnPlayerRemoving)