forked from ChimneySwift/fancy_vend
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helpers.lua
More file actions
138 lines (121 loc) · 3.86 KB
/
node_helpers.lua
File metadata and controls
138 lines (121 loc) · 3.86 KB
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
function fancy_vend.swap_vendor(pos, vendor_type)
local node = core.get_node(pos)
node.name = vendor_type
core.swap_node(pos, node)
end
function fancy_vend.get_correct_vendor(settings)
if settings.admin_vendor then
if settings.depositor then
return "fancy_vend:admin_depo"
else
return "fancy_vend:admin_vendor"
end
else
if settings.depositor then
return "fancy_vend:player_depo"
else
return "fancy_vend:player_vendor"
end
end
end
local vendor_names = {
"fancy_vend:player_vendor",
"fancy_vend:player_depo",
"fancy_vend:admin_vendor",
"fancy_vend:admin_depo",
}
function fancy_vend.is_vendor(name)
for _,n in ipairs(vendor_names) do
if name == n then
return true
end
end
return false
end
function fancy_vend.refresh_vendor(pos)
local node = core.get_node(pos)
if node.name:split(":")[1] ~= "fancy_vend" then
return false, "not a vendor"
end
local settings = fancy_vend.get_vendor_settings(pos)
local meta = core.get_meta(pos)
local status, errorcode = fancy_vend.get_vendor_status(pos)
local correct_vendor = fancy_vend.get_correct_vendor(settings)
if status or errorcode ~= "no_output" then
meta:set_string("alerted", "false")
end
if status then
local input_desc = fancy_vend.get_item_description(settings.input_item)
local output_desc = fancy_vend.get_item_description(settings.output_item)
meta:set_string("infotext", (settings.admin_vendor and "Admin" or "Player")..
" Vendor trading "..settings.input_item_qty.." "..input_desc..
" for "..settings.output_item_qty.." "..output_desc..
" (owned by "..meta:get_string("owner")..")"
)
if meta:get_string("configured") == "" then
meta:set_string("configured", "true")
if core.get_modpath("awards") then
local name = meta:get_string("owner")
local data = awards.player(name)
-- Ensure fancy_vend_configure table is in data
if not data.fancy_vend_configure then
data.fancy_vend_configure = {}
end
awards.increment_item_counter(data, "fancy_vend_configure", correct_vendor)
local total_item_count = 0
for _, v in pairs(data.fancy_vend_configure) do
total_item_count = total_item_count + v
end
if awards.get_item_count(data, "fancy_vend_configure", "fancy_vend:player_vendor") >= 1 then
awards.unlock(name, "fancy_vend:seller")
end
if awards.get_item_count(data, "fancy_vend_configure", "fancy_vend:player_depo") >= 1 then
awards.unlock(name, "fancy_vend:trader")
end
if total_item_count >= 10 then
awards.unlock(name, "fancy_vend:shop_keeper")
end
if total_item_count >= 25 then
awards.unlock(name, "fancy_vend:merchant")
end
if total_item_count >= 100 then
awards.unlock(name, "fancy_vend:super_merchant")
end
if total_item_count >= 9001 then
awards.unlock(name, "fancy_vend:god_merchant")
end
end
end
if settings.depositor then
if meta:get_string("item") ~= settings.input_item then
meta:set_string("item", settings.input_item)
fancy_vend.update_item(pos, node)
end
else
if meta:get_string("item") ~= settings.output_item then
meta:set_string("item", settings.output_item)
fancy_vend.update_item(pos, node)
end
end
else
meta:set_string("infotext", "Inactive "..
(settings.admin_vendor and "Admin" or "Player")..
" Vendor"..fancy_vend.make_inactive_string(errorcode)..
" (owned by "..meta:get_string("owner")..")"
)
if meta:get_string("item") ~= "fancy_vend:inactive" then
meta:set_string("item", "fancy_vend:inactive")
fancy_vend.update_item(pos, node)
end
if not status and errorcode == "no_room" then
core.chat_send_player(meta:get_string("owner"),
"[Fancy_Vend]: Error with vendor at "..core.pos_to_string(pos, 0)..
": does not have room for payment."
)
meta:set_string("alerted", "true")
end
end
if correct_vendor ~= node.name then
fancy_vend.swap_vendor(pos, correct_vendor)
end
end