forked from Randolio/randol_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsv_notes.lua
More file actions
178 lines (158 loc) · 7.17 KB
/
sv_notes.lua
File metadata and controls
178 lines (158 loc) · 7.17 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
local ox_inv = exports.ox_inventory
local hasgiven
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
QBCore.Functions.CreateUseableItem("notepad", function(source, item)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if ox_inv:Search(src, 'count', item.name) > 0 then
if item.metadata.noteid then
if Config.PrivateNotes and item.metadata.noteowner ~= Player.PlayerData.citizenid then
QBCore.Functions.Notify(src, 'You do not own this notepad.', 'error')
return
end
TriggerClientEvent("randol_notes:client:useItem", src, item.metadata.noteid)
end
end
end)
lib.callback.register('randol_notes:server:getNotes', function(source, noteid)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local notesData = {}
local result
if Config.PrivateNotes then
result = MySQL.query.await('SELECT notes FROM rnotes WHERE citizenid = ? and noteid = ?', {Player.PlayerData.citizenid, noteid})
else
result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
end
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 = QBCore.Functions.GetPlayer(src)
local currentDate = os.date("%d-%m-%Y")
local existingNotes = {}
local result
if Config.PrivateNotes then
result = MySQL.query.await('SELECT notes FROM rnotes WHERE citizenid = ? and noteid = ?', {Player.PlayerData.citizenid, noteid})
else
result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
end
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('UPDATE rnotes SET notes = ? WHERE citizenid = ? and noteid = ?', {json.encode(existingNotes), Player.PlayerData.citizenid, noteid})
QBCore.Functions.Notify(src, 'New note added with ID ' .. 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 = QBCore.Functions.GetPlayer(src)
local noteId = tonumber(data.id)
local result
if Config.PrivateNotes then
result = MySQL.query.await('SELECT notes FROM rnotes WHERE citizenid = ? and noteid = ?', {Player.PlayerData.citizenid, noteid})
else
result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', {noteid})
end
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('UPDATE rnotes SET notes = ? WHERE citizenid = ? and noteid = ?', {json.encode(existingNotes), Player.PlayerData.citizenid, noteid})
QBCore.Functions.Notify(src, 'Successfully deleted note with ID ' .. 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 = QBCore.Functions.GetPlayer(src)
local name = (isSigned and Player.PlayerData.charinfo.firstname..' '..Player.PlayerData.charinfo.lastname) 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 = QBCore.Functions.GetPlayer(src)
local name = (data.signed and Player.PlayerData.charinfo.firstname..' '..Player.PlayerData.charinfo.lastname) 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
if Config.PrivateNotes then
result = MySQL.query.await('SELECT notes FROM rnotes WHERE citizenid = ? and noteid = ?', {Player.PlayerData.citizenid, noteid})
else
result = MySQL.query.await('SELECT notes FROM rnotes WHERE noteid = ?', { noteid})
end
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('UPDATE rnotes SET notes = ? WHERE citizenid = ? and noteid = ?', {json.encode(existingNotes), Player.PlayerData.citizenid, noteid})
QBCore.Functions.Notify(src, "Note successfully ripped out.", "success")
ox_inv:AddItem(src, 'tornnote', 1, metadata)
end
end
end)
local NOTEPAD_GIVE = ox_inv:registerHook('swapItems', function(payload)
hasgiven = true
CreateThread(function()
Wait(100)
hasgiven = false
end)
end, { print = false, itemFilter = { notepad = true } })
local NOTEPAD_HOOK = ox_inv:registerHook('createItem', function(payload)
local metadata = payload.metadata
local Player = QBCore.Functions.GetPlayer(payload.inventoryId)
local charname = Player.PlayerData.charinfo.firstname..' '..Player.PlayerData.charinfo.lastname
if not hasgiven then
metadata.noteid = GenerateNotepadId()
metadata.noteowner = Player.PlayerData.citizenid
metadata.description = ('Barcode: %s\n\nOwner: %s'):format(metadata.noteid, charname)
MySQL.insert.await('INSERT INTO rnotes (citizenid, notes, noteid) VALUES (?, ?, ?)', {Player.PlayerData.citizenid, json.encode({}), metadata.noteid})
return metadata
else
hasgiven = false
return metadata
end
end, { print = false, itemFilter = { notepad = true } })