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

View File

@ -25,7 +25,10 @@ local function build_list()
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 = {
"size[7,6]",
"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()
self.list = list
context.list = list
if not self.selected then
self.selected = 1
elseif self.selected > #list then
self.selected = #list
if not context.selected then
context.selected = 1
elseif context.selected > #list then
context.selected = #list
end
for i=1, #list do
@ -74,13 +77,13 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
end
fs[#fs + 1] = ";"
if self.selected then
fs[#fs + 1] = tostring(self.selected)
if context.selected then
fs[#fs + 1] = tostring(context.selected)
end
fs[#fs + 1] = "]"
if self.selected then
-- local area = list[self.selected]
if context.selected then
-- local area = list[context.selected]
-- 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,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, "")
end,
function(self, player, formname, fields)
on_receive_fields = function(context, player, fields)
if fields["list_areas"] then
local evt = minetest.explode_table_event(fields["list_areas"])
self.selected = evt.row
context.selected = evt.row
return true
end
@ -112,13 +116,13 @@ land.show_debug_to = lib_quickfs.register("land:debug", function(self, playernam
end
local function do_set(type)
do_set_list({ self.list[self.selected] }, type)
do_set_list({ context.list[context.selected] }, type)
areas:save()
return true
end
if self.selected then
if context.selected then
if fields.to_comm then
return do_set("commercial")
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
return do_set("residential")
elseif fields.unzone then
local area = self.list[self.selected]
local area = context.list[context.selected]
land.remove_zone(area.id)
return true
end
end
end, { land_admin = true })
end,
})
land.show_set_price_to = lib_quickfs.register("land:set_price", function(self, playername)
local area = self.args[1]
land.show_set_price_to = lib_quickfs.register("land:set_price", {
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)
local fs = {
"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, "")
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
land.set_price(area, player:get_player_name(),
tonumber(fields.price) or 100000)
end
end)
end,
})
land.show_buy_to = lib_quickfs.register("land:set_price", function(self, pname)
local area = self.args[1]
land.show_buy_to = lib_quickfs.register("land:buy", {
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.land_sale)
local price_changed = self.price and self.price ~= area.land_sale
self.price = area.land_sale
local price_changed = context.price and context.price ~= area.land_sale
context.price = area.land_sale
local fs = {
"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, "")
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
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
if fields.buy then
if self.price ~= area.land_sale then
if context.price ~= area.land_sale then
return true
end
land.buy(area, player:get_player_name())
end
end)
end,
})
company.register_panel({

View File

@ -8,10 +8,12 @@ function shop.show_shop_form(pname, pos)
end
shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(context, pname, pos)
assert(shop.can_admin(pname, pos))
shop.show_admin_form = lib_quickfs.register("shop:counter_admin", {
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)
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;",
"#999,Description,Stock,Price,Sales",
}
-- Description Stock PricePI Sold
-- Description Stock PricePI Sold
local items_kv = s:get_items()
local items = {}
@ -87,7 +89,8 @@ shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(conte
return table.concat(fs, "")
end,
function(context, player, formname, fields)
on_receive_fields = function(context, player, fields, pos)
if fields.list_items then
local evt = minetest.explode_table_event(fields.list_items)
context.selected = evt.row - 1
@ -102,10 +105,18 @@ shop.show_admin_form = lib_quickfs.register("shop:counter_admin", function(conte
shop.dirty = true
return true
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 })
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, "")
end,
function(context, player, formname, fields)
on_receive_fields = function(context, player, fields)
if fields.unassign then
local s = context.args[1]
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])
return false
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 = {}
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 = {}
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)
if formname ~= name then
return
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
end
local playername = player:get_player_name()
local context = player_contexts[playername]
if context and cb(context, player, formname, fields) then
local formspec = func(context, playername, unpack(context.args))
minetest.show_formspec(playername, name, formspec)
if context and def.on_receive_fields(context, player, fields, unpack(context.args)) then
def.show(context, player)
end
end)
return function(playername, ...)
if privs and not minetest.check_player_privs(playername, privs) then
def.show = function(context, player)
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
end
assert(playername, "Player name is nil!")
assert(pname, "Player name is nil!")
local context = {
playername = playername,
pname = pname,
args = { ... },
}
player_contexts[playername] = context
local formspec = func(context, playername, ...)
minetest.show_formspec(playername, name, formspec)
local player = minetest.get_player_by_name(pname)
if def.check and not def.check(context, player, ...) then
return
end
player_contexts[pname] = context
def.show(context, player)
end
end