Skip to content

Commit

Permalink
Merge pull request #9 from Tazmondo/dev
Browse files Browse the repository at this point in the history
Implement SharedEvents
  • Loading branch information
jackdotink authored Jan 2, 2024
2 parents 28f98a5 + 78edead commit b81363c
Show file tree
Hide file tree
Showing 5 changed files with 518 additions and 91 deletions.
108 changes: 77 additions & 31 deletions Test/Client/Client.client.luau
Original file line number Diff line number Diff line change
@@ -1,48 +1,94 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ComplexFunction = require(ReplicatedStorage.ComplexFunction)
local EmptyFunction = require(ReplicatedStorage.EmptyFunction)
local SimpleFunction = require(ReplicatedStorage.SimpleFunction)
function TestEvents()
local ComplexFunction = require(ReplicatedStorage.ComplexFunction)
local EmptyFunction = require(ReplicatedStorage.EmptyFunction)
local SimpleFunction = require(ReplicatedStorage.SimpleFunction)

local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Client()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Client()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Client()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Client()
local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Client()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Client()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Client()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Client()
task.wait(0.5)

task.wait(0.5)
print("Registering Listeners")

print("Registering Listeners")
ComplexEvent:On(function(Value1, Value2, Value3)
print("ComplexEvent", Value1, Value2, Value3)
end)

ComplexEvent:On(function(Value1, Value2, Value3)
print("ComplexEvent", Value1, Value2, Value3)
end)
SimpleEvent:On(function(Value)
print("SimpleEvent", Value)
end)

SimpleEvent:On(function(Value)
print("SimpleEvent", Value)
end)
EmptyEvent:On(function()
print("EmptyEvent")
end)

EmptyEvent:On(function()
print("EmptyEvent")
end)
task.wait(0.5)

task.wait(0.5)
print("Firing Events")

print("Firing Events")
ComplexEvent:Fire({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hello world again", 123)
SimpleEvent:Fire(123)
EmptyEvent:Fire()

ComplexEvent:Fire({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hello world again", 123)
SimpleEvent:Fire(123)
EmptyEvent:Fire()
task.wait(0.5)

task.wait(0.5)
print("Calling Functions")

print("Calling Functions")
print(ComplexFunction:Call({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hi", 12):Await())
print(SimpleFunction:Call(123):Await())
print(EmptyFunction:Call():Await())

print(ComplexFunction:Call({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hi", 12):Await())
print(SimpleFunction:Call(123):Await())
print(EmptyFunction:Call():Await())
task.wait(0.5)

task.wait(0.5)
print("Firing Ready")

print("Firing Ready")
ReadyEvent:Fire()
end

ReadyEvent:Fire()
function TestSharedEvents()
print("Testing Shared Events!")
local SharedEvents = require(ReplicatedStorage.SharedEvents)

task.wait(0.5)
print("Registering Listeners")

SharedEvents.Empty:SetClientListener(function()
print("Received empty!")
end)

SharedEvents.Simple:SetClientListener(function(Value)
print("Received simple!", Value)
end)

SharedEvents.Signal:OnClient(function(Value)
print("Signal 1 called!", Value)
end)
SharedEvents.Signal:OnClient(function(Value)
print("Signal 2 called!", Value)
end)

SharedEvents.Complex:SetClientListener(function(Value1, Value2, Value3)
print("Received complex!", Value1, Value2, Value3)
end)

task.wait(0.5)
print("Firing events")

SharedEvents.Empty:FireServer()
SharedEvents.Simple:FireServer(5)
SharedEvents.Signal:FireServer(6)
SharedEvents.Complex:FireServer(
{ one = { "String Literal", 17 }, two = { 123, "String Literal" } },
"another string",
5
)

task.wait(0.5)
print("Firing ready")
SharedEvents.Ready:FireServer()
end

TestSharedEvents()
207 changes: 147 additions & 60 deletions Test/Server/Server.server.luau
Original file line number Diff line number Diff line change
@@ -1,82 +1,169 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ComplexFunction = require(ReplicatedStorage.ComplexFunction)
local SimpleFunction = require(ReplicatedStorage.SimpleFunction)
local EmptyFunction = require(ReplicatedStorage.EmptyFunction)
function TestEvents()
local ComplexFunction = require(ReplicatedStorage.ComplexFunction)
local SimpleFunction = require(ReplicatedStorage.SimpleFunction)
local EmptyFunction = require(ReplicatedStorage.EmptyFunction)

local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Server()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Server()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Server()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Server()
local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Server()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Server()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Server()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Server()

print("Registering Function Callbacks")
print("Registering Function Callbacks")

ComplexFunction:SetCallback(function(Player, Value1, Value2, Value3)
print("ComplexFunction", Player, Value1, Value2, Value3)
task.wait(0.2)
ComplexFunction:SetCallback(function(Player, Value1, Value2, Value3)
print("ComplexFunction", Player, Value1, Value2, Value3)
task.wait(0.2)

return Value1, Value2, Value3
end)
return Value1, Value2, Value3
end)

SimpleFunction:SetCallback(function(Player, Value)
print("SimpleFunction", Player, Value)

SimpleFunction:SetCallback(function(Player, Value)
print("SimpleFunction", Player, Value)
return tostring(Value)
end)

return tostring(Value)
end)
EmptyFunction:SetCallback(function(Player)
print("EmptyFunction", Player)

EmptyFunction:SetCallback(function(Player)
print("EmptyFunction", Player)
return
end)

return
end)
print("Registering Event Listeners")

print("Registering Event Listeners")
ComplexEvent:On(function(Player, Value1, Value2, Value3)
print("ComplexEvent", Player, Value1, Value2, Value3)
end)

SimpleEvent:On(function(Player, Value)
print("SimpleEvent", Player, Value)
end)

ComplexEvent:On(function(Player, Value1, Value2, Value3)
print("ComplexEvent", Player, Value1, Value2, Value3)
end)
EmptyEvent:On(function(Player)
print("EmptyEvent", Player)
end)

SimpleEvent:On(function(Player, Value)
print("SimpleEvent", Player, Value)
end)
ReadyEvent:On(function(Player)
print(`[{Player.Name}] Ready, firing events!`)

print(`[{Player.Name}] :Fire`)
SimpleEvent:Fire(Player, 1)
EmptyEvent:Fire(Player)
ComplexEvent:Fire(Player, nil, "hello world again", 1)

print(`[{Player.Name}] :FireAll`)
SimpleEvent:FireAll(2)
EmptyEvent:FireAll()
ComplexEvent:FireAll(
{ one = { "String Literal", 123 }, two = { 123, "String Literal" } },
"hello world again",
2
)

print(`[{Player.Name}] :FireAllExcept`)
SimpleEvent:FireAllExcept(Player, 3)
EmptyEvent:FireAllExcept(Player)
ComplexEvent:FireAllExcept(Player, nil, "this should be skipped", 3)

print(`[{Player.Name}] :FireList`)
SimpleEvent:FireList({ Player }, 4)
EmptyEvent:FireList({ Player })
ComplexEvent:FireList({ Player }, { one = { "String Literal", 8 }, two = { 1, "String Literal" } }, "hi", 4)

print(`[{Player.Name}] :FireWithFilter`)
SimpleEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, 5)
EmptyEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end)
ComplexEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, { one = { "String Literal", 17 }, two = { 123, "String Literal" } }, "another string", 5)

print(`[{Player.Name}] Done!`)
end)
end

EmptyEvent:On(function(Player)
print("EmptyEvent", Player)
end)
function TestSharedEvents()
print("Testing Shared Events")
local SharedEvents = require(ReplicatedStorage.SharedEvents)

ReadyEvent:On(function(Player)
print(`[{Player.Name}] Ready, firing events!`)
print("Registering Event Listeners")

print(`[{Player.Name}] :Fire`)
SimpleEvent:Fire(Player, 1)
EmptyEvent:Fire(Player)
ComplexEvent:Fire(Player, nil, "hello world again", 1)
SharedEvents.Complex:SetServerListener(function(Player, Value1, Value2, Value3)
print("ComplexEvent", Player, Value1, Value2, Value3)
end)

print(`[{Player.Name}] :FireAll`)
SimpleEvent:FireAll(2)
EmptyEvent:FireAll()
ComplexEvent:FireAll({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hello world again", 2)
SharedEvents.Simple:SetServerListener(function(Player, Value)
print("SimpleEvent", Player, Value)
end)

print(`[{Player.Name}] :FireAllExcept`)
SimpleEvent:FireAllExcept(Player, 3)
EmptyEvent:FireAllExcept(Player)
ComplexEvent:FireAllExcept(Player, nil, "this should be skipped", 3)
SharedEvents.Empty:SetServerListener(function(Player)
print("EmptyEvent", Player)
end)

print(`[{Player.Name}] :FireList`)
SimpleEvent:FireList({ Player }, 4)
EmptyEvent:FireList({ Player })
ComplexEvent:FireList({ Player }, { one = { "String Literal", 8 }, two = { 1, "String Literal" } }, "hi", 4)
SharedEvents.Signal:OnServer(function(Player, Value)
print("SignalEvent1", Player, Value)
end)
SharedEvents.Signal:OnServer(function(Player, Value)
print("SignalEvent2", Player, Value)
end)

print(`[{Player.Name}] :FireWithFilter`)
SimpleEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, 5)
EmptyEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
SharedEvents.Ready:SetServerListener(function(Player)
print(`[{Player.Name}] Ready, firing events!`)

print(`[{Player.Name}] :FireClient`)
SharedEvents.Empty:FireClient(Player)
SharedEvents.Simple:FireClient(Player, 1)
SharedEvents.Signal:FireClient(Player, 1)
SharedEvents.Complex:FireClient(Player, nil, "hello world again", 1)

print(`[{Player.Name}] :FireAllClients`)
SharedEvents.Empty:FireAllClients()
SharedEvents.Simple:FireAllClients(2)
SharedEvents.Signal:FireAllClients(2)
SharedEvents.Complex:FireAllClients(
{ one = { "String Literal", 123 }, two = { 123, "String Literal" } },
"hello world again",
2
)

print(`[{Player.Name}] :FireAllClientsExcept`)
SharedEvents.Empty:FireAllClientsExcept(Player)
SharedEvents.Simple:FireAllClientsExcept(Player, 3)
SharedEvents.Signal:FireAllClientsExcept(Player, 3)
SharedEvents.Complex:FireAllClientsExcept(Player, nil, "this should be skipped", 3)

print(`[{Player.Name}] :FireClients`)
SharedEvents.Empty:FireClients({ Player })
SharedEvents.Simple:FireClients({ Player }, 4)
SharedEvents.Signal:FireClients({ Player }, 4)
SharedEvents.Complex:FireClients(
{ Player },
{ one = { "String Literal", 8 }, two = { 1, "String Literal" } },
"hi",
4
)

print(`[{Player.Name}] :FireFilteredClients`)
SharedEvents.Empty:FireFilteredClients(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end)
SharedEvents.Simple:FireFilteredClients(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, 5)
SharedEvents.Signal:FireFilteredClients(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, 5)
SharedEvents.Complex:FireFilteredClients(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, { one = { "String Literal", 17 }, two = { 123, "String Literal" } }, "another string", 5)

print(`[{Player.Name}] Done!`)
end)
ComplexEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, { one = { "String Literal", 17 }, two = { 123, "String Literal" } }, "another string", 5)
end

print(`[{Player.Name}] Done!`)
end)
TestSharedEvents()
24 changes: 24 additions & 0 deletions Test/Shared/SharedEvents.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Guard = require(ReplicatedStorage.Packages.Guard)
local Red = require(ReplicatedStorage.Packages.Red)

local ValueCheck =
Guard.Optional(Guard.Map(Guard.String, Guard.List(Guard.Or(Guard.Literal("String Literal"), Guard.Number))))

return {
Empty = Red.SharedEvent("Empty", function() end),

Ready = Red.SharedEvent("Ready", function() end),

Simple = Red.SharedEvent("Simple", function(Number)
return Guard.Number(Number)
end),

Signal = Red.SharedSignalEvent("Signal", function(Number)
return Guard.Number(Number)
end),

Complex = Red.SharedEvent("Complex", function(Value1, Value2, Value3)
return ValueCheck(Value1), Guard.String(Value2), Guard.Number(Value3)
end),
}
Loading

0 comments on commit b81363c

Please sign in to comment.