· 6 years ago · May 14, 2019, 02:40 AM
1local lang = vRP.lang
2
3-- Money module, wallet/bank API
4-- The money is managed with direct SQL requests to prevent most potential value corruptions
5-- the wallet empty itself when respawning (after death)
6
7MySQL.createCommand("vRP/money_tables", [[
8CREATE TABLE IF NOT EXISTS vrp_user_moneys(
9 user_id INTEGER,
10 wallet INTEGER,
11 bank INTEGER,
12 CONSTRAINT pk_user_moneys PRIMARY KEY(user_id),
13 CONSTRAINT fk_user_moneys_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE
14);
15]])
16
17MySQL.createCommand("vRP/money_init_user","INSERT IGNORE INTO vrp_user_moneys(user_id,wallet,bank) VALUES(@user_id,@wallet,@bank)")
18MySQL.createCommand("vRP/get_money","SELECT wallet,bank FROM vrp_user_moneys WHERE user_id = @user_id")
19MySQL.createCommand("vRP/set_money","UPDATE vrp_user_moneys SET wallet = @wallet, bank = @bank WHERE user_id = @user_id")
20
21-- init tables
22MySQL.execute("vRP/money_tables")
23
24-- load config
25local cfg = module("cfg/money")
26
27-- API
28
29-- get money
30-- cbreturn nil if error
31function vRP.getMoney(user_id)
32 local tmp = vRP.getUserTmpTable(user_id)
33 if tmp then
34 return tmp.wallet or 0
35 else
36 return 0
37 end
38end
39
40-- set money
41function vRP.setMoney(user_id,value)
42 local tmp = vRP.getUserTmpTable(user_id)
43 if tmp then
44 tmp.wallet = value
45 end
46
47 -- update client display
48 local source = vRP.getUserSource(user_id)
49 if source ~= nil then
50 vRPclient.setDivContent(source,{"money",lang.money.display({value})})
51 end
52end
53
54function vRP.getSujo(user_id,value)
55 local tmp = vRP.getUserTmpTable(user_id)
56 if tmp then
57 tmp.wallet = value
58 end
59
60 local value = vRP.getInventoryItemAmount(user_id,"dirty_money")
61
62 -- update client display
63 local source = vRP.getUserSource(user_id)
64 if source ~= nil then
65 vRPclient.setDivContent(source,{"sujo",lang.money.display({value})})
66 end
67end
68
69
70
71
72-- try a payment
73-- return true or false (debited if true)
74function vRP.tryPayment(user_id,amount)
75 if amount < 0 then
76 print("**ADMIN INJECT NEGATIVE MONEY tryPayment**")
77 SendWebhookMessage(ac_webhook_gameplay, "**ADMIN INJECT NEGATIVE MONEY tryPayment** \n```\nUser ID: "..user_id.." Amount: "..amount.."```")
78 return false
79 else
80 local money = vRP.getMoney(user_id)
81 if money >= amount then
82 vRP.setMoney(user_id,money-amount)
83 return true
84 else
85 return false
86 end
87 end
88end
89
90-- give money
91function vRP.giveMoney(user_id,amount)
92 if amount > 0 then
93 local money = vRP.getMoney(user_id)
94 vRP.setMoney(user_id,money+amount)
95 end
96end
97
98-- get bank money
99function vRP.getBankMoney(user_id)
100 local tmp = vRP.getUserTmpTable(user_id)
101 if tmp then
102 return tmp.bank or 0
103 else
104 return 0
105 end
106end
107
108-- set bank money
109function vRP.setBankMoney(user_id,value)
110 local tmp = vRP.getUserTmpTable(user_id)
111 if tmp then
112 tmp.bank = value
113 end
114 local source = vRP.getUserSource(user_id)
115 TriggerClientEvent('vrp:displayBank', source, value) -- gcphone
116end
117
118-- give bank money
119function vRP.giveBankMoney(user_id,amount)
120 if amount > 0 then
121 local money = vRP.getBankMoney(user_id)
122 vRP.setBankMoney(user_id,money+amount)
123 end
124end
125
126-- try a withdraw
127-- return true or false (withdrawn if true)
128function vRP.tryWithdraw(user_id,amount)
129 local money = vRP.getBankMoney(user_id)
130 if amount > 0 and money >= amount then
131 vRP.setBankMoney(user_id,money-amount)
132 vRP.giveMoney(user_id,amount)
133 return true
134 else
135 return false
136 end
137end
138
139-- try a deposit
140-- return true or false (deposited if true)
141function vRP.tryDeposit(user_id,amount)
142 if amount > 0 and vRP.tryPayment(user_id,amount) then
143 vRP.giveBankMoney(user_id,amount)
144 return true
145 else
146 return false
147 end
148end
149
150-- try full payment (wallet + bank to complete payment)
151-- return true or false (debited if true)
152function vRP.tryFullPayment(user_id,amount)
153 if amount == nil then return false
154
155 elseif amount < 0 then
156 vRPclient.notify(player,{"~r~Tentativa de ~w~exploit ~r~reportada!"})
157 print("**ADMIN INJECT NEGATIVE MONEY tryFullPayment**")
158 SendWebhookMessage(ac_webhook_gameplay, "**ADMIN INJECT NEGATIVE MONEY tryFullPayment** \n```\nUser ID: "..user_id.." Amount: "..amount.."```")
159 return false
160 else
161 local money = vRP.getMoney(user_id)
162 if money >= amount then -- enough, simple payment
163 return vRP.tryPayment(user_id, amount)
164 else -- not enough, withdraw -> payment
165 if vRP.tryWithdraw(user_id, amount-money) then -- withdraw to complete amount
166 return vRP.tryPayment(user_id, amount)
167 end
168 end
169
170 return false
171 end
172end
173
174-- events, init user account if doesn't exist at connection
175AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
176 MySQL.execute("vRP/money_init_user", {user_id = user_id, wallet = cfg.open_wallet, bank = cfg.open_bank}, function(affected)
177 -- load money (wallet,bank)
178 local tmp = vRP.getUserTmpTable(user_id)
179 if tmp then
180 MySQL.query("vRP/get_money", {user_id = user_id}, function(rows, affected)
181 if #rows > 0 then
182 tmp.bank = rows[1].bank
183 tmp.wallet = rows[1].wallet
184 end
185 end)
186 end
187 end)
188end)
189
190-- save money on leave
191AddEventHandler("vRP:playerLeave",function(user_id,source)
192 -- (wallet,bank)
193 local tmp = vRP.getUserTmpTable(user_id)
194 if tmp and tmp.wallet ~= nil and tmp.bank ~= nil then
195 MySQL.execute("vRP/set_money", {user_id = user_id, wallet = tmp.wallet, bank = tmp.bank})
196 end
197end)
198
199-- save money (at same time that save datatables)
200AddEventHandler("vRP:save", function()
201 for k,v in pairs(vRP.user_tmp_tables) do
202 if v.wallet ~= nil and v.bank ~= nil then
203 MySQL.execute("vRP/set_money", {user_id = k, wallet = v.wallet, bank = v.bank})
204 end
205 end
206end)
207
208-- money hud
209AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
210 if first_spawn then
211 -- add money display
212 vRPclient.setDiv(source,{"money",cfg.display_css,lang.money.display({vRP.getMoney(user_id)})})
213 vRPclient.setDiv(source,{"sujo",cfg.display_css2,lang.money.display({vRP.getInventoryItemAmount(user_id, "dirty_money")})})
214 end
215end)
216
217local function ch_give(player,choice)
218 -- get nearest player
219 local user_id = vRP.getUserId(player)
220 if user_id ~= nil then
221 vRPclient.getNearestPlayer(player,{10},function(nplayer)
222 if nplayer ~= nil then
223 local nuser_id = vRP.getUserId(nplayer)
224 if nuser_id ~= nil then
225 -- prompt number
226 vRP.prompt(player,lang.money.give.prompt(),"",function(player,amount)
227 local amount = parseInt(amount)
228 if amount > 0 and vRP.tryPayment(user_id,amount) then
229 vRP.logToFile("moneySendLog.txt","Enviado Por: "..user_id.." - Para: "..nuser_id.." - Amount: " ..amount)
230 vRP.giveMoney(nuser_id,amount)
231 vRPclient.notify(player,{lang.money.given({amount})})
232 vRPclient.notify(nplayer,{lang.money.received({amount})})
233 else
234 vRPclient.notify(player,{lang.money.not_enough()})
235 end
236 end)
237 else
238 vRPclient.notify(player,{lang.common.no_player_near()})
239 end
240 else
241 vRPclient.notify(player,{lang.common.no_player_near()})
242 end
243 end)
244 end
245end
246
247-- add player give money to main menu
248vRP.registerMenuBuilder("main", function(add, data)
249 local user_id = vRP.getUserId(data.player)
250 if user_id ~= nil then
251 local choices = {}
252 choices[lang.money.give.title()] = {ch_give, lang.money.give.description()}
253
254 add(choices)
255 end
256end)