Jump to content

Delete ghosts with toptimes


koragg

Recommended Posts

I thought that this would be easy but I really have no idea what I am doing wrong here. I just want when admin types /deletetime the ghost file of that map to be deleted as well. Here's my code, no idea why it's not working, no errors too.

race_toptimes/toptiems_server.lua

function deltops(player, cmd, place)
		if not _TESTING and not isPlayerInACLGroup(player, g_Settings.admingroup) then
			return
		end
		if g_SToptimesManager and g_SToptimesManager.mapTimes then
			local row = g_SToptimesManager.mapTimes:deletetime(place)
			if row then
				g_SToptimesManager:updateTopText()
				local mapName = tostring(g_SToptimesManager.mapTimes.mapName)
				local placeText = place and "#"..tostring(place).." " or ""
				local slotDesc = "#FF0000"..placeText.."#00FF00["..tostring(row.timeText).."]#00FFFF from #FFFFFF"..tostring(getAccountData(getAccount(row.playerName), "currentPlayerName")).."#FFFFFF"
				local adminName = tostring(getPlayerNameWithColor(player))
				local adminLogName = getAdminNameForLog(player)
				for _, p in ipairs(getElementsByType("player")) do
					outputChatBox("#00FFFFToptime "..slotDesc.."#00FFFF deleted by "..adminName.."#00FFFF!", p, 235, 115, 70, true)
				end
				outputServerLog("Toptimes: "..adminLogName.." deleted "..slotDesc.." from map '"..mapName.."'")
				if place == 1 then
					local currentMap = exports.mapmanager:getRunningGamemodeMap()
					local mapName = getResourceName(currentMap)
					triggerServerEvent("onGhostDataDelete", getRootElement(), mapName)
				end
			end
		end
end
addCommandHandler("deletetime", deltops)
addCommandHandler("deletetop", deltops)
addCommandHandler("deltime", deltops)
addCommandHandler("deltop", deltops)

raceghost/record_server.lua

addEvent("onGhostDataDelete", true)
function delGhost(mapName)
-- Delete current ghost
	if fileExists("ghosts/"..mapName..".ghost") then
		fileDelete("ghosts/"..mapName..".ghost")
	end
end
addEventHandler("onGhostDataDelete", g_Root, delGhost)

 

Link to comment
3 hours ago, pa3ck said:

Is this evaluates to true or false


if fileExists("ghosts/"..mapName..".ghost") then

Is there any chance that the file is in use by a resource therefore not letting it be removed?

Well the ghost is active while the map is running. I don't know if that's the problem... but how to delete it then? I mean, it's made to be active (moving) after countdown ends to show how the top1 player drove.

Link to comment
7 minutes ago, pa3ck said:

Is it running at the very moment you are trying to delete the file?

When map loads it loads the ghost file as well. When countdown ends the ghost starts moving. So I'd say yes, it's active throughout the whole map (even while I'm trying to delete it). Now it's another thing that I'm not sure if it keeps requesting and recieving data while it's moving or it gets all it's data on map start.

Edited by koragg
Link to comment

I have no idea how race or (whatever resource this is) works, but why not? Toptimes are probably saved somewhere, you should be able to access that data from anywhere, anytime. But if you find it difficult or you can not achieve that, the other thing I would try is to delete the toptime, then when new map loads, check a variable if the previous ghost file should be deleted. eg:

local tTimeRemoved = "someMapName/ghost file path"

addEventHandler("onMapLoaded", root, function()
    
    if tTimeRemoved then
      fileDelete(tTimeRemoved)
      tTimeRemoved = false
    end
    
end)

Then again, I don't know if there's an event called onMapLoaded, but there must be something similar event.

Edited by pa3ck
Link to comment
local mapName = false
if place == 1 then
	local currentMap = exports.mapmanager:getRunningGamemodeMap()
	mapName = getResourceName(currentMap)
end
addEvent("onMapStarting", true)
function delGhost()
	if mapName then
		triggerServerEvent("onGhostDataDelete", getRootElement(), mapName)
	end
end
addEventHandler("onMapStarting", root, delGhost)

First part of code is in toptimes' /deletetime command function. If the deleted top is a top1 it records the current map's name into 'currentMap' and then gets the resource name in 'mapName'. Second part is a seperate function checking if 'mapName' is not false, and if it's not, it triggers the event to delete ghost of previous map as the 'mapName' contains the resource name of the previous map (the one in which an admin has wrote /deletetime 1). @pa3ck Something like this?

Edited by koragg
But what if players vote to play the same map again ._.
Link to comment

That^ didn't work but I found out why it's not working. The "if place == 1 then" part is never true (idk why) and the event never gets triggered. I tried this and it worked perfectly with the command even while the ghost was active (moving):

raceghost/record_server.lua

addEvent("onGhostDataDelete")
function delGhost(client)
	local accName = getAccountName(getPlayerAccount(client))
	local currentMap = exports.mapmanager:getRunningGamemodeMap()
	local mapName = getResourceName(currentMap)
	if isObjectInACLGroup("user."..accName, aclGetGroup("Admin")) or isObjectInACLGroup("user."..accName, aclGetGroup("SuperModerator")) then
		fileDelete( "ghosts/"..mapName..".ghost" )
		outputChatBox("Ghost record for "..mapName.." deleted!", client, 255, 0, 0, true)
	end
end
addEventHandler("onGhostDataDelete", g_Root, delGhost)
addCommandHandler("deleteghost", delGhost)

The only problem is that I don't know how to check if the deleted toptime is indeed a top1 or not in the race_toptimes function which deletes toptimes. As if it's a top2-top9, the ghost shouldn't be deleted. If I do a correct check for a top1, then it's easy to just use executeCommandHandler or to trigger the event (both ways do same thing, personal preference).

Edited by koragg
Link to comment

Done @pa3ck :) Here's how I did it:

race_toptimes/toptimes_server.lua

function deltops( player, cmd, place )
		if not _TESTING and not isPlayerInACLGroup(player, g_Settings.admingroup) then
			return
		end
		if g_SToptimesManager and g_SToptimesManager.mapTimes then
			local row = g_SToptimesManager.mapTimes:deletetime(place)
			if row then
				g_SToptimesManager:updateTopText()
				local mapName = tostring(g_SToptimesManager.mapTimes.mapName)
				local placeText = place and "#" .. tostring(place) .. " " or ""
				local slotDesc = "#FF0000" .. placeText .. "#00FF00[" .. tostring(row.timeText) .. "]#00FFFF from #FFFFFF" .. tostring(getAccountData(getAccount(row.playerName), "currentPlayerName")) .. "#FFFFFF"
				local adminName = tostring(getPlayerNameWithColor(player))
				local adminLogName = getAdminNameForLog(player)
				for _, p in ipairs(getElementsByType("player")) do
					outputChatBox("#00FFFFToptime " .. slotDesc .. "#00FFFF deleted by " .. adminName .."#00FFFF!", p, 235, 115, 70, true)
				end
				outputServerLog( "Toptimes: " .. adminLogName .. " deleted " .. slotDesc .. " from map '" .. mapName .. "'" )
				if tonumber(place) == 1 or not place then
					executeCommandHandler("deleteghost", player)
				end
			end
		end
end
addCommandHandler( "deletetime", deltops)
addCommandHandler( "deletetop", deltops)
addCommandHandler( "deltime", deltops)
addCommandHandler( "deltop", deltops)

raceghost/record_server.lua

function delGhost(client)
	local accName = getAccountName(getPlayerAccount(client))
	local currentMap = exports.mapmanager:getRunningGamemodeMap()
	local mapName = getResourceName(currentMap)
	if isObjectInACLGroup("user."..accName, aclGetGroup("Admin")) or isObjectInACLGroup("user."..accName, aclGetGroup("SuperModerator")) then
		fileDelete( "ghosts/"..mapName..".ghost" )
		outputServerLog("Ghost record for "..mapName.." deleted!")
	end
end
addCommandHandler("deleteghost", delGhost)

:signthanks: for helping me out yet again ;)

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...