· 10 years ago · Aug 25, 2016, 04:18 PM
1--[[
2********************************************************************************
3 Developer: Chronos
4
5 Version: Open source
6 License: BSD
7 Status: beta release
8********************************************************************************
9]]--
10
11local isInSaveDB = {}
12local titles = {}
13--Database
14local db = dbConnect("sqlite", "titles.db")
15dbExec(db, "CREATE TABLE IF NOT EXISTS accounts (acc TEXT, title TEXT)")
16dbExec(db, "CREATE TABLE IF NOT EXISTS squads (squad TEXT, title TEXT)")
17dbExec(db, "CREATE TABLE IF NOT EXISTS groups (gro TEXT, title TEXT)")
18
19function loadAccounts(query)
20 local d = dbPoll(query, 0)
21
22 if (not d) then return end
23 for ind, data in ipairs(d) do
24 isInSaveDB[data.acc] = {data.title}
25 end
26end
27dbQuery(loadAccounts, db, "SELECT * FROM accounts")
28
29function loadGroups(query)
30 local d = dbPoll(query, 0)
31
32 if (not d) then return end
33 for ind, data in ipairs(d) do
34 isInSaveDB[data.gro] = {data.title}
35 end
36end
37dbQuery(loadGroups, db, "SELECT * FROM groups")
38
39function loadSquads(query)
40 local d = dbPoll(query, 0)
41
42 if (not d) then return end
43 for ind, data in ipairs(d) do
44 isInSaveDB[data.squad] = {data.title}
45 end
46end
47dbQuery(loadSquads, db, "SELECT * FROM squads")
48
49function setData(plr)
50
51 local acc = getAccountName(getPlayerAccount(plr))
52 if (acc ~= "guest") then
53
54 local title = getElementData(plr, "title", value)
55
56 if (isInSaveDB[acc]) then
57 dbExec(db, "UPDATE accounts SET title=? WHERE acc=?", tostring(title), tostring(acc))
58 isInSaveDB[acc] = {title}
59 else
60 dbExec(db, "INSERT INTO accounts (acc, title) VALUES (?, ?)", tostring(acc), tostring(title))
61 isInSaveDB[acc] = {title}
62 end
63 end
64end
65
66function setGroupData(plr)
67
68 local acc = getAccountName(getPlayerAccount(plr))
69 if (acc ~= "guest") then
70
71 local gro = getElementData(plr, "group")
72 local title = getElementData(plr, "grouptitle")
73
74 if (isInSaveDB[gro]) then
75 dbExec(db, "UPDATE groups SET title=? WHERE gro=?", tostring(title), tostring(gro))
76 isInSaveDB[gro] = {title}
77 else
78 dbExec(db, "INSERT INTO groups (gro, title) VALUES (?, ?)", tostring(gro), tostring(title))
79 isInSaveDB[gro] = {title}
80 end
81 end
82end
83
84function setSquadData(plr)
85
86 local acc = getAccountName(getPlayerAccount(plr))
87 if (acc ~= "guest") then
88
89 local squad = getElementData(plr, "s")
90 local title = getElementData(plr, "grouptitle")
91
92 if (isInSaveDB[squad]) then
93 dbExec(db, "UPDATE squads SET title=? WHERE squad=?", tostring(title), tostring(squad))
94 isInSaveDB[squad] = {title}
95 else
96 dbExec(db, "INSERT INTO squads (squad, title) VALUES (?, ?)", tostring(squad), tostring(title))
97 isInSaveDB[squad] = {title}
98 end
99 end
100end
101
102function executeData()
103
104 local account = getAccountName(getPlayerAccount(source))
105 if (isInSaveDB[account] and account ~= "guest") then
106 local title = isInSaveDB[account][1]
107
108 setElementData(source, "customTitleType", "Personal")
109 setElementData(source, "title", tostring(title))
110 setElementData(source, "ct", tostring(title))
111 end
112end
113addEventHandler("onPlayerLogin", root, executeData)
114
115function executeGroupData()
116
117 local gro = getElementData(source, "group")
118 if (isInSaveDB[gro]) then
119 local title = isInSaveDB[gro][1]
120
121 setElementData(source, "customTitleType", "group")
122 setElementData(source, "grouptitle", tostring(title))
123 setElementData(source, "ct", tostring(title))
124 end
125end
126addEventHandler("onPlayerLogin", root, executeGroupData)
127
128function executeSquadData()
129
130 local squad = getElementData(source, "s")
131 if (isInSaveDB[squad]) then
132 local title = isInSaveDB[squad][1]
133
134 setElementData(source, "customTitleType", "squad")
135 setElementData(source, "squadtitle", tostring(title))
136 setElementData(source, "ct", tostring(title))
137 end
138end
139addEventHandler("onPlayerLogin", root, executeSquadData)
140
141function doAction(plr, action, value)
142
143 if (not action) then
144 outputChatBox("Unable to find what action or on what player to perform it on.", client, 255, 0, 0)
145 return
146 end
147
148 --useTitle,removeTitle, pickColor, personal, group, squad
149 --triggerServerEvent("CRtitle.getData", root, localPlayer, d)
150 if (action == "personal") then
151
152 local account = getPlayerAccount(plr)
153 local amount = 2
154 local isaccount = dbPoll(dbQuery(db, "SELECT * FROM accounts WHERE acc=?", getAccountName(account)), -1)
155 if (#isaccount == 0) then
156 dbExec(db, "INSERT INTO accounts (acc, title) VALUES (?, ?)", getAccountName(getPlayerAccount(plr)), value)
157 log = getPlayerName(plr).." has bought a personal title $"..exports.CRmisc:formatNumber(amount)
158 takePlayerMoney(plr, amount)
159 setElementData(plr, "customTitleType", "Personal")
160 setElementData(plr, "title", value)
161 setElementData(plr, "ct", value)
162 else
163 outputChatBox("Already own an account title.", plr, 225, 225, 225)
164 end
165
166 elseif (action == "group") then
167
168 if not getElementData(plr, "group") then return end
169 local amount = 5
170 local group = getElementData(plr, "group")
171 local isgroup = dbPoll(dbQuery(db, "SELECT * FROM groups WHERE gro=?", group), -1)
172 if (#isgroup == 0) then
173 dbExec(db, "INSERT INTO groups (gro, title) VALUES (?, ?)", group, value)
174 log = getPlayerName(plr).." has bought a group title $"..exports.CRmisc:formatNumber(amount)
175 takePlayerMoney(plr, amount)
176 setElementData(plr, "customTitleType", "Group")
177 setElementData(plr, "grouptitle", value)
178 setElementData(plr, "ct", value)
179 else
180 outputChatBox("Already own a group title.", plr, 225, 225, 225)
181 end
182
183 elseif (action == "squad") then
184
185 if not getElementData(plr, "s") then return end
186 local amount = 4
187 local squad = getElementData(plr, "s") or "Not Available."
188 local issquad = dbPoll(dbQuery(db, "SELECT * FROM squads WHERE squad=?", squad), -1)
189 if (#issquad == 0) then
190 dbExec(db, "INSERT INTO squads (squad, title) VALUES (?, ?)", squad, value)
191 log = getPlayerName(plr).." has bought a squad title $"..exports.CRmisc:formatNumber(amount)
192 takePlayerMoney(plr, amount)
193 setElementData(plr, "customTitleType", "Squad")
194 setElementData(plr, "squadtitle", value)
195 setElementData(plr, "ct", value)
196 else
197 outputChatBox("Already own a squad title.", plr, 225, 225, 225)
198 end
199
200 end
201 if (log) then
202 --exports.CRlogs:log(plr, log, "moneyLog") --enable if exist
203 end
204end
205addEvent("CRtitle.getData", true)
206addEventHandler("CRtitle.getData", root, doAction)