Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
dig1t committed Oct 3, 2024
1 parent ae51df0 commit a73612c
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 588 deletions.
18 changes: 18 additions & 0 deletions example/Client/_init.client.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local red = require(ReplicatedStorage.Packages.red)

local store: red.StoreType = red.Store.new()

store:subscribe(function(action: red.Action<any>)
if action.type == "PLAYER_KILL" then
print(`{action.payload} was killed!`)
end
end)

-- "get" will wait for a result
local data = store:get({ type = "GET_DATA" })

if data.success then
print(data.payload.data) -- Hello, world!
end
30 changes: 30 additions & 0 deletions example/Server/Controllers/PlayerController.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local PlayerController = {}

function PlayerController.kill(player: Player): boolean?
local character = player.Character :: Model?
local humanoid = character
and character:FindFirstChildOfClass("Humanoid") :: Humanoid?

if humanoid then
humanoid.Health = 0

print(player.Name .. " was killed!")

return true
end

return false
end

function PlayerController.damage(player: Player, amount: number)
local character = player.Character :: Model?
local humanoid = character
and character:FindFirstChildOfClass("Humanoid") :: Humanoid?

if humanoid then
humanoid.Health -= amount
print(`{player.Name} took {amount} damage!`)
end
end

return PlayerController
13 changes: 13 additions & 0 deletions example/Server/Handlers/Loader.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local red = require(game.ReplicatedStorage.Packages.red)

return function(server: red.ServerType)
server:bind("GET_DATA", function(_player: Player)
print("Getting data...")
task.wait(1)

return {
type = "DATA",
payload = { data = "Hello, world!" },
}
end)
end
7 changes: 7 additions & 0 deletions example/Server/Handlers/PingPong.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local red = require(game.ReplicatedStorage.Packages.red)

return function(server: red.ServerType)
server:bind("PING", function(player: Player)
server:dispatch(player, { type = "PONG" })
end)
end
20 changes: 20 additions & 0 deletions example/Server/Handlers/Player.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local red = require(game.ReplicatedStorage.Packages.red)

local PlayerController = require(script.Parent.Parent.Controllers.PlayerController)

return function(server: red.ServerType)
server:bind("PLAYER_KILL", function(player: Player)
local success = PlayerController.kill(player)

if success then
server:dispatch(true, { type = "PLAYER_KILLED", payload = player.Name })
end
end)

server:bind(
"PLAYER_DAMAGE",
function(player: Player, payload: red.ActionPayload<{ amount: number }>)
PlayerController.damage(player, payload.amount)
end
)
end
7 changes: 7 additions & 0 deletions example/Server/_init.server.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local red = require(ReplicatedStorage.Packages.red)

local server: red.ServerType = red.Server.new()

server:useHandlers(script.Parent.Handlers)
55 changes: 0 additions & 55 deletions examples/Game/ServerScriptService/red/Config.luau

This file was deleted.

Loading

0 comments on commit a73612c

Please sign in to comment.