-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilding-Server-Handler.lua
More file actions
146 lines (109 loc) · 4.41 KB
/
Building-Server-Handler.lua
File metadata and controls
146 lines (109 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local valid = require(Modules.Data.Validator)
local handler = require(ServerStorage.Modules.Data.DataHandler)
local ECS = require(Modules.ECS.ECS)
local Components = require(Modules.ECS.ECS.Components)
local Blink = require(ServerStorage.Modules.Blink.Server)
local world = ECS.world
local map = ECS.map
local BuildingAction = {}
local function setAttrs(p: Player, playerData: any)
p:SetAttribute("Gold", playerData.Gold or 0)
end
function BuildingAction:OnReceive(player: Player, packet: any)
if typeof(packet) ~= "table" or typeof(packet.action_type) ~= "number" or type(packet.meta) ~= "table" then
warn("Invalid BuildingAction packet from", player)
return
end
if packet.action_type == 0 then
self:handle_buy(player, packet.meta)
elseif packet.action_type == 1 then
self:handle_move(player, packet.meta)
elseif packet.action_type == 2 then
self:handle_upgrade(player, packet.meta)
elseif packet.action_type == 3 then
self:handle_cancel(player, packet.meta)
end
end
function BuildingAction:handle_buy(p: Player, meta: any)
local buildingType = meta.building_type
local pos = meta.position
local rot = meta.rotation
if typeof(buildingType) ~= "number" or typeof(pos) ~= "Vector3" then return end
if rot ~= nil and typeof(rot) ~= "number" then rot = nil end
local playerEntity = map:get(p)
local playerData = world:get(playerEntity, Components.Data)
local details = valid.canPurchase(playerData, buildingType, 1)
if not details.ok then return end
if not valid.canPlace(playerData, buildingType, pos, rot) then return end
local uuid = handler:create_building(p, buildingType, pos, rot, details.amount or 0)
-- update UI attrs
playerData = world:get(playerEntity, Components.Data)
setAttrs(p, playerData)
task.delay(details.time, function()
handler:finish_construction(uuid)
end)
end
function BuildingAction:handle_move(p: Player, meta: any)
local uuid = meta.uuid
local pos = meta.position
local rot = meta.rotation
if typeof(uuid) ~= "string" or typeof(pos) ~= "Vector3" then return end
if rot ~= nil and typeof(rot) ~= "number" then rot = nil end
local buildingEntity = valid.getBuildingEntityByUUID(uuid)
if not buildingEntity then print("Invalid building uuid") return end
local playerEntity = map:get(p)
local playerData = world:get(playerEntity, Components.Data)
if not playerData then return end;
handler:place_building(playerEntity, uuid, pos, rot)
world:set(playerEntity, Components.Data, playerData)
end
function BuildingAction:handle_upgrade(p: Player, meta: any)
local uuid = meta.uuid
if typeof(uuid) ~= "string" then return end
local buildingEntity = valid.getBuildingEntityByUUID(uuid)
if not buildingEntity then print("Invalid building uuid") return end
local playerEntity = map:get(p)
local playerData = world:get(playerEntity, Components.Data)
if not playerData then print("Invalid player data") return end;
-- find buildingType (quick scan)
local buildingType: number? = nil
if type(playerData.Buildings) == "table" then
for bt, dict in pairs(playerData.Buildings) do
if type(dict) == "table" and ((dict.builds and dict.builds[uuid]) or dict[uuid]) then
buildingType = bt
break
end
end
end
if not buildingType then print("Invalid building type") return end
local details = valid.canUpgrade(playerData, buildingType, uuid)
if not details.ok then print(details.reason) return end
handler:upgrade_building(playerData, buildingEntity, uuid, details.amount or 0)
world:set(playerEntity, Components.Data, playerData)
setAttrs(p, playerData)
task.delay(details.time or 0, function()
handler:finish_construction(uuid)
end)
end
function BuildingAction:handle_cancel(p: Player, meta: any)
local uuid = meta.uuid
if typeof(uuid) ~= "string" then return end
local buildingEntity = handler:resolve_entity_from_uuid(uuid)
if not buildingEntity then return end
local playerEntity = map:get(p)
local playerData = world:get(playerEntity, Components.Data)
if not playerData then return end;
handler:cancel_construction(playerData, buildingEntity, 0)
world:set(playerEntity, Components.Data, playerData)
setAttrs(p, playerData)
end
function BuildingAction:init()
Blink.Actions.BuildingAction.On(function(player, packet)
BuildingAction:OnReceive(player, packet)
end)
end
return BuildingActi