local A=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local v=require(A.common.ApiSpec)local Y={}Y.AllPlayers=newproxy(true)Y.prototype={AllPlayers=Y.AllPlayers}Y.__index=Y.prototype function Y.create(A)assert(typeof(A)=="\116\097\098\108\101")local v={handlers=A}setmetatable(v,Y)return v end function Y.prototype.connect(i)local C=i.handlers local R=Instance.new("\070\111\108\100\101\114")R.Name="\069\118\101\110\116\115"for A,v in pairs(v.fromClient)do local Y=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")Y.Name="\102\114\111\109\067\108\105\101\110\116-"..A Y.Parent=R local i=C[A]if i==nil then error(("\078\101\101\100 \116\111 \105\109\112\108\101\109\101\110\116 \115\101\114\118\101\114 \104\097\110\100\108\101\114 \102\111\114 \037\113"):format(A),2)end Y.OnServerEvent:Connect(function(A,...)assert(typeof(A)=="\073\110\115\116\097\110\099\101"and A:IsA("\080\108\097\121\101\114"))v.arguments(...)i(A,...)end)end for A,v in pairs(v.fromServer)do local C=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")C.Name="\102\114\111\109\083\101\114\118\101\114-"..A C.Parent=R i[A]=function(A,i,...)v.arguments(...)if i==Y.AllPlayers then C:FireAllClients(...)else assert(typeof(i)=="\073\110\115\116\097\110\099\101"and i:IsA("\080\108\097\121\101\114"))C:FireClient(i,...)end end end for A in pairs(C)do if v.fromClient[A]==nil then error(("\073\110\118\097\108\105\100 \104\097\110\100\108\101\114 \037\113 \115\112\101\099\105\102\105\101\100\033"):format(A),2)end end R.Parent=A i.remotes=R end function Y.prototype.destroy(A)A.remotes:Destroy()end return Y
-- Author: Lucien Greathouse
-- https://github.com/LPGhatguy/rdc-project/blob/master/src/server/ServerApi.lua

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ApiSpec = require(ReplicatedStorage.common.ApiSpec)

local ServerApi = {}
ServerApi.AllPlayers = newproxy(true)
ServerApi.prototype = {AllPlayers = ServerApi.AllPlayers}
ServerApi.__index = ServerApi.prototype


function ServerApi.create(handlers)
	assert(typeof(handlers) == "table")

	local self = {handlers = handlers}

	setmetatable(self, ServerApi)

	return self
end

function ServerApi.prototype:connect()
	local handlers = self.handlers
	local remotes = Instance.new("Folder")
	remotes.Name = "Events"

	for name, endpoint in pairs(ApiSpec.fromClient) do
		local remote = Instance.new("RemoteEvent")
		remote.Name = "fromClient-" .. name
		remote.Parent = remotes

		local handler = handlers[name]

		if handler == nil then
			error(("Need to implement server handler for %q"):format(name), 2)
		end

		remote.OnServerEvent:Connect(function(player, ...)
			assert(typeof(player) == "Instance" and player:IsA("Player"))

			endpoint.arguments(...)

			handler(player, ...)
		end)
	end

	for name, endpoint in pairs(ApiSpec.fromServer) do
		local remote = Instance.new("RemoteEvent")
		remote.Name = "fromServer-" .. name
		remote.Parent = remotes

		self[name] = function(_, player, ...)
			endpoint.arguments(...)

			if player == ServerApi.AllPlayers then
				remote:FireAllClients(...)
			else
				assert(typeof(player) == "Instance" and player:IsA("Player"))

				remote:FireClient(player, ...)
			end
		end
	end

	for name in pairs(handlers) do
		if ApiSpec.fromClient[name] == nil then
			error(("Invalid handler %q specified!"):format(name), 2)
		end
	end

	remotes.Parent = ReplicatedStorage
	self.remotes = remotes
end

function ServerApi.prototype:destroy()
	self.remotes:Destroy()
end

return ServerApi