· 6 years ago · Apr 03, 2019, 07: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 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.getMoney(user_id)
42 local tmp = vRP.getUserTmpTable(user_id)
43 if tmp then
44 return tmp.wallet or 0
45 else
46 return 0
47 end
48end
49
50 -- update client display
51 local source = vRP.getUserSource(user_id)
52 if source ~= nil then
53 vRPclient.setDivContent(source,{"money",lang.money.display({value})})
54 end
55
56
57
58-- try a payment
59-- return true or false (debited if true)
60function vRP.tryPayment(user_id,amount)
61 local money = vRP.getMoney(user_id)
62 if money >= amount then
63 vRP.setMoney(user_id,money-amount)
64 return true
65 else
66 return false
67 end
68end
69
70-- give money
71function vRP.giveMoney(user_id,amount)
72 local money = vRP.getMoney(user_id)
73 vRP.setMoney(user_id,money+amount)
74end
75
76-- get bank money
77function vRP.getBankMoney(user_id)
78 local tmp = vRP.getUserTmpTable(user_id)
79 if tmp then
80 return tmp.bank or 0
81 else
82 return 0
83 end
84end
85
86-- set bank money
87function vRP.setBankMoney(user_id,value)
88 local tmp = vRP.getUserTmpTable(user_id)
89 if tmp then
90 tmp.bank = value
91 end
92 local source = vRP.getUserSource(user_id)
93 if source ~= nil then
94 vRPclient.setDivContent(source,{"bmoney",lang.money.bdisplay({value})})
95 end
96end
97
98-- give bank money
99function vRP.giveBankMoney(user_id,amount)
100 if amount > 0 then
101 local money = vRP.getBankMoney(user_id)
102 vRP.setBankMoney(user_id,money+amount)
103 end
104end
105
106-- try a withdraw
107-- return true or false (withdrawn if true)
108function vRP.tryWithdraw(user_id,amount)
109 local money = vRP.getBankMoney(user_id)
110 if amount > 0 and money >= amount then
111 vRP.setBankMoney(user_id,money-amount)
112 vRP.giveMoney(user_id,amount)
113 return true
114 else
115 return false
116 end
117end
118
119-- try a deposit
120-- return true or false (deposited if true)
121function vRP.tryDeposit(user_id,amount)
122 if amount > 0 and vRP.tryPayment(user_id,amount) then
123 vRP.giveBankMoney(user_id,amount)
124 return true
125 else
126 return false
127 end
128end
129
130-- try full payment (wallet + bank to complete payment)
131-- return true or false (debited if true)
132function vRP.tryFullPayment(user_id,amount)
133 local money = vRP.getMoney(user_id)
134 if money >= amount then -- enough, simple payment
135 return vRP.tryPayment(user_id, amount)
136 else -- not enough, withdraw -> payment
137 if vRP.tryWithdraw(user_id, amount-money) then -- withdraw to complete amount
138 return vRP.tryPayment(user_id, amount)
139 end
140 end
141
142 return false
143end
144
145-- events, init user account if doesn't exist at connection
146AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
147 MySQL.execute("vRP/money_init_user", {user_id = user_id, wallet = cfg.open_wallet, bank = cfg.open_bank}, function(affected)
148 -- load money (wallet,bank)
149 local tmp = vRP.getUserTmpTable(user_id)
150 if tmp then
151 MySQL.query("vRP/get_money", {user_id = user_id}, function(rows, affected)
152 if #rows > 0 then
153 tmp.bank = rows[1].bank
154 tmp.wallet = rows[1].wallet
155 end
156 end)
157 end
158 end)
159end)
160
161-- save money on leave
162AddEventHandler("vRP:playerLeave",function(user_id,source)
163 -- (wallet,bank)
164 local tmp = vRP.getUserTmpTable(user_id)
165 if tmp and tmp.wallet ~= nil and tmp.bank ~= nil then
166 MySQL.execute("vRP/set_money", {user_id = user_id, wallet = tmp.wallet, bank = tmp.bank})
167 end
168end)
169
170-- save money (at same time that save datatables)
171AddEventHandler("vRP:save", function()
172 for k,v in pairs(vRP.user_tmp_tables) do
173 if v.wallet ~= nil and v.bank ~= nil then
174 MySQL.execute("vRP/set_money", {user_id = k, wallet = v.wallet, bank = v.bank})
175 end
176 end
177end)
178
179-- money hud
180AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
181 if first_spawn then
182 -- add money display
183 vRPclient.setDiv(source,{"money",cfg.display_css,lang.money.display({vRP.getMoney(user_id)})})
184 vRPclient.setDiv(source,{"bmoney",cfg.display_css,lang.money.bdisplay({vRP.getBankMoney(user_id)})})
185 end
186end)
187
188local function ch_give(player,choice)
189 -- get nearest player
190 local user_id = vRP.getUserId(player)
191 if user_id ~= nil then
192 vRPclient.getNearestPlayer(player,{10},function(nplayer)
193 if nplayer ~= nil then
194 local nuser_id = vRP.getUserId(nplayer)
195 if nuser_id ~= nil then
196 -- prompt number
197 vRP.prompt(player,lang.money.give.prompt(),"",function(player,amount)
198 local amount = parseInt(amount)
199 if amount > 0 and vRP.tryPayment(user_id,amount) then
200 vRP.giveMoney(nuser_id,amount)
201 vRPclient.notify(player,{lang.money.given({amount})})
202 vRPclient.notify(nplayer,{lang.money.received({amount})})
203 else
204 vRPclient.notify(player,{lang.money.not_enough()})
205 end
206 end)
207 else
208 vRPclient.notify(player,{lang.common.no_player_near()})
209 end
210 else
211 vRPclient.notify(player,{lang.common.no_player_near()})
212 end
213 end)
214 end
215end
216
217-- add player give money to main menu
218vRP.registerMenuBuilder("main", function(add, data)
219 local user_id = vRP.getUserId(data.player)
220 if user_id ~= nil then
221 local choices = {}
222 choices[lang.money.give.title()] = {ch_give, lang.money.give.description()}
223
224 add(choices)
225 end
226end)