v2.01 SaferLua Terminal to be connected to the SaferLua Controller

This commit is contained in:
Joachim Stolberg 2019-01-27 22:07:54 +01:00
parent 3560082270
commit f62ee1db4a
12 changed files with 263 additions and 17 deletions

@ -1,4 +1,4 @@
# TechPack V2.00
# TechPack V2.01
TechPack, a Mining, Crafting, & Farming Modpack for Minetest.
@ -55,6 +55,7 @@ TechPack provides:
- sending mails or chat messages
- output on a display
- a Controller to be programmed in Lua
- a Terminal to be connected to the Lua Controller
- a Signal Tower node showing machine states
- a Display node for text outputs of the Controller
- Metal ladders, stairways, and bridges
@ -143,6 +144,8 @@ tubelib_addons1 optional: unified_inventory
- 2018-12-23 V2.xx * on the way to v2
- 2018-12-29 V2.00 * beta
- 2019-01-12 V2.00 * release
- 2019-01-27 V2.01 * SaferLua Controller Terminal added
## New in v2 (from players point of view)
- Almost all machines break after a certain amount of time (switch into the state 'defect') and have to be repaired.

@ -2,6 +2,20 @@
## V2.01 (2019-01-27)
### Additions
- SaferLua Terminal to be connected to the SaferLua Controller
### Removals
### Changes
- Server: formspec added to enter valid usernames for server access
### Fixes
## V2.00.07 (2019-01-27)
### Additions

@ -25,6 +25,17 @@ sl_controller.register_function("get_input", {
" The device has to be connected with the controller."
})
sl_controller.register_function("get_string", {
cmnd = function(self, num)
num = tostring(num or "")
return sl_controller.get_command(self.meta.number)
end,
help = ' $get_string() --> text sting or nil\n'..
' Read an entered string (command) from the Terminal.\n'..
' example: s = $get_string()\n'..
" The Terminal has to be connected with the controller."
})
sl_controller.register_function("get_status", {
cmnd = function(self, num)
num = tostring(num or "")
@ -170,6 +181,16 @@ sl_controller.register_action("display", {
' example: $display("0123", 1, "Hello ", name, " !")'
})
sl_controller.register_action("terminal", {
cmnd = function(self, num, text)
text = tostring(text or "")
tubelib.send_message(num, self.meta.owner, nil, "term", text)
end,
help = " $terminal(num, text)\n"..
' Send a text line to the terminal with number "num".\n'..
' example: $terminal("0123", "Hello "..name)'
})
sl_controller.register_action("clear_screen", {
cmnd = function(self, num)
num = tostring(num or "")

@ -283,6 +283,7 @@ local function compile(pos, meta, number)
if code then
Cache[number] = {code=code, inputs={}}
Cache[number].inputs.term = "" -- terminal inputs
return true
end
return false
@ -543,6 +544,16 @@ function sl_controller.get_input(number, input)
return "off"
end
-- used for Terminal commands
function sl_controller.get_command(number)
if Cache[number] and Cache[number].inputs then
local cmnd = Cache[number].inputs["term"] or ""
Cache[number].inputs["term"] = ""
return cmnd
end
return ""
end
tubelib.register_node("sl_controller:controller", {}, {
on_recv_message = function(pos, topic, payload)
local meta = minetest.get_meta(pos)
@ -552,6 +563,8 @@ tubelib.register_node("sl_controller:controller", {}, {
set_input(pos, number, payload, topic)
elseif topic == "off" then
set_input(pos, number, payload, topic)
elseif topic == "term" then
set_input(pos, number, "term", payload)
elseif topic == "state" then
local running = meta:get_int("running") or tubelib.STATE_STOPPED
return tubelib.statestring(running)

@ -17,3 +17,4 @@ dofile(minetest.get_modpath("sl_controller") .. "/controller.lua")
dofile(minetest.get_modpath("sl_controller") .. "/commands.lua")
dofile(minetest.get_modpath("sl_controller") .. "/battery.lua")
dofile(minetest.get_modpath("sl_controller") .. "/server.lua")
dofile(minetest.get_modpath("sl_controller") .. "/terminal.lua")

@ -21,6 +21,16 @@ local DEFAULT_MEM = {
}
}
local function formspec(meta)
local names = meta:get_string("names") or ""
return "size[9,4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"field[0.2,1;9,1;names;Allowed user names (spaces separated):;"..names.."]" ..
"button_exit[3.5,2.5;2,1;exit;Save]"
end
local function on_time(pos, elasped)
local meta = minetest.get_meta(pos)
@ -65,10 +75,20 @@ minetest.register_node("sl_controller:server", {
meta:set_string("owner", placer:get_player_name())
meta:set_string("number", number)
tubelib.set_data(number, "memory", table.copy(DEFAULT_MEM))
meta:set_string("formspec", formspec(meta))
on_time(pos, 0)
minetest.get_node_timer(pos):start(20)
end,
on_receive_fields = function(pos, formname, fields, player)
local meta = minetest.get_meta(pos)
if fields.names and fields.names ~= "" then
local names = string.gsub(fields.names, " +", " ")
meta:set_string("names", names)
meta:set_string("formspec", formspec(meta))
end
end,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
@ -113,6 +133,23 @@ local function calc_size(v)
end
end
local function get_memory(num, name)
local info = tubelib.get_node_info(num)
if info and info.name == "sl_controller:server" then
local meta = minetest.get_meta(info.pos)
local owner = meta:get_string("owner")
if name == owner then
return tubelib.get_data(num, "memory") or table.copy(DEFAULT_MEM)
end
local names = meta:get_string("names")
for _,n in ipairs(string.split(names, " ")) do
if name == n then
return tubelib.get_data(num, "memory") or table.copy(DEFAULT_MEM)
end
end
end
end
local function write_value(mem, key, item)
if mem and mem.size < SERVER_CAPA then
if mem.data[key] then
@ -136,19 +173,7 @@ end
tubelib.register_node("sl_controller:server", {}, {
on_recv_message = function(pos, topic, payload)
local meta = minetest.get_meta(pos)
if meta then
local number = meta:get_string("number")
local mem = tubelib.get_data(number, "memory") or table.copy(DEFAULT_MEM)
if topic == "read" then
return read_value(mem, payload)
elseif topic == "write" then
write_value(mem, payload.key, payload.value)
tubelib.set_data(number, "memory", mem)
else
return "unsupported"
end
end
return "unsupported"
end,
on_node_load = function(pos)
minetest.get_node_timer(pos):start(20)
@ -159,7 +184,10 @@ tubelib.register_node("sl_controller:server", {}, {
sl_controller.register_function("server_read", {
cmnd = function(self, num, key)
if type(key) == "string" then
return tubelib.send_request(num, "read", key)
local mem = get_memory(num, self.meta.owner)
if mem then
return read_value(mem, key)
end
else
self.error("Invalid server_read parameter")
end
@ -173,7 +201,10 @@ sl_controller.register_function("server_read", {
sl_controller.register_action("server_write", {
cmnd = function(self, num, key, value)
if type(key) == "string" then
tubelib.send_message(num, self.meta.owner, nil, "write", {key=key, value=value})
local mem = get_memory(num, self.meta.owner)
if mem then
write_value(mem, key, value)
end
else
self.error("Invalid server_write parameter")
end

163
sl_controller/terminal.lua Normal file

@ -0,0 +1,163 @@
--[[
sl_controller
=============
Copyright (C) 2018-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
terminal.lua:
]]--
local HELP = [[#### SmartLine Controller Terminal ####
Send commands to your Controller
and output text messages from your
Controller to the Terminal.
Commands can have up to 80 characters.
Local commands:
- cls = clear screen
- help = this message
- pub = switch to public use
- priv = switch to private use
]]
local function formspec1()
return "size[6,4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"field[0.5,1;5,1;number;SaferLua Controller number:;]" ..
"button_exit[1.5,2.5;2,1;exit;Save]"
end
local function formspec2(meta)
local output = meta:get_string("output")
output = minetest.formspec_escape(output)
output = output:gsub("\n", ",")
return "size[9,8]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"table[0.1,0.1;8.6,6.6;output;"..output..";200]"..
"field[0.5,7.6;6,1;cmnd;Enter command;]" ..
"field_close_on_enter[cmnd;false]"..
"button[6.7,7.3;2,1;ok;Enter]"
end
local function output(pos, text)
local meta = minetest.get_meta(pos)
text = meta:get_string("output") .. "\n" .. (text or "")
text = text:sub(-500,-1)
meta:set_string("output", text)
meta:set_string("formspec", formspec2(meta))
end
local function command(pos, cmnd, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner") or ""
if cmnd then
cmnd = cmnd:sub(1,80)
if cmnd == "cls" then
meta:set_string("output", "")
meta:set_string("formspec", formspec2(meta))
elseif cmnd == "help" then
local meta = minetest.get_meta(pos)
meta:set_string("output", HELP)
meta:set_string("formspec", formspec2(meta))
elseif cmnd == "pub" and owner == player then
meta:set_int("public", 1)
output(pos, player..":$ "..cmnd)
output(pos, "Switched to public use!")
elseif cmnd == "priv" and owner == player then
meta:set_int("public", 0)
output(pos, player..":$ "..cmnd)
output(pos, "Switched to private use!")
elseif meta:get_int("public") == 1 or owner == player then
local number = meta:get_string("number") or "0000"
output(pos, player..":$ "..cmnd)
tubelib.send_message(number, owner, nil, "term", cmnd)
end
end
end
minetest.register_node("sl_controller:terminal", {
description = "SaferLua Controller Terminal",
tiles = {
-- up, down, right, left, back, front
'sl_controller_terminal_top.png',
'sl_controller_terminal_bottom.png',
'sl_controller_terminal_side.png',
'sl_controller_terminal_side.png',
'sl_controller_terminal_bottom.png',
"sl_controller_terminal_front.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-12/32, -16/32, -8/32, 12/32, -14/32, 12/32},
{-12/32, -14/32, 12/32, 12/32, 6/32, 14/32},
},
},
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "sl_controller:terminal")
local meta = minetest.get_meta(pos)
meta:set_string("own_number", number)
meta:set_string("formspec", formspec1())
meta:set_string("owner", placer:get_player_name())
meta:set_string("infotext", "SaferLua Controller Terminal "..number..": not connected")
end,
on_receive_fields = function(pos, formname, fields, player)
local meta = minetest.get_meta(pos)
if fields.number and fields.number ~= "" then
if tubelib.check_numbers(fields.number) then
meta:set_string("number", fields.number)
local own_number = meta:get_string("own_number")
meta:set_string("infotext", "SaferLua Controller Terminal "..own_number..": connected with "..fields.number)
meta:set_string("formspec", formspec2(meta))
end
elseif (fields.key_enter == "true" or fields.ok == "Enter") and fields.cmnd ~= "" then
command(pos, fields.cmnd, player:get_player_name())
end
end,
after_dig_node = function(pos)
tubelib.remove_node(pos)
end,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_craft({
output = "sl_controller:terminal",
recipe = {
{"", "smartline:display", ""},
{"", "", ""},
{"dye:black", "tubelib:wlanchip", "default:copper_ingot"},
},
})
tubelib.register_node("sl_controller:terminal", {}, {
on_recv_message = function(pos, topic, payload)
if topic == "term" then
output(pos, payload)
return true
end
end,
})

Binary file not shown.

Before

Width:  |  Height:  |  Size: 546 B

After

Width:  |  Height:  |  Size: 346 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 317 B