Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to repair the station each time the primary formspec is opened #54

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions actions/repair_station.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local S = minetest.get_translator("travelnet")

return function (node_info, _, player)
local owner_name = node_info.props.owner
local owner_name = node_info.props.owner_name
local station_name = node_info.props.station_name
local station_network = node_info.props.station_network

Expand All @@ -15,25 +15,41 @@ return function (node_info, _, player)
return false, S("Update failed! Resetting this box on the travelnet.")
end

if travelnet.get_station(owner_name, station_network, station_name) then
return true, { formspec = travelnet.formspecs.primary }
end

-- if the station got lost from the network for some reason (savefile corrupted?) then add it again
if not travelnet.get_station(owner_name, station_network, station_name) then
local network = travelnet.get_or_create_network(owner_name, station_network)
local network = travelnet.get_or_create_network(owner_name, station_network)

local zeit = node_info.meta:get_int("timestamp")
if not zeit or type(zeit) ~= "number" or zeit < 100000 then
zeit = os.time()
end
local zeit = node_info.meta:get_int("timestamp")
if not zeit or type(zeit) ~= "number" or zeit < 100000 then
zeit = os.time()
end

-- add this station
network[station_name] = {
pos = node_info.pos,
timestamp = zeit
}
local station_count = 1 -- start at one, assume the station about to be created already exists
for existing_station_name in pairs(network) do
if existing_station_name == station_name then
return false, S("A station named '@1' already exists on this network. Please choose a different name!", station_name)
end
station_count = station_count+1
end

minetest.chat_send_player(owner_name,
S("Station '@1'" .. " " ..
"has been reattached to the network '@2'.", station_name, station_network))
travelnet.save_data()
-- we don't want too many stations in the same network because that would get confusing when displaying the targets
if travelnet.MAX_STATIONS_PER_NETWORK ~= 0 and station_count > travelnet.MAX_STATIONS_PER_NETWORK then
return false, S("Network '@1', already contains the maximum number (@2) of allowed stations per network. " ..
"Please choose a different/new network name.", station_network, travelnet.MAX_STATIONS_PER_NETWORK)
end

-- add this station
network[station_name] = {
pos = node_info.pos,
timestamp = zeit
}

minetest.chat_send_player(owner_name,
S("Station '@1'" .. " " ..
"has been reattached to the network '@2'.", station_name, station_network))
travelnet.save_data()
return true, { formspec = travelnet.formspecs.primary }
end
28 changes: 22 additions & 6 deletions formspecs-legacy.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local S = minetest.get_translator("travelnet")

local travelnet_form_name = "travelnet:show"

local player_formspec_data = travelnet.player_formspec_data
Expand All @@ -20,13 +22,27 @@ function travelnet.show_current_formspec(pos, meta, player_name)
player_formspec_data[player_name] = player_formspec_data[player_name] or {}
player_formspec_data[player_name].pos = pos
local node = minetest.get_node(pos)
local props = {
station_network = meta:get_string("station_network"),
station_name = meta:get_string("station_name"),
owner_name = meta:get_string("owner"),
is_elevator = travelnet.is_elevator(node.name)
}
if not travelnet.is_falsey_string(props.station_network) then
local success, result = travelnet.actions.repair_station({
pos = pos,
meta = meta,
node = node,
props = props
}, {}, minetest.get_player_by_name(player_name))

if not success then
minetest.chat_send_player(player_name, S("Error") .. ": " .. result)
end
end

travelnet.show_formspec(player_name,
travelnet.formspecs.current({
station_network = meta:get_string("station_network"),
station_name = meta:get_string("station_name"),
owner_name = meta:get_string("owner"),
is_elevator = travelnet.is_elevator(node.name)
}, player_name))
travelnet.formspecs.current(props, player_name))
end

-- a player clicked on something in the formspec hse was manually shown
Expand Down