Jump to content

Hide HEX Nick | guiGridList


Recommended Posts

Olá pessoal,

Estou com um painel de Ranking de Drift, e gostaria de retirar os codigos HEX dos nicks na qual aparecem na lista, tentei pelo getPlayerFromNamePart e também tentei fazer um string.gsub, porém nenhuma das duas opções funcionou

Imagem da Situação:

https://imgur.com/a/JAY9u

client

local screenWidth,screenHeight = guiGetScreenSize()
local score = 0
local combo = 0
local multiplier = 1
local failDrift = false
local size = 2
local fps = getFPSLimit()
local vehicle
local inVehicle = false
local multipliers = {
{100000,5},
{50000,4},
{25000,3},
{10000,2},
{0,1},
}

local textX = screenWidth/2
local textY = screenHeight/4
local lineY = 45
local textScale = 2                               
local red = tocolor(255,0,0)
local green = tocolor(255,140,0)
local font = "default-bold"
local alignX = "center"
local alignY = "center"

local gui = {}

local allowedType =
{
	["Automobile"] = true,
	["Quad"] = true,
	["Monster Truck"] = true,
}

local forcedEvents =
{
	["onClientElementDestroy"] = true,
	["onClientPlayerWasted"] = true,
}

local function convertNumber ( number )
	local formatted = number
	while true do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1 %2')
		if ( k==0 ) then
			break
		end
	end
	return formatted
end

local function checkVehicleHandling()

	local flags = getVehicleHandling(vehicle)["handlingFlags"]
	if (bitAnd( flags, 32 ) == 32) or (bitAnd( flags, 64 ) == 64) then
		return true
	end
	
end

local function driftEnd (endscore,endcombo)
	local oldBestDrift = getElementData (localPlayer,"Best Drift") or 0
	local oldTotalDrift = getElementData (localPlayer,"Total Drift") or 0

	score = 0
	combo = 0
	
	if endscore ~= 0 then
		setElementData(localPlayer,"Total Drift",math.floor(endscore+oldTotalDrift))
		setElementData(localPlayer,"Last Drift",math.floor(endscore))
	end

	if endscore > oldBestDrift then
		setElementData(localPlayer,"Best Drift",endscore)
	end
	if endscore >= 2000 then
		triggerServerEvent("onDriftEnd",localPlayer,endscore)
		triggerEvent("onClientDriftEnd",localPlayer,endscore)
	end
end

local function calculateAngle ()
	if not allowedType[vehicle.vehicleType] then return 0,0 end
	if not isVehicleOnGround(vehicle) then return 0,0 end
	if failDrift then return 0,0 end
	
	local vx,vy,vz = getElementVelocity(vehicle)
	local rx,ry,rz = getElementRotation(vehicle)
	local sn,cs = -math.sin(math.rad(rz)), math.cos(math.rad(rz))
	local speed = (vx^2 + vy^2 + vz^2)^(0.5)
	local modV = math.sqrt(vx*vx + vy*vy)
	local cosX = (sn*vx + cs*vy)/modV
	
	if modV <= 0.2 then return 0,0 end
	if cosX > 0.966 or cosX < 0 then return 0,0 end
	
	return math.deg(math.acos(cosX))*0.5,speed
end

local function updateFPS(msSinceLastFrame)
    fps = (1 / msSinceLastFrame) * 1000
end

local function resetFail()

	failDrift = false
	showScore=false

end

local function onCollide(attacker)

	if attacker or failDrift then return end
	
	failDrift = true
	driftEnd(0,0)
	setTimer(resetFail,2000,1)
	
end

local function drawMeter ()
	if isWorldSpecialPropertyEnabled("hovercars") then return end
	if checkVehicleHandling() then return end
	
	if localPlayer.vehicle ~= vehicle then
		removeEventHandler("onClientVehicleDamage",vehicle,onCollide)
		removeEventHandler("onClientElementDestroy",vehicle,checkVehicle)
		removeEventHandler("onClientPlayerWasted",localPlayer,checkVehicle)
		removeEventHandler("onClientRender",root,drawMeter)
		removeEventHandler("onClientPreRender", root, updateFPS)
		vehicle=nil
		inVehicle=false
		checkVehicle()
		return
	end
	
	local angle,speed = calculateAngle()
	if isTimer (resetTimer) and angle ~= 0 then
		killTimer(resetTimer)
		showScore = true
		if comboReady then
			combo = combo + 1
			comboReady = false
		end
	end
	if angle == 0 then
		if not isTimer(resetTimer) then
			comboReady = true
			resetTimer = setTimer (function()
				if score == 0 then return end
				driftEnd (score,combo)
				score = 0
				combo = 0
				showScore = false
			end,1300,1)
		end
	end
	local gameSpeed = getGameSpeed()
	for k,v in ipairs(multipliers) do
		local pointsNeeded,multi = unpack(multipliers[k])
		if score > pointsNeeded then
			multiplier = multi
			break
		end
	end
	local fpsMultiplier = 100/fps
	local angleScore = angle/2
	local speedScore = speed*3
	local driftScore = angleScore*speedScore
	local addScore = math.floor(driftScore*multiplier)
	local gameSpeedFixedScore = math.floor(gameSpeed*addScore)
	score = score + math.floor(fpsMultiplier*gameSpeedFixedScore)
	if showScore then
		local color = (failDrift and red or green)
		dxDrawText ("Pontos de Drift x"..tostring(combo),textX,0,textX,textY-lineY,color,textScale,font,alignX,alignY)
		dxDrawText (score,textX,0,textX,textY,color,textScale,font,alignX,alignY)
	end

end

function checkVehicle(vehicleEntered)

	local isForcedFalse = forcedEvents[eventName] ~= true
	if localPlayer.inVehicle == inVehicle and isForcedFalse then return end
	
	local tempVehicle = vehicleEntered or getPedOccupiedVehicle(localPlayer)
	local seat = getPedOccupiedVehicleSeat(localPlayer)
	inVehicle = ((seat == 0 and allowedType[tempVehicle.vehicleType]) and (isForcedFalse and localPlayer.inVehicle or false) or false)

	if inVehicle and seat==0 then
		vehicle = tempVehicle
		addEventHandler("onClientVehicleDamage",vehicle,onCollide)
		addEventHandler("onClientElementDestroy",vehicle,checkVehicle)
		addEventHandler("onClientPlayerWasted",localPlayer,checkVehicle)
		addEventHandler("onClientRender",root,drawMeter)
		addEventHandler("onClientPreRender", root, updateFPS)
	elseif not inVehicle and vehicle then
		removeEventHandler("onClientVehicleDamage",vehicle,onCollide)
		removeEventHandler("onClientElementDestroy",vehicle,checkVehicle)
		removeEventHandler("onClientPlayerWasted",localPlayer,checkVehicle)
		removeEventHandler("onClientRender",root,drawMeter)
		removeEventHandler("onClientPreRender", root, updateFPS)
		vehicle=nil
	end

end

local function loadRecords(records,maxPosition,myAcc)

	guiSetText(gui.window,"Top "..tostring(maxPosition).." drifters")  

	guiGridListClear(gui.list)
	local guestID = 0
	for position,record in ipairs(records) do
		local player,score,name,isGuest = record.username,record.score,record.playername,record.isGuest
		if isGuest == "true" then
			guestID=guestID+1
			if myAcc == "guest" then
				myAcc = hash("sha512",getPlayerSerial())
			end
		end
		local row = guiGridListAddRow(gui.list,"#"..tostring(position).." "..name,(isGuest == "true" and "guest_"..tostring(guestID) or player),convertNumber(score))
		if player == myAcc then
			guiGridListSetItemColor(gui.list,row,gui.namecolumn,255,255,255,255)
			guiGridListSetItemColor(gui.list,row,gui.usercolumn,255,255,255,255)
			guiGridListSetItemColor(gui.list,row,gui.scorecolumn,255,255,255,255)
		end
	end
	
end

local function toggleGUI()

	local isVisible = guiGetVisible(gui.window)
	
	if isVisible then
		guiSetVisible(gui.window,false)
		showCursor(false)
	else
		guiSetVisible(gui.window,true)
		showCursor(true)
	end

end

local function initScript()

	addEvent("onClientDriftEnd",false)
	addEvent("Drift:loadRecords",true)
	
	gui.window = guiCreateWindow(0.35,0.15,0.3,0.7,"",true)
	gui.list = guiCreateGridList(0,0.05,1,0.95,true,gui.window)
	gui.namecolumn = guiGridListAddColumn(gui.list,"Jogador",0.4)
	gui.usercolumn = guiGridListAddColumn(gui.list,"Login",0.3)
	gui.scorecolumn = guiGridListAddColumn(gui.list,"Pontos",0.2)
	guiGridListSetSortingEnabled(gui.list,false)
	
	guiSetVisible(gui.window,false)
	
	bindKey("F5","down",toggleGUI)
	
	checkVehicle()
	addEventHandler("Drift:loadRecords",localPlayer,loadRecords)
	addEventHandler("onClientPlayerVehicleEnter",localPlayer,checkVehicle)
	addEventHandler("onClientPlayerVehicleExit",localPlayer,checkVehicle)
	
	triggerServerEvent("Drift:scriptLoaded",localPlayer)

end

addEventHandler("onClientResourceStart",resourceRoot,initScript)

server

local connection
local driftRecords = {}
local loadedClients = {}
local recordListMaxPosition = 50

local excludedUsernames = -- Add accountnames of who you want to blacklist from ranking, like to prevent players tracking (undercover) admins through F5 (current nick + username)
{
	["Adminusername1"] = true,
	["Test2"] = true,
}

local function comp(a,b)

	return a.score > b.score

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

local function checkDriftRecord(score)

	if not client.account then return end
	if excludedUsernames[client.account.name] then return end
	
	local acc = (client.account.name~="guest" and client.account.name or hash("sha512",client.serial))
	local name = client.name
	local oldJSON = toJSON(driftRecords)
	local isGuest = tostring(isGuestAccount(client.account))
	
	if driftRecords[#driftRecords] == nil or (score > driftRecords[#driftRecords].score or #driftRecords < recordListMaxPosition) then
		local existingPosition = false
		for position,record in ipairs(driftRecords) do
			if record.username == acc then
				existingPosition = position
				break
			end
		end
		if existingPosition and score > driftRecords[existingPosition].score then
			driftRecords[existingPosition].score = score
			driftRecords[existingPosition].playername = name
		elseif not existingPosition then
			table.insert(driftRecords,{username=acc,score=score,playername=name,isGuest=isGuest})
		end
	else
		return
	end
	
	table.sort(driftRecords,comp)
	
	if #driftRecords > recordListMaxPosition then
		for position=recordListMaxPosition+1,#driftRecords do
			driftRecords[position]=nil
		end
	end
	
	if oldJSON == toJSON(driftRecords) then
		return
	end
	
	for player,_ in pairs(loadedClients) do
		triggerClientEvent(player,"Drift:loadRecords",player,driftRecords,recordListMaxPosition,player.account.name)
	end
	
	connection:exec("DELETE FROM records")
	
	for position,record in ipairs(driftRecords) do
		connection:exec("INSERT INTO records VALUES (?,?,?,?)",record.username,record.playername,record.score,record.isGuest)
	end
	
end

local function recheckPlayer()

	triggerClientEvent(source,"Drift:loadRecords",source,driftRecords,recordListMaxPosition,source.account.name)

end

local function resetPlayer()

	loadedClients[source] = nil

end

local function clientLoaded()

	if source~=client then return end
	
	triggerClientEvent(client,"Drift:loadRecords",client,driftRecords,recordListMaxPosition,client.account.name)
	addEventHandler("onDriftEnd",client,checkDriftRecord)
	loadedClients[client] = true
	addEventHandler("onPlayerLogin",client,recheckPlayer)
	addEventHandler("onPlayerLogout",client,recheckPlayer)
	addEventHandler("onPlayerQuit",client,resetPlayer)
	
end

local function initScript()
	
	connection = Connection("sqlite",":/drift.db")
	connection:exec("CREATE TABLE IF NOT EXISTS records (username TEXT, playername TEXT, score NUMBER, isGuest TEXT)")
	
	local handle = connection:query("SELECT * FROM records")
	driftRecords = handle:poll(-1)
	
	addEvent("Drift:scriptLoaded",true)
	addEvent("onDriftEnd",true)
	
	addEventHandler("Drift:scriptLoaded",root,clientLoaded)

	for _,v in ipairs({"Best Drift","Total Drift","Last Drift"}) do exports["scoreboard"]:addScoreboardColumn(v) end
	
end

addEventHandler("onResourceStart",resourceRoot,initScript)

Gostaria de apesar retirar os códigos hex dos nicks, e deixar apenas o name 

Edited by Bryan321
Link to comment

A solução que do Gw8 só vai remover 1 código de cor, caso o jogador usar mais que isso, os seguintes irão aparecer.

Mas, isso pode ser solucionado usando isso:

local name = client.name
while name:find("#%x%x%x%x%x%x") do
	name = name:gsub("#%x%x%x%x%x%x","")
end

 

Link to comment
  • Other Languages Moderators

Funciona sim.
Fiz o teste com um nick com 2 códigos HEX e removeu os 2 códigos.

name = "#909090The#00bfffLord"

function teste (thePlayer, cmd)
	local name = name:gsub('#%x%x%x%x%x%x','')
	outputChatBox (name, thePlayer)
end
addCommandHandler ("asd", teste)

De acordo com o manual da linguagem LUA:

string.gsub (s, pattern, replacement [, n])

Retorna uma cópia de s em que todas as ocorrências (ou as primeiras n ocorrências, se fornecidas) de pattern foram substituídas pelo que for especificado em replacement, que pode ser uma string, uma tabela ou uma função. gsub também retorna, como seu segundo valor, o número total de correspondências que ocorreram.

Isso significa que por padrão ele já substitui todas as ocorrências. Se você quiser que ele substitua apenas a 1ª ocorrência, especifique 1 em n.

Edited by Lord Henry
Link to comment
  • Other Languages Moderators

Desculpe a intromissão neste post, mas devo deixar claro que o @Banex está certo sobre dois códigos de cores, mas não da forma que o @Lord Henry usou o nickname.

Use: ##ff000000ff00Hello como um nickname, o script só irá remover o " #ff0000 ", sendo assim o que resta (#00ff00) ainda será uma cor.

Link to comment

Na verdade esse ##ff000000ff00 funciona como um hack pra fazer só o primeiro código ser checado, você pode até usar esse código no mta que a maioria dos scripts não vão remover o código corretamente, então pra ser efetivo em todos casos, só usar o código do @Banex.

  • Like 1
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...