Skip to content

Commit

Permalink
another checkpoint commit, nothing is tested, i doubt it even loads
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxionary committed Mar 5, 2022
1 parent 718b2c1 commit e9f76a2
Show file tree
Hide file tree
Showing 21 changed files with 924 additions and 192 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* redo how storage is linked - punch node instead of right-click
* remove dependency on default
2 changes: 2 additions & 0 deletions aliases.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

minetest.register_alias("smartshop:wifistorage", "smartshop:storage")
63 changes: 12 additions & 51 deletions api/formspec.lua
Original file line number Diff line number Diff line change
@@ -1,58 +1,19 @@
local formspec_escape = minetest.formspec_escape

local formspec_pos = smartshop.util.formspec_pos
local string_to_pos = minetest.string_to_pos

--------------------

local mesein_descriptions = {
"Don't send",
"Incoming",
"Outgoing",
"Both",
}

function smartshop.api.build_wifi_formspec(obj)
local fpos = formspec_pos(obj.pos)
local title = formspec_escape(obj:get_title())

local gui_parts = {
"size[12,9]",
("field[0.3,5.3;2,1;title;;%s]"):format(title),
"tooltip[title;Used with connected smartshops]",
"button_exit[0,6;2,1;save;Save]",
("list[nodemeta:%s;main;0,0;12,5;]"):format(fpos),
"list[current_player;main;2,5;8,4;]",
("listring[nodemeta:%s;main]"):format(fpos),
"listring[current_player;main]"
}

if smartshop.has.mesecons then
local mesein = obj:get_mesein()
local description = mesein_descriptions[mesein]
table.insert(gui_parts, ("button[0,7;2,1;mesesin;%s]"):format(description))
table.insert(gui_parts, "tooltip[mesesin;Send mesecon signal when items from shops does:]")
function smartshop.api.on_player_receive_fields(player, formname, fields)
local spos = formname:match("smartshop:(.+)")
local pos = spos and string_to_pos(spos)
local obj = smartshop.api.get_object(pos)
if obj then
obj:receive_fields(player, fields)
return true
end

return table.concat(gui_parts, "")
end

function smartshop.api.wifi_receive_fields(player, pressed)
local player_name = player:get_player_name()
if not pos then return end
minetest.register_on_player_receive_fields(function(player, formname, fields)
return smartshop.api.on_player_receive_fields(player, formname, fields)
end)

if pressed.mesesin then
toggle_mesein(meta)
smartshop.wifi_showform(pos, player)
elseif pressed.save then
local title = pressed.title
if not title or title == "" then
title = "wifi " .. minetest.pos_to_string(pos)
end
smartshop.set_title(meta, title)
local spos = minetest.pos_to_string(pos)
smartshop.log("action", "%s set title of wifi storage at %s to %s", player_name, spos, title)
smartshop.player_pos[player_name] = nil
elseif pressed.quit then
smartshop.player_pos[player_name] = nil
end
end
--------------------
48 changes: 31 additions & 17 deletions api/init.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
smartshop.api = {}


local shop_node_names = {
"smartshop:shop",
"smartshop:shop_full",
"smartshop:shop_empty",
"smartshop:shop_used",
"smartshop:shop_admin"
}

function smartshop.api.is_smartshop(pos)
function smartshop.api.is_shop(pos)
if not pos then return end
local node_name = minetest.get_node(pos).name
for _, name in ipairs(shop_node_names) do
for _, name in ipairs(smartshop.shop_node_names) do
if name == node_name then
return true
end
end
return false
end

smartshop.dofile("api", "node_class")
smartshop.dofile("api", "shop_class")
smartshop.dofile("api", "storage_class")
function smartshop.api.is_storage(pos)
if not pos then return end
local node_name = minetest.get_node(pos).name
for _, name in ipairs(smartshop.storage_node_names) do
if name == node_name then
return true
end
end
return false
end

--[[
TODO: i'm not certain whether memoizing the returned objects is worth doing or not.
also it'd require clearing the memo when a node is destroyed.
]]
function smartshop.api.get_object(pos)
if smartshop.api.is_smartshop(pos) then
return smartshop.node_class:new(pos)
else
if not pos then return end
if smartshop.api.is_shop(pos) then
return smartshop.shop_class:new(pos)
elseif smartshop.api.is_storage(pos) then
return smartshop.storage_class:new(pos)
end
end

smartshop.dofile("api", "node_class")
smartshop.dofile("api", "shop_class")
smartshop.dofile("api", "storage_class")
smartshop.dofile("api", "player_inv_class")

smartshop.dofile("api", "formspec")

smartshop.dofile("api", "purchase_mechanics")
smartshop.dofile("api", "storage_linking")

smartshop.dofile("api", "entities")
116 changes: 94 additions & 22 deletions api/node_class.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local get_formspec_pos = smartshop.util.get_formspec_pos
local check_player_privs = minetest.check_player_privs
local pos_to_string = minetest.pos_to_string

local check_shop_add_remainder = smartshop.util.check_shop_add_remainder
local check_shop_remove_remainder = smartshop.util.check_shop_remove_remainder
local get_formspec_pos = smartshop.util.get_formspec_pos

--------------------

local node_class = {}
Expand All @@ -9,9 +13,12 @@ smartshop.node_class = node_class
--------------------

function node_class:new(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local object = {
pos = pos,
meta = minetest.get_meta(pos),
meta = meta,
inv = inv,
}
setmetatable(object, self)
self.__index = self -- magic for inheritance?
Expand All @@ -28,30 +35,80 @@ end

--------------------

function node_class:set_owner(owner)
self.meta:set_string("owner", owner)
self.meta:mark_as_private("owner")
end

function node_class:get_owner()
return self.meta:get_string("owner")
end

function node_class:set_owner(owner)
self.meta:set_string("owner", owner)
function node_class:is_owner(player)
if type(player) == "userdata" then
return player:get_player_name() == self:get_owner()
else
return player == self:get_owner()
end
end

function node_class:set_infotext(format, ...)
self:set_string("infotext", format:format(...))
end

function node_class:get_infotext()
return self.meta:get_string("infotext")
end

function node_class:set_infotext(format, ...)
self:set_string("infotext", format:format(...))
function node_class:room_for_item(stack)
return self.inv:room_for_item("main", stack)
end

function node_class:get_inventory()
return self.meta:get_inventory()
function node_class:add_item(stack)
local remainder = self.inv:add_item("main", stack)
check_shop_add_remainder(self, remainder)
return remainder
end

function node_class:contains_item(stack, match_meta)
return self.inv:contains_item("main", stack, match_meta)
end

function node_class:remove_item(stack, match_meta)
local inv = self.inv

local remainder
if match_meta then
local stack_string = stack:to_string()

local index
for i, inv_stack in ipairs(inv:get_list("main")) do
if inv_stack:to_string() == stack_string then
index = i
break
end
end

if index then
remainder = ItemStack()
inv:set_stack("main", index, remainder)
else
remainder = stack
end

else
remainder = inv:remove_item("main", stack)
end

check_shop_remove_remainder(self, remainder)

return remainder
end

--------------------

function node_class:initialize_metadata(player)
local player_name = player:get_player_name()
function node_class:initialize_metadata(owner)
local player_name = owner:get_player_name()
self:set_owner(player_name)
end

Expand All @@ -61,23 +118,46 @@ end

--------------------

function node_class:on_destruct()
-- noop
end

--------------------

function node_class:on_rightclick(node, player, itemstack, pointed_thing)
self:show_formspec(player)
end

function node_class:show_formspec(player)
-- noop
end

function node_class:receive_fields(player, fields)
-- noop
end

--------------------

function node_class:update_appearance()
-- noop
end

function node_class:can_access(player)
local player_name = player:get_player_name()
--------------------

function node_class:can_access(player)
return (
self:get_owner(pos) == player_name or
minetest.check_player_privs(player_name, {protection_bypass = true})
self:is_owner(player) or
check_player_privs(player, {protection_bypass = true})
)
end

function node_class:can_dig(player)
local inv = self.inv
return inv:is_empty("main") and self:can_access(player)
end

--------------------

function node_class:allow_metadata_inventory_put(listname, index, stack, player)
if not self:can_access(player) then
return 0
Expand Down Expand Up @@ -125,11 +205,3 @@ function node_class:on_metadata_inventory_take(listname, index, stack, player)
minetest.pos_to_string(self.pos)
)
end

function node_class:can_dig(player)
local inv = self:get_inventory()
local owner = self:get_owner()
if (owner == "" or self:can_access(player)) and inv:is_empty("main") then
return true
end
end
52 changes: 52 additions & 0 deletions api/player_inv_class.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- this will allow us to more easily extend behavior e.g. interacting directly w/ inventory bags

local check_player_add_remainder = smartshop.util.check_player_add_remainder
local check_player_remove_remainder = smartshop.util.check_player_remove_remainder

--------------------


local player_inv_class = {}
smartshop.player_inv_class = player_inv_class

--------------------

function player_inv_class:new(player)
local name = player:get_player_name()
local inv = player:get_inventory()
local object = {
player = player,
name = name,
inv = inv,
}
setmetatable(object, self)
self.__index = self -- magic for inheritance?
return object
end

function smartshop.api.get_player_inv(player)
return player_inv_class:new(player)
end

--------------------

function player_inv_class:room_for_item(stack)
return self.inv:room_for_item("main", stack)
end

function player_inv_class:add_item(stack)
local remainder = self.inv:add_item("main", stack)
check_player_add_remainder(remainder)
return remainder
end

function player_inv_class:contains_item(stack, match_meta)
return self.inv:contains_item("main", stack, match_meta)
end

function player_inv_class:remove_item(stack)
local remainder = self.inv:remove_item("main", stack)
check_player_remove_remainder(remainder)
return remainder
end

Loading

0 comments on commit e9f76a2

Please sign in to comment.