local z=script:FindFirstAncestor("\067\109\100\114\065\100\100\105\116\105\111\110\115")local A=z.Packages local Y=require(A.Promise)local m=require(A.Util)local W={}W.__index=W function W.new(A)local Y=setmetatable({CmdrAdditions=A,_remoteEvent=z:FindFirstChildOfClass("\082\101\109\111\116\101\069\118\101\110\116"),_remoteFunction=z:FindFirstChildOfClass("\082\101\109\111\116\101\070\117\110\099\116\105\111\110"),_events={}},W)Y._remoteEvent.OnClientEvent:Connect(function(z,...)(Y:_getEvent(z)):fire(...)end)return Y end function W._getEvent(A,z)local Y=A._events[z]if not Y then Y=m.Event.new()A._events[z]=Y end return Y end function W.HandleEvent(Y,z,A)return(Y:_getEvent(z)):connect(A)end function W.Request(A,z,...)local m={...}return Y.new(function(Y,W)local a={A._remoteFunction:InvokeServer(z,unpack(m))}local H=table.remove(a,1)if H then Y(unpack(a))else W(unpack(a))end end)end return W
local Root = script:FindFirstAncestor("CmdrAdditions")
local Packages = Root.Packages

local Promise = require(Packages.Promise)
local Util = require(Packages.Util)

local NetworkClient = {}
NetworkClient.__index = NetworkClient

function NetworkClient.new(cmdrAdditions)
	local self = setmetatable({
		CmdrAdditions = cmdrAdditions,

		_remoteEvent = Root:FindFirstChildOfClass("RemoteEvent"),
		_remoteFunction = Root:FindFirstChildOfClass("RemoteFunction"),

		_events = {}
	}, NetworkClient)

	self._remoteEvent.OnClientEvent:Connect(function(eventName, ...)
		self:_getEvent(eventName):fire(...)
	end)

	return self
end

function NetworkClient:_getEvent(eventName)
	local event = self._events[eventName]

	if not event then
		event = Util.Event.new()
		self._events[eventName] = event
	end

	return event
end

function NetworkClient:HandleEvent(eventName, handler)
	return self:_getEvent(eventName):connect(handler)
end

function NetworkClient:Request(handlerName, ...)
	local arguments = { ... }
	return Promise.new(function(resolve, reject)
		local results = { self._remoteFunction:InvokeServer(handlerName, unpack(arguments)) }
		local success = table.remove(results, 1)

		if success then
			resolve(unpack(results))
		else
			reject(unpack(results))
		end
	end)
end

return NetworkClient