From 71315bcd1756bf6ed39173003a73111215f5eab3 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Mon, 3 Feb 2020 11:12:55 +0100 Subject: [PATCH] Use async to limit paying rent --- README.md | 4 +++- config.lua | 6 ++---- fxmanifest.lua | 4 +++- server/main.lua | 40 ++++++++++++++++++++++++++-------------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 37449fa6..8c230af9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # esx_property ### Requirements + +- [async](https://github.com/ESX-Org/async) - [instance](https://github.com/ESX-Org/instance) - [cron](https://github.com/ESX-Org/cron) - [esx_addonaccount](https://github.com/ESX-Org/esx_addonaccount) @@ -37,7 +39,7 @@ start esx_property ### License esx_property - own a property! -Copyright (C) 2015-2018 Jérémie N'gadi +Copyright (C) 2015-2020 Jérémie N'gadi This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version. diff --git a/config.lua b/config.lua index 2666d3c2..f71a9871 100644 --- a/config.lua +++ b/config.lua @@ -4,9 +4,7 @@ Config.MarkerSize = {x = 1.5, y = 1.5, z = 1.0} Config.MarkerColor = {r = 102, g = 102, b = 204} Config.RoomMenuMarkerColor = {r = 102, g = 204, b = 102} Config.MarkerType = 1 -Config.Zones = {} -Config.Properties = {} -Config.EnablePlayerManagement = false -- If set to true you need esx_realestateagentjob -Config.Locale = 'fr' Config.Properties = {} +Config.EnablePlayerManagement = false -- If set to true you use esx_realestateagentjob +Config.Locale = 'fr' diff --git a/fxmanifest.lua b/fxmanifest.lua index 53d33fb4..1c060718 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,8 +7,9 @@ description 'ESX Property' version '1.0.4' server_scripts { - '@es_extended/locale.lua', + '@async/async.lua', '@mysql-async/lib/MySQL.lua', + '@es_extended/locale.lua', 'locales/de.lua', 'locales/br.lua', 'locales/en.lua', @@ -36,6 +37,7 @@ client_scripts { } dependencies { + 'async', 'es_extended', 'instance', 'cron', diff --git a/server/main.lua b/server/main.lua index 2e0a6e5a..ef07a67a 100644 --- a/server/main.lua +++ b/server/main.lua @@ -416,25 +416,37 @@ AddEventHandler('esx_property:removeOutfit', function(label) end) function PayRent(d, h, m) - MySQL.Async.fetchAll('SELECT * FROM owned_properties WHERE rented = 1', {}, function (result) + local tasks, timeStart = {}, os.clock() + print('[esx_property] [^2INFO^7] Paying rent cron job started') + + MySQL.Async.fetchAll('SELECT * FROM owned_properties WHERE rented = 1', {}, function(result) for i=1, #result, 1 do - local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner) + table.insert(tasks, function(cb) + local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner) - -- message player if connected - if xPlayer then - xPlayer.removeAccountMoney('bank', result[i].price) - xPlayer.showNotification(_U('paid_rent', ESX.Math.GroupDigits(result[i].price))) - else -- pay rent either way - MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', { - ['@bank'] = result[i].price, - ['@identifier'] = result[i].owner - }) - end + -- message player if connected + if xPlayer then + xPlayer.removeAccountMoney('bank', result[i].price) + xPlayer.showNotification(_U('paid_rent', ESX.Math.GroupDigits(result[i].price))) + else -- pay rent either way + MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier', { + ['@bank'] = result[i].price, + ['@identifier'] = result[i].owner + }) + end - TriggerEvent('esx_addonaccount:getSharedAccount', 'society_realestateagent', function(account) - account.addMoney(result[i].price) + TriggerEvent('esx_addonaccount:getSharedAccount', 'society_realestateagent', function(account) + account.addMoney(result[i].price) + end) + + cb() end) end + + Async.parallelLimit(tasks, 5, function(results) end) + + local elapsedTime = os.clock() - timeStart + print(('[esx_property] [^2INFO^7] Paying rent cron job took %s seconds'):format(elapsedTime)) end) end