From 635e18b655f4d1303bfb2a301b731d73b689cdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20N=27gadi?= Date: Mon, 16 Oct 2017 13:13:40 +0200 Subject: [PATCH] feat(Add dot selectors + count function): --- server/classes/datastore.lua | 50 ++++++++++++++++++++++++++++++++++-- server/main.lua | 1 + 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/server/classes/datastore.lua b/server/classes/datastore.lua index acef8514..de95b2cf 100644 --- a/server/classes/datastore.lua +++ b/server/classes/datastore.lua @@ -1,3 +1,15 @@ +function stringsplit(inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} ; i=1 + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + t[i] = str + i = i + 1 + end + return t +end + function CreateDataStore(name, owner, data) local self = {} @@ -13,8 +25,42 @@ function CreateDataStore(name, owner, data) self.save() end - self.get = function(key) - return data[key] + self.get = function(key, i) + + local path = stringsplit(key, '.') + local obj = self.data + + for i=1, #path, 1 do + obj = obj[path[i]] + end + + if i == nil then + return obj + else + return obj[i] + end + + end + + self.count = function(key, i) + + local path = stringsplit(key, '.') + local obj = self.data + + for i=1, #path, 1 do + obj = obj[path[i]] + end + + if i ~= nil then + obj = obj[i] + end + + if obj == nil then + return 0 + else + return #obj + end + end self.save = function() diff --git a/server/main.lua b/server/main.lua index e4df6157..83644fae 100644 --- a/server/main.lua +++ b/server/main.lua @@ -138,3 +138,4 @@ AddEventHandler('esx:playerLoaded', function(source) xPlayer.set('dataStores', dataStores) end) +