From 0c3ad9b445fccdd005a706b33c6c032e5c2a44bd Mon Sep 17 00:00:00 2001 From: jimy-byerley Date: Sun, 22 Jun 2014 10:01:16 +0200 Subject: [PATCH] modified: README.md modified: init.lua modified: os.lua --- README.md | 2 +- init.lua | 22 +++-- os.lua | 275 ++++++++---------------------------------------------- 3 files changed, 53 insertions(+), 246 deletions(-) diff --git a/README.md b/README.md index 57e4239..2b4321e 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,4 @@ This minetest mod allow you to craft realistics laptop and screens. Both are ani Install ======= -As other minetest mods, just extract it in minetest/games/minetest_game/mods directory. \ No newline at end of file +As other minetest mods, just extract it in minetest/games/minetest_game/mods directory and rename the new folder "computers". \ No newline at end of file diff --git a/init.lua b/init.lua index 3d82e9b..ca0be43 100644 --- a/init.lua +++ b/init.lua @@ -11,26 +11,28 @@ local computer_action = function(pos, formname, fields, sender) computers.execute_oscommand(fields.text, pos, sender) end -computers.registered_oscommands = {} -computers.computer_help = {} +computers.registered_command_names = {} +computers.registered_commands = {} -computers.register_oscommand = function(name, exe, help) - computers.registered_oscommands[name] = exe - computers.computer_help[name] = help +computers.register_oscommand = function(name, short_description, long_description, exe) + computers.registered_command_names[#computers.registered_command_names+1] = name + computers.registered_commands[name] = {short_description=short_description, long_description=long_description, exe=exe} end computers.execute_oscommand = function(cmdline, pos, player) if cmdline == nil then return end local command = string.match(cmdline, "([^ ]+) *") if command == nil then return end - local message = "["..command.." : command not found]" + local message = command..": command not found" local continue = false print("pass command to computer : "..command) - local func = computers.registered_oscommands[command] - if func then - continue = true - message, continue = func(cmdline, pos, player) + if computers.registered_commands[command] then + local func = computers.registered_commands[command].exe + if func then + continue = true + message, continue = func(cmdline, pos, player) + end end --minetest.chat_send_player(player:get_player_name(), message) diff --git a/os.lua b/os.lua index cb9c003..31b83b0 100644 --- a/os.lua +++ b/os.lua @@ -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 message + local message = "" + 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" - else - message = computers.computer_help[command] + for i=1,#computers.registered_command_names do + local name = computers.registered_command_names[i] + 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 - if message == nil then + if message == "" then message = "no help for this command" end + return message, true end) -computers.register_oscommand("date", function(cmdline, pos, player) - local message = "[local time : "..(minetest.env:get_timeofday()*24).." ]" +computers.register_oscommand("time", "get the time of day", "time", function(cmdline, pos, player) + local message = "local time : "..(minetest.env:get_timeofday()*24) return message, true -end, -"date: give the faction time of day") +end) -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 -end, -"localisation: give the computer coordinates") +end) +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) - local command = "" - local continue = true - local x = 0 - local y = 0 - local z = 0 - local mode = "" - 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]" +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, opt = string.match(cmdline, "^([^ ]+) *(%a+)") + if opt == "-c" then + local x, y, z + command, opt,x,y,z = string.match(cmdline, "^([^ ]+) *(%a+) *(%d+)[, ] *(%d+)[, ] *(%d+)") + -- ... + return command..": unable to connect: no network available (in devel program)" else - local remote_pos = {x=x, y=y, z=z} - local node = minetest.env:get_node(remote_pos - ) - if node.name == "ignore" then - message = "no host online" - 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 + local command, opt, playername = string.match(cmdline, "^([^ ]+) *(%a+) *(%a+)") + local player = minetest.get_player_by_name(playername) + local p = player.getpos() + -- ... + return command..": unable to connect: no network available (in devel program)" end - return message, continue -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" -]] - +end);