Skip to content
This repository was archived by the owner on Oct 1, 2023. It is now read-only.

Commit ae16634

Browse files
author
Isigar
committed
debug info fix job, validate lua update
1 parent 7ae1494 commit ae16634

File tree

4 files changed

+209
-35
lines changed

4 files changed

+209
-35
lines changed

readme.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# rcore.cz
22

3-
Our discord with more scripts: https://discord.gg/F28PfsY
3+
Our discord with more scripts: [https://discord.gg/F28PfsY](https://discord.gg/F28PfsY)
44

55
The simple way to code with ESX. rcore is a framework that's the whole point is to simplify life to your developers and coders, you can imagine if you are creating your scripts, you always do the same thinks, create markers, check distances, check keypress and lot of more that you have to do at every single script, or using datastore, esx\_addoninventory, addonaccount and always you have to look at some other script, how I can get datastore or account instance and functions... I really don't like creating code twice so I create rcore, its take of your solders all thinks that you have done at every script, now you can do the complex task with simple code, like register society, number or datastore with a single line of code.
66

7-
8-
9-
### What does rcore?
7+
## What does rcore?
108

119
* create distance markers
1210
* create blips
@@ -21,5 +19,3 @@ The simple way to code with ESX. rcore is a framework that's the whole point is
2119
* getting cached esx instance
2220
* and a lot more...
2321

24-
###
25-

server/esx/job.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local dbg = rdebug()
2+
13
---@param player number
24
---@param job string
35
---@param grade number
@@ -11,7 +13,7 @@ end
1113
exports('setPlayerJob',setPlayerJob)
1214

1315
function isJobExists(job,cb)
14-
rdebug(string.format('Checking job %s if exists.', job))
16+
dbg.info(string.format('Checking job %s if exists.', job))
1517
MySQL.ready(function()
1618
local data = MySQL.Sync.fetchScalar('SELECT COUNT(name) FROM jobs WHERE name = @name', {
1719
['@name'] = job
@@ -29,17 +31,17 @@ exports('isJobExists',isJobExists)
2931
function createJob(name,label,whitelisted)
3032
isJobExists(name,function(is)
3133
if is then
32-
rdebug(string.format('Job %s already created, skipping creation',name))
34+
dbg.info(string.format('Job %s already created, skipping creation',name))
3335
else
3436
MySQL.Async.execute('INSERT INTO `jobs` (`name`, `label`, `whitelisted`) VALUES (@name, @label, @whitelisted)',{
3537
['@name'] = name,
3638
['@label'] = label,
3739
['@whitelisted'] = whitelisted
3840
},function(changes)
3941
if changes then
40-
rdebug(string.format('Job %s successfully created!',name))
42+
dbg.info(string.format('Job %s successfully created!',name))
4143
else
42-
rdebug(string.format('Error occured! Job %s cannot be created!',name))
44+
dbg.info(string.format('Error occured! Job %s cannot be created!',name))
4345
end
4446
end)
4547
end
@@ -49,7 +51,7 @@ end
4951
exports('createJob',createJob)
5052

5153
function isJobGradeExists(name,job_name,cb)
52-
rdebug(string.format('Checking job grade %s for job %s if exists.', name,job_name))
54+
dbg.info(string.format('Checking job grade %s for job %s if exists.', name,job_name))
5355
MySQL.ready(function()
5456
local data = MySQL.Sync.fetchAll('SELECT id,job_name,grade,name,label,salary FROM job_grades WHERE name=@name AND job_name=@job', {
5557
['@name'] = name,
@@ -76,14 +78,14 @@ function createJobGrade(job_name,grade,name,label,salary)
7678
['@salary'] = salary,
7779
},function(changes)
7880
if changes then
79-
rdebug(string.format('Job grade %s successfully updated!',name))
81+
dbg.info(string.format('Job grade %s successfully updated!',name))
8082
else
81-
rdebug(string.format('Error occured! Job grade %s cannot be updated!',name))
83+
dbg.info(string.format('Error occured! Job grade %s cannot be updated!',name))
8284
end
8385
end)
84-
rdebug(string.format('Updating job grade %s - changes found!',name))
86+
dbg.info(string.format('Updating job grade %s - changes found!',name))
8587
else
86-
rdebug(string.format('Job grade %s already created, skipping creation',name))
88+
dbg.info(string.format('Job grade %s already created, skipping creation',name))
8789
end
8890
else
8991
MySQL.Async.execute('INSERT INTO `job_grades` (`job_name`, `grade`, `name`, `label`, `salary`, `skin_male`, `skin_female`) VALUES (@job_name, @grade, @name, @label, @salary, @skin, @skin)',{
@@ -95,13 +97,13 @@ function createJobGrade(job_name,grade,name,label,salary)
9597
['@skin'] = json.encode({})
9698
},function(changes)
9799
if changes then
98-
rdebug(string.format('Job grade %s successfully created!',name))
100+
dbg.info(string.format('Job grade %s successfully created!',name))
99101
else
100-
rdebug(string.format('Error occured! Job grade %s cannot be created!',name))
102+
dbg.info(string.format('Error occured! Job grade %s cannot be created!',name))
101103
end
102104
end)
103105
end
104106
end)
105107
end
106108

107-
exports('createJobGrade',createJobGrade)
109+
exports('createJobGrade',createJobGrade)

shared/common.lua

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,41 @@ Keys = {
7777
}
7878

7979
-- Stolen from: https://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string
80-
function normalizeString( str )
80+
function normalizeString(str)
8181

8282
local s = ""
8383
for i = 1, str:len() do
8484
if str:byte(i) >= 32 and str:byte(i) <= 126 then
85-
s = s .. str:sub(i,i)
85+
s = s .. str:sub(i, i)
8686
end
8787
end
8888
return s
8989

9090
end
9191

92-
exports('normalizeString',normalizeString)
92+
exports('normalizeString', normalizeString)
9393

9494
-- Stolen from: https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/
9595
function urlencode(str)
9696
if (str) then
97-
str = string.gsub (str, "\n", "\r\n")
98-
str = string.gsub (str, "([^%w ])",
99-
function() return string.format ("%%%02X", string.byte) end)
100-
str = string.gsub (str, " ", "+")
97+
str = string.gsub(str, "\n", "\r\n")
98+
str = string.gsub(str, "([^%w ])",
99+
function()
100+
return string.format("%%%02X", string.byte)
101+
end)
102+
str = string.gsub(str, " ", "+")
101103
end
102104
return str
103105
end
104106

105-
exports('urlencode',urlencode)
107+
exports('urlencode', urlencode)
106108

107109
function round(num, numDecimalPlaces)
108-
local mult = 10^(numDecimalPlaces or 0)
110+
local mult = 10 ^ (numDecimalPlaces or 0)
109111
return math.floor(num * mult + 0.5) / mult
110112
end
111113

112-
exports('round',round)
114+
exports('round', round)
113115

114116
--- @param object object
115117
--- stolen: https://forums.coronalabs.com/topic/27482-copy-not-direct-reference-of-table/
@@ -131,7 +133,7 @@ function deepCopy(object)
131133
return _copy(object)
132134
end
133135

134-
exports('deepCopy',deepCopy)
136+
exports('deepCopy', deepCopy)
135137

136138
--- @param sourceTable table
137139
--- @param targetTable table
@@ -241,7 +243,7 @@ function getConfig()
241243
return Config
242244
end
243245

244-
exports('getConfig',getConfig)
246+
exports('getConfig', getConfig)
245247

246248
--Taken from ESX
247249
function dumpTable(table, nb)
@@ -256,12 +258,14 @@ function dumpTable(table, nb)
256258
end
257259

258260
s = '{\n'
259-
for k,v in pairs(table) do
260-
if type(k) ~= 'number' then k = '"'..k..'"' end
261+
for k, v in pairs(table) do
262+
if type(k) ~= 'number' then
263+
k = '"' .. k .. '"'
264+
end
261265
for i = 1, nb, 1 do
262266
s = s .. " "
263267
end
264-
s = s .. '['..k..'] = ' .. dumpTable(v, nb + 1) .. ',\n'
268+
s = s .. '[' .. k .. '] = ' .. dumpTable(v, nb + 1) .. ',\n'
265269
end
266270

267271
for i = 1, nb, 1 do
@@ -274,8 +278,8 @@ function dumpTable(table, nb)
274278
end
275279
end
276280

277-
exports('dumpTable',dumpTable)
281+
exports('dumpTable', dumpTable)
278282

279283
function triggerName(event)
280-
return string.format('rcore:%s',event)
284+
return string.format('rcore:%s', event)
281285
end

shared/validate.lua

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
--Validation of data
2+
--rcore.cz
3+
--https://discord.gg/F28PfsY
4+
--Supported checks
5+
6+
local boolRelatives = {
7+
['true'] = true,
8+
['false'] = false,
9+
['1'] = true,
10+
['0'] = false,
11+
}
12+
13+
SPLIT_TERM = '|'
14+
PARAM_TERM = ':'
15+
16+
STRUCT_RULE = 'rule'
17+
STRUCT_RULE_PARAMS = 'params'
18+
19+
--Cannot be empty/nil
20+
RULE_REQUIRED = 'required'
21+
--Is type string
22+
RULE_STRING = 'string'
23+
--Is type string
24+
RULE_VECTOR4 = 'vector4'
25+
--Is type string
26+
RULE_VECTOR3 = 'vector3'
27+
--Is type string
28+
RULE_VECTOR2 = 'vector2'
29+
--Is type table
30+
RULE_TABLE = 'table'
31+
--Is type bool (if 0/1, string true,false it convert to bool)
32+
RULE_BOOL = 'bool'
33+
--Check if string has @ in it
34+
RULE_EMAIL = 'email'
35+
--Min count of characters
36+
RULE_MIN = 'min'
37+
--Max count of characters
38+
RULE_MAX = 'max'
39+
--Min value of integer
40+
RULE_INTMIN = 'intMin'
41+
--Max value of integer
42+
RULE_INTMAX = 'intMax'
43+
44+
local ruleTable = {
45+
[RULE_REQUIRED] = true,
46+
[RULE_STRING] = true,
47+
[RULE_EMAIL] = true,
48+
[RULE_TABLE] = true,
49+
[RULE_BOOL] = true,
50+
[RULE_MIN] = true,
51+
[RULE_MAX] = true,
52+
[RULE_INTMIN] = true,
53+
[RULE_INTMAX] = true,
54+
[RULE_VECTOR2] = true,
55+
[RULE_VECTOR3] = true,
56+
[RULE_VECTOR4] = true,
57+
[RULE_NUMBER] = true,
58+
}
59+
60+
local ruleTranslateTable = {
61+
['required'] = RULE_REQUIRED,
62+
['string'] = RULE_STRING,
63+
['email'] = RULE_EMAIL,
64+
['table'] = RULE_TABLE,
65+
['bool'] = RULE_BOOL,
66+
['min'] = RULE_MIN,
67+
['max'] = RULE_MAX,
68+
['intMin'] = RULE_INTMIN,
69+
['intMax'] = RULE_INTMAX,
70+
['vector3'] = RULE_VECTOR3,
71+
['vector2'] = RULE_VECTOR2,
72+
['vector4'] = RULE_VECTOR4,
73+
['number'] = RULE_NUMBER,
74+
}
75+
76+
--Parse function
77+
--min:256|max:512
78+
local function parse(str)
79+
local rules = {}
80+
for rule in string.gmatch(str, "([^"..SPLIT_TERM.."]+)") do
81+
local rulesParams = {}
82+
if string.find(rule, PARAM_TERM) then
83+
for params in string.gmatch(rule, "([^"..PARAM_TERM.."]+)") do
84+
if ruleTranslateTable[params] == nil then
85+
table.insert(rulesParams, params)
86+
rule = string.gsub(rule, PARAM_TERM..params, '')
87+
end
88+
end
89+
end
90+
table.insert(rules, {
91+
[STRUCT_RULE] = rule,
92+
[STRUCT_RULE_PARAMS] = rulesParams
93+
})
94+
end
95+
96+
return rules
97+
end
98+
99+
local function applyRule(value, ruleData)
100+
local rule = ruleData[STRUCT_RULE]
101+
local params = ruleData[STRUCT_RULE_PARAMS]
102+
if rule == RULE_REQUIRED then
103+
if value == nil or value == '' then
104+
return false, RULE_REQUIRED, params[1] or nil
105+
end
106+
elseif rule == RULE_STRING then
107+
if type(value) ~= 'string' then
108+
return false, RULE_STRING, params[1] or nil
109+
end
110+
elseif rule == RULE_VECTOR2 then
111+
if type(value) ~= 'vector2' then
112+
return false, RULE_VECTOR2, params[1] or nil
113+
end
114+
elseif rule == RULE_VECTOR3 then
115+
if type(value) ~= 'vector3' then
116+
return false, RULE_VECTOR3, params[1] or nil
117+
end
118+
elseif rule == RULE_VECTOR4 then
119+
if type(value) ~= 'vector4' then
120+
return false, RULE_VECTOR4, params[1] or nil
121+
end
122+
elseif rule == RULE_TABLE then
123+
if type(value) ~= 'table' then
124+
return false, RULE_TABLE, params[1] or nil
125+
end
126+
elseif rule == RULE_BOOl then
127+
local valueString = tostring(value)
128+
129+
if boolRelatives[string.lower(valueString)] == nil then
130+
return false, RULE_BOOL, params[1] or nil
131+
end
132+
elseif rule == RULE_EMAIL then
133+
print(exports.rcore:dumpTable(params))
134+
if string.match(tostring(value), '@') == nil then
135+
return false, RULE_EMAIL, params[1] or nil
136+
end
137+
elseif rule == RULE_MIN then
138+
local count = tonumber(params[1])
139+
if string.len(tostring(value)) < count then
140+
return false, RULE_MIN, params[2] or nil
141+
end
142+
elseif rule == RULE_MAX then
143+
local count = tonumber(params[1])
144+
if string.len(tostring(value)) > count then
145+
return false, RULE_MAX, params[2] or nil
146+
end
147+
end
148+
149+
return true
150+
end
151+
152+
--Example
153+
--local rules = {
154+
-- ['required|string'] = 'some value'
155+
--}
156+
function validate(data)
157+
if type(data) ~= 'table' then
158+
print('[VALIDATE ERROR] Validation data must be in type table')
159+
return false
160+
end
161+
162+
for rule,v in pairs(data) do
163+
local parseRule = parse(rule)
164+
for _,params in pairs(parseRule) do
165+
local ok, rl, error = applyRule(v, params)
166+
if ok == false then
167+
return false, rl, error
168+
end
169+
end
170+
end
171+
return true
172+
end

0 commit comments

Comments
 (0)