Jump to content

Drift Meter SE - fight with SQL function


acp___(PL)

Recommended Posts

Working on a script that will give points for the drift, will take into account: angle in the corner and speed.

Speed is not the problem ..., but angle yes ...

my way is not working like I wanted ...

local gMe = getLocalPlayer()
local screenWidth, screenHeight = guiGetScreenSize()
local screenPlayerWidth = screenWidth / 1280
local screenPlayerHeight = screenHeight / 1024
local g_rot = getRootElement()
local myX, myY = getElementPosition(gMe)
local driftX1 = myX
local driftY1 = myY
 
function uruchomDriftMeter()
if getVehicleType(getPedOccupiedVehicle(gMe)) == "Automobile" then 
	myX, myY = getElementPosition(gMe)
	local driftX0 = driftX1
	local driftY0 = driftY1
	driftX1 = myX
	driftY1 = myY
	local myRX, myRY, myRZ = getElementRotation(getPedOccupiedVehicle(gMe))
	local distVectorNorth = getDistanceBetweenPoints2D(driftX0, driftY0, driftX0, driftY1)
	local distVectorSpeed = getDistanceBetweenPoints2D(driftX0, driftY0, driftX1, driftY1)
	local cosinusDrift = distVectorNorth / distVectorSpeed
	local angelDriftBezwzgledny = math.acos(cosinusDrift)
	local angelDriftAbs = angelDriftBezwzgledny * (180 / math.pi)
	local angelDrift = math.floor(math.abs(angelDriftAbs - myRZ))
	if driftY1 < driftY0 and driftX1 < driftX0 then angelDrift = angelDrift + 90
	elseif driftY1 < driftY0 and driftX1 > driftX0 then angelDrift = angelDrift + 180
	elseif driftY1 > driftY0 and driftX1 > driftX0 then angelDrift = angelDrift + 270 end
		dxDrawText( "Drift angel: " ..angelDrift, math.floor(5*screenPlayerWidth), math.floor(580*screenPlayerHeight), screenWidth, screenHeight, tocolor ( 0, 255, 0, 255 ), 1, "default-bold" )
		dxDrawText( "angelDriftAbs: " ..angelDriftAbs, math.floor(5*screenPlayerWidth), math.floor(555*screenPlayerHeight), screenWidth, screenHeight, tocolor ( 0, 255, 0, 255 ), 1, "default-bold" )
		dxDrawText( "myRZ: " ..myRZ, math.floor(5*screenPlayerWidth), math.floor(530*screenPlayerHeight), screenWidth, screenHeight, tocolor ( 0, 255, 0, 255 ), 1, "default-bold" )
end
end
addEventHandler("onClientResourceStart", g_rot,
addEventHandler ('onClientRender', g_rot, uruchomDriftMeter)
)

Edited by Guest
Link to comment
local speedx,speedy = getElementVelocity(vehicle)
local px,py = getElementPosition(vehicle)
local nx,ny = speedx * 50 + px, speedy * 50 + py
local staticRot = ( 360 - math.deg ( math.atan2 ( ( nx - px ), ( ny - py ) ) ) ) % 360 
local _,_,currRot = getVehicleRotation(vehicle)
local driftAngle = math.abs(staticRot - currRot)

Link to comment
  • 2 weeks later...

thanks Buffalo, this is a much simpler way, but it works the same and in both cases are some angle extent is difficult to eliminate confusion ...

Now I use the mix of the two method

local driftPlayerVehicle = getPedOccupiedVehicle(gMe)
if getVehicleType(driftPlayerVehicle) == "Automobile" then 
	myX, myY = getElementPosition(gMe)
	local speedXdrift, speedYdrift, speedZdrift = getElementVelocity (driftPlayerVehicle)
	local driftSpeed = math.sqrt(speedXdrift^2 + speedYdrift^2 + speedZdrift^2) * 161
--2paq method
	local driftX0 = driftX1
	local driftY0 = driftY1
	driftX1 = myX
	driftY1 = myY
	local myRX, myRY, myRZ = getElementRotation(driftPlayerVehicle)
	local distVectorNorth = getDistanceBetweenPoints2D(driftX0, driftY0, driftX0, driftY1)
	local distVectorSpeed = getDistanceBetweenPoints2D(driftX0, driftY0, driftX1, driftY1)
	local cosinusDrift = distVectorNorth / distVectorSpeed
	local angelDriftBezwzgledny = math.acos(cosinusDrift)
	local angelDriftAbs = angelDriftBezwzgledny * (180 / math.pi)
--Inclusion of a quadrant of the coordinate system
	if driftY1 < driftY0 and driftX1 < driftX0 then angelDriftAbs = 180 - angelDriftAbs
	elseif driftY1 < driftY0 and driftX1 > driftX0 then angelDriftAbs = 180 + angelDriftAbs
	elseif driftY1 > driftY0 and driftX1 > driftX0 then angelDriftAbs = 360 - angelDriftAbs end
	local angelDrift = math.floor(math.abs(myRZ - angelDriftAbs))
--Inclusion of a quadrant of the coordinate system and direction of drifting
	if angelDrift > 5 then
		if driftY1 > driftY0 and driftX1 < driftX0 and myRZ > 180 then angelDrift = angelDrift - 180
		elseif driftY1 > driftY0 and driftX1 > driftX0 and myRZ < 180 then angelDrift = angelDrift - 180 end
	end
--Buffalo method
	local nxDrift,nyDrift = speedXdrift * 50 + myX, speedYdrift * 50 + myY
	local staticRotInDrift = ( 360 - math.deg ( math.atan2 ( ( nxDrift - myX ), ( nyDrift - myY ) ) ) ) % 360
	local angelDriftBuffalo = math.floor(math.abs(staticRotInDrift - myRZ))
--Restrictions 2paq method
	if driftSpeed <= 25 or (angelDrift/driftSpeed) >= 1.5 then angelDrift = 0 
	elseif angelDrift <= 5 then angelDrift = 0 end
--Restrictions Buffalo method
	if driftSpeed <= 25 or (angelDriftBuffalo/driftSpeed) >= 1.5 then angelDriftBuffalo = 0 
	elseif angelDriftBuffalo <= 5 then angelDriftBuffalo = 0 end
--Restrictions
	if math.abs(angelDriftBuffalo - angelDrift) > 15 then angelDrift = 0 end
	if myRX > 30 or myRY > 30 then angelDrift = 0 end
	angelDrift = ( angelDrift + angelDriftBuffalo ) / 2
	if driftSpeed < 1 or driftSpeed > 350 then driftSpeed = 0 end
--Time interval between drifts ~3s (limit FPS = 36)
	if angelDrift >= 18 then
		driftTime = 150
	elseif angelDrift > 5 then
		driftTimeTP = 150
	end
	if driftTime >= 0 then driftTime = driftTime - 1 end
	if driftTimeTP >= 0 then driftTimeTP = driftTimeTP - 1 end
--Reset factor and temporary points after a time
	if driftTime == 0 or driftTimeTP == 0 then
		if bestDriftPoints < driftPointsTEMP then bestDriftPoints = driftPointsTEMP end
		driftFactor = 1
		driftPointsTEMP = 0
		driftKolorR = 255
		driftKolorG = 255
		driftKolorB = 0
--Icrement factor 1s after last drift (limit FPS = 36)
	elseif driftTime == 132 and driftCpTimer > 0 and driftFactor <= 12 then
		driftFactor = driftFactor + 1
		if driftFactor > 11 then
			driftKolorR = 255
			driftKolorB = 0
		elseif driftFactor > 8 then
			driftKolorB = driftKolorB + 51
		elseif driftFactor > 6 then
			driftKolorB = driftKolorB + 51
			driftKolorG = driftKolorG - 51
		elseif driftFactor > 3 then
			driftKolorG = driftKolorG - 51
			driftKolorR = driftKolorR - 51
		elseif driftFactor > 1 then
			driftKolorR = driftKolorR - 51
		end
	end
	if driftCpTimer > 0 then
		driftCpTimer = driftCpTimer - 1
	else driftFactor = 0 end
--Restriction against donuts one way
	if ((myRZ > 55 and myRZ < 65) or (myRZ > 175 and myRZ < 185) or (myRZ > 295 and myRZ < 305)) and math.abs(myRZ_poprzedni - myRZ) > 2 then
		if myRZ_poprzedni > myRZ then
			antyBonczek = antyBonczek + 1
		else
			antyBonczek = antyBonczek - 1
		end
	end
--		dxDrawText( "antyBonczek: " ..antyBonczek,  math.floor(40*screenPlayerWidth),  math.floor(200*screenPlayerHeight), screenWidth, screenHeight, tocolor ( 0, 255, 0, 255 ), 1, "pricedown")
	myRZ_poprzedni = myRZ
	if math.abs(antyBonczek) > 14 then
		driftFactorAntyBonczek = 0
		antyBonczekTimer = 36 * 60
		antyBonczek = 0
		driftFactor = 0
		driftPointsTEMP = 0
	end
	if antyBonczekTimer > 0 then
		antyBonczekTimer = antyBonczekTimer - 1
		dxDrawText( "DRIFT x0 (" ..antyBonczekTimer.. ")",  math.floor(5*screenPlayerWidth),  math.floor(557*screenPlayerHeight), screenWidth, screenHeight, tocolor ( 255, 255, 0, 255 ), 1, "pricedown")
	end
	if antyBonczekTimer == 2 then
		antyBonczek = 0
		driftFactorAntyBonczek = 1
	end
--points to view and remember
	local vehDriftHealth = getElementHealth(getPedOccupiedVehicle(gMe))
	if vehDriftHealth and vehDriftHealth > 1 and playerIsNotSpawn == 1 then
		local thisRenderDriftPoints = (driftSpeed * angelDrift * driftFactor * driftFactorAntyBonczek)/850
		driftPointsTEMP = driftPointsTEMP + thisRenderDriftPoints
		driftPoints = driftPoints + (thisRenderDriftPoints/333)
		allDriftPoints = allDriftPoints + thisRenderDriftPoints
	end

Link to comment
  • 1 month later...
  • 1 year later...

Now I trying to develop my script, I want to add a table of results combined with a map

How to save a two-dimensional table ">>>newtableDriftRecords<<<" to the database?

See the third part of code line 32

addEvent('onMapStarting') 
addEventHandler('onMapStarting', g_rot, 
    function(mapInfo) 
        thisDriftMapName = "drift_records-" ..mapInfo.name 
        executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
    end 
) 

addEvent('onThisIsMyDriftPoints', true) 
addEventHandler('onThisIsMyDriftPoints', g_rot , 
    function (allPlayerDriftPoints, bestPlayerDriftPoints, the_source) 
        local namePlayerDM = getPlayerName ( the_source ) 
        local playerDriftRecords = executeSQLSelect( thisDriftMapName, "drift_record", "best_drift", "record_day", "player_nick='"..namePlayerDM.."'"  ) 
        if ( type( playerDriftRecords ) == "table" and #playerDriftRecords == 0 ) or not playerDriftRecords then 
            writeRecordToTable(namePlayerDM, allPlayerDriftPoints, bestPlayerDriftPoints, 0) 
        elseif type( playerDriftRecords ) == "table"  then 
            if allPlayerDriftPoints > playerDriftRecords[1]['drift_record']then 
                writeRecordToTable(namePlayerDM, allPlayerDriftPoints, bestPlayerDriftPoints, 1) 
            end 
        end 
    end 
) 

function writeRecordToTable(driftNick, allDriftPoints, bestDrift, old_record) 
    local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick", "drift_record", "best_drift", "record_day" ) 
    local newtableDriftRecords = tableDriftRecords 
    if ( type( tableDriftRecords ) == "table" and #tableDriftRecords == 0 ) or not tableDriftRecords then 
        outputConsole("nie ma tabeli tableDriftRecords" ) 
    elseif type( tableDriftRecords ) == "table"  then 
        local i = 1 
        while tableDriftRecords[i]['drift_record'] > allDriftPoints do 
            i = i + 1 
        end 
        local playerIdOldRecord = -1 
        if old_record == 1 then 
            playerIdOldRecord = 1 
            while tableDriftRecords[i]['player_nick'] ~= driftNick do 
                playerIdOldRecord = playerIdOldRecord + 1 
            end 
        end 
        newtableDriftRecords[i]['player_nick'] = driftNick 
        newtableDriftRecords[i]['drift_record'] = allDriftPoints 
        newtableDriftRecords[i]['best_drift'] = bestDrift 
        newtableDriftRecords[i]['record_day'] = getRealDateTimeNowString() 
        local k 
        while tableDriftRecords[i] do 
            i = i + 1 
            k = i - 1 
            if playerIdOldRecord == k then break end 
            newtableDriftRecords[i]['player_nick'] = newtableDriftRecords[k]['player_nick'] 
            newtableDriftRecords[i]['drift_record'] = newtableDriftRecords[k]['drift_record'] 
            newtableDriftRecords[i]['best_drift'] = newtableDriftRecords[k]['best_drift'] 
            newtableDriftRecords[i]['record_day'] = newtableDriftRecords[k]['record_day'] 
        end 
        This table ">>>[b]newtableDriftRecords[/b]<<<" to database! --NEED HELP 
    end 
end 
function getRealDateTimeNowString() 
    return getRealDateTimeString( getRealTime() ) 
end 
function getRealDateTimeString( time ) 
    return string.format( '%04d-%02d-%02d %02d:%02d:%02d' 
                        ,time.year + 1900 
                        ,time.month + 1 
                        ,time.monthday 
                        ,time.hour 
                        ,time.minute 
                        ,time.second 
                        ) 
end 

Link to comment

I want to save the table this way:

    executeSQLDropTable( thisDriftMapName ) 
        local n = 1 
        while newtableDriftRecords[n] do 
            executeSQLInsert( thisDriftMapName, "'"..newtableDriftRecords[n]['player_nick'].."','"..newtableDriftRecords[n]['drift_record'].."','"..newtableDriftRecords[n]['best_drift'].."','"..newtableDriftRecords[n]['record_day'].."'" ) 
            n = n + 1 
        end 

I can't check it because appeared an error:

ERROR: 2paq_driftmeter\driftmeter_s.lua:46: Database query failed: near "-": syntax error

:arrowdown::arrow: second part of code line 5 (earlier post)

local playerDriftRecords = executeSQLSelect( thisDriftMapName, "drift_record", "best_drift", "record_day", "player_nick='"..namePlayerDM.."'"  ) 

ERROR: 2paq_driftmeter\driftmeter_s.lua:109: Database query failed: near "-": syntax error

:arrowdown::arrow: third part of code line 2 (earlier post)

local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick", "drift_record", "best_drift", "record_day" ) 

I have also question, how the function works:

getMapName() 

I used it here and returns "none"

addEventHandler('onResourceStart', g_ResRoot, 
    function() 
        thisDriftMapName = "drift_records-" ..getMapName() 
        executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
    end 

Resource starts by running from file "meta.xml" map

Link to comment

ERROR: unzipped\2paq_driftmeter\driftmeter_s.lua:>>>11<<<: attempt to concatenate field 'drift_record' (a nil value) 

addEvent('onMapStarting') 
addEventHandler('onMapStarting', g_rot, 
 function(mapInfo, mapOptions, gameOptions) 
  thisDriftMapName = "drift_records_on_map_" ..mapInfo.name 
  executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
 end 
) 
function writeRecordToTable(driftNick, allDriftPoints, bestDrift, old_record) 
 local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick", "drift_record", "best_drift", "record_day" ) 
 if type( tableDriftRecords ) == "table" and #tableDriftRecords > 0 then 
  outputDebugString("Test: " ..tableDriftRecords[1]['drift_record'], 3, 0, 255, 255) 
 end 
end 

ocokamany.jpg

Edit: :oops:

local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick, drift_record, best_drift, record_day" ) 

Link to comment

Why do something that works if checking the "outputDebug", in the loop "while" dosen't?

outputDebugString("Wartość [1].drift_record: " ..tableDriftRecords[1].drift_record, 3, 0, 255, 255) 

[2011-01-13 18:56:01] INFO: Wartość [1].drift_record: 15836.505859375
local i = 1 
outputDebugString("Wartość [i][drift_record]: " ..tableDriftRecords[i]['drift_record'], 3, 0, 255, 255) 

[2011-01-13 18:56:01] INFO: Wartość [drift_record]: 15836.505859375

local i = 1 
local playerIdOldRecord = -1 
while tableDriftRecords[i]['drift_record'] > allDriftPoints do 
   i = i + 1 
end 

[2011-01-13 18:56:01] ERROR: unzipped\2paq_driftmeter\driftmeter_s.lua:>>>3 line(code above)<<<: attempt to index field '?' (a nil value)
Link to comment

1. I change something in one table (copy of original), and this change also my second table (original)?!?!

2. Why only one repeat loops "repeat-until"?

tabelaprzed.png

function writeRecordToTable(driftNick, allDriftPoints, bestDrift, old_record) 
    --wczytanie tabeli dwuwymiarowej 
    outputDebugString("Algorytm wpisu do BD", 3, 0, 0, 255) 
    local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick, drift_record, best_drift, record_day" ) 
    if not tableDriftRecords then 
        outputDebugString("Brak tabeli z rekordami driftowymi!!!", 3, 255, 0, 0) 
    elseif type( tableDriftRecords ) == "table"  then 
        local newtableDriftRecords = {} 
        if #tableDriftRecords > 0 then 
            outputDebugString("Ilosc wierszy w bazie danych: " ..#tableDriftRecords, 3, 0, 255, 255) 
            outputDebugString("Wartość allDriftPoints: " ..allDriftPoints, 3, 0, 255, 255) 
            newtableDriftRecords = tableDriftRecords 
            local i = 1 
            outputDebugString("Wartość [i][drift_record]: " ..tableDriftRecords[i]['drift_record'], 3, 0, 255, 255) 
            local playerIdOldRecord = -1 
            --początek procedury testowej 
            for i_key, i_value in pairs(tableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w tableDriftRecords (IPT)", 3, 0, 255, 255) 
            end 
            for i_key, i_value in pairs(newtableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w newtableDriftRecords (IPT)", 3, 0, 255, 255) 
            end 
            --koniec procedury testowej 
            for i_key, i_value in pairs(tableDriftRecords) do 
                if i_value['drift_record'] < allDriftPoints then 
                    i = i_key 
                    outputDebugString("Znaleziono i=" ..i, 3, 0, 255, 255) 
                    break 
                end 
                outputDebugString(i_value['player_nick'].. " ma więcej punktów!", 3, 0, 255, 255) 
            end 
            if old_record == 1 then 
                outputDebugString("Szukam old_record", 3, 0, 255, 255) 
                for j_key, j_value in pairs(tableDriftRecords) do 
                    if j_value['player_nick'] == driftNick then 
                        playerIdOldRecord = j_key 
                        outputDebugString("Znaleziono playerIdOldRecord=" ..playerIdOldRecord, 3, 0, 255, 255) 
                        break 
                    end 
                end 
            end 
            newtableDriftRecords[i]['player_nick'] = driftNick 
            newtableDriftRecords[i]['drift_record'] = allDriftPoints 
            newtableDriftRecords[i]['best_drift'] = bestDrift 
            newtableDriftRecords[i]['record_day'] = getRealDateTimeNowString() 
            --początek procedury testowej 
            for i_key, i_value in pairs(tableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w tableDriftRecords (IIPT)", 3, 0, 255, 255) 
            end 
            for i_key, i_value in pairs(newtableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w newtableDriftRecords (IIPT)", 3, 0, 255, 255) 
            end 
            --koniec procedury testowej 
            local k 
            repeat 
                outputDebugString("newtableDriftRecords[i][player_nick]: " ..newtableDriftRecords[i]['player_nick'], 3, 0, 255, 255) 
                k = i 
                i = i + 1 
                outputDebugString("tableDriftRecords[k][player_nick]: " ..tableDriftRecords[k]['player_nick'], 3, 0, 255, 255) 
                outputDebugString("k=" ..k.. " & i=" ..i.. " (wierszy: " ..#tableDriftRecords.. ")", 3, 0, 255, 255) 
                if playerIdOldRecord == k then break end 
                if not newtableDriftRecords[i] then 
                    newtableDriftRecords[i] = { 
                        ['player_nick'] = tableDriftRecords[k]['player_nick'], 
                        ['drift_record'] = tableDriftRecords[k]['drift_record'], 
                        ['best_drift'] = tableDriftRecords[k]['best_drift'], 
                        ['record_day'] = tableDriftRecords[k]['record_day'] 
                    } 
                else 
                    newtableDriftRecords[i]['player_nick'] = tableDriftRecords[k]['player_nick'] 
                    newtableDriftRecords[i]['drift_record'] = tableDriftRecords[k]['drift_record'] 
                    newtableDriftRecords[i]['best_drift'] = tableDriftRecords[k]['best_drift'] 
                    newtableDriftRecords[i]['record_day'] = tableDriftRecords[k]['record_day'] 
                end 
            until k <= #tableDriftRecords 
            --początek procedury testowej 
            for i_key, i_value in pairs(tableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w tableDriftRecords (IIIPT)", 3, 0, 255, 255) 
            end 
            for i_key, i_value in pairs(newtableDriftRecords) do 
                outputDebugString(i_value['player_nick'].. " to " ..i_key.. " nick w newtableDriftRecords (IIIPT)", 3, 0, 255, 255) 
            end 
            --koniec procedury testowej 
            executeSQLDropTable( thisDriftMapName ) 
            executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
            local n = 1 
            while newtableDriftRecords[n] do 
                executeSQLInsert( thisDriftMapName, "'"..newtableDriftRecords[n]['player_nick'].."','"..newtableDriftRecords[n]['drift_record'].."','"..newtableDriftRecords[n]['best_drift'].."','"..newtableDriftRecords[n]['record_day'].."'" ) 
                outputDebugString("Wpis nr: " ..n.. " do tabeli: " ..newtableDriftRecords[n]['player_nick'], 3, 0, 255, 0) 
                n = n + 1 
            end 
        else 
            outputDebugString("Wpis bezpośredni", 3, 0, 255, 0) 
            executeSQLInsert( thisDriftMapName, "'"..driftNick.."','"..allDriftPoints.."','"..bestDrift.."','"..getRealDateTimeNowString().."'" ) 
        end 
    end 
end 

[2011-01-15 20:04:23] Creating new DB table drift_records_on_map_Drift

[2011-01-15 20:06:19] INFO: Nowy wpis

[2011-01-15 20:06:19] INFO: Algorytm wpisu do BD

[2011-01-15 20:06:19] INFO: Ilosc wierszy w bazie danych: 6

[2011-01-15 20:06:19] INFO: Wartość allDriftPoints: 7580.720703125

[2011-01-15 20:06:19] INFO: Wartość [drift_record]: 6000

[2011-01-15 20:06:19] INFO: Keny Block to 1 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Polody to 2 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w tableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Keny Block to 1 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Polody to 2 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w newtableDriftRecords (IPT)

[2011-01-15 20:06:19] INFO: Znaleziono i=1

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 1 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Polody to 2 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w tableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 1 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Polody to 2 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w newtableDriftRecords (IIPT)

[2011-01-15 20:06:19] INFO: newtableDriftRecords[player_nick]: [2RT]acp___(PL)

[2011-01-15 20:06:19] INFO: tableDriftRecords[k][player_nick]: [2RT]acp___(PL)

[2011-01-15 20:06:19] INFO: k=1 & i=2 (wierszy: 6)

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 1 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 2 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w tableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 1 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: [2RT]acp___(PL) to 2 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: Wojak to 3 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: demon to 4 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: DeTeR to 5 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] INFO: Trystek to 6 nick w newtableDriftRecords (IIIPT)

[2011-01-15 20:06:19] Dropping DB table drift_records_on_map_Drift

[2011-01-15 20:06:19] Creating new DB table drift_records_on_map_Drift

[2011-01-15 20:06:19] INFO: Wpis nr: 1 do tabeli: [2RT]acp___(PL)

[2011-01-15 20:06:19] INFO: Wpis nr: 2 do tabeli: [2RT]acp___(PL)

[2011-01-15 20:06:19] INFO: Wpis nr: 3 do tabeli: Wojak

[2011-01-15 20:06:19] INFO: Wpis nr: 4 do tabeli: demon

[2011-01-15 20:06:19] INFO: Wpis nr: 5 do tabeli: DeTeR

[2011-01-15 20:06:19] INFO: Wpis nr: 6 do tabeli: Trystek

tabelapo.png

Link to comment

Text shows me when clicked , but it will not disappear after the next , you need a few times clicked

function showMeDriftScoreFromDB() 
    if not renderujeTabeleTopDriftersDB then 
        triggerServerEvent ( "giveMeDriftScoreFromDB", g_rot, gMe ) 
        renderujeTabeleTopDriftersDB = true 
    else 
        removeEventHandler('onClientRender', g_rot, pokazTopDriftersFromDB) 
        setTimer( 
            function() 
                renderujeTabeleTopDriftersDB = false 
            end, 
        1000, 1) 
    end 
end 

Link to comment

I need to replace characters: " ", "[", "]" in the name of the map

addEvent('onMapStarting') 
addEventHandler('onMapStarting', g_rot, 
    function(mapInfo, mapOptions, gameOptions) 
        thisDriftMapName = string.gsub( "drift_records_on_map_" ..mapInfo.name, " ", "_" ) 
        thisDriftMapName = string.gsub( thisDriftMapName, "[", "_" ) 
        thisDriftMapName = string.gsub( thisDriftMapName, "]", "_" ) 
        executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
        setTimer(selectDriftScoreFromDB, 1000, 1, "all") 
    end 
) 

[2011-01-22 19:01:40] ERROR: unzipped\2paq_driftmeter\driftmeter_s.lua:5: malformed pattern (missing ']')

What am I doing wrong?

Link to comment

I tried to play around with removing [ and ] with gsub and it seems that it expects a real pattern when using [ or ] ...

Try this:

addEvent('onMapStarting') 
addEventHandler('onMapStarting', g_rot, 
    function(mapInfo, mapOptions, gameOptions) 
        thisDriftMapName = string.gsub( "drift_records_on_map_" ..mapInfo.name, " ", "_" ) 
        thisDriftMapName = string.gsub( thisDriftMapName, "[\]\[]", "_" ) 
        executeSQLCreateTable ( thisDriftMapName, "player_nick TEXT, drift_record INTEGER, best_drift INTEGER, record_day TEXT" ) 
        setTimer(selectDriftScoreFromDB, 1000, 1, "all") 
    end 
) 

Link to comment

It works!

If someone wanted to use this (replace character in string), it better (looks better) to remove the sign:

thisDriftMapName = "[TR]Musles" 
thisDriftMapName = string.gsub( thisDriftMapName, "[\]\[]", "" ) 
outputDebugString( "string.gsub [\]\[]: " ..thisDriftMapName, 3, 155, 155, 155) ) 

INFO: string.gsub [][]: TRMuscles

Solving the problem with copying tables

I change something in one table (copy of original), and this change also my second table (original)?!?!

viewtopic.php?p=334970#p334970

A table is always anonymous. There is no fixed relationship between a variable that holds a table and the table itself: a = {} a["x"] = 10 b = a -- `b' refers to the same table as `a' print(b["x"]) --> 10 b["x"] = 20 print(a["x"]) --> 20 a = nil -- now only `b' still refers to the table b = nil -- now there are no references left to the table When a program has no references to a table left, Lua memory management will eventually delete the table and reuse its memory.
local tableDriftRecords = executeSQLSelect( thisDriftMapName, "player_nick, drift_record, best_drift, record_day" ) 
local newtableDriftRecords = {} 
  for i_key, i_value in pairs(tableDriftRecords) do 
    newtableDriftRecords[i_key] = { 
      ['player_nick'] = i_value['player_nick'], 
      ['drift_record'] = i_value['drift_record'], 
      ['best_drift'] = i_value['best_drift'], 
      ['record_day'] = i_value['record_day'] 
    } 
  end 

Link to comment
  • 3 weeks later...

This should get it, but I'm not sure

local name = the_map_name -- fill in this bit 
local times = executeSQLQuery("race maptimes Sprint " .. name, "timeMS") 

That should have been executeSQLSelect ^

Edited by Guest
Link to comment

well, I took a quick look in race_toptimes and this is how it creates the table names:

function SMaptimes:makeDatabaseTableName( raceModeName, mapName ) 
    return 'race maptimes ' .. raceModeName .. ' ' .. mapName 
end 

raceModeName and mapName are received from the onMapStarting event from race, so I don't think there are any problems with it (I never had problems with it)

The problem you had was with gsub, [ and ] are used in patterns

Link to comment

It is exactly as I thought :( , it's not so easy to read this table :cry:

How in resource race-toptimes they create, read and write table with special characters: " ", "-", "[", "]" :?::?

local g_rot = getRootElement() 
local sprintMapTimes = 0 
  
addEventHandler('onMapStarting', g_rot, 
  function(mapInfo, mapOptions, gameOptions) 
    timesFromSprintTop10 = executeSQLQuery("race maptimes Sprint " ..mapInfo.name, "timeMS") 
    if not timesFromSprintTop10 then 
      sprintMapTimes = false 
      outputDebugString("No TopTimes table!!!", 3, 255, 0, 0) 
    elseif type( timesFromSprintTop10 ) == "table" and #timesFromSprintTop10 > 0 then 
      for top_key, top_value in pairs(timesFromSprintTop10) do 
        if top_key == 11 then break end 
          sprintMapTimes = sprintMapTimes + top_value 
          if not timesFromSprintTop10[top_key + 1]['timeMS'] then 
             sprintMapTimes = sprintMapTimes / top_key 
           end 
        outputDebugString("sprintMapTimes = " ..sprintMapTimes, 3, 0, 255, 255) 
      end 
    else 
      sprintMapTimes = false 
      outputDebugString("No rows in table TopTimes!!!", 3, 255, 0, 0) 
  end 
) 

ERROR: 2paq_driftmeter\driftmeter_s.lua:6: Database query failed: near "race": syntax error 
INFO: No TopTimes table!!! 

Link to comment
Oops, my bad, I meant to use executeSQLSelect in my post...

So it's like ccw says, or using executeSQLSelect:

local tableName = "race maptimes Sprint " .. mapInfo.name 
local result = executeSQLSelect( tableName, "timeMS"  ) 

I also should notice that it should be executeSQLSelect, but it does not work ... :?

ERROR: 2paq_driftmeter\driftmeter_s.lua:12: Database query failed: near "Sprint": syntax error 

    local tableName = "race maptimes Sprint " ..mapInfo.name 
timesFromSprintTop10 = executeSQLQuery( "SELECT * FROM ?", tableName ) 

work, but I do not know yet what it returns :oops: I know that table - but do not know how to indexed

for top_key, top_value in pairs(timesFromSprintTop10) do 
  if top_key == 11 then break end 
  sprintMapTimes = sprintMapTimes + top_value['timeMS'] 
  if not timesFromSprintTop10[top_key + 1]['timeMS'] then 
    sprintMapTimes = sprintMapTimes / top_key 
  end 
  outputDebugString("sprintMapTimes = " ..sprintMapTimes, 3, 0, 255, 255) 
end 

ERROR: unzipped\2paq_driftmeter\driftmeter_s.lua:3: attempt to perform arithmetic on field 'timeMS' (a nil value)

Edit:

I added a line debugger

outputDebugString("rowid 1 timeMS timesFromSprintTop10: " ..timesFromSprintTop10[1]['timeMS'], 3, 0, 255, 255) 

ERROR: unzipped\2paq_driftmeter\driftmeter_s.lua:1: attempt to concatenate field 'timeMS' (a nil value)
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...