Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Lua Online

--local "modem_message", local_address, remote_address, port, distance, "nanomachines", command, ...messages=event.pull(5,"modem_message")
local computer=require("computer")
local component=require("component")
local term=require("term")
local event=require("event")
local m=component.modem
local gpu=component.gpu
local input_state,file_data={}
local tic,sai,start,user_input=1,1,1,1
local username,nick=""
os.execute("cls")

function main()
    aquire_user_data()
    initialize()
    print_gui()
    while start==1 do
        print_menu()
        user_input=request_input("n",0,tic)
        if user_input==0 then
            print_gui()
            print_options()
            user_input=request_input("n",0,9)
            check_command(user_input)
        else 
            toggle_input(user_input)
            computer.pullSignal(1)
        end
    end
end
--initialize
function aquire_user_data()
    while true do
        os.execute("cls")
        io.write("username: ")
        username=request_input("s",1,30)
        nick=username
        username="/usr/nanomech_data/"..username..".txt"
        local f=io.open(username,"r")
        if f==nil then
            print("No user found. Create a new user?(y/n) : ")
            local check=request_input("s",1,1)
            if check=="y" then
                local data=""
                for i=1,25 do
                    data=data.."."..i.."XXXXX"
                end
                f=io.open(username,"w")
                f:write(data)
                f:close()
                file_data=txt2table(data)
                return
            end
        else
            local data=f:read()
            io.close(f)
            file_data=txt2table(data)
            return
        end
    end
end
function initialize()
    os.execute("cls")
    gpu.set(10,10,"Welcome "..nick.."!")
    m.open(1)
    m.broadcast(1,"nanomachines","setResponsePort",1)
    local _=event.pull(5,"modem_message")
    gpu.set(10,12,"Loading...")
    tic,sai=check_input_info()
    init_inputs()
end
function init_inputs()
    gpu.set(10,13,"|")
    gpu.set((3*tic)+10,13,"|")
    term.setCursor(11,13)
    for i=1,tic do
        send_command("getInput",i)
        local _, _, _, _, _, header, _, _, state = event.pull(5,"modem_message")
        io.write("|||")
        input_state[i]=state
    end
end
function check_input_info()
    send_command("getTotalInputCount")
    local _, _, _, _, _, _, command, total_input_count=event.pull(5,"modem_message")
    send_command("getSafeActiveInputs")
    local _, _, _, _, _, _, command, safe_active_inputs=event.pull(5,"modem_message")
    return total_input_count,safe_active_inputs
end
--gui
function print_gui()
    os.execute("cls")
    gpu.set(57,1,"Available inputs  : "..tic)
    gpu.set(57,2,"Safe active inputs: "..sai)
    for i=1,25 do gpu.set(56,i,"|") end
    for i=1,tic do
        if i<10 then gpu.set(61,i+2,i..". ")
        else gpu.set(60,i+2,i..". ") end
        if input_state[i]==true then gpu.setForeground(0x00FF00)
        elseif input_state[i]==false then gpu.setForeground(0xFF0000) end
        gpu.set(64,i+2,file_data[i])
        gpu.setForeground(0xFFFFFF)
    end
end
function print_menu()
    clear_screen()
    term.setCursor(1,1)
    print("Enter 0 for other options")
    print("Enter input number to toggle")
    io.write("Command: ")
end
function print_options()
    clear_screen()
    term.setCursor(1,1)
    print("1. Check remaining power")
    print("2. Rename input")
    print("3. Reset all input names")
    print("4. Save user data")
    print("\n9. Back")
    print("0. Close app")
    io.write("Command: ")
end
--operations
function toggle_input(input_no)
    if input_state[input_no]==false then
        input_state[input_no]=true
        send_command("setInput",input_no,true)
        gpu.setForeground(0x00FF00)
    elseif input_state[input_no]==true then
        input_state[input_no]=false
        send_command("setInput",input_no,false)
        gpu.setForeground(0xFF0000)
    end
    gpu.set(64,input_no+2,file_data[input_no])
    gpu.setForeground(0xFFFFFF)
end
function check_command(comnum)
    if comnum==1 then
        print("Sending signal...")
        send_command("getPowerState")
        local _, _, _, _, _, header, _, current_power, max_power = event.pull(5,"modem_message")
        print((current_power*100/max_power).."%")
        print("Press enter to continue. . .\n")
        io.read("*l")
        return
    elseif comnum==2 then
        io.write("Enter the input number: ")
        local n=request_input("n",1,tic)
        io.write("Enter new name (1-15 letters): ")
        local newname=request_input("s",1,15)
        file_data[n]=newname
        print_gui()
        return
    elseif comnum==3 then
        file_data=reset_input_names()
        print_gui()
        return
    elseif comnum==4 then
        save_data(file_data)
        return
    elseif comnum==9 then
        return
    elseif comnum==0 then
        save_data(file_data)
        start=0
        os.execute("cls")
        return
    else
        return
    end
    
end
--mini functions
function request_input(t,mn,mx)
    while true do
        local usr_input=io.read("*l")
        if t=="n" then
            usr_input=tonumber(usr_input)
            if input_check(t,usr_input,mn,mx) then
                return usr_input
            else
                error_input_msg()
            end
        elseif t=="s" then
            usr_input=tostring(usr_input)
            if input_check(t,usr_input,mn,mx) then
                return usr_input
            else
                error_input_msg()
            end
        end
    end
end
function error_input_msg()
    print("Fuk You! Enter a proper input!!!")
    computer.pullSignal(1) print(".  ") computer.pullSignal(1)  print(".  ") computer.pullSignal(1)
    io.write("Try again: ")
end
function input_check(t,n,mn,mx)
    if t=="n" then
        if n~=nil then
            return (type(n)=="number") and (math.floor(n)==n) and n<=mx and n>=mn 
        else
            return false
        end
    elseif t=="s" then
        if n~=nil then
            return (type(n)=="string") and (string.len(n)<=mx) and (string.len(n)>=mn)
        else
            return false
        end
    end
end
function send_command(command,...)
    m.broadcast(1,"nanomachines",command,...)
end
function clear_screen()
    gpu.fill(1,1,55,25," ")
end
function txt2table(x)
    local t={}
    for section in string.gmatch(x,"%.%d*%a+") do
        local i=string.match(section,"%d+")
        i=tonumber(i)
        local name=string.match(section,"%a+")
        t[i]=tostring(name)
    end
    return t
end
function table2txt(x)
    local t=""
    for i,name in ipairs(x) do
        name=tostring(name)
        t=t.."."..i..name
    end
    return t
end
function reset_input_names()
    local x={}
    for i=1,25 do
        x[i]="XXXXX"
    end
    return x
end
function save_data(x)
    local data=""
    data=table2txt(x)
    local f=io.open(username,"w")
    f:flush()
    f:write(data)
    f:close()
end
main()

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.