local i=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local M=require(i.common.ApiSpec)local D={}D.prototype={}D.__index=D.prototype function D.new(i)local M={handlers=i}setmetatable(M,D)return M end function D.prototype.connect(D)local p=D.handlers local x=i:WaitForChild("\069\118\101\110\116\115")for i,M in pairs(M.fromClient)do local p=x:WaitForChild("\102\114\111\109\067\108\105\101\110\116-"..i)D[i]=function(i,...)M.arguments(...)p:FireServer(...)end end for i,M in pairs(M.fromServer)do local D=x:WaitForChild("\102\114\111\109\083\101\114\118\101\114-"..i)local K=p[i]if K==nil then error(("\078\101\101\100 \116\111 \105\109\112\108\101\109\101\110\116 \099\108\105\101\110\116 \104\097\110\100\108\101\114 \102\111\114 \037\113"):format(i),2)end D.OnClientEvent:Connect(function(...)M.arguments(...)K(...)end)end for i in pairs(p)do if M.fromServer[i]==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(i),2)end end end return D
-- Author: Lucien Greathouse (LPGhatguy)
-- https://github.com/LPGhatguy/rdc-project/blob/master/src/client/ClientApi.lua

-- Modifications by Austin Reuschle (Nimblz)

--[[
	This object's job is to read the common ApiSpec, which defines the protocol
	for communicating with the server and the types that each method accepts.
	On connecting to the server via `connect`, we generate an object that has
	a method for each RemoteEvent that attached validation on both ends.
	I've found that this is a super nice way to think about network
	communication in Roblox, since it lines up with other strongly-typed RPC
	systems.
]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ApiSpec = require(ReplicatedStorage.common.ApiSpec)

local ClientApi = {}
ClientApi.prototype = {}
ClientApi.__index = ClientApi.prototype

function ClientApi.new(handlers)
	local self = {handlers = handlers}

	setmetatable(self, ClientApi)

	return self
end

function ClientApi.prototype:connect()
	local handlers = self.handlers
	local remotes = ReplicatedStorage:WaitForChild("Events")

	for name, endpoint in pairs(ApiSpec.fromClient) do
		local remote = remotes:WaitForChild("fromClient-" .. name)

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

			remote:FireServer(...)
		end
	end

	for name, endpoint in pairs(ApiSpec.fromServer) do
		local remote = remotes:WaitForChild("fromServer-" .. name)

		local handler = handlers[name]

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

		remote.OnClientEvent:Connect(function(...)
			endpoint.arguments(...)

			handler(...)
		end)
	end

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

return ClientApi