diff --git a/__resource.lua b/__resource.lua index 05e9c5a..d6fa8f5 100644 --- a/__resource.lua +++ b/__resource.lua @@ -1,7 +1,7 @@ ui_page "html/index.html" exports { - 'GetPhoneBattery', + 'getBattery', 'IsPlayerInCall', 'IsPlayerUsingPhone' } diff --git a/client/battery.lua b/client/battery.lua index caea7b9..e6374b9 100644 --- a/client/battery.lua +++ b/client/battery.lua @@ -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 @@ -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 @@ -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) @@ -113,7 +114,7 @@ function replaceBatteryField(string) return string.gsub(string, "${battery}", tostring(battery)) end -function GetPhoneBattery() +function getBattery() return battery end @@ -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 @@ -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 diff --git a/server/main.lua b/server/main.lua index bb9bf2b..8eda41f 100644 --- a/server/main.lua +++ b/server/main.lua @@ -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" @@ -10,10 +12,20 @@ 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() @@ -21,7 +33,28 @@ AddEventHandler('onResourceStart', function(resource) 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') @@ -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) @@ -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