Jump to content

group panel


orcun99

Recommended Posts



validGroups = {}
wssCache = {}
playerCache = {}

function errorLog(errorString)
	local file = fileOpen("errors.txt")
	fileSetPos(file, fileGetSize(file))
	fileWrite(file, "\n" .. errorString)
	fileClose(file)
end

function readLog(player)
	if isPlayerInGroup(player, "admin") then
		local file = fileOpen("errors.txt")
		local result = fileRead( file, fileGetSize ( file ))
		fileClose(file)
		
		triggerClientEvent(player, "copySQLlogs", player, result)
	end
end
addCommandHandler("readsqllog", readLog)

addEvent ( "onPlayerGroupAdd", true )
addEvent ( "onPlayerGroupRemove", true )
function validateGroups(valid)
	groupListTable = {}
	for k,v in ipairs(split(valid,",")) do
		validGroups[v] = true
	end
	outputDebugString("WSSsql - Updated valid groups: "..valid)
end

function getValidGroups(valid)
	local result = {}
	for k,v in pairs(validGroups) do
		table.insert(result, k)
	end
	return result
end

function onSettingChangeHandler(name,oldvalue,value)
	if name and oldvalue and value and name == "validSQLGroups" and type(value) == "string" then
		validateGroups(value)
	end
end

function onResourceStartHandler()
	validateGroups(get("*validSQLGroups"))
	if not fileExists("errors.txt") then
		fileCreate("errors.txt")
	end
end
addEventHandler("onSettingChange",resourceRoot,onSettingChangeHandler)
addEventHandler ( "onResourceStart", resourceRoot, onResourceStartHandler )

function getPlayerGroups(player)
	if not playerCache[player] then cachePlayer(player) end
	local returnTable = {}
	for group, value in pairs(playerCache[player]) do
		if value then
			table.insert(returnTable, group)
		end
	end
	
	local altTable = exports.WSSacl:getPlayerGroups(player)
	if returnTable ~= altTable then
		local timeArray = getRealTime()
		local hours = timeArray.hour
		local minutes = timeArray.minute
		local day = timeArray.monthday
		local month = timeArray.month
		local year = timeArray.year + 1900
	
		local logString = string.format("[%02d-%02d-%04d %02d:%02d]", day, month, year, hours, minutes) .. " WSSacl record mismatch on player " .. getPlayerName(player) .. " with accountname " .. getAccountName(getPlayerAccount(player)) .. " on getPlayerGroups. SQL result: " .. table.concat(resultTable, ",") .. " and ACL result: " .. table.concat(altTable, ",") .. "."

		errorLog(logString)
		outputDebugString(logString)
	end
	return returnTable
end
addEvent ( "getPlayerGroups", true )
addEventHandler ( "getPlayerGroups", getRootElement(), getPlayerGroups )

function cachePlayer(player)
	local account = getPlayerAccount(player)
	if account then
		local groupString = getAccountData(account, "groups") or ""
		playerCache[player] = {}
		for i,v in ipairs(split(groupString, ",")) do
			playerCache[player][v] = true
		end
		return true
	else
		outputDebugString("Failed caching player information for player " .. getPlayerName(player) .. " : No account set.")
		return false
	end
end
addEventHandler("onPlayerLogIn",root,function() cachePlayer(source) end)
addEventHandler("onResourceStart",resourceRoot,function() for k, v in ipairs(getElementsByType("player")) do cachePlayer(v) end end)

function uncachePlayer(player)
	local player = iPlayer or source
	if player then
		if playerCache[player] then playerCache[player] = nil end
	end
	return true
end
addEventHandler("onPlayerQuit",root,uncachePlayer)
addEventHandler("onPlayerLogOut",root,uncachePlayer)

function getPlayersInGroup(group)
	if group and tostring(group) then
		local returnTable = {}
		for i,v in pairs(playerCache) do
			if v[group] then
				table.insert(returnTable, i)
			end
		end
		
		local altTable = exports.WSSacl:getPlayersInGroup(group)
		if returnTable ~= altTable then
			local timeArray = getRealTime()
			local hours = timeArray.hour
			local minutes = timeArray.minute
			local day = timeArray.monthday
			local month = timeArray.month
			local year = timeArray.year + 1900
		
			local logString = string.format("[%02d-%02d-%04d %02d:%02d]", day, month, year, hours, minutes) .. " WSSacl record mismatch on group " .. group .. " on getPlayersInGroup."

			errorLog(logString)
			outputDebugString(logString)
		end
		
		return returnTable
	else
		outputDebugString("Failed getting players in group " ..tostring(group) .. " : Invalid group name.")
		return false
	end
end

function isPlayerInGroup(player,group)
	if player and group and tostring(group) then
		if isElement(player) and getElementType(player) == "player" then
			if not playerCache[player] then cachePlayer(player) end
			
			local result = false
			if playerCache[player][group] then
				result = true
			end
			
			local altResult = exports.WSSacl:isPlayerInGroup(player, group)
			if result ~= altResult then
				local timeArray = getRealTime()
				local hours = timeArray.hour
				local minutes = timeArray.minute
				local day = timeArray.monthday
				local month = timeArray.month
				local year = timeArray.year + 1900
			
				local logString = string.format("[%02d-%02d-%04d %02d:%02d]", day, month, year, hours, minutes) .. " WSSacl record mismatch on player " .. getPlayerName(player) .. " with accountname " .. getAccountName(getPlayerAccount(player)) .. " on isPlayerInGroup. SQL result: " .. tostring(result) .. " and ACL result: " .. tostring(altResult) .. "."

				errorLog(logString)
				outputDebugString(logString)
			end
			
			return result
		else
			outputDebugString("*wssSQL* isPlayerInGroup (wssSQL) invalid variables (player, group).")
		end
	else
		outputDebugString("*wssSQL* isPlayerInGroup (wssSQL) variables not passed (player, group).")
	end
end
addEvent ( "isPlayerInGroup", true )
addEventHandler ( "isPlayerInGroup", getRootElement(), isPlayerInGroup )

function updateLastNickUsed(player,nick)
	if not player or not nick then return false end
	local account = getPlayerAccount(player)
	if not account then return false end
	setAccountData(account, "lastnick", nick)
	if query then
		return true
	else
		return false
	end
end
addEvent("updateLastNickUsed",true)
addEventHandler("updateLastNickUsed",root,updateLastNickUsed)

addEventHandler("onPlayerChangeNick", root, function(old, new) updateLastNickUsed(source, new) end) 

function lastNickUsed(username)
	if not getAccount(username) then return "Unknown" end
	local account = getAccount(username)
	return getAccountData(account, "lastnick")
end
addEvent("lastNickUsed",true)
addEventHandler("lastNickUsed",root,lastNickUsed)

addCommandHandler("addgroup",
function(sourcePlayer, commandName, userName, group)
	if isPlayerInGroup ( sourcePlayer, "admin" ) then
		--[[if ( userName and group ) then
			if ( validGroups[group] == true ) then
				local account = getAccount(userName)
				if account then
					local groupString = getAccountData(account, "groups") or ""
					local groups = split(groupString, ",")
					local isAlreadyIn = false
					for i,v in ipairs(groups) do
						if v == group then
							isAlreadyIn = true
							break
						end
					end
					if not isAlreadyIn then
						table.insert(groups, group)
						local newGroups = table.concat(groups, ",")
						setAccountData(account, "groups", newGroups)
						exports.WSScommands:sendMessage("Account " .. userName .. " added to group " .. group .. " successfully.", 0, 255, 0, sourcePlayer)
						if getAccountPlayer(account) then
							local player = getAccountPlayer(account)
							cachePlayer(player)
							exports.WSScommands:sendMessage("You have been added to group " .. group .. " by admin " .. getPlayerName(sourcePlayer) .. ".", 0, 255, 0, player)
						end
					else
						exports.WSScommands:sendMessage("This player is already in this group.", 255, 255, 0, sourcePlayer)
					end
				else
					exports.WSScommands:sendMessage("Account not found.", 255, 255, 0, sourcePlayer)
				end
			else
				exports.WSScommands:sendMessage("Invalid group. Use /validgroups for a list of valid groups.", 255, 255, 0, sourcePlayer)
			end
		else
			exports.WSScommands:sendMessage("Invalid parameters. Correct syntax: /addgroup username group.", 255, 255, 0, sourcePlayer)
		end]]--
		exports.WSScommands:sendMessage("This service is temporarily disabled due to work on permissions resources.", 255, 255, 0, sourcePlayer)
	end
end)

addCommandHandler("listgroups", function(sourcePlayer, commandName, userName)
	if isPlayerInGroup ( sourcePlayer, "admin" ) then
		--[[if userName then
			local account = getAccount(userName)
			if account then
				local groups = getAccountData(account, "groups")
				if groups then
					exports.WSScommands:sendMessage("Groups account ".. userName .. " is in: " .. groups .. " .", 255, 255, 0, sourcePlayer)
				else
					exports.WSScommands:sendMessage("Account ".. userName .. " isn't in any groups.", 255, 255, 0, sourcePlayer)
				end
			else
				exports.WSScommands:sendMessage("Account not found.", 255, 255, 0, sourcePlayer)
			end
		else
			exports.WSScommands:sendMessage("Invalid parameters. Correct syntax: /listgroups username.", 255, 255, 0, sourcePlayer)
		end--]]
		exports.WSScommands:sendMessage("This service is temporarily disabled due to work on permissions resources.", 255, 255, 0, sourcePlayer)
	end
end)

function getAccountGroups(userName)
	if userName then
		local account = getAccount(userName)
		if account then
			local groups = getAccountData(account, "groups")
			if groups then
				return groups
			else
				return ""
			end
		else
			return false
		end
	else
		return false
	end
end

addCommandHandler("removegroup",
function(sourcePlayer, commandName, userName, group)
	if isPlayerInGroup ( sourcePlayer, "admin" ) then
		--[[if ( userName and group ) then
			if ( validGroups[group] == true ) then
				local account = getAccount(userName)
				if account then
					local groupString = getAccountData(account, "groups") or ""
					local groups = split(groupString, ",")
					local isAlreadyIn = false
					for i,v in ipairs(groups) do
						if v == group then
							isAlreadyIn = i
							break
						end
					end
					if isAlreadyIn ~= false then
						table.remove(groups, isAlreadyIn)
						local newGroups = table.concat(groups, ",")
						setAccountData(account, "groups", newGroups)
						exports.WSScommands:sendMessage("Account " .. userName .. " removed from group " .. group .. " successfully.", 0, 255, 0, sourcePlayer)
						if getAccountPlayer(account) then
							local player = getAccountPlayer(account)
							cachePlayer(player)
							exports.WSScommands:sendMessage("You have been removed from the " .. group .. " group by admin " .. getPlayerName(sourcePlayer) .. ".", 0, 255, 0, player)
						end
					else
						exports.WSScommands:sendMessage("This player is not in that group.", 255, 255, 0, sourcePlayer)
					end
				else
					exports.WSScommands:sendMessage("Account not found.", 255, 255, 0, sourcePlayer)
				end
			else
				exports.WSScommands:sendMessage("Invalid group. Use /validgroups for a list of valid groups.", 255, 255, 0, sourcePlayer)
			end
		else
			exports.WSScommands:sendMessage("Invalid parameters. Correct syntax: /removegroup username group.", 255, 255, 0, sourcePlayer)
		end]]--
		exports.WSScommands:sendMessage("This service is temporarily disabled due to work on permissions resources.", 255, 255, 0, sourcePlayer)
	end
end)

addCommandHandler("getusername", function(sourcePlayer, command, partial)
	if isPlayerInGroup ( sourcePlayer, "admin" ) then
		if partial then
			local player = getPlayerFromNamePart(partial)
			if player then
				local account = getPlayerAccount(player)
				if account then
					exports.WSScommands:sendMessage("Username of player " .. getPlayerName(player) .. " is: " .. getAccountName(account) .. ".", 0, 255, 0, sourcePlayer)
				else
					exports.WSScommands:sendMessage("Player " .. getPlayerName(player) .. " isn't logged in at the moment.", 255, 255, 0, sourcePlayer)
				end
			else
				exports.WSScommands:sendMessage("No player with this name found.", 255, 255, 0, sourcePlayer)
			end
		else
			exports.WSScommands:sendMessage("Please specify a player name.", 255, 255, 0, sourcePlayer)
		end
	end
end)

function getPlayerFromNamePart(name)
    if name then 
        for i, player in ipairs(getElementsByType("player")) do
            if string.find(getPlayerName(player):lower(), tostring(name):lower(), 1, true) then
                return player 
            end
        end
    end
    return false
end

function addWSSMemberToCache ()
	if source and isPlayerInGroup(source,"wss") then
		table.insert(wssCache,source)
	end
end

function removeWSSMemberFromCache()
	if source then
		for k,v in ipairs(wssCache) do
			if v == source then
				table.remove(wssCache,k)
			end
		end
	end
end

function getOnlineWSSMembers()
	return wssCache or false
end

addEventHandler("onPlayerLogIn",root,addWSSMemberToCache)
addEventHandler("onPlayerLogOut",root,removeWSSMemberFromCache)
addEventHandler("onPlayerQuit",root,removeWSSMemberFromCache)-

 

Edited by orcun99
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...