Skip to content

Commit

Permalink
Battery now updates in database
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelvlad committed Jun 20, 2017
1 parent a5ab6ca commit ae6f029
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
2 changes: 1 addition & 1 deletion __resource.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ui_page "html/index.html"

exports {
'GetPhoneBattery',
'getBattery',
'IsPlayerInCall',
'IsPlayerUsingPhone'
}
Expand Down
18 changes: 13 additions & 5 deletions client/battery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
--------------------------------------------------------------------------------
enable_battery = true -- Enable or disable the addon

battery = 100 -- Percentage at connection // Warning: If battery is disabled and set at 0 the player will cannot open his phone
battery = 100 -- Percentage at connection // Warning: If battery is disabled and set at 0 the player will cannot use his phone

is_baterry_in_charge = false

local autoCharge = 30 -- How many seconds it takes to the phone to charge without being used of 1%

local autoDischarge = 120 -- How many seconds it takes to the phone to discharge without being used of 1% // Default: 60 seconds
local DischargeInUse = 30 -- How many seconds it takes to the phone to discharge of 1%
local DischargeInUse = 1 -- How many seconds it takes to the phone to discharge of 1%

local enable_charger_connection_sound = true -- Enable or disable
local enable_low_battery_sound = true -- Enable or disable
Expand All @@ -37,7 +37,7 @@ local chargeSoundSent = 0
-- FUNCTIONS
--
--------------------------------------------------------------------------------
function updateBattery(battery)
function updateBattery()
if battery <= 15 and battery > 0 and battery % 5 == 0 then -- if Battery == Trigger low sound
if is_baterry_in_charge then -- If player is charging
if chargeSoundSent == 0 and enable_charger_connection_sound then
Expand Down Expand Up @@ -96,6 +96,7 @@ function updateBattery(battery)
battery = battery
})
end
TriggerServerEvent('ephone:updateBattery', battery)
end

function drawTxt(x,y,scale, text, r,g,b,font)
Expand All @@ -113,7 +114,7 @@ function replaceBatteryField(string)
return string.gsub(string, "${battery}", tostring(battery))
end

function GetPhoneBattery()
function getBattery()
return battery
end

Expand All @@ -124,9 +125,11 @@ end
--
--------------------------------------------------------------------------------
Citizen.CreateThread(function()
TriggerServerEvent('ephone:getBattery')
--Citizen.Wait(1000)
while true do Citizen.Wait(1)
if enable_battery and enable_phone then
updateBattery(battery)
updateBattery()
if is_baterry_in_charge and enable_charging_battery_message then
drawTxt(0.80, 0.96, 0.4, replaceBatteryField(charging_battery_message), 255, 255, 255, 0)
elseif not is_baterry_in_charge then
Expand Down Expand Up @@ -195,6 +198,11 @@ end)
-- EVENTS
--
--------------------------------------------------------------------------------
RegisterNetEvent("ephone:loadBattery")
AddEventHandler("ephone:loadBattery", function(nb)
battery = nb
end)

RegisterNetEvent("ephone:battery_in_charge")
AddEventHandler("ephone:battery_in_charge", function()
is_baterry_in_charge = true
Expand Down
46 changes: 45 additions & 1 deletion server/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local number_length = 10 -- 19 Max !! NEVER GO BELOW YOUR PLAYER LIST
local number_prefix = 213

local battery_list = {}

apps = {}

require "resources/mysql-async/lib/MySQL"
Expand All @@ -10,18 +12,49 @@ require "resources/mysql-async/lib/MySQL"
-- EVENTS
--
--------------------------------------------------------------------------------
AddEventHandler('chatMessageEntered', function(name, color, message)
end)

AddEventHandler('playerConnecting', function(playerName, setKickReason)
addUser(source)
end)

AddEventHandler('playerDropped', function(reason)
getUserId(source, function(uid)
updateBattery(uid, battery_list[source])
battery_list[source] = nil
end)
end)

AddEventHandler('onResourceStart', function(resource)
if resource == "ephone" then
setupPhone()
end
end)

AddEventHandler('onResourceStop', function(resource)
if resource == "ephone" then
for k, v in pairs(battery_list) do
getUserId(k, function(uid)
updateBattery(uid, v)
battery_list[k] = nil
end)
end
end
end)

RegisterServerEvent('ephone:getBattery')
AddEventHandler('ephone:getBattery', function()
getUserId(source, function(uid)
getBattery(uid, function(data)
TriggerClientEvent('ephone:loadBattery', source, data)
end)
end)
end)

RegisterServerEvent('ephone:updateBattery')
AddEventHandler('ephone:updateBattery', function(battery)
battery_list[source] = battery
end)

RegisterServerEvent('ephone:addApp')
Expand Down Expand Up @@ -111,10 +144,11 @@ end)
--
--------------------------------------------------------------------------------
function setupPhone()
MySQL.Async.execute("CREATE TABLE IF NOT EXISTS `ephone_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `playerid` varchar(255) NOT NULL, `phone_number` bigint(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", {}, function(changes)
MySQL.Async.execute("CREATE TABLE IF NOT EXISTS `ephone_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `playerid` varchar(255) NOT NULL, `phone_number` bigint(20) NOT NULL, `battery` int(11) NOT NULL DEFAULT '100', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", {}, function(changes)
checkColumn("ephone_users", "id", "int(11) NOT NULL AUTO_INCREMENT")
checkColumn("ephone_users", "playerid", "varchar(255) NOT NULL AFTER `id`")
checkColumn("ephone_users", "phone_number", "bigint(20) NOT NULL AFTER `playerid`")
checkColumn("ephone_users", "battery", "int(11) NOT NULL DEFAULT '100' AFTER `phone_number`")
end)

MySQL.Async.execute("CREATE TABLE IF NOT EXISTS `ephone_groups` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `phone_number` bigint(20) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", {}, function(changes)
Expand Down Expand Up @@ -302,3 +336,13 @@ end
function deleteGroup(name)
MySQL.Async.execute("DELETE FROM ephone_groups WHERE name=@name", {['@name'] = name})
end

function getBattery(uid, callback)
MySQL.Async.fetchAll("SELECT * FROM ephone_users WHERE id = @uid", {['@uid'] = uid}, function(data)
callback(data[1].battery)
end)
end

function updateBattery(uid, battery)
MySQL.Async.execute("UPDATE ephone_users SET battery=@battery WHERE id = @id", {['@battery'] = battery, ['@id'] = uid})
end

0 comments on commit ae6f029

Please sign in to comment.