· 6 years ago · Nov 26, 2019, 03:06 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 credit INTEGER,
13 loan INTEGER,
14 CONSTRAINT pk_user_moneys PRIMARY KEY(user_id),
15 CONSTRAINT fk_user_moneys_users FOREIGN KEY(user_id) REFERENCES vrp_users(id) ON DELETE CASCADE
16);
17]])
18
19-- 세금
20
21MySQL.createCommand("vRP/get_tax","SELECT statecoffers FROM elysium_tax")
22
23MySQL.createCommand("vRP/add_tax","UPDATE elysium_tax SET statecoffers = statecoffers + @statecoffers")
24MySQL.createCommand("vRP/del_tax","UPDATE elysium_tax SET statecoffers = statecoffers - @statecoffers")
25
26MySQL.createCommand("vRP/add_hi","UPDATE elysium_tax SET hi = hi + @hi")
27MySQL.createCommand("vRP/delete_hi","UPDATE elysium_tax SET hi = hi - @hi")
28
29MySQL.createCommand("vRP/add_army","UPDATE elysium_tax SET army = army + @army")
30MySQL.createCommand("vRP/del_army","UPDATE elysium_tax SET army = army - @army")
31
32-- 법인
33
34MySQL.createCommand("vRP/add_companycapital","UPDATE elysium_company SET capital = capital + @capital WHERE user_id = @user_id")
35MySQL.createCommand("vRP/del_companycapital","UPDATE elysium_company SET capital = capital - @capital WHERE user_id = @user_id")
36MySQL.createCommand("vRP/get_company","SELECT code,name,capital,stockprice,stocks,salary,salary1,salary2,salary3,salary4,salary5,salary6,salary7,salary8,ceosalary FROM elysium_company WHERE user_id = @user_id")
37
38MySQL.createCommand("vRP/money_init_user","INSERT IGNORE INTO vrp_user_moneys(user_id,wallet,bank,credit,loanlimit,loan,creditrating,exp) VALUES(@user_id,@wallet,@bank,@credit,@loanlimit,@loan,@creditrating,@exp)")
39MySQL.createCommand("vRP/get_money","SELECT wallet,bank,credit,loanlimit,loan,creditrating,exp FROM vrp_user_moneys WHERE user_id = @user_id")
40MySQL.createCommand("vRP/set_money","UPDATE vrp_user_moneys SET wallet = @wallet, bank = @bank, credit = @credit, loanlimit = @loanlimit, loan = @loan, creditrating = @creditrating, exp = @exp WHERE user_id = @user_id")
41
42-- init tables
43MySQL.execute("vRP/money_tables")
44
45-- load config
46local cfg = module("cfg/money")
47
48-- API
49
50function comma_value(amount)
51 local formatted = amount
52 while true do
53 formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
54 if (k==0) then
55 break
56 end
57 end
58 return formatted
59end
60
61function vRP.getTax(cbr)
62 local task = Task(cbr)
63 MySQL.query("vRP/get_tax", {}, function(rows, affected)
64 local tax = rows[1]
65 task({tax})
66 end)
67end
68
69-- 법인
70
71function vRP.getUserCompany(user_id, cbr)
72 local task = Task(cbr)
73
74 if user_id ~= nil then
75 MySQL.query("vRP/get_company", {user_id = user_id}, function(rows, affected)
76 local company = rows[1]
77 task({company})
78 end)
79 else
80 task()
81 end
82end
83
84-- 경험치 시스템
85
86function vRP.getEXP(user_id)
87 local tmp = vRP.getUserTmpTable(user_id)
88 if tmp then
89 return tmp.exp or 0
90 else
91 return 0
92 end
93end
94
95function vRP.setEXP(user_id,value)
96 local tmp = vRP.getUserTmpTable(user_id)
97 if tmp then
98 tmp.exp = value
99 end
100end
101
102-- 대출 시스템
103
104function vRP.getLoan(user_id)
105 local tmp = vRP.getUserTmpTable(user_id)
106 if tmp then
107 return tmp.loan or 0
108 else
109 return 0
110 end
111end
112
113function vRP.setLoan(user_id,value)
114 local tmp = vRP.getUserTmpTable(user_id)
115 if tmp then
116 tmp.loan = value
117 end
118end
119
120function vRP.getLoanLimit(user_id)
121 local tmp = vRP.getUserTmpTable(user_id)
122 if tmp then
123 return tmp.loanlimit or 0
124 else
125 return 0
126 end
127end
128
129function vRP.setLoanLimit(user_id,value)
130 local tmp = vRP.getUserTmpTable(user_id)
131 if tmp then
132 tmp.loanlimit = value
133 end
134end
135
136function vRP.getCR(user_id)
137 local tmp = vRP.getUserTmpTable(user_id)
138 if tmp then
139 return tmp.creditrating or 0
140 else
141 return 0
142 end
143end
144
145function vRP.setCR(user_id,value)
146 local tmp = vRP.getUserTmpTable(user_id)
147 if tmp then
148 tmp.creditrating = value
149 end
150end
151
152
153-- get money
154-- cbreturn nil if error
155function vRP.getMoney(user_id)
156 local tmp = vRP.getUserTmpTable(user_id)
157 if tmp then
158 return tmp.wallet or 0
159 else
160 return 0
161 end
162end
163
164-- set money
165function vRP.setMoney(user_id,value)
166 local tmp = vRP.getUserTmpTable(user_id)
167 if tmp then
168 tmp.wallet = value
169 end
170
171-- update client display
172 local source = vRP.getUserSource(user_id)
173 if source ~= nil then
174 vRPclient.setDiv(source,{"money",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getMoney(user_id)).." $"})
175 end
176end
177
178
179-- 크레딧 겟
180
181function vRP.getCredit(user_id)
182 local tmp = vRP.getUserTmpTable(user_id)
183 if tmp then
184 return tmp.credit or 0
185 else
186 return 0
187 end
188end
189
190-- 크레딧 셋
191
192function vRP.setCredit(user_id,value)
193 local tmp = vRP.getUserTmpTable(user_id)
194 if tmp then
195 tmp.credit = value
196 end
197
198
199-- 크레딧 디스플레이
200
201 local source = vRP.getUserSource(user_id)
202 if source ~= nil then
203 vRPclient.setDiv(source,{"credit",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getCredit(user_id))..""})
204 end
205end
206
207-- try a payment (돈)
208-- return true or false (debited if true)
209function vRP.tryPayment(user_id,amount)
210 local money = vRP.getMoney(user_id)
211 if money >= amount then
212 vRP.setMoney(user_id,money-amount)
213 return true
214 else
215 return false
216 end
217end
218
219-- 트라이 페이먼트 (크레딧)
220function vRP.tryPaymentCredit(user_id,amount)
221 local credit = vRP.getCredit(user_id)
222 if credit >= amount then
223 vRP.setCredit(user_id,credit-amount)
224 return true
225 else
226 return false
227 end
228end
229
230-- give money
231function vRP.giveMoney(user_id,amount)
232 local money = vRP.getMoney(user_id)
233 vRP.setMoney(user_id,money+amount)
234end
235
236-- 크레딧 지급
237function vRP.giveCredit(user_id,amount)
238 local credita = vRP.getCredit(user_id)
239 vRP.setCredit(user_id,credita+amount)
240end
241
242-- get bank money
243function vRP.getBankMoney(user_id)
244 local tmp = vRP.getUserTmpTable(user_id)
245 if tmp then
246 return tmp.bank or 0
247 else
248 return 0
249 end
250end
251
252-- set bank money
253function vRP.setBankMoney(user_id,value)
254 local tmp = vRP.getUserTmpTable(user_id)
255 if tmp then
256 tmp.bank = value
257 end
258 local source = vRP.getUserSource(user_id)
259 if source ~= nil then
260 vRPclient.setDiv(source,{"bmoney",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getBankMoney(user_id)).." $"})
261 end
262end
263
264-- give bank money
265function vRP.giveBankMoney(user_id,amount)
266 if amount > 0 then
267 local money = vRP.getBankMoney(user_id)
268 vRP.setBankMoney(user_id,money+amount)
269 end
270end
271
272-- 출금
273-- return true or false (withdrawn if true)
274function vRP.tryWithdraw(user_id,amount)
275 local money = vRP.getBankMoney(user_id)
276 if amount > 0 and money >= amount then
277 vRP.setBankMoney(user_id,money-amount)
278 vRP.giveMoney(user_id,amount)
279 return true
280 else
281 return false
282 end
283end
284
285
286
287--법인 입금
288function vRP.tryDepositToCompany(user_id,company_id,amount)
289 local money = vRP.getBankMoney(user_id)
290 if amount > 0 and money >= amount then
291 vRP.setBankMoney(user_id,money-amount)
292 MySQL.execute("vRP/add_companycapital", {capital = amount, user_id = company_id})
293 return true
294 else
295 return false
296 end
297end
298
299--법인 출금
300function vRP.tryWithdrawToCompany(user_id,company_id,amount)
301 local money = vRP.getBankMoney(user_id)
302 if amount > 0 then
303 vRP.setBankMoney(user_id,money+amount)
304 MySQL.execute("vRP/del_companycapital", {capital = amount, user_id = company_id})
305 return true
306 else
307 return false
308 end
309end
310
311
312-- 국고 출금
313
314function vRP.tryWithdrawToTax(user_id,amount)
315 local money = vRP.getBankMoney(user_id)
316 if amount > 0 then
317 vRP.setBankMoney(user_id,money+amount)
318 MySQL.execute("vRP/del_tax", {statecoffers = amount})
319 return true
320 else
321 return false
322 end
323end
324
325
326
327-- 입금
328-- return true or false (deposited if true)
329function vRP.tryDeposit(user_id,amount)
330 if amount > 0 and vRP.tryPayment(user_id,amount) then
331 vRP.giveBankMoney(user_id,amount)
332 return true
333 else
334 return false
335 end
336end
337
338function vRP.addTax(amount)
339 if amount > 0 then
340 MySQL.execute("vRP/add_tax", {statecoffers = amount})
341 return true
342 else
343 return false
344 end
345end
346
347-- 크레딧 환전
348function vRP.tryDepositCredit(user_id,amount)
349 if amount > 0 and vRP.tryPaymentCredit(user_id,amount) then
350 vRP.giveBankMoney(user_id,amount*1000)
351 return true
352 else
353 return false
354 end
355end
356
357-- 크레딧 역환전
358function vRP.tryMoneyToCredit(user_id,amount)
359 if amount > 0 and vRP.tryFullPayment(user_id,amount*1200) then
360 vRP.giveCredit(user_id,amount)
361 return true
362 else
363 return false
364 end
365end
366
367-- try full payment (wallet + bank to complete payment)
368-- return true or false (debited if true)
369function vRP.tryFullPayment(user_id,amount)
370 local money = vRP.getMoney(user_id)
371 if money >= amount then -- enough, simple payment
372 return vRP.tryPayment(user_id, amount)
373 else -- not enough, withdraw -> payment
374 if vRP.tryWithdraw(user_id, amount-money) then -- withdraw to complete amount
375 return vRP.tryPayment(user_id, amount)
376 end
377 end
378
379 return false
380end
381
382-- events, init user account if doesn't exist at connection
383AddEventHandler("vRP:playerJoin",function(user_id,source,name,last_login)
384 MySQL.execute("vRP/money_init_user", {user_id = user_id, wallet = cfg.open_wallet, bank = cfg.open_bank, credit = cfg.open_credit, loanlimit = cfg.open_loanlimit, loan = cfg.open_loan, creditrating = cfg.open_creditrating, exp = cfg.open_exp}, function(affected)
385 -- load money (wallet,bank)
386 local tmp = vRP.getUserTmpTable(user_id)
387 if tmp then
388 MySQL.query("vRP/get_money", {user_id = user_id}, function(rows, affected)
389 if #rows > 0 then
390 tmp.bank = rows[1].bank
391 tmp.wallet = rows[1].wallet
392 tmp.credit = rows[1].credit
393 tmp.loanlimit = rows[1].loanlimit
394 tmp.loan = rows[1].loan
395 tmp.creditrating = rows[1].creditrating
396 tmp.exp = rows[1].exp
397 end
398 end)
399 end
400 end)
401end)
402
403-- save money on leave
404AddEventHandler("vRP:playerLeave",function(user_id,source)
405 -- (wallet,bank)
406 local tmp = vRP.getUserTmpTable(user_id)
407 if tmp and tmp.wallet ~= nil and tmp.bank ~= nil and tmp.credit ~= nil and tmp.loanlimit ~= nil and tmp.loan ~= nil and tmp.creditrating ~= nil and tmp.exp ~= nil then
408 MySQL.execute("vRP/set_money", {user_id = user_id, wallet = tmp.wallet, bank = tmp.bank, credit = tmp.credit, loanlimit = tmp.loanlimit, loan = tmp.loan, creditrating = tmp.creditrating, exp = tmp.exp})
409 end
410end)
411
412-- save money (at same time that save datatables)
413AddEventHandler("vRP:save", function()
414 for k,v in pairs(vRP.user_tmp_tables) do
415 if v.wallet ~= nil and v.bank ~= nil and v.credit ~= nil and v.loanlimit ~= nil and v.loan ~= nil and v.creditrating ~= nil and v.exp ~= nil then
416 MySQL.execute("vRP/set_money", {user_id = k, wallet = v.wallet, bank = v.bank, credit = v.credit, loanlimit = v.loanlimit, loan = v.loan, creditrating = v.creditrating, exp = v.exp})
417 end
418 end
419end)
420
421-- money hud
422AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
423 if first_spawn then
424 -- add money display
425 vRPclient.setDiv(source,{"money",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getMoney(user_id)).." $"})
426 vRPclient.setDiv(source,{"bmoney",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getBankMoney(user_id)).." $"})
427 vRPclient.setDiv(source,{"credit",cfg.display_css," <span class=\"symbol\"></span> "..comma_value(vRP.getCredit(user_id))..""})
428 end
429end)
430
431local function ch_give(player,choice)
432 -- get nearest player
433 local user_id = vRP.getUserId(player)
434 if user_id ~= nil then
435 vRPclient.getNearestPlayer(player,{10},function(nplayer)
436 if nplayer ~= nil then
437 local nuser_id = vRP.getUserId(nplayer)
438 if nuser_id ~= nil then
439 -- prompt number
440 vRP.prompt(player,lang.money.give.prompt(),"",function(player,amount)
441 local amount = parseInt(amount)
442 if amount > 0 and vRP.tryPayment(user_id,amount) then
443 vRP.giveMoney(nuser_id,amount)
444 vRPclient.notify(player,{lang.money.given({amount})})
445 vRPclient.notify(nplayer,{lang.money.received({amount})})
446 else
447 vRPclient.notify(player,{lang.money.not_enough()})
448 end
449 end)
450 else
451 vRPclient.notify(player,{lang.common.no_player_near()})
452 end
453 else
454 vRPclient.notify(player,{lang.common.no_player_near()})
455 end
456 end)
457 end
458end
459
460local function ch_givec(player,choice)
461 -- 크레딧 송금
462 local user_id = vRP.getUserId(player)
463 if user_id ~= nil then
464 vRPclient.getNearestPlayer(player,{10},function(nplayer)
465 if nplayer ~= nil then
466 local nuser_id = vRP.getUserId(nplayer)
467 if nuser_id ~= nil then
468 -- prompt number
469 vRP.prompt(player,lang.credit.give.prompt(),"",function(player,amount)
470 local amount = parseInt(amount)
471 if amount > 0 and vRP.tryPaymentCredit(user_id,amount) then
472 vRP.giveCredit(nuser_id,amount)
473 vRPclient.notify(player,{lang.credit.given({amount})})
474 vRPclient.notify(nplayer,{lang.credit.received({amount})})
475 else
476 vRPclient.notify(player,{lang.credit.not_enough()})
477 end
478 end)
479 else
480 vRPclient.notify(player,{lang.common.no_player_near()})
481 end
482 else
483 vRPclient.notify(player,{lang.common.no_player_near()})
484 end
485 end)
486 end
487end
488
489-- add player give money to main menu
490vRP.registerMenuBuilder("main", function(add, data)
491 local user_id = vRP.getUserId(data.player)
492 if user_id ~= nil then
493 local choices = {}
494 choices[lang.money.give.title()] = {ch_give, lang.money.give.description()}
495
496 add(choices)
497 end
498end)
499
500vRP.registerMenuBuilder("main", function(add, data)
501 local user_id = vRP.getUserId(data.player)
502 --if user_id == 1 or 2 then
503 if user_id ~= nil then
504 local choices = {}
505 choices[lang.credit.give.title()] = {ch_givec, lang.credit.give.description()}
506
507 add(choices)
508 end
509end)