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