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