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