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