· 7 years ago · Dec 12, 2018, 11:46 AM
1------------------------------------------------------------------------------------
2-- COMMUNITY CONTRIBUTION FOR CIT2.NET
3-- PURPOSE: Feedback for the updates
4-- DEVELOPERS: Nikos, Zelda - ( Server side )
5------------------------------------------------------------------------------------
6
7local con = dbConnect("mysql", "dbname=;host=", "", "")
8-- local con = dbConnect("sqlite", "//feedbacks.db")
9dbExec(con, "CREATE TABLE IF NOT EXISTS feedbacks (update_post TEXT, message TEXT)")
10
11local removeTable ={
12 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
13 "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th",
14 "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st",
15 "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st",
16}
17
18local antibug1 = { -- with this we filter and remove the months with the year. example August 2018. If we do not do this the gridlist will show the dates.
19 "January", "February", "March", "April", "May", "June", "July",
20 "August", "September", "October", "November", "December",
21}
22
23local antibug2 = { -- with this we can fix the error caused by detecting something like this inside the string (-30 decreased).
24 "a","b","c","d","e","f","g","h","i","j","k","l","m",
25 "n","o","p","k","r","s","t","u","v","w","x","y","z",
26 "1","2","3","4","5","6","7","8","9","10",
27}
28
29local startAge, endAge = 2018, 2070 -- Years filter
30
31function processUpdates(data)
32 local cache = {}
33 local updates = ""..data..""
34 for k = startAge, endAge do
35 for index, v in ipairs(antibug1) do
36 updates = string.gsub(updates, ""..v.." "..k.."", "")
37 end
38 end
39 for index, v in ipairs(removeTable) do
40 updates = string.gsub(updates, ""..v.." ", "")
41 end
42 local updates = string.gsub(updates, "\n", "")
43 local updates = string.gsub(updates, "\r", "")
44 for i, v in ipairs(antibug2) do
45 updates = string.gsub(updates, "-"..v.."", "~"..v.."")
46 end
47 local ent = split(updates, "-") or {}
48 for i, v in ipairs(ent) do
49 table.insert(cache, ""..i..". "..v)
50 end
51 return cache
52end
53
54function getUpdatesList(player)
55 if (isGuestAccount(getPlayerAccount(player))) then
56 return false
57 end
58 if (exports.CITchecking:getPlayerTimeSinceLastOccurence(player, "feedback") < 3000) then
59 return false
60 end
61 local conversion = processUpdates(exports.CITupdatesInfo:getUpdates()) or {}
62 exports.CITchecking:setPlayerOccurenceTime(player, "feedback")
63 if (#conversion == 0) then
64 outputChatBox("The feedback list is not loaded yet, retry later.", player, 255, 0, 0)
65 else
66 triggerClientEvent(player, "CITfeedback.sendFeedbackList", player, conversion)
67 end
68end
69addCommandHandler("feedback", getUpdatesList)
70
71function sendFeedback(message, update)
72 if (isGuestAccount(getPlayerAccount(client))) then
73 return false
74 end
75 local name = getPlayerName(client)
76 local accname = getAccountName(getPlayerAccount(client))
77 local results = dbPoll(dbQuery(con, "SELECT * FROM feedbacks WHERE update_post=?", update) ,-1)
78 if (#results == 0) then
79 local cc = "- "..name.." ("..accname..")\n"..message..""
80 dbExec(con,"INSERT INTO feedbacks VALUES (?, ?)", update, cc, accname)
81 exports.CIThelp:dm("Feedback sended", client, 0, 255, 0)
82 else
83 if (string.find(results[1]["message"], "("..accname..")")) then
84 exports.CIThelp:dm("You have already made a comment to this update.", client, 255, 0, 0)
85 return false
86 end
87 local cc = ""..results[1]["message"].."\n- "..name.." ("..accname..")\n"..message..""
88 dbExec(con, "UPDATE feedbacks SET message=? WHERE update_post=?", cc, update)
89 exports.CIThelp:dm("Feedback sended", client, 0, 255, 0)
90 end
91end
92addEvent("CITfeedback.sendFeedback", true )
93addEventHandler("CITfeedback.sendFeedback", root, sendFeedback)
94
95function getGridData(update)
96 local result = dbPoll( dbQuery(con, "SELECT * FROM feedbacks WHERE update_post=?", update) , -1)
97 if (type(result) == "table" and #result ~= 0) then
98 triggerClientEvent(client, "CITfeedback.sendGridData", client, result[1]["message"])
99 else
100 triggerClientEvent(client, "CITfeedback.sendGridData", client, "No comments yet.")
101 end
102end
103addEvent("CITfeedback.getGridData", true )
104addEventHandler("CITfeedback.getGridData", root, getGridData)
105
106function cleanDB(player)
107 if exports.CITadmin:getPlayerAdminLevel(player) == 5 then
108 dbExec(con, "DELETE FROM feedbacks")
109 exports.CIThelp:dm("Feedback cleaned", player, 255, 255, 0)
110 end
111end
112addCommandHandler("cleancomments", cleanDB)