Jump to content

Delete all toptimes of a map


koragg

Recommended Posts

Hello, I have a little problem. I want when I type /deletemap everything connected to it to be deleted along with it (ghosts, delays, tops). But I get an error when it tries to delete the map's toptimes. Error is this: Database query failed: no such table: 'race maptimes Sprint LS Tour'. How can I fix this? I want all of the map's toptimes to get deleted alongside all the other things. Here's full code:

addEvent('onMapStarting', true)
function onNewMapStart()
    local map = g_Map
    if map == exports.mapmanager:getRunningGamemodeMap() then
        return
    end
    local mapName = getResourceInfo(map, "name")
	local resourceName = getResourceName(map)
	if fileExists(":raceghost/ghosts/"..resourceName..".ghost") then
		fileDelete(":raceghost/ghosts/"..resourceName..".ghost")
		outputServerLog("Ghost record for "..resourceName.." deleted!")
	end
	local sql = executeSQLQuery("SELECT * FROM mapinterims WHERE mapname = ?", resourceName)
	if #sql > 0 then
		executeSQLQuery("DELETE FROM mapinterims WHERE mapname=?", resourceName)
		outputServerLog("Delay time for "..resourceName.." deleted!")
	end
	
	local topsTable = getModeAndMap('Sprint', mapName)
	local topsSql = executeSQLQuery('SELECT playerName AS topName FROM '..qsafetablename(topsTable))
	if type(topsSql) == 'table' and #topsSql > 0 then
		executeSQLQuery("DELETE FROM ? WHERE mapname=?", qsafetablename(topsTable), resourceName)
	end
	
	local deleteMap = deleteResource(resourceName)
	
    if not deleteMap then
		outputChatBox("Error: Map cannot be deleted.", g_P, 255, 255, 255, true) 
        canUseCommand = true
        g_Map = nil
        g_P = nil
        removeEventHandler('onMapStarting', root, onNewMapStart)
		return
    end
    outputChatBox("#00FFFFMap '#00FF00"..mapName.."#00FFFF' deleted by #FFFFFF"..addTeamColor(g_P).."#00FFFF!", root, 255, 255, 255, true)
    exports.IRC:outputIRC("Map '"..mapName.."' deleted by "..addTeamColor(g_P):gsub('#%x%x%x%x%x%x', '').."!")
	refreshResources()
	local mapscenter = getResourceFromName("mapscenter")
	if getResourceState(mapscenter) == "running" then
		restartResource(mapscenter)
	end
	local maptops = getResourceFromName("maptops")
	if getResourceState(maptops) == "running" then
		restartResource(maptops)
	end
	local cwmanagersmapspanel = getResourceFromName("clanwarmanagersmapspanel")
	if getResourceState(cwmanagersmapspanel) == "running" then
		restartResource(cwmanagersmapspanel)
	end
	local moderatorsmapspanel = getResourceFromName("moderatorsmapspanel")
	if getResourceState(moderatorsmapspanel) == "running" then
		restartResource(moderatorsmapspanel)
	end
    canUseCommand = true
    g_Map = nil
    g_P = nil
    removeEventHandler('onMapStarting', root, onNewMapStart)
end
-------------------------------------------------------------------------------------------------------------------------
canUseCommand = true
g_Map = nil
g_P = nil
-------------------------------------------------------------------------------------------------------------------------
function delmap(p)
	local playeraccount = getPlayerAccount (p)
	local accountname = getAccountName(playeraccount)
	if isGuestAccount(playeraccount) then
		return
	end
    if isObjectInACLGroup ("user." .. accountname, aclGetGroup("Admin")) or isObjectInACLGroup ("user." .. accountname, aclGetGroup("MapManagers")) then
    if not canUseCommand then 
		outputChatBox("Error: Cannot use command. An admin has already deleted this map.", p, 255, 255, 255, true) 
		return 
    end
    local map = exports.mapmanager:getRunningGamemodeMap()
    if not map then 
		outputChatBox("Error: No map is currently running.", p, 255, 255, 255, true) 
		return 
	end
		outputChatBox("Deleting current map at the start of next one!", root, 255, 0, 0, true)
    g_Map = map
    g_P = p
    addEventHandler('onMapStarting', root, onNewMapStart)
    canUseCommand = false
	end
end
addCommandHandler('deletemap', delmap)
-------------------------------------------------------------------------------------------------------------------------
function addTeamColor(player)
	local playerTeam = getPlayerTeam ( player ) 
	if ( playerTeam ) then
		local r,g,b = getTeamColor ( playerTeam )
		local n1 = toHex(r)
		local n2 = toHex(g)
		local n3 = toHex(b)
		if r <= 16 then n1 = "0"..n1 end
		if g <= 16 then n2 = "0"..n2 end
		if b <= 16 then n3 = "0"..n3 end
		return "#"..n1..""..n2..""..n3..""..getPlayerNametagText(player)
	else
		return getPlayerNametagText(player)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function toHex(n)
    local hexnums = {"0","1","2","3","4","5","6","7",
                     "8","9","A","B","C","D","E","F"}
    local str,r = "",n%16
    if n-r == 0 then str = hexnums[r+1]
    else str = toHex((n-r)/16)..hexnums[r+1] end
    return str
end
-------------------------------------------------------------------------------------------------------------------------
function getModeAndMap(gMode, mapName)
	return 'race maptimes '..gMode..' '..mapName
end
-------------------------------------------------------------------------------------------------------------------------
function qsafetablename(s)
    return qsafestring(s)
end
-------------------------------------------------------------------------------------------------------------------------
function qsafestring(s)
    return "'"..safestring(s).."'"
end
-------------------------------------------------------------------------------------------------------------------------
function safestring(s)
    return s:gsub("(['])", "''")
end

My attempt at deleting the toptimes is between lines 19 and 23 (utilities between 114 and 128). Would appreciate some help, cheers :)

Link to comment

Fixed :D

addEvent('onMapStarting', true)
function onNewMapStart()
    local map = g_Map
    if map == exports.mapmanager:getRunningGamemodeMap() then
        return
    end
    local mapName = getResourceInfo(map, "name")
	local resourceName = getResourceName(map)
	-- Delete map's ghost file
	if fileExists(":raceghost/ghosts/"..resourceName..".ghost") then
		fileDelete(":raceghost/ghosts/"..resourceName..".ghost")
		outputServerLog("Ghost record for "..resourceName.." deleted!")
	end
	-- Delete map's delay time
	local sql = executeSQLQuery("SELECT * FROM mapinterims WHERE mapname = ?", resourceName)
	if #sql > 0 then
		executeSQLQuery("DELETE FROM mapinterims WHERE mapname=?", resourceName)
		outputServerLog("Delay time for "..resourceName.." deleted!")
	end
	-- Delete map's toptimes
	local map_name = {}
	local mode = 'Sprint'
	map_name['race maptimes '..mode..' ' .. (getResourceInfo(map, 'name' ) or getResourceName(map))] = map
	local map_table = executeSQLQuery("SELECT tbl_name FROM sqlite_master WHERE tbl_name LIKE 'race maptimes %' ")
	for k, v in ipairs(map_table) do
		local mapTable = v.tbl_name
		if map_name[mapTable] then
			local mapTimes = executeSQLQuery("SELECT playerName FROM ?", mapTable)
			for i, t in ipairs(mapTimes) do
				executeSQLQuery("DELETE FROM ? WHERE playerName=?", mapTable, t.playerName)
			end
		end
	end
	local deleteMap = deleteResource(resourceName)
	
    if not deleteMap then
		outputChatBox("Error: Map cannot be deleted.", g_P, 255, 255, 255, true) 
        canUseCommand = true
        g_Map = nil
        g_P = nil
        removeEventHandler('onMapStarting', root, onNewMapStart)
		return
    end
    outputChatBox("#00FFFFMap '#00FF00"..mapName.."#00FFFF' deleted by #FFFFFF"..addTeamColor(g_P).."#00FFFF!", root, 255, 255, 255, true)
    exports.IRC:outputIRC("Map '"..mapName.."' deleted by "..addTeamColor(g_P):gsub('#%x%x%x%x%x%x', '').."!")
	
	refreshResources()
	
	local mapscenter = getResourceFromName("mapscenter")
	if mapscenter then
		if getResourceState(mapscenter) == "running" then
			triggerEvent("updateMaps", root, true)
		end
	end
	
	local maptops = getResourceFromName("maptops")
	if maptops then
		if getResourceState(maptops) == "running" then
			restartResource(maptops, true)
		end
	end
	
	local cwmanagersmapspanel = getResourceFromName("clanwarmanagersmapspanel")
	if cwmanagersmapspanel then
		if getResourceState(cwmanagersmapspanel) == "running" then
			triggerEvent("updateCWManagersMaps", root, true)
		end
	end
	
	local modsmapspanel = getResourceFromName("moderatorsmapspanel")
	if modsmapspanel then
		if getResourceState(modsmapspanel) == "running" then
			triggerEvent("updateModsMaps", root, true)
		end
	end
	
	refreshResources()
	
    canUseCommand = true
    g_Map = nil
    g_P = nil
    removeEventHandler('onMapStarting', root, onNewMapStart)
end
-------------------------------------------------------------------------------------------------------------------------
canUseCommand = true
g_Map = nil
g_P = nil
-------------------------------------------------------------------------------------------------------------------------
function delmap(p)
	local playeraccount = getPlayerAccount (p)
	local accountname = getAccountName(playeraccount)
	if isGuestAccount(playeraccount) then
		return
	end
    if isObjectInACLGroup ("user." .. accountname, aclGetGroup("Admin")) or isObjectInACLGroup ("user." .. accountname, aclGetGroup("MapManagers")) then
    if not canUseCommand then 
		outputChatBox("Error: Cannot use command. An admin has already deleted this map.", p, 255, 255, 255, true) 
		return 
    end
    local map = exports.mapmanager:getRunningGamemodeMap()
    if not map then 
		outputChatBox("Error: No map is currently running.", p, 255, 255, 255, true) 
		return 
	end
		outputChatBox("Deleting current map at the start of next one!", root, 255, 0, 0, true)
    g_Map = map
    g_P = p
    addEventHandler('onMapStarting', root, onNewMapStart)
    canUseCommand = false
	end
end
addCommandHandler('deletemap', delmap)
-------------------------------------------------------------------------------------------------------------------------
function addTeamColor(player)
	local playerTeam = getPlayerTeam ( player ) 
	if ( playerTeam ) then
		local r,g,b = getTeamColor ( playerTeam )
		local n1 = toHex(r)
		local n2 = toHex(g)
		local n3 = toHex(b)
		if r <= 16 then n1 = "0"..n1 end
		if g <= 16 then n2 = "0"..n2 end
		if b <= 16 then n3 = "0"..n3 end
		return "#"..n1..""..n2..""..n3..""..getPlayerNametagText(player)
	else
		return getPlayerNametagText(player)
	end
end
-------------------------------------------------------------------------------------------------------------------------
function toHex(n)
    local hexnums = {"0","1","2","3","4","5","6","7",
                     "8","9","A","B","C","D","E","F"}
    local str,r = "",n%16
    if n-r == 0 then str = hexnums[r+1]
    else str = toHex((n-r)/16)..hexnums[r+1] end
    return str
end

Lines 20-33

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