forked from Simnation/Main
ed
This commit is contained in:
parent
875c8448e1
commit
c81ae4bb6d
219 changed files with 8036 additions and 7 deletions
29
resources/[tools]/bl_idcard/server/decoder.lua
Normal file
29
resources/[tools]/bl_idcard/server/decoder.lua
Normal file
|
@ -0,0 +1,29 @@
|
|||
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
local resourcePath = GetResourcePath(cache.resource)
|
||||
local imagePath = '/web/mugshots'
|
||||
|
||||
local function base64Decode(data)
|
||||
data = data:gsub('[^'..b..'=]', '')
|
||||
return (data:gsub('.', function(x)
|
||||
if x == '=' then return '' end
|
||||
local r, f = '', (b:find(x) - 1)
|
||||
for i = 6, 1, -1 do r = r .. (f % 2^i - f % 2^(i-1) > 0 and '1' or '0') end
|
||||
return r
|
||||
end):gsub('%d%d%d%d%d%d%d%d', function(x)
|
||||
return string.char(tonumber(x, 2))
|
||||
end))
|
||||
end
|
||||
|
||||
local function saveBase64AsPng(base64String, imageName)
|
||||
local updatedPath = resourcePath:match("resources/.*")
|
||||
local base64Data = base64String:gsub("^data:image/png;base64,", "")
|
||||
local decodedData = base64Decode(base64Data)
|
||||
local file = io.open(('%s/%s/%s.png'):format(updatedPath, imagePath, imageName), "wb")
|
||||
if file then
|
||||
file:write(decodedData)
|
||||
file:close()
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return saveBase64AsPng
|
105
resources/[tools]/bl_idcard/server/init.lua
Normal file
105
resources/[tools]/bl_idcard/server/init.lua
Normal file
|
@ -0,0 +1,105 @@
|
|||
local config = require "shared.config"
|
||||
local callback = lib.callback
|
||||
local core = Framework.core
|
||||
local unSavedMugshots = {}
|
||||
|
||||
local function decodeBase(mugshot, itemImage)
|
||||
local decodeBase64 = require'server.decoder'
|
||||
decodeBase64(mugshot, itemImage)
|
||||
return itemImage
|
||||
end
|
||||
|
||||
---comment
|
||||
---@param source number
|
||||
---@param licenses string | table The key in Config.items
|
||||
local function createLicense(source, licenses)
|
||||
if type(licenses) == 'string' then
|
||||
licenses = {licenses}
|
||||
end
|
||||
|
||||
local mugshot = callback.await('bl_idcard:cb:getMugShot', source)
|
||||
if not mugshot then
|
||||
print('Mugshot for license creation not found. Source: ' .. source .. ' | ' .. GetPlayerName(source))
|
||||
return
|
||||
end
|
||||
|
||||
local player = core.GetPlayer(source)
|
||||
local itemImage = decodeBase(mugshot, ('%s_mugshot'):format(player.id))
|
||||
|
||||
unSavedMugshots[itemImage] = mugshot
|
||||
local playerCharInfo = player.charinfo
|
||||
local charInfo = {
|
||||
id = player.id,
|
||||
dob = player.dob,
|
||||
firstName = playerCharInfo.firstname,
|
||||
lastName = playerCharInfo.lastname,
|
||||
sex = player.gender,
|
||||
imageURL = itemImage
|
||||
}
|
||||
|
||||
for _, license in ipairs(licenses) do
|
||||
local configType = config.items[license]
|
||||
|
||||
if configType then
|
||||
local idType = configType.genderIdType and configType.genderIdType[charInfo.sex] or configType.idType
|
||||
|
||||
charInfo.idType = idType
|
||||
player.addItem(license, 1, charInfo)
|
||||
return charInfo
|
||||
else
|
||||
print('License type not found in config: ' .. license)
|
||||
end
|
||||
end
|
||||
end
|
||||
exports('createLicense', createLicense)
|
||||
|
||||
lib.addCommand('giveidcard', {
|
||||
help = 'Gives an item to a player',
|
||||
params = {
|
||||
{
|
||||
name = 'target',
|
||||
type = 'playerId',
|
||||
help = 'Target player\'s server id',
|
||||
},
|
||||
{
|
||||
name = 'license',
|
||||
type = 'string',
|
||||
help = 'License Name: [id_card, driver_license]',
|
||||
},
|
||||
},
|
||||
restricted = not config.Debug and 'group.admin'
|
||||
}, function(source, args, raw)
|
||||
createLicense(args.target, args.license)
|
||||
end)
|
||||
|
||||
CreateThread(function()
|
||||
local items = config.items
|
||||
for k, v in pairs(items) do
|
||||
core.RegisterUsableItem(k, function(source, slotId, metadata)
|
||||
|
||||
local idType = metadata?.idType
|
||||
local player
|
||||
if not idType then
|
||||
player = core.GetPlayer(source)
|
||||
player.removeItem(k, 1)
|
||||
metadata = createLicense(source, k)
|
||||
end
|
||||
|
||||
local target = callback.await('bl_idcard:use', source, k)
|
||||
|
||||
if target and metadata then
|
||||
local base64Code = metadata.imageURL
|
||||
if base64Code and base64Code:find("data:image/png;base64") then
|
||||
player = player or core.GetPlayer(source)
|
||||
local itemImage = decodeBase(base64Code, ('%s_mugshot'):format(player.id))
|
||||
metadata.imageURL = itemImage
|
||||
unSavedMugshots[itemImage] = base64Code
|
||||
player.setMetaData(slotId, metadata)
|
||||
end
|
||||
|
||||
metadata.imageURL = unSavedMugshots[metadata.imageURL] or metadata.imageURL
|
||||
TriggerClientEvent('bl_idcard:open', target, metadata)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end)
|
Loading…
Add table
Add a link
Reference in a new issue