-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsv_notes.lua
More file actions
159 lines (138 loc) · 5.81 KB
/
sv_notes.lua
File metadata and controls
159 lines (138 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
local ox_inv = exports.ox_inventory
local function generateNotepadId()
local characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local gen = ""
for i = 1, 8 do
local randomChar = math.random(1, #characters)
gen = gen..string.sub(characters, randomChar, randomChar)
end
local result = MySQL.query.await('SELECT noteid FROM rnotes WHERE noteid = ?', {gen})
return (result[1] and generateNotepadId()) or gen
end
exports('notepad', function(event, item, inventory, slot, data)
if event == 'usingItem' then
local metadata = inventory.items[slot].metadata
if metadata.noteid then
TriggerClientEvent("randol_notes:client:useItem", inventory.id, metadata.noteid)
end
end
end)
lib.callback.register('randol_notes:server:getNotes', function(source, noteid)
local src = source
local player = GetPlayer(src)
local notesData = {}
local result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
if result and result[1] then
notesData = json.decode(result[1].notes)
table.sort(notesData, function(a, b)
return a.date > b.date
end)
end
return notesData
end)
RegisterNetEvent('randol_notes:server:newNote', function(note, isSigned, noteid)
local src = source
local player = GetPlayer(src)
local currentDate = os.date("%d-%m-%Y")
local existingNotes = {}
local result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
if result and result[1] then
existingNotes = json.decode(result[1].notes)
local newNote = { id = #existingNotes + 1, text = note, signed = isSigned, date = currentDate }
existingNotes[#existingNotes+1] = newNote
MySQL.update.await('UPDATE rnotes SET notes = ? WHERE noteid = ?', {json.encode(existingNotes), noteid})
DoNotification(src, ('New note added with ID %s : %s'):format(newNote.id, note), 'success')
Wait(500)
TriggerClientEvent("randol_notes:client:useItem", src, noteid)
end
end)
RegisterNetEvent('randol_notes:server:deleteNote', function(data, noteid)
local src = source
local player = GetPlayer(src)
local noteId = tonumber(data.id)
local result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
local existingNotes = {}
if result[1] then
local removed = false
existingNotes = json.decode(result[1].notes)
for i, note in ipairs(existingNotes) do
if note.id == noteId then
table.remove(existingNotes, i)
removed = true
break
end
end
if removed then
MySQL.update.await('UPDATE rnotes SET notes = ? WHERE noteid = ?', {json.encode(existingNotes), noteid})
DoNotification(src, ('Successfully deleted note #%s'):format(noteId), 'success')
Wait(500)
TriggerClientEvent("randol_notes:client:useItem", src, noteid)
end
end
end)
RegisterNetEvent('randol_notes:server:newTornNote', function(note, isSigned)
local src = source
local player = GetPlayer(src)
local name = (isSigned and GetCharacterName(player)) or 'Unsigned'
local message = note
local currentDate = os.date("%d-%m-%Y")
local date = currentDate
local description = ('%s | %s\n\nNote: %s'):format(date, name, message)
local metadata = { note = message, description = description, }
ox_inv:AddItem(src, 'tornnote', 1, metadata)
end)
RegisterNetEvent('randol_notes:server:ripNote', function(data, noteid)
local src = source
local player = GetPlayer(src)
local name = (data.signed and GetCharacterName(player)) or 'Unsigned'
local message = data.text
local date = data.date
local description = ('%s | %s\n\nNote: %s'):format(date, name, message)
local metadata = { note = message, description = description, }
local result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
local existingNotes = {}
if result[1] then
local removed = false
existingNotes = json.decode(result[1].notes)
for i, note in ipairs(existingNotes) do
if note.id == data.id then
table.remove(existingNotes, i)
removed = true
break
end
end
if removed then
MySQL.update.await('UPDATE rnotes SET notes = ? WHERE noteid = ?', {json.encode(existingNotes), noteid})
DoNotification(src, "Note successfully ripped out.", "success")
ox_inv:AddItem(src, 'tornnote', 1, metadata)
end
end
end)
local NOTEPAD_HOOK = ox_inv:registerHook('createItem', function(payload)
local metadata = payload.metadata
if metadata.unique then return metadata end
local player = GetPlayer(payload.inventoryId)
local cid = GetPlyIdentifier(player)
local charname = GetCharacterName(player)
metadata.noteid = generateNotepadId()
metadata.unique = true
metadata.description = ('Barcode: %s\n\nOwner: %s'):format(metadata.noteid, charname)
MySQL.insert.await('INSERT INTO rnotes (citizenid, notes, noteid) VALUES (?, ?, ?)', {cid, json.encode({}), metadata.noteid})
return metadata
end, {
print = false,
itemFilter = {
notepad = true
}
})
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
MySQL.query([=[
CREATE TABLE IF NOT EXISTS `rnotes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`noteid` varchar(50) NOT NULL DEFAULT '0',
`citizenid` varchar(100) NOT NULL DEFAULT '0',
`notes` longtext NOT NULL,
PRIMARY KEY (`id`));
]=])
end)