forked from MinetestForFun/minercantile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallets.lua
308 lines (275 loc) · 10.1 KB
/
wallets.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
local states = {}
--function load a player's wallet
function minercantile.wallet.load_wallet(name)
if minercantile.wallets[name] == nil then
minercantile.wallets[name] = {}
end
local file, _ = io.open(minercantile.path_wallet..name, "r")
if file then
local data = minetest.deserialize(file:read("*all"))
file:close()
if data and type(data) == "table" then
if data.money then
minercantile.wallets[name].money = data.money
else
minercantile.wallets[name].money = 0
end
if data.transactions then
minercantile.wallets[name].transactions = table.copy(data.transactions)
else
minercantile.wallets[name].transactions = {}
end
return
end
end
--if new player then wallet is empty
minercantile.wallets[name].money = 0
minercantile.wallets[name].transactions = {}
end
function minercantile.wallet.save_wallet(name)
local input, err = io.open(minercantile.path_wallet..name, "w")
if input then
input:write(minetest.serialize(minercantile.wallets[name]))
input:close()
minetest.log("info", "saved " .. minercantile.path_wallet..name)
else
minetest.log("error", "open(" .. minercantile.path_wallet..name .. ", 'w') failed: " .. err)
end
--unload wallet if player offline
local connected = false
for _, player in pairs(minetest.get_connected_players()) do
local player_name = player:get_player_name()
if player_name and player_name ~= "" and player_name == name then
connected = true
break
end
end
if not connected then
minercantile.wallets[name] = nil
end
end
function minercantile.wallet.get_money(name)
if minercantile.wallets[name] == nil then
minercantile.wallet.load_wallet(name)
end
return minercantile.wallets[name].money
end
function minercantile.wallet.give_money(name, amount, transaction)
if minercantile.wallets[name] == nil then
minercantile.wallet.load_wallet(name)
end
minercantile.wallets[name].money = minercantile.wallet.get_money(name) + amount
if transaction then
local trans = os.date().. ":"..transaction
minercantile.add_transactions(name, trans)
end
minercantile.wallet.save_wallet(name)
end
function minercantile.wallet.take_money(name, amount, transaction)
if minercantile.wallets[name] == nil then
minercantile.wallet.load_wallet(name)
end
minercantile.wallets[name].money = minercantile.wallet.get_money(name) - amount
if transaction then
local trans = os.date().. ": "..transaction
minercantile.add_transactions(name, trans)
end
minercantile.wallet.save_wallet(name)
end
function minercantile.wallet.get_transactions(name)
if minercantile.wallets[name] == nil then
minercantile.wallet.load_wallet(name)
end
return minercantile.wallets[name].transactions
end
function minercantile.add_transactions(name, new_transaction)
local old = minercantile.wallet.get_transactions(name)
minercantile.wallets[name].transactions = {new_transaction}
for _, trans in pairs(old) do
table.insert(minercantile.wallets[name].transactions, trans)
if #minercantile.wallets[name].transactions > 9 then
break
end
end
end
function minercantile.send_money(sender, receiver, amount)
if minercantile.wallet.get_money(sender) < amount then
return false
end
minercantile.wallet.take_money(sender, amount, "Send "..amount.."$ to "..receiver)
minercantile.wallet.give_money(receiver, amount, "Received "..amount.."$ from "..sender)
return true
end
function minercantile.get_formspec_wallet(name)
if minercantile.wallets[name] == nil then
minercantile.wallet.load_wallet(name)
end
local formspec = {}
table.insert(formspec,"size[10,9]bgcolor[#2A2A2A;]label[4.4,0;My Wallet]")
table.insert(formspec,"label[0.5,1;Sold: ".. tostring(minercantile.wallet.get_money(name)) .."$]")
table.insert(formspec,"label[4,2.3;10 last transactions]")
local transactions = minercantile.wallet.get_transactions(name)
if #transactions < 1 then
table.insert(formspec,"label[3.5,4;There are no transactions]")
else
local y = 3
for _,transac in pairs(transactions) do
table.insert(formspec,"label[1.5,"..y..";".. transac .."]")
y = y+0.4
end
end
table.insert(formspec,"button[0,8.2;1.5,1;page;Transfert]")
table.insert(formspec,"button_exit[8,8.2;1.5,1;close;Close]")
return table.concat(formspec)
end
function minercantile.get_formspec_wallet_transfert(name)
local money = minercantile.wallet.get_money(name)
local formspec = {}
table.insert(formspec,"size[10,9]bgcolor[#2A2A2A;]label[4.4,0;My Wallet]")
table.insert(formspec,"label[0.5,1;Sold: ".. tostring(money) .."$]")
if money < 5 then
table.insert(formspec, "label[2,4.5;Sorry you can't send money, minimum amount is 5$]")
else
if not states[name] then
states[name] = {}
end
if not states[name].players_list or states[name].refresh then
states[name].refresh = nil
states[name].players_list = {}
states[name].selected_id = 0
for _,player in pairs(minetest.get_connected_players()) do
local player_name = player:get_player_name()
if player_name and player_name ~= "" and player_name ~= name then
table.insert(states[name].players_list, player_name)
end
end
states[name]["receiver"] = nil
end
if not states[name].amount then
states[name].amount = 5
end
if #states[name].players_list == 0 then
table.insert(formspec, "label[2,3.6;There are no player, refresh]")
table.insert(formspec,"button[6,3.4;2,1;refresh;refresh list]")
else
table.insert(formspec, "dropdown[3,3.5;3,1;receiver;"..table.concat(states[name].players_list, ",")..";"..states[name].selected_id.."]")
table.insert(formspec, "label[3.5,6.4;Send "..states[name]["amount"].."$ to "..(states[name]["receiver"] or "").." ?]")
table.insert(formspec,"button[4.1,7;1.5,1;send;send]")
table.insert(formspec,"button[6,3.4;1.5,1;refresh;refresh list]")
table.insert(formspec, "label[3.5,4.5;Amount to send (minimum 5$)]")
table.insert(formspec, "button[1.7,5;1,1;amount;-1]")
table.insert(formspec, "button[2.7,5;1,1;amount;-10]")
table.insert(formspec, "button[3.7,5;1,1;amount;-100]")
table.insert(formspec, "label[4.7,5.2;"..tostring(states[name]["amount"]).."]")
table.insert(formspec, "button[5.4,5;1,1;amount;+100]")
table.insert(formspec, "button[6.4,5;1,1;amount;+10]")
table.insert(formspec, "button[7.4,5;1,1;amount;+1]")
end
end
table.insert(formspec,"button[0,8.2;1.5,1;page;wallet]")
table.insert(formspec,"button_exit[8,8.2;1.5,1;close;Close]")
return table.concat(formspec)
end
function minercantile.get_formspec_wallet_transfert_send(name)
local formspec = {"size[6,3]bgcolor[#2A2A2A;]label[2,0;Validate sending]"}
table.insert(formspec, "label[2,1.2;Send "..tostring(states[name]["amount"]).."$ to ".. states[name]["receiver"] .."]")
table.insert(formspec, "button_exit[1.1,2.1;1.5,1;close;Abort]")
table.insert(formspec, "button[3.3,2.1;1.5,1;send;Send]")
return table.concat(formspec)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if not name or name == "" then return end
if formname == "minercantile:ended" then
states[name] = nil
return
elseif formname == "minercantile:wallet" then
if fields["quit"] or fields["close"] then
states[name] = nil
return
elseif fields["page"] then
minetest.show_formspec(name, "minercantile:transfert", minercantile.get_formspec_wallet_transfert(name))
return
end
elseif formname == "minercantile:transfert" then
if fields["quit"] then
states[name] = nil
return
elseif fields["page"] then
minetest.show_formspec(name, "minercantile:wallet", minercantile.get_formspec_wallet(name))
return
elseif fields["refresh"] then
states[name].refresh = true
elseif fields["amount"] then
local inc = tonumber(fields["amount"])
if inc ~= nil then
states[name]["amount"] = states[name]["amount"] + inc
end
if states[name]["amount"] > minercantile.wallet.get_money(name) then
states[name]["amount"] = minercantile.wallet.get_money(name)
end
if states[name]["amount"] < 5 then
states[name]["amount"] = 5
end
elseif fields["send"] then
if states[name]["receiver"] and states[name]["receiver"] ~= "" then
minetest.show_formspec(name, "minercantile:transfert_send", minercantile.get_formspec_wallet_transfert_send(name))
return
end
elseif fields["receiver"] then
for i, n in pairs(states[name].players_list) do
if n == fields["receiver"] then
states[name]["receiver"] = fields["receiver"]
states[name].selected_id = i
break
end
end
end
minetest.show_formspec(name, "minercantile:transfert", minercantile.get_formspec_wallet_transfert(name))
elseif formname == "minercantile:transfert_send" then
if fields["send"] then
if minercantile.send_money( name, states[name]["receiver"], states[name]["amount"]) then
minetest.show_formspec(name, "minercantile:ended", "size[5,3]bgcolor[#2A2A2A;]label[1.8,0;Validated]label[1.7,1;Money sent]button_exit[1.8,2.1;1.5,1;close;Close]")
else
minetest.show_formspec(name, "minercantile:ended", "size[5,3]bgcolor[#2A2A2A;]label[1.6,0;Error]label[1.6,1;Error occured]button_exit[1.8,2.1;1.5,1;close;Close]")
end
elseif fields["quit"] or fields["close"] then
states[name] = nil
return
end
end
end)
if (minetest.get_modpath("unified_inventory")) then
unified_inventory.register_button("wallet", {
type = "image",
image = "minercantile_gold_coin.png",
tooltip = "My Wallet",
show_with = "interact",
action = function(player)
local name = player:get_player_name()
if not name then return end
local formspec = minercantile.get_formspec_wallet(name)
minetest.show_formspec(name, "minercantile:wallet", formspec)
end,
})
else
minetest.register_chatcommand("wallet",{
params = "",
description = "Shows your money wallet",
privs = {interact= true},
func = function (name, params)
local formspec = minercantile.get_formspec_wallet(name)
minetest.show_formspec(name, "minercantile:wallet", formspec)
end,
})
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
if not name or name == "" then return end
minercantile.wallet.load_wallet(name)
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
if not name or name == "" then return end
minercantile.wallets[name] = nil
end)