modified: README.md

modified:   init.lua
	modified:   os.lua
This commit is contained in:
jimy-byerley 2014-06-22 10:01:16 +02:00
parent 815141ff69
commit 0c3ad9b445
3 changed files with 53 additions and 246 deletions

View File

@ -6,4 +6,4 @@ This minetest mod allow you to craft realistics laptop and screens. Both are ani
Install Install
======= =======
As other minetest mods, just extract it in minetest/games/minetest_game/mods directory. As other minetest mods, just extract it in minetest/games/minetest_game/mods directory and rename the new folder "computers".

View File

@ -11,26 +11,28 @@ local computer_action = function(pos, formname, fields, sender)
computers.execute_oscommand(fields.text, pos, sender) computers.execute_oscommand(fields.text, pos, sender)
end end
computers.registered_oscommands = {} computers.registered_command_names = {}
computers.computer_help = {} computers.registered_commands = {}
computers.register_oscommand = function(name, exe, help) computers.register_oscommand = function(name, short_description, long_description, exe)
computers.registered_oscommands[name] = exe computers.registered_command_names[#computers.registered_command_names+1] = name
computers.computer_help[name] = help computers.registered_commands[name] = {short_description=short_description, long_description=long_description, exe=exe}
end end
computers.execute_oscommand = function(cmdline, pos, player) computers.execute_oscommand = function(cmdline, pos, player)
if cmdline == nil then return end if cmdline == nil then return end
local command = string.match(cmdline, "([^ ]+) *") local command = string.match(cmdline, "([^ ]+) *")
if command == nil then return end if command == nil then return end
local message = "["..command.." : command not found]" local message = command..": command not found"
local continue = false local continue = false
print("pass command to computer : "..command) print("pass command to computer : "..command)
local func = computers.registered_oscommands[command] if computers.registered_commands[command] then
if func then local func = computers.registered_commands[command].exe
continue = true if func then
message, continue = func(cmdline, pos, player) continue = true
message, continue = func(cmdline, pos, player)
end
end end
--minetest.chat_send_player(player:get_player_name(), message) --minetest.chat_send_player(player:get_player_name(), message)

275
os.lua
View File

@ -1,250 +1,55 @@
computers.register_oscommand("help", function(cmdline, pos, player) computers.register_oscommand("help", "get help about a function", "help [COMMAND]", function(cmdline, pos, player)
local command = string.match(cmdline, "help *(.+)") local command = string.match(cmdline, "help *(.+)")
local message local message = ""
if command == nil then if command == nil then
message = "available commands : exit date localisation write read login logout password mail\n\ttype help COMMAND to have arguments of command" for i=1,#computers.registered_command_names do
else local name = computers.registered_command_names[i]
message = computers.computer_help[command] local short_desc = computers.registered_commands[name].short_description
message = message..name.." "..short_desc.."\n"
end
message = message.."\ntype \"help COMMAND\" to get usage"
elseif command ~= nil and computers.registered_commands[command] ~= nil then
message = command..":\t"..computers.registered_commands[command].short_description .. "\nusage:\n" .. computers.registered_commands[command].long_description
end end
if message == nil then if message == "" then
message = "no help for this command" message = "no help for this command"
end end
return message, true return message, true
end) end)
computers.register_oscommand("date", function(cmdline, pos, player) computers.register_oscommand("time", "get the time of day", "time", function(cmdline, pos, player)
local message = "[local time : "..(minetest.env:get_timeofday()*24).." ]" local message = "local time : "..(minetest.env:get_timeofday()*24)
return message, true return message, true
end, end)
"date: give the faction time of day")
computers.register_oscommand("localisation", function(cmdline, pos, player)
message = "local coordinates : "..pos.x..", "..pos.y..", "..pos.z.."]" computers.register_oscommand("gps", "localize a player", "gps [-c PLAYER get coordinates]\n [-d PLAYER get distance between computer and player]\n [-r PLAYER get relative coordinates]",
function(cmdline, pos, player)
local message = "gps: error: unable to connect to satellite (in devel program)"
return message, true return message, true
end, end)
"localisation: give the computer coordinates")
computers.register_oscommand("mat", "get the material name of a bloc next to the computer", "mat [z+1] [z-1] [y+1] [y-1] [x+1] [x-1]",
function(cmdline, pos, player)
local message = "mat: error: incompatible driver (in devel program)"
return message, true
end)
computers.register_oscommand("connect", function(cmdline, pos, player) computers.register_oscommand("com", "create a connexion between two computers", "com [-c COODINATES make a connexion between this computer and an other at coordinates]\n [-p PLAYERNAME make a connexion between this computer and the closest computer to the player]", function(cmdline, pos, player)
local command = "" local command, opt = string.match(cmdline, "^([^ ]+) *(%a+)")
local continue = true if opt == "-c" then
local x = 0 local x, y, z
local y = 0 command, opt,x,y,z = string.match(cmdline, "^([^ ]+) *(%a+) *(%d+)[, ] *(%d+)[, ] *(%d+)")
local z = 0 -- ...
local mode = "" return command..": unable to connect: no network available (in devel program)"
command, x, y, z, mode = string.match(cmdline, "^([^ ]+) *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *(.+)")
if x==nil or y==nil or z==nil then
message = "bad command : connect X,Y,Z [OPTIONS]"
else else
local remote_pos = {x=x, y=y, z=z} local command, opt, playername = string.match(cmdline, "^([^ ]+) *(%a+) *(%a+)")
local node = minetest.env:get_node(remote_pos local player = minetest.get_player_by_name(playername)
) local p = player.getpos()
if node.name == "ignore" then -- ...
message = "no host online" return command..": unable to connect: no network available (in devel program)"
else
message = "connect : bad mode"
if mode=="test" then message = "host online" end
if mode=="enable" then
minetest.env:punch_node({x=x, y=y, z=z})
message = "[host enabled]"
end
if mode=="tunnel" then
if node.name == "computers:laptop_close" then
message = "disabled remote host, can't connect to it"
else
if node.name == "computers:laptop_connect" then
message = "remote host is busy"
else
if node.name ~= "computers:laptop_blank" and node.name ~= "computers:laptop_smalltext" and node.name ~= "computers:laptop_bigtext" then
message = "destination is not a computer"
else
node.name = "computers:laptop_connect"
minetest.env:set_node(remote_pos, node)
local self = minetest.env:get_node(pos)
self.name = "computers:laptop_connect"
minetest.env:set_node(pos, self)
--set metadata
local meta = minetest.env:get_meta(pos)
local remotemeta = minetest.env:get_meta(remote_pos)
meta:set_string("formspec", "field[destination;;${destination}]field[text;;${text}]")
meta:set_string("infotext", "")
meta:set_string("destination", ""..remote_pos.x..","..remote_pos.y..","..remote_pos.z)
remotemeta:set_string("formspec", "field[destination;;${destination}]field[text;;${text}]")
remotemeta:set_string("infotext", "")
remotemeta:set_string("destination", ""..pos.x..","..pos.y..","..pos.z)
--connection message
continue = false
message = "connection etablished"
end
end
end
end
end
end end
return message, continue end);
end,
"connect X, Y, Z MODE [OPTIONS]: connect to a node or computer")
computers.register_oscommand("write", function(cmdline, pos, player)
local command
local x = 0
local y = 0
local z = 0
local file = nil
local str = nil
command, x, y, z, file, str = string.match(cmdline, "^([^ ]+) *(%d+)[, ] *(%d+)[, ] *(%d+) *(%a+) *(.+)")
if x==nil or y==nil or z==nil or file==nil or str==nil then
message = "bad command : memory-write X,Y,Z FILENAME STRING"
else
local node = minetest.env:get_node({x=x, y=y, z=z})
if node.name == "air" then
message = "no support, aborted"
else
local meta = minetest.env:get_meta({x=x, y=y, z=z})
meta:set_string(file, str)
message = "memory added to "..node.name.." "..file.." "..str
end
end
return message, true
end,
"write X, Y, Z FILENAME STR: write a string in a node file")
computers.register_oscommand("read", function(cmdline, pos, player)
local command
local x = 0
local y = 0
local z = 0
local file = nil
command, x, y, z, file = string.match(cmdline, "^([^ ]+) *(%d+)[, ] *(%d+)[, ] *(%d+) *(%a+)")
if x==nil or y==nil or z==nil or file==nil then
message = "bad command : memory-read X,Y,Z FILENAME STRING"
else
--if node has not metadata
--else read contain
local node = minetest.env:get_node({x=x, y=y, z=z})
if node.name == "air" then
message = "inexistant destination, aborted"
else
local meta = minetest.env:get_meta({x=x, y=y, z=z})
local contain = meta:get_string(file)
if contain then
message = "memory read from "..node.name.." : "..contain
else
message = "no data in this node"
end
end
end
return message, true
end,
"read X, Y, Z FILENAME STR read a node file")
--[[ commande autentificate :
autentificate user password
autentifie la machine hote par le nom d'utilisateur si le mot de passe est correct
]]
COMPUTER_FILE = minetest.get_worldpath()..'/computer_accounts.txt'
local get_password = function(user)
local file = io.open(COMPUTER_FILE, "r")
local list = ""
local u
local p
local i=1
while file:lines(i)("*(.+) *(.+)") ~= nil do
u,p = file:lines(i)("*(.+) *(.+)")
print(u.." "..p)
--u,p = string.match(list, "*(.+) *(.+)")
if u==user then
return p
end
i = i+1
end
io.close(file)
end
local add_user = function(user, password)
local file = io.open(COMPUTER_FILE, "r")
local text = file:lines()
io.close(file)
file = io.open(COMPUTER_FILE, "w")
text = user.." "..password.."\n"..text
file:write(text)
io.close(file)
end
local del_user = function(user)
local file = io.open(COMPUTER_FILE, "r")
local list = file:lines()
local text = ""
io.close(file)
local u
local p
for i in 1,#list do
u,p = string.match(list[1], "*(.+) *(.+)")
if u~=user then
text = text..list[1]
end
end
file = io.open(COMPUTER_FILE, "w")
file:write(text)
io.close(file)
end
computers.register_oscommand("login", function(cmdline, pos, player)
local command
local user
local password
command, user, password = string.match(cmdline, "^([^ ]+) *(.+) *(.+)")
if user==nil then
message = "bad command : autentificate USER PASSWORD"
else
local pass = get_password(user)
if pass then
if pass==password then
message = "correct password"
else
message = "incorrect password"
end
else
add_user(user, password)
message = "user created"
end
end
return message, true
end,
"login USER PASSWORD: use a username")
--[[ commande deautentificate :
deautentificate
desauthentifie l'utilisateur courrant
"logout: don't use username"
]]
--[[ commande userdel :
deluser
supprime le compte utilisateur actuel
]]
--[[ commande password :
password user password new
change useer's password to new password
"password USER PASSWORD NEW: change user password by new"
]]
--[[ commande mail :
mail receiver
ajoute un mail de l'utilisateur courant au compte nomme
la saisie du mail se fait apres cette commande
mail
affiche les mails recus
mail 1
lis le premier mail
"mail [USER | NUM]: send mail to user or display mail list or display mail for number"
]]