Implement new quick FS API

master
rubenwardy 2018-11-25 20:37:47 +00:00
parent a22c6f1238
commit 3830040ad6
5 changed files with 137 additions and 66 deletions

View File

@ -1,6 +1,7 @@
company.show_company_select_dialog = company.show_company_select_dialog = lib_quickfs.register("company:set_company", {
lib_quickfs.register("company:set_company", function(context, name) get = function(context, player)
local comps = company.get_companies_for_player(name) local pname = player:get_player_name()
local comps = company.get_companies_for_player(pname)
context.comps = _.map(comps, function(comp) context.comps = _.map(comps, function(comp)
return comp.name return comp.name
end) end)
@ -17,7 +18,7 @@ company.show_company_select_dialog =
"label[0,-0.1;Select a Company]", "label[0,-0.1;Select a Company]",
"textlist[-0.1,0.5;4,4;companies;", "textlist[-0.1,0.5;4,4;companies;",
table.concat(_.map(comps, function(comp) table.concat(_.map(comps, function(comp)
if comp:get_ceo_name() == name then if comp:get_ceo_name() == pname then
return minetest.formspec_escape(comp.title) return minetest.formspec_escape(comp.title)
else else
return minetest.formspec_escape(minetest.colorize("#c0c0c0", comp.name)) return minetest.formspec_escape(minetest.colorize("#c0c0c0", comp.name))
@ -31,7 +32,8 @@ company.show_company_select_dialog =
return table.concat(formspec, "") return table.concat(formspec, "")
end, end,
function(context, player, formname, fields)
on_receive_fields = function(context, player, fields)
if fields.companies then if fields.companies then
local evt = minetest.explode_textlist_event(fields.companies) local evt = minetest.explode_textlist_event(fields.companies)
if evt.type == "CHG" then if evt.type == "CHG" then
@ -50,7 +52,8 @@ company.show_company_select_dialog =
ret(player) ret(player)
return return
end end
end) end,
})
function company.get_company_header(pname, width, mode) function company.get_company_header(pname, width, mode)
local comp = company.get_active(pname) local comp = company.get_active(pname)

View File

@ -25,7 +25,10 @@ local function build_list()
end end
land.show_debug_to = lib_quickfs.register("land:debug", function(self, playername) land.show_debug_to = lib_quickfs.register("land:debug", {
privs = { land_admin = true },
get = function(context, player)
local fs = { local fs = {
"size[7,6]", "size[7,6]",
"tablecolumns[color;tree;text,width=10;text]", "tablecolumns[color;tree;text,width=10;text]",
@ -34,12 +37,12 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
} }
local list = build_list() local list = build_list()
self.list = list context.list = list
if not self.selected then if not context.selected then
self.selected = 1 context.selected = 1
elseif self.selected > #list then elseif context.selected > #list then
self.selected = #list context.selected = #list
end end
for i=1, #list do for i=1, #list do
@ -74,13 +77,13 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
end end
fs[#fs + 1] = ";" fs[#fs + 1] = ";"
if self.selected then if context.selected then
fs[#fs + 1] = tostring(self.selected) fs[#fs + 1] = tostring(context.selected)
end end
fs[#fs + 1] = "]" fs[#fs + 1] = "]"
if self.selected then if context.selected then
-- local area = list[self.selected] -- local area = list[context.selected]
-- fs[#fs + 1] = "box[5,1;1.8,0.8;#222]" -- fs[#fs + 1] = "box[5,1;1.8,0.8;#222]"
fs[#fs + 1] = "button[5,0;2,1;to_comm;Commercial]" fs[#fs + 1] = "button[5,0;2,1;to_comm;Commercial]"
fs[#fs + 1] = "button[5,1;2,1;to_inds;Industrial]" fs[#fs + 1] = "button[5,1;2,1;to_inds;Industrial]"
@ -95,10 +98,11 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
return table.concat(fs, "") return table.concat(fs, "")
end, end,
function(self, player, formname, fields)
on_receive_fields = function(context, player, fields)
if fields["list_areas"] then if fields["list_areas"] then
local evt = minetest.explode_table_event(fields["list_areas"]) local evt = minetest.explode_table_event(fields["list_areas"])
self.selected = evt.row context.selected = evt.row
return true return true
end end
@ -112,13 +116,13 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
end end
local function do_set(type) local function do_set(type)
do_set_list({ self.list[self.selected] }, type) do_set_list({ context.list[context.selected] }, type)
areas:save() areas:save()
return true return true
end end
if self.selected then if context.selected then
if fields.to_comm then if fields.to_comm then
return do_set("commercial") return do_set("commercial")
elseif fields.to_inds then elseif fields.to_inds then
@ -126,15 +130,20 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
elseif fields.to_resd then elseif fields.to_resd then
return do_set("residential") return do_set("residential")
elseif fields.unzone then elseif fields.unzone then
local area = self.list[self.selected] local area = context.list[context.selected]
land.remove_zone(area.id) land.remove_zone(area.id)
return true return true
end end
end end
end, { land_admin = true }) end,
})
land.show_set_price_to = lib_quickfs.register("land:set_price", function(self, playername) land.show_set_price_to = lib_quickfs.register("land:set_price", {
local area = self.args[1] check = function(context, player, area)
return land.can_set_price(area, player:get_player_name())
end,
get = function(context, player, area)
assert(area.owner and area.pos2) assert(area.owner and area.pos2)
local fs = { local fs = {
"size[3,2]", "size[3,2]",
@ -144,26 +153,28 @@ land.show_set_price_to = lib_quickfs.register("land:set_price", function(self, p
return table.concat(fs, "") return table.concat(fs, "")
end, end,
function(self, player, formname, fields)
local area = self.args[1]
if not area then
return
end
on_receive_fields = function(context, player, fields, area)
if fields.set then if fields.set then
land.set_price(area, player:get_player_name(), land.set_price(area, player:get_player_name(),
tonumber(fields.price) or 100000) tonumber(fields.price) or 100000)
end end
end) end,
})
land.show_buy_to = lib_quickfs.register("land:set_price", function(self, pname) land.show_buy_to = lib_quickfs.register("land:buy", {
local area = self.args[1] check = function(context, player, area)
return area ~= nil
end,
get = function(context, player, area)
local pname = context.pname
assert(area.owner and area.pos2) assert(area.owner and area.pos2)
assert(area.land_sale) assert(area.land_sale)
local price_changed = self.price and self.price ~= area.land_sale local price_changed = context.price and context.price ~= area.land_sale
self.price = area.land_sale context.price = area.land_sale
local fs = { local fs = {
"size[5,2.4]", "size[5,2.4]",
@ -189,25 +200,22 @@ land.show_buy_to = lib_quickfs.register("land:set_price", function(self, pname)
return table.concat(fs, "") return table.concat(fs, "")
end, end,
function(self, player, formname, fields)
local area = self.args[1]
if not area then
return
end
on_receive_fields = function(context, player, fields, area)
if fields.switch then if fields.switch then
company.show_company_select_dialog(player:get_player_name(), function(player2) company.show_company_select_dialog(player:get_player_name(), function(player2)
land.show_buy_to(player2:get_player_name(), unpack(self.args)) land.show_buy_to(player2:get_player_name(), unpack(context.args))
end) end)
end end
if fields.buy then if fields.buy then
if self.price ~= area.land_sale then if context.price ~= area.land_sale then
return true return true
end end
land.buy(area, player:get_player_name()) land.buy(area, player:get_player_name())
end end
end) end,
})
company.register_panel({ company.register_panel({

View File

@ -8,10 +8,12 @@ function shop.show_shop_form(pname, pos)
end end
shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(context, pname, pos) shop.show_admin_form = lib_quickfs.register("shop:counter_admin", {
assert(shop.can_admin(pname, pos)) check = function(context, player, pos)
return shop.can_admin(player:get_player_name(), pos)
end,
get = function(context, player, pos)
local s = shop.get_by_pos(pos) local s = shop.get_by_pos(pos)
assert(s) assert(s)
@ -26,8 +28,8 @@ shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(conte
"table[0,1;4.8,6;list_items;", "table[0,1;4.8,6;list_items;",
"#999,Description,Stock,Price,Sales", "#999,Description,Stock,Price,Sales",
} }
-- Description Stock PricePI Sold
-- Description Stock PricePI Sold
local items_kv = s:get_items() local items_kv = s:get_items()
local items = {} local items = {}
@ -87,7 +89,8 @@ shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(conte
return table.concat(fs, "") return table.concat(fs, "")
end, end,
function(context, player, formname, fields)
on_receive_fields = function(context, player, fields, pos)
if fields.list_items then if fields.list_items then
local evt = minetest.explode_table_event(fields.list_items) local evt = minetest.explode_table_event(fields.list_items)
context.selected = evt.row - 1 context.selected = evt.row - 1
@ -102,10 +105,18 @@ shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(conte
shop.dirty = true shop.dirty = true
return true return true
end end
end) end,
})
shop.show_chest_form = lib_quickfs.register("shop:chest", function(context, pname, s, pos) shop.show_chest_form = lib_quickfs.register("shop:chest", {
check = function(context, player, s, pos)
local area = land.get_by_pos(pos)
return area and company.check_perm(context.pname, area.owner,
"SHOP_CHEST", { area = area })
end,
get = function(context, player, s, pos)
local inv = minetest.get_inventory({ type = "node", pos = pos }) local inv = minetest.get_inventory({ type = "node", pos = pos })
s:chest_poll(pos, inv) s:chest_poll(pos, inv)
@ -143,7 +154,8 @@ shop.show_chest_form = lib_quickfs.register("shop:chest", function(context, pnam
return table.concat(fs, "") return table.concat(fs, "")
end, end,
function(context, player, formname, fields)
on_receive_fields = function(context, player, fields)
if fields.unassign then if fields.unassign then
local s = context.args[1] local s = context.args[1]
local pos = context.args[2] local pos = context.args[2]
@ -156,4 +168,5 @@ shop.show_chest_form = lib_quickfs.register("shop:chest", function(context, pnam
shop.show_admin_form(player:get_player_name(), context.args[2]) shop.show_admin_form(player:get_player_name(), context.args[2])
return false return false
end end
end) end,
})

View File

@ -0,0 +1,23 @@
# Lib QuickFS
```lua
mymod.show_form_to = lib_quickfs.register("mymod:form", {
-- Callback to check for permissions
check = function(context, player, ...)
return true
end,
--QuickFS can automatically make or wrap a check method with priv checks
privs = { kick = true },
-- Build the FS
get = function(context, pname, ...)
return "size[1,1]button[1,1;1,1;a;OK]"
end,
-- Handle event, return true to re-send the formspec
on_receive_fields = function(context, player, fields, ...)
end
})
```

View File

@ -1,39 +1,63 @@
lib_quickfs = {} lib_quickfs = {}
function lib_quickfs.register(name, func, cb, privs) function lib_quickfs.register(name, def)
assert(type(def) == "table")
assert(type(def.get) == "function")
assert(type(def.on_receive_fields) == "function")
local player_contexts = {} local player_contexts = {}
if def.privs then
local oldcheck = def.check
def.check = function(context, player, ...)
if not minetest.check_player_privs(player, def.privs) then
return false
end
return oldcheck and oldcheck(...) or true
end
end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= name then if formname ~= name then
return return
end end
if privs and not minetest.check_player_privs(player, privs) then local pname = player:get_player_name()
local context = player_contexts[pname]
if def.check and not def.check(context, player, unpack(context.args)) then
return return
end end
local playername = player:get_player_name() if context and def.on_receive_fields(context, player, fields, unpack(context.args)) then
local context = player_contexts[playername] def.show(context, player)
if context and cb(context, player, formname, fields) then
local formspec = func(context, playername, unpack(context.args))
minetest.show_formspec(playername, name, formspec)
end end
end) end)
return function(playername, ...) def.show = function(context, player)
if privs and not minetest.check_player_privs(playername, privs) then local formspec = def.get(context, player, unpack(context.args))
minetest.show_formspec(context.pname, name, formspec)
end
return function(pname, ...)
if def.privs and not minetest.check_player_privs(pname, def.privs) then
return return
end end
assert(playername, "Player name is nil!") assert(pname, "Player name is nil!")
local context = { local context = {
playername = playername, pname = pname,
args = { ... }, args = { ... },
} }
player_contexts[playername] = context
local formspec = func(context, playername, ...) local player = minetest.get_player_by_name(pname)
minetest.show_formspec(playername, name, formspec) if def.check and not def.check(context, player, ...) then
return
end
player_contexts[pname] = context
def.show(context, player)
end end
end end