salve, sono nuovo nel forum.
Ho una base pittosto scarsa in programmazione e avrei una serie di problemi nella realizzazione di una funzione.
Sto realizzando un controllo per l'afk per un server race.
Il problema è che non riesco a rilevare se la gara è in svolgimento o se è finita (lato client).
Allego il codice che ho scritto/modificato da una resource già esistente. La funzione isRaceStarted() è quella che devo creare.
CLIENT
local MaxAFKtime = 30
local warnTime = 20
local v = 0
local AFKtime = 0
local sx,sy = guiGetScreenSize()
local wEvent = false
function rWarn()
dxDrawImage( sx/2 - 300, sy/2 - 100, 600, 200, 'warn.png' )
end
function stopWarn()
if(wEvent) then
removeEventHandler("onClientRender", getRootElement(), rWarn)
wEvent = false
end
end
function checkMain()
if isPedInVehicle ( getLocalPlayer() ) and isRaceStarted() then
local v = getElementVelocity ( getPedOccupiedVehicle ( getLocalPlayer() ) )
outputChatBox ( "cacca")
if(v == 0 or wEvent) then
aTimeAdd()
else
aTimeClear()
end
end
end
function aTimeAdd()
AFKtime = AFKtime + 0.1
if(AFKtime >= warnTime and AFKtime < MaxAFKtime) then
triggerServerEvent("afkSlap",getLocalPlayer(), 2)
addEventHandler("onClientRender", getRootElement(), rWarn)
if(not wEvent) then
wEvent = true
end
elseif(AFKtime >=MaxAFKtime) then
--triggerServerEvent ( "afkSlap", getLocalPlayer(), 2)
--triggerServerEvent ( "afkWarn", getLocalPlayer(), 1)
end
end
function aTimeClear()
AFKtime = 0
end
function afkClear(key, keyState)
if(wEvent)then
stopWarn()
aTimeClear()
end
end
setTimer ( checkMain, 100, -1)
function onClientLoaded(res)
bindKey ("space", "down", afkClear)
triggerServerEvent("onClientLoaded",getLocalPlayer())
end
addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()),onClientLoaded)
function loadSettings(maxtime, wtime )
MaxAFKtime = tonumber(maxtime)
warnTime = tonumber(wtime)
end
addEvent( "onSettingsReady", true )
addEventHandler( "onSettingsReady", getRootElement(), loadSettings )
SERVER
local raceState = ""
local enableMessages = true
local messageColor = "#FF5555"
function afkKill()
if(getResourceState ( getResourceFromName("race") ) == "running") then
local isFinished = call(getResourceFromName("race"), "isPlayerFinished", source)
if(not isFinished and raceState == "Running") then
setElementHealth(source, 0)
end
end
end
addEvent( "afkSlap", true )
addEventHandler( "afkSlap", getRootElement(), afkKill)
function changeRaceState(newStateName)
raceState = newStateName
end
addEvent("onRaceStateChanging", true)
addEventHandler( "onRaceStateChanging", getRootElement(), changeRaceState)
function clientLoaded()
local msgenable = get( "msgenable" )
local msgcol = get( "msgcolor" )
if(msgenable) then
if(msgenable == "true") then
enableMessages = true
else
enableMessages = false
end
if(msgcol) then
messageColor = msgcol
end
end
local maxtime = get( "maxafktime" )
local wtime = get( "warnTime" )
triggerClientEvent ( "onSettingsReady", getRootElement(), maxtime, wtime)
end
addEvent("onClientLoaded", true)
addEventHandler( "onClientLoaded", getRootElement(), clientLoaded)
Se ci dovessero essere ottimizazzioni, saranno ben accettate.
La resource sarà poi disponibile nella community