Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4238a60
Load player weapons when player has been loaded
ThymonA Oct 2, 2020
6689b8f
Stage new version
ThymonA Oct 25, 2020
6886d40
Stage (Working on skin resource)
ThymonA Oct 27, 2020
7339ac7
stage updated tool
ThymonA Oct 28, 2020
dfb1d47
Tattoos will be available in skins
ThymonA Oct 28, 2020
6ff8928
removed cvf_spawnmanager and relocate it in cvf_skins, skins function…
ThymonA Oct 28, 2020
65bf69f
Added corev:getIdentifier to returns player's primary identifier
ThymonA Oct 29, 2020
d705815
corev:ensure has now support for ector2 and ector3, [tools] removed…
ThymonA Oct 29, 2020
b5df53c
Prevent exports from craching and added identifier resource for easy …
ThymonA Oct 29, 2020
9016a8c
fix client @corev/client/import.lua
ThymonA Oct 29, 2020
1a02f3f
cvf_identifier now logging identifiers when player is connecting with…
ThymonA Oct 29, 2020
58cf04c
Banner updated
ThymonA Oct 29, 2020
f9d8d32
resources can now be marked as migration dependent, prevent resource …
ThymonA Oct 29, 2020
c8e02d7
Migration from .sql to .lua in order to fix dependency in migrations
ThymonA Oct 30, 2020
b2bd91f
Prevent users from joining while database is updating
ThymonA Oct 30, 2020
03d372e
cvf_jobs updates database (no .sql files for inerting jobs)
ThymonA Oct 31, 2020
11bb441
stage
ThymonA Nov 1, 2020
8462018
playerConnecting event is now Sync instead of Parallel, better error …
ThymonA Nov 2, 2020
4534acd
When player is connecting, Adaptive Card is will automatically be gen…
ThymonA Nov 2, 2020
dccf45d
Remove CreateThread for PlayerConnecting event
ThymonA Nov 2, 2020
75d001a
cvf_player will now add players to database if they connect for there…
ThymonA Nov 2, 2020
16e4601
Disconnect event added to corev.events
ThymonA Nov 3, 2020
0898646
globals.json removed
ThymonA Nov 3, 2020
de38a79
Allow resources from loading player identifiers (online and offline)
ThymonA Nov 3, 2020
d89a73d
Fix repo
ThymonA Nov 3, 2020
3a314d7
redo commit: 'Allow resources from loading player identifiers (online…
ThymonA Nov 3, 2020
2555266
vPlayer added, class library changed
ThymonA Nov 3, 2020
8509d31
Change README.md class reference
ThymonA Nov 3, 2020
37493c8
Allow to spawn in server (again)
ThymonA Nov 3, 2020
b7e0a62
cvf_events will now remove triggers if matching resource has been sto…
ThymonA Nov 3, 2020
4cdd82d
cvf_commands added, able to register commands with permissions
ThymonA Nov 3, 2020
f0090e7
streaming added to @corev/client/import.lua and `cvf_ids` removed fro…
ThymonA Nov 4, 2020
14dc6fb
Performance tweaks, aces removed (Use FiveM's aces)
ThymonA Nov 4, 2020
e8b6265
We will use GetHashKey instead
ThymonA Nov 4, 2020
3c8f62f
command parser didn't allow false as default
ThymonA Nov 4, 2020
97bd478
stage
ThymonA Nov 7, 2020
e6082df
hashString is now lua instead of calling Native
ThymonA Nov 10, 2020
04f9344
Added INT32_MAX and INT64_MAX to make code more readable
ThymonA Nov 10, 2020
82c5904
stage
ThymonA Nov 11, 2020
7ffb30f
(CORE) resources can now be restarted, menu (lua) has written and JS …
ThymonA Nov 12, 2020
49f7721
class library removed for better IDE support
ThymonA Nov 18, 2020
52c0e5c
Added support for the spanish translation
Valdorz Jan 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,4 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/

corev_error.log
debug/external_resources/client/
debug/external_resources/server/
debug/internal_resources/client/
debug/internal_resources/server/
debug/internal_modules/client/
debug/internal_modules/server/
debug/external_resources/client/*.lua
debug/external_resources/server/*.lua
debug/internal_resources/client/*.lua
debug/internal_resources/server/*.lua
debug/internal_modules/client/*.lua
debug/internal_modules/server/*.lua

# End of https://www.toptal.com/developers/gitignore/api/lua,git,jspm,visualstudio,visualstudiocode,vue,vuejs
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# CoreV Framework [CVF]
[![N|CoreV](https://i.imgur.com/K7ywz4K.png)](https://i.imgur.com/K7ywz4K.png)
[![N|CoreV](https://i.imgur.com/3XeDqC0.png)](https://i.imgur.com/3XeDqC0.png)

[![N|Project](https://img.shields.io/badge/GitHub%20Project-corv%20framework-lightgray?logo=github&style=for-the-badge)](https://github.com/ThymonA/CoreV-Framework)
[![N|Project](https://img.shields.io/badge/GitLab%20Project-corv%20framework-orange?logo=gitlab&style=for-the-badge)](https://git.arens.io/ThymonA/corev-framework)
[![N|Project](https://img.shields.io/badge/Discord-Tigo%239999-7289da?logo=discord&style=for-the-badge)](https://discordapp.com/users/733686533873467463)

---
## This project is still under development, you can't use it yet!
---

#### Credits
CoreV Framework is inspired by [es_extended](https://github.com/esx-framework/es_extended) but is written from scratch by [ThymonA](https://github.com/ThymonA) (also known as TigoDevelopmet).

Expand All @@ -13,11 +17,7 @@ All 3rd party code in CoreV Framework

Developer(s) | Project | Repository | Version | File
:-------- | :------ | :---------- | :--------- | :-----
**[Yonaba](https://github.com/Yonaba)** | *30log* | **[GitHub](https://github.com/Yonaba/30log)** | *1.3.0* | **[class.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/vendors/class.lua)**
**[IllidanS4](https://github.com/IllidanS4)** | *entityiter.lua* | **[GitHub](https://gist.github.com/IllidanS4/9865ed17f60576425369fc1da70259b2)** | *14 Jul 2017* | **[entityiter.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/vendors/entityiter.lua)**
**[Olivine Labs](https://github.com/Olivine-Labs)**, **[ThymonA](https://github.com/ThymonA)** | *FiveM-Mustache* | **[GitHub](https://github.com/ThymonA/FiveM-Mustache)** | *1.3.1-0* | **[mustache.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/vendors/mustache.lua)**
**[VenomXNL](https://github.com/VenomXNL/)** | *ObjectNameFromHash* | **[GitHub](https://github.com/VenomXNL/ObjectNameFromHash)** | *21 Feb 2019* | **[hashlist.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/vendors/hashlist.lua)**
**[luiseduardohd](https://github.com/luiseduardohd)** | *luaregex.lua* | **[GitHub](https://gist.github.com/luiseduardohd/10668729)** | *130911* | **[regex.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/vendors/regex.lua)**
**[Yonaba](https://github.com/Yonaba/)** | *30log* | **[GitHub](https://github.com/Yonaba/30log/)** | *1.3.0* | **[class.lua](https://git.arens.io/ThymonA/corev-framework/-/blob/master/corev/vendors/class.lua)**
**[esx-framework](https://github.com/esx-framework)** | *es_extended* | **[GitHub](https://github.com/esx-framework/es_extended)** | v1-final | **..**

## License
Expand Down
309 changes: 309 additions & 0 deletions [players]/cvf_jobs/classes/job.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
----------------------- [ CoreV ] -----------------------
-- GitLab: https://git.arens.io/ThymonA/corev-framework/
-- GitHub: https://github.com/ThymonA/CoreV-Framework/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <[email protected]>
-- Name: CoreV
-- Version: 1.0.0
-- Description: Custom FiveM Framework
----------------------- [ CoreV ] -----------------------

--- Cache global variables
local assert = assert
---@type corev_server
local corev = assert(corev_server)
local ipairs = assert(ipairs)
local pairs = assert(pairs)
local insert = assert(table.insert)
local lower = assert(string.lower)
local error = assert(error)

--- Create a `jobs` class
---@class jobs
local jobs = setmetatable({ __class = 'jobs' }, {})

jobs.jobs = {}
jobs.defaultJob = {
job_id = nil,
grade = nil
}

--- Create a new `job` class
---@param id number|nil Job ID
---@param name string Name of Job
---@param label string Label of Job
---@param grades table Grades of Job
---@return job New `job` class
local function createJobClass(id, name, label, grades)
--- Create a `job` class
---@class job
local job = setmetatable({ __class = 'job' }, {})

job.id = id
---@type string
job.name = name
---@type string
job.label = label
---@type table
job.grades = grades

return job
end

--- Creates a job object based on given `name` and `grades`
---@param name string Name of job, example: unemployed, police etc. (lowercase)
---@param label string Label of job, this will be displayed as name of given job
---@param grades table List of grades as table, every grade needs to be a table as well
---@return job|nil Returns a `job` class if found or created, otherwise `nil`
local function createJobObject(name, label, grades)
name = corev:ensure(name, 'unknown')
label = corev:ensure(label, 'Unknown')
grades = corev:ensure(grades, {})

if (name == 'unknown') then
return nil
end

name = lower(name)

--- If job already exists, then return stored job and don't override existing one
for _, _job in pairs(jobs.jobs) do
if (_job.name == name) then
return _job
end
end

--- Create a `job` class
local job = createJobClass(nil, name, label, {})

local existingId = corev.db:fetchScalar('SELECT `id` FROM `jobs` WHERE `name` = @name LIMIT 1', {
['@name'] = job.name
})

if (existingId == nil) then
job.id = corev.db:insert('INSERT INTO `jobs` (`name`, `label`) VALUES (@name, @label)', {
['@name'] = job.name,
['@label'] = job.label
})
else
job.id = corev:ensure(existingId, 0)
end

if (corev:typeof(grades) == 'table') then
for index, grade in ipairs(grades) do
if (corev:typeof(grade) == 'table') then
--- Create a `job-grade` class
---@class job_grade
local jobGrade = setmetatable({ __class = 'job_grade' }, {})

jobGrade.job_id = job.id
jobGrade.grade = corev:ensure(index, 1) - 1
jobGrade.name = lower(corev:ensure(grade.name, 'unknown'))
jobGrade.label = corev:ensure(grade.label, 'Unknown')

job.grades[jobGrade.grade] = jobGrade
end
end
end

local dbGrades = corev.db:fetchAll('SELECT * FROM `job_grades` WHERE `job_id` = @jobId', {
['@jobId'] = job.id
})

local existingGrades = {}

--- Update job_grades
for _, grade in pairs(job.grades) do
for _, dbGrade in pairs(dbGrades) do
if (corev:ensure(dbGrade.grade, -1) == grade.grade) then
insert(existingGrades, grade)

if (grade.name ~= dbGrade.name or grade.label ~= dbGrade.label) then
corev.db:execute('UPDATE `job_grades` SET `name` = @name, `label` = @label WHERE `job_id` = @jobId AND `grade` = @grade', {
['@name'] = grade.name,
['@label'] = grade.label,
['@jobId'] = job.id,
['@grade'] = grade.grade
})
end
end
end
end

--- Add job_grades
for _, grade in pairs(job.grades) do
local needToBeAdded = true

for _, existingGrade in pairs(existingGrades) do
if (grade.grade == existingGrade.grade) then
needToBeAdded = false
end
end

if (needToBeAdded) then
corev.db:insert('INSERT INTO `job_grades` (`job_id`, `grade`, `name`, `label`) VALUES (@jobId, @grade, @name, @label)', {
['@jobId'] = job.id,
['@grade'] = grade.grade,
['@name'] = grade.name,
['@label'] = grade.label
})
end
end

--- Load default job if not already loaded
if (jobs.defaultJob.job_id == nil) then
--- Load default job names from configuration
local defaultJobName = lower(corev:ensure(corev:cfg('jobs', 'defaultJob', 'name'), 'unemployed'))
local defaultJobGrade = lower(corev:ensure(corev:cfg('jobs', 'defaultGrade', 'name'), 'unemployed'))

local jobGradeResult = corev.db:fetchAll('SELECT `job_id`, `grade` FROM `job_grades` WHERE `job_id` = (SELECT `id` FROM `jobs` WHERE `name` = @jobName LIMIT 1) AND `name` = @gradeName LIMIT 1', {
['@jobName'] = defaultJobName,
['@gradeName'] = defaultJobGrade
})

jobGradeResult = corev:ensure(jobGradeResult, {})

if (#jobGradeResult <= 0) then
error(corev:t('default_job_not_exists'))
return
end

local dbJobId = corev:ensure(jobGradeResult[1].job_id, -1)
local dbJobGrade = corev:ensure(jobGradeResult[1].grade, -1)

if (dbJobId < 0 or dbJobGrade < 0) then
error(corev:t('default_job_not_exists'))
return
end

jobs.defaultJob.job_id = dbJobId
jobs.defaultJob.grade = dbJobGrade
end

--- Delete job_grades
for _, dbGrade in pairs(dbGrades) do
local canBeRemoved = true

for _, existingGrade in pairs(existingGrades) do
if (corev:ensure(dbGrade.grade, -1) == existingGrade.grade) then
canBeRemoved = false
end
end

if (canBeRemoved) then
if (corev.db:tableExists('players')) then
--- Change `job` values if player has removable `job` and `grade`
corev.db:execute('UPDATE `players` SET `job` = @newJob, `grade` = @newGrade WHERE `job` = @oldJob AND `grade` = @oldGrade', {
['@oldJob'] = dbGrade.job_id,
['@oldGrade'] = dbGrade.grade,
['@newJob'] = jobs.defaultJob.job_id,
['@newGrade'] = jobs.defaultJob.grade
})

--- Change `job2` values if player has removable `job2` and `grade2`
corev.db:execute('UPDATE `players` SET `job2` = @newJob, `grade2` = @newGrade WHERE `job2` = @oldJob AND `grade2` = @oldGrade', {
['@oldJob'] = dbGrade.job_id,
['@oldGrade'] = dbGrade.grade,
['@newJob'] = jobs.defaultJob.job_id,
['@newGrade'] = jobs.defaultJob.grade
})
end

--- After players has been updated, `job_grades` is safe to remove
corev.db:execute('DELETE FROM `job_grades` WHERE `job_id` = @jobId AND `grade` = @grade', {
['@jobId'] = dbGrade.job_id,
['@grade'] = dbGrade.grade
})
end
end

jobs.jobs[job.id] = job

return job
end

--- Returns `job` bases on given `name`
---@param input string|number Name of job or ID of job
---@return job|nil Returns a `job` class or nil
local function getCacheJob(input)
input = corev:typeof(input) == 'number' and input or corev:ensure(input, 'unknown')

if (corev:typeof(input) == 'number') then
return (jobs.jobs or {})[input] or nil
end

input = corev:ensure(input, 'unknown')

if (input == 'unknown') then
return nil
end

input = lower(input)

--- If job already exists, then return stored job and don't override existing one
for _, job in pairs(jobs.jobs) do
if (job.name == input) then
return job
end
end

return nil
end

--- Returns `job` bases on given `name`
---@param input string|number Name of job or ID of job
---@return job|nil Returns a `job` class or nil
local function getJob(input)
input = corev:typeof(input) == 'number' and input or corev:ensure(input, 'unknown')

local jobFromCache = getCacheJob(input)

if (jobFromCache ~= nil) then return jobFromCache end

local dbJob

if (corev:typeof(input) == 'number') then
dbJob = corev.db:fetchAll('SELECT * FROM `jobs` WHERE `id` = @id LIMIT 1', {
['@id'] = input
})
else
dbJob = corev.db:fetchAll('SELECT * FROM `jobs` WHERE `name` = @name LIMIT 1', {
['@name'] = input
})
end

dbJob = corev:ensure(dbJob, {})

if (#dbJob == 0) then return nil end

--- Create a `job` class
local job = createJobClass(dbJob[1].id, dbJob[1].name, dbJob[1].label, {})

local dbGrades = corev.db:fetchAll('SELECT * FROM `job_grades` WHERE `job_id` = @id ORDER BY `grade` ASC', {
['@id'] = job.id
})

dbGrades = corev:ensure(dbGrades, {})

if (#dbGrades == 0) then return job end

for _, dbGrade in pairs(dbGrades) do
--- Create a `grade` class
---@class grade
local grade = setmetatable({ __class = 'grade' }, {})

grade.job_id = dbGrade.job_id
grade.grade = corev:ensure(dbGrade.grade, 0)
grade.name = dbGrade.name
grade.label = dbGrade.label

job.grades[grade.grade] = grade
end

return job
end

--- Register `createJobObject` as global function
_G.createJobObject = createJobObject
_G.getJob = getJob
Loading