xMKHx

Remaining Time

Recommended Posts

Hello, 

i'm working on a VIP Script, its important in RPG Servers

Well i made the full script, i can give anyone vip and remove it

but still one thing, its the timer, peoples call it vip hours!

I want to make an expired date or something like that, i mean i will give someone 100 hours of vip, and after 100 hours the vip will be removed from him, i think you understand what i mean

Share this post


Link to post

well you can use 

local time = getRealTime()
local timestamp = time.timestamp 
or 
getTickCount()
setTime

 

  • Like 1

Share this post


Link to post

An easy way, set the player's account data as 'vip' into true and at the same time set a timer to set the player's account data as 'vip' into false. This will only work properly if the resource doesn't get restarted (due to server restart or resource restart) so timers will still exist.

Other way, which is simple and easy but harder than the first is to set the 'vip' data as a number which presents the time of seconds he has as a vip and set a timer to reduce the 'vip' data value by 1 every second, and whenever the value is higher than 0, the player can use the vip, no otherwise. (As long as the account data 'vip' doesn't get touched, this will work perfectly even when server crashs or something)

Share this post


Link to post
16 minutes ago, Dimos7 said:

well you can use 


local time = getRealTime()
local timestamp = time.timestamp 
or 
getTickCount()
setTime

Actually i liked your first method but can you give me an example to how to use it

 

12 minutes ago, SuperCroz said:

An easy way, set the player's account data as 'vip' into true and at the same time set a timer to set the player's account data as 'vip' into false. This will only work properly if the resource doesn't get restarted (due to server restart or resource restart) so timers will still exist.

Other way, which is simple and easy but harder than the first is to set the 'vip' data as a number which presents the time of seconds he has as a vip and set a timer to reduce the 'vip' data value by 1 every second, and whenever the value is higher than 0, the player can use the vip, no otherwise. (As long as the account data 'vip' doesn't get touched, this will work perfectly even when server crashs or something)

I tried it before i post this topic but nothing, not working :/ !

 

Share this post


Link to post

for example if you want 10 h someone have vip you can have it like this 

local time = getRealTime()
local timestamp = time.timestamp
timestamp+10*3600

ofc you put that when you add them to vip

Share this post


Link to post
19 hours ago, Dimos7 said:

for example if you want 10 h someone have vip you can have it like this 


local time = getRealTime()
local timestamp = time.timestamp
timestamp+10*3600

ofc you put that when you add them to vip

Not working dude :/

 

Share this post


Link to post
function onResourceStart ()
	executeSQLQuery("CREATE TABLE IF NOT EXISTS vip (account TEXT, hours INT)")
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

function isPlayerVIP (player)
	local account = getAccountName(getPlayerAccount(player))
	local query = executeSQLQuery("SELECT * FROM vip WHERE account=?", account)
	if (query) and (#query > 0) then
		return true
	else
		return false
	end
end

function getPlayerVIPHours (player)
	if (isPlayerVIP(player)) then
		local account = getAccountName(getPlayerAccount(player))
		local query = executeSQLQuery("SELECT * FROM vip WHERE account=?", account)
		for k,v in ipairs (query) do
			vHours = v.hours
		end
		return vHours
	end
end

function myHours (player)
if (isPlayerVIP(player) == false) then
return exports.chgnote:createNote("You are not VIP", player, 255, 0, 0)
end
local hours = getPlayerVIPHours(player)
exports.chgnote:createNote("You have "..hours.." VIP Hours", player, 0, 255, 0)
end
addCommandHandler("viphours", myHours)


function addVIP (player, cmd, newVIP, hours)
local target = getPlayerFromNick ( newVIP )
local adderName = getPlayerName(player)
local targetN = getPlayerName( target )
if (target == false) then
return exports.chgnote:createNote("There is no player with that name !", player, 255, 0, 0)
end
if ( hours == false) then
return exports.chgnote:createNote("Please insert a valid hours !", player, 255, 0, 0)
end
	if (isPlayerVIP(target) == false) then
		if cmd == "addvip" then
			if targetN then
				if hours then
				local time = getRealTime()
					local timestamp = time.timestamp
					local vHours = timestamp+hours*3600
					outputChatBox(adderName.." gave "..targetN.." "..vHours.." VIP Hours !", root, 0, 255, 0)
					local acc = getAccountName(getPlayerAccount(target))
					executeSQLQuery("INSERT INTO vip(account,hours) VALUES(?,?)", acc, vHours)
					setElementData(target, "VIP", "Yes")
				end
			end
		end
	end
end
	addCommandHandler("addvip", addVIP)
	

not completed

Edited by xMKHx

Share this post


Link to post

yes after you need to remove the remain time with the current time so if have give 10 hours 

local time= getRealTime()
local timestamp = time.timestamp
local vhours = executeSQLQuery("Select hours from vip WHERE account =?", getAccountName(getPlayerAccount(target)))
if tonumber(vHours) > 0 then
   vhours - timestamp
else
  setElementData(target, "VIP", "NO")
end

 

Edited by Dimos7

Share this post


Link to post

 

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

function getPlayerVIPHours (player)
	if (isPlayerVIP(player)) then
		local account = getAccountName(getPlayerAccount(player))
		local query = executeSQLQuery("SELECT hours FROM vip WHERE account=?", account)
		local hours = round((tonumber (query[1].hours) - getRealTime ().timestamp)/3600, 2)
	    return hours
    end
end

 

Share this post


Link to post
function onResourceStart ()
	executeSQLQuery("CREATE TABLE IF NOT EXISTS vip (account TEXT, hours INT)")
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

function isPlayerVIP (player)
	local account = getAccountName(getPlayerAccount(player))
	local query = executeSQLQuery("SELECT * FROM vip WHERE account=?", account)
	if (query) and (#query > 0) then
		return true
	else
		return false
	end
end

function getPlayerVIPHours (player)
	if (isPlayerVIP(player)) then
		local account = getAccountName(getPlayerAccount(player))
		local query = executeSQLQuery("SELECT * FROM vip WHERE account=?", account)
		for k,v in ipairs (query) do
			vHours = v.hours
		end
		return vHours
	end
end
function endVIP (player)
setElementData(player, "VIP", "No")
local acc = getAccountName(getPlayerAccount(player))
executeSQLQuery("DELETE FROM vip WHERE account=?", acc)
end

function myHours (player)
if (isPlayerVIP(player) == false) then
return exports.chgnote:createNote("You are not VIP", player, 255, 0, 0)
end
local hours = getPlayerVIPHours(player)
local sec = hours / 60
local min = sec / 60
local hour = min / 1000
exports.chgnote:createNote("You have "..hour.." VIP Hours", player, 0, 255, 0)
exports.chgnote:createNote("You have "..min.." VIP Mins", player, 0, 255, 0)
exports.chgnote:createNote("You have "..sec.." VIP Secs", player, 0, 255, 0)
end
addCommandHandler("viphours", myHours)


function addVIP (player, cmd, newVIP, hours)
local target = getPlayerFromNick ( newVIP )
local adderName = getPlayerName(player)
local targetN = getPlayerName( target )
if (target == false) then
return exports.chgnote:createNote("There is no player with that name !", player, 255, 0, 0)
end
if ( hours == false) then
return exports.chgnote:createNote("Please insert a valid hours !", player, 255, 0, 0)
end
	if (isPlayerVIP(target) == false) then
		if cmd == "addvip" then
			if targetN then
				if hours then
					outputChatBox(adderName.." gave "..targetN.." "..hours.." VIP Hours !", root, 0, 255, 0)
					setElementData(target, "VIP", "Yes")
					local sec = 1000
					local min = 60 * 1000
					local hour = min * 60
					local playeraccount = getPlayerAccount ( target )
					remaining_time = setTimer(endVIP, hours * hour, 1, player)
					setAccountData(playeraccount, "VIP_Hours", getTimerDetails(remaining_time))
					local acc = getAccountName(getPlayerAccount(target))
					local remainingHours = getTimerDetails(remaining_time)
					if (remainingHours) then
					executeSQLQuery("INSERT INTO vip(account,hours) VALUES(?,?)", acc, remainingHours)
					outputChatBox("[Test] Remaining Time is: "..remainingHours , root, 255, 255, 0)
					end
				end
			end
		end
	end
end
	addCommandHandler("addvip", addVIP)
	

Well i used this method, as you can see at line 16 i added a function to know your vip hours remaining, but nothing if happening

@Sasu i'll try your method :D

Share this post


Link to post
On 8/20/2017 at 19:39, xMKHx said:

Hello, 

i'm working on a VIP Script, its important in RPG Servers

Well i made the full script, i can give anyone vip and remove it

but still one thing, its the timer, peoples call it vip hours!

I want to make an expired date or something like that, i mean i will give someone 100 hours of vip, and after 100 hours the vip will be removed from him, i think you understand what i mean

You could do one thing make a account data vip_hours in which you set a real time stamp(when you given a vip to a player) in millisecond  and then every time a player logs in to a account check it with the server time if the difference is greater than 100 then remove the vip use mta wiki for reference of getRealTime function :)

Edited by Ayush Rathore
  • Like 1

Share this post


Link to post
12 minutes ago, Ayush Rathore said:

You could do one thing make a account data vip_hours in which you set a real time stamp(when you given a vip to a player) in millisecond  and then every time a player logs in to a account check it with the server time if the difference is greater than 100 then remove the vip use mta wiki for reference of getRealTime function :)

You mean something like this

local time = getRealTime ()
local timestamp = time.timestamp
local remaining_hours = getPlayerVIPHours - timestamp

????

Share this post


Link to post
4 hours ago, Sasu said:

Did my method work?

No :/

anyone helpme please

Share this post


Link to post
15 hours ago, xMKHx said:

No :/

anyone helpme please

Did you check if the column 'hours' was succesfully created? Use an application to see the table.

Also in my script you can try to use outputChatBox to see what the variable 'hours' return.

Share this post


Link to post
31 minutes ago, Sasu said:

Did you check if the column 'hours' was succesfully created? Use an application to see the table.

Also in my script you can try to use outputChatBox to see what the variable 'hours' return.

Hours table already created, i use outputChatBox but no result the same time

Share this post


Link to post
local vip = {}
local minute = 60000
local hour = minute*60
local db = dbConnect("sqlite", "vip.db")

function resourceStart()
	dbExec(db, "CREATE TABLE IF NOT EXISTS player_vip (account TEXT, vip INT)")
	for i,v in pairs(getElementsByType("player")) do
		loadVIP(v)
	end
end
addEventHandler("onResourceStart", resourceRoot, resourceStart)

function resourceStop()
	for i,v in pairs(getElementsByType("player")) do
		saveVIP(v)
	end
end
addEventHandler("onResourceStop", resourceRoot, resourceStop)

function login()
	loadVIP(source)
end
addEventHandler("onPlayerLogin", getRootElement(), login)

function quit()
	saveVIP(source)
end
addEventHandler("onPlayerQuit", getRootElement(), quit)

function loadVIP(p)
	local account = getAccountName(getPlayerAccount(p))
	local result = dbPoll(dbQuery(db, "SELECT vip FROM player_vip WHERE account = ?", account), -1)
	vip[account] = {}
	if not result or (type(result) == "table" and #result == 0) then
		dbExec(db, "INSERT INTO player_vip (account, vip) VALUES(?, ?)", account, 0)
		vip[account].vip = 0
		return
	end
	vip[account].vip = result[1].vip
	vip[account].tick = getTickCount()
end

function saveVIP(p)
	local account = getAccountName(getPlayerAccount(p))
	dbExec(db, "UPDATE player_vip SET vip = ? WHERE account = ?", vip[account].vip, account)	
end

function vips(p)
	local account = getAccountName(getPlayerAccount(p))
	if vip[account].tick then
		local now = getTickCount() - vip[account].tick
		local final = round(now/hour, 1)
		outputChatBox(final)
		if now >= hour and vip[account].vip > 0 then
			vip[account].vip = vip[account].vip - final
		end
	end
	outputChatBox(vip[account].vip)
end
addCommandHandler("vip", vips)

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

did u try something like this? u don't have to use my database part i just made it to make the script work 

Share this post


Link to post
6 hours ago, kikos500 said:

local vip = {}local minute = 60000local hour = minute*60local db = dbConnect("sqlite", "vip.db")function resourceStart()	dbExec(db, "CREATE TABLE IF NOT EXISTS player_vip (account TEXT, vip INT)")	for i,v in pairs(getElementsByType("player")) do		loadVIP(v)	endendaddEventHandler("onResourceStart", resourceRoot, resourceStart)function resourceStop()	for i,v in pairs(getElementsByType("player")) do		saveVIP(v)	endendaddEventHandler("onResourceStop", resourceRoot, resourceStop)function login()	loadVIP(source)endaddEventHandler("onPlayerLogin", getRootElement(), login)function quit()	saveVIP(source)endaddEventHandler("onPlayerQuit", getRootElement(), quit)function loadVIP(p)	local account = getAccountName(getPlayerAccount(p))	local result = dbPoll(dbQuery(db, "SELECT vip FROM player_vip WHERE account = ?", account), -1)	vip[account] = {}	if not result or (type(result) == "table" and #result == 0) then		dbExec(db, "INSERT INTO player_vip (account, vip) VALUES(?, ?)", account, 0)		vip[account].vip = 0		return	end	vip[account].vip = result[1].vip	vip[account].tick = getTickCount()endfunction saveVIP(p)	local account = getAccountName(getPlayerAccount(p))	dbExec(db, "UPDATE player_vip SET vip = ? WHERE account = ?", vip[account].vip, account)	endfunction vips(p)	local account = getAccountName(getPlayerAccount(p))	if vip[account].tick then		local now = getTickCount() - vip[account].tick		local final = round(now/hour, 1)		outputChatBox(final)		if now >= hour and vip[account].vip > 0 then			vip[account].vip = vip[account].vip - final		end	end	outputChatBox(vip[account].vip)endaddCommandHandler("vip", vips)function round(num, numDecimalPlaces)  local mult = 10^(numDecimalPlaces or 0)  return math.floor(num * mult + 0.5) / multend

did u try something like this? u don't have to use my database part i just made it to make the script work 

Worked fine, but first the vip sets auto for all server accounts online and offline 2nd the resul in the db is 0

Share this post


Link to post

I had already tested, and added some features.

function onResourceStart ()
	executeSQLQuery("CREATE TABLE IF NOT EXISTS vip (account TEXT, hours INT)")
end
addEventHandler("onResourceStart", resourceRoot, onResourceStart)

function isPlayerVIP (player)
	local account = getAccountName(getPlayerAccount(player))
	local query = executeSQLQuery("SELECT * FROM vip WHERE account=?", account)
	if (query) and (#query > 0) then
		return true
	else
		return false
	end
end


function getPlayerVIPHours (player)
	if (isPlayerVIP(player)) then
		local account = getAccountName(getPlayerAccount(player))
		local query = executeSQLQuery("SELECT hours FROM vip WHERE account=?", account)
		local hours = (tonumber (query[1].hours) - getRealTime ().timestamp)/3600
	    return hours
    end
end

function myHours (player)
	if (not isPlayerVIP(player)) then
		return exports.chgnote:createNote("You are not VIP", player, 255, 0, 0)
	end
	
	local hours, minutes, seconds = hoursToTime(getPlayerVIPHours(player))
	exports.chgnote:createNote("You have "..string.format("%02d hours %02d minutes %02d seconds", hours, minutes, seconds).." VIP Hours", player, 0, 255, 0)
end
addCommandHandler("viphours", myHours)


function addVIP (player, cmd, newVIP, hours)
	local target = getPlayerFromNick ( newVIP )
	local adderName = getPlayerName(player)
	local targetN = getPlayerName( target )
	
	if (not isElement(target)) then
		return exports.chgnote:createNote("There is no player with that name !", player, 255, 0, 0)
	end
	if (not hours) then
		return exports.chgnote:createNote("Please insert a valid hours !", player, 255, 0, 0)
	end
	
	hours = tonumber(hours)
	if ( not isPlayerVIP(target)) then
		if cmd == "addvip" then
			if targetN then
				if hours then
				local time = getRealTime()
					local timestamp = time.timestamp
					local vHours = timestamp+hours*3600
					outputChatBox(adderName.." gave "..targetN.." "..hours.." VIP Hours !", root, 0, 255, 0)
					local acc = getAccountName(getPlayerAccount(target))
					executeSQLQuery("INSERT INTO vip(account,hours) VALUES(?,?)", acc, vHours)
					setElementData(target, "VIP", "Yes")
				end
			end
		end
	end
end
	addCommandHandler("addvip", addVIP)
	
--Return hours, minutes, seconds
function hoursToTime(hours)
	if hours and type(hours) == "number" then
		local minutes = hours*60
		hours = 0
		while minutes >= 60 do
			minutes = minutes - 60
			hours = hours + 1
		end
		local seconds = math.floor((minutes - math.floor(minutes)) * 60)
		return hours, minutes, seconds
	end
end

Share this post


Link to post

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.