local D=math.huge local p=0 local n=game:GetService("\071\114\111\117\112\083\101\114\118\105\099\101")local I=script.Parent local b=require(I.Core.Remote)local F=b.getManager()local L=F.getModule("\068\097\116\097")local g=F.getModule("\085\116\105\108\115")local J=F.getModule("\067\111\110\102\105\103")local y={}function y.getLevel(I)local b=p I=g.getUserId(I)if not I then return end local F=y.cache[I]if F and F>b then b=F end local J=L.playerStore:get(I.UserId)if J and J.level>b then b=J.level end if game.CreatorType==Enum.CreatorType.Group then local p=n:GetGroupInfoAsync(game.CreatorId)if p.Owner.Id==I then b=D end else if game.CreatorId==I then b=D end end return b end function y.setLevel(D,p,n)D=g.getUserId(D)if not D then return end y.cache[D]=p local I=J.getConfig()if n and I.saveLevels then L.playerStore:update(D,{level=p})end end function y.getNameFromLevel(n)if n==D then return"\067\114\101\097\116\111\114"elseif n==p then return"\080\108\097\121\101\114"end local I=J.getConfig()for D,p in ipairs(I.levels)do if p[1]==n then return p[2]end end end function y.getLevelFromName(n)n=string.lower(n)if n=="\099\114\101\097\116\111\114"then return D elseif n=="\112\108\097\121\101\114"then return p end local I=J.getConfig()for D,p in ipairs(I.levels)do if string.lower(p[2])==n then return p[1]end end end return y
-- OptimisticSide
-- 5/9/2021
-- Player permission level system

local CREATOR_LEVEL = math.huge
local PLAYER_LEVEL = 0

local GroupService = game:GetService("GroupService")

local server = script.Parent
local Remote = require(server.Core.Remote)

local Manager = Remote.getManager()
local Data = Manager.getModule("Data")
local Utils = Manager.getModule("Utils")
local Config = Manager.getModule("Config")

local Levels = {}

-- Retrieve the level of a player.
function Levels.getLevel(player)
	local level = PLAYER_LEVEL

	-- Ensure "player" is a
	-- user id.
	player = Utils.getUserId(player)
	if not player then
		return
	end

	-- Retrieve cached level.
	local cached = Levels.cache[player]
	if cached and cached > level then
		level = cached
	end

	-- Retrive stored data for player.
	local stored = Data.playerStore:get(player.UserId)
	if stored and stored.level > level then
		level = stored.level
	end

	-- Handle the player being the game's owner.
	if game.CreatorType == Enum.CreatorType.Group then
		local groupInfo = GroupService:GetGroupInfoAsync(game.CreatorId)
		if groupInfo.Owner.Id == player then
			level = CREATOR_LEVEL
		end
	else
		if game.CreatorId == player then
			level = CREATOR_LEVEL
		end
	end

	return level
end

-- Updates the level of a player.
function Levels.setLevel(player, level, shouldSave)
	player = Utils.getUserId(player)
	if not player then
		return
	end
	
	Levels.cache[player] = level
	
	-- Update data store if should save.
	local config = Config.getConfig()
	if shouldSave and config.saveLevels then
		Data.playerStore:update(player, { level = level })
	end
end

-- Get a level's name from it's number.
function Levels.getNameFromLevel(level)
	if level == CREATOR_LEVEL then
		return "Creator"
	elseif level == PLAYER_LEVEL then
		return "Player"
	end

	local config = Config.getConfig()
	for _, levelInfo in ipairs(config.levels) do
		if levelInfo[1] == level then
			return levelInfo[2]
		end
	end
end

-- Get a level form its name.
function Levels.getLevelFromName(name)
	-- Level names are not case-sensitive.
	name = string.lower(name)

	if name == "creator" then
		return CREATOR_LEVEL
	elseif name == "player" then
		return PLAYER_LEVEL
	end

	local config = Config.getConfig()
	for _, levelInfo in ipairs(config.levels) do
		if string.lower(levelInfo[2]) == name then
			return levelInfo[1]
		end
	end
end

return Levels
