Skip to content

Commit

Permalink
Add command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jiwonz committed Oct 21, 2024
1 parent d074217 commit 30dd421
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 103 deletions.
2 changes: 1 addition & 1 deletion dev/shared/testPackets.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local HandyNet = require(ReplicatedStorage.Packages.HandyNet)
local HandyNet = require(ReplicatedStorage.Packages.HandyNet.lib)

return HandyNet.defineNamespace("game", function()
return {
Expand Down
34 changes: 34 additions & 0 deletions src/commands/command.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--!native
--!optimize 2

local RunService = game:GetService("RunService")

local packet = require(script.Parent.Parent.packets.packet)
local nothing = require(script.Parent.Parent.dataTypes.nothing)()

local moduleRunContext: "server" | "client" = if RunService:IsServer() then "server" else "client"

return function(fn: () -> (), reliable: boolean, id: number)
local innerPacket = packet(nothing, reliable, id)
local command

if moduleRunContext == "server" then
innerPacket.onServerReceived:connect(fn)

command = function(player: Player?)
if player then
innerPacket.sendTo(nil, player)
else
innerPacket.broadcast(nil)
end
end
elseif moduleRunContext == "client" then
innerPacket.onClientReceived:connect(fn)

command = function()
innerPacket.sendToServer(nil)
end
end

return command
end
10 changes: 10 additions & 0 deletions src/commands/defineReliableCommand.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--!native
--!optimize 2

local command = require(script.Parent.command)

return function(fn: () -> ())
return function(id: number)
return command(fn, true, id)
end
end
10 changes: 10 additions & 0 deletions src/commands/defineUnreliableCommand.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--!native
--!optimize 2

local command = require(script.Parent.command)

return function(fn: () -> ())
return function(id: number)
return command(fn, false, id)
end
end
74 changes: 0 additions & 74 deletions src/init.luau

This file was deleted.

29 changes: 1 addition & 28 deletions src/packets/packet.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--!native
--!optimize 2
local Players = game:GetService("Players")

local RunService = game:GetService("RunService")

local types = require(script.Parent.Parent.types)
Expand Down Expand Up @@ -33,42 +33,15 @@ return function(value: types.dataTypeInterface<any>, reliable: boolean, id: numb

local exported = {}

-- RunContext error checking that doesn't have performance drawbacks
-- setmetatable(exported, {
-- __index = function(index)
-- if
-- (index == "sendTo" or index == "sendToAllExcept" or index == "sendToAll")
-- and moduleRunContext == "client"
-- then
-- error("You cannot use sendTo, sendToAllExcept, or sendToAll on the client")
-- elseif index == "send" and moduleRunContext == "server" then
-- error("You cannot use send on the server")
-- end
-- end,
-- })

-- exposed for the reader file
exported.reader = value.read

if moduleRunContext == "server" then
-- function exported.sendToList(data, players: { Player })
-- for _, player in players do
-- serverSendFunction(player, id, writer, data)
-- end
-- end

function exported.sendTo(data, player: Player)
serverSendFunction(player, id, writer, data)
end

-- function exported.sendToAllExcept(data, except: Player)
-- for _, player: Player in Players:GetPlayers() do
-- if player ~= except then
-- serverSendFunction(player, id, writer, data)
-- end
-- end
-- end

function exported.broadcast(data)
serverSendAllFunction(id, writer, data)
end
Expand Down

0 comments on commit 30dd421

Please sign in to comment.