jimthenerd

PDATerm Client 0.2

Apr 19th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. rednet.open("back");
  2.  
  3. local connected = 0;
  4. local serverName = nil;
  5. local serverID = 0;
  6.  
  7.  
  8. function repaint()
  9.     term.setCursorPos(1, 1);
  10.  
  11.     term.setBackgroundColor(colors.white);
  12.     for i = 1, 20 do
  13.         for j = 1, 26 do
  14.             write(" ");
  15.         end
  16.         print();
  17.     end
  18.    
  19.     if (connected == 0) then
  20.         term.setBackgroundColor(colors.red);
  21.     else
  22.         term.setBackgroundColor(colors.green);
  23.     end
  24.    
  25.     term.setTextColor(colors.white);
  26.        
  27.     term.setCursorPos(1, 1);
  28.     write("                          ");
  29.    
  30.     term.setCursorPos(1, 20);
  31.     write("                          ");
  32.    
  33.    
  34.     term.setCursorPos(1, 1);
  35.     write(serverName);
  36.    
  37.    
  38. end
  39.  
  40. function connectToServer()
  41.     rednet.broadcast("$INFO");
  42.     id, msg = rednet.receive(5);
  43.    
  44.     if (msg == nil) then
  45.         showErrorMessage("No Carrier");
  46.         sleep(3);
  47.         os.reboot();
  48.     else
  49.         serverName = msg;
  50.         serverID = id;
  51.         connected = 1;
  52.     end
  53. end
  54.  
  55.  
  56. function showErrorMessage(message)
  57.     term.setBackgroundColor(colors.red);
  58.     term.setTextColor(colors.white);
  59.    
  60.     for i = 8, 13 do
  61.         term.setCursorPos(2, i);
  62.         for j = 1, 24 do
  63.             write(" ");
  64.         end
  65.         print();
  66.     end
  67.    
  68.     term.setCursorPos(11, 9);
  69.     write("Error");
  70.    
  71.    
  72.     term.setCursorPos(4, 11);
  73.     write(message);
  74. end
  75.  
  76. function showInformationMessage(line1, line2)
  77.     term.setBackgroundColor(colors.lightBlue);
  78.     term.setTextColor(colors.white);
  79.    
  80.     for i = 8, 13 do
  81.         term.setCursorPos(2, i);
  82.         for j = 1, 24 do
  83.             write(" ");
  84.         end
  85.         print();
  86.     end
  87.    
  88.     term.setCursorPos(11, 9);
  89.     write("Message");
  90.    
  91.    
  92.     term.setCursorPos(4, 11);
  93.     write(line1);
  94.     term.setCursorPos(4, 12);
  95.     write(line2);
  96. end
  97.  
  98. function showInputDialog(message)
  99.     term.setBackgroundColor(colors.lightBlue);
  100.     term.setTextColor(colors.brown);
  101.    
  102.     for i = 4, 17 do
  103.         term.setCursorPos(2, i);
  104.         for j = 1, 24 do
  105.             write(" ");
  106.         end
  107.     end
  108.    
  109.     term.setCursorPos(4, 6);
  110.     write(message);
  111.    
  112.     term.setCursorPos(5, 14);
  113.     input = read();
  114.    
  115.     return input;
  116. end
  117.  
  118.  
  119. function showOSInformation()
  120.     term.setBackgroundColor(colors.white);
  121.     term.setTextColor(colors.lightGray);
  122.    
  123.     term.setCursorPos(4, 5);
  124.     write("PDATerm v0.2");
  125.     term.setCursorPos(4, 7);
  126.     write("A Free Mobile Terminal");
  127.     term.setCursorPos(4, 8);
  128.     write("for ComputerCraft");
  129. end
  130.  
  131. function lock()
  132.     term.setCursorPos(1, 1);
  133.     term.setBackgroundColor(colors.black);
  134.     term.setTextColor(colors.white);
  135.    
  136.     for i = 1, 20 do
  137.         for j = 1, 26 do
  138.             if (i == 1 or i == 20 or j == 1 or j == 26) then
  139.                 term.setBackgroundColor(colors.red);
  140.             end
  141.             write(" ");
  142.             term.setBackgroundColor(colors.black);
  143.         end
  144.         if (i ~= 20) then
  145.             print();
  146.         end
  147.     end
  148.    
  149.     term.setCursorPos(5, 3);
  150.     write("--Enter Passcode--");
  151.    
  152.     cpass = "1234";
  153.     pass = "";
  154.    
  155.     term.setTextColor(colors.white);
  156.    
  157.     while(pass ~= cpass) do
  158.    
  159.         pass = "";
  160.        
  161.         term.setCursorPos(5, 15);
  162.         write("   -  -  -  -   ");
  163.        
  164.         for i = 1, 4 do
  165.             term.setCursorPos(5 + i * 3, 15);
  166.             local event, key = os.pullEvent("char");
  167.             pass = pass .. key;
  168.             write("*");
  169.         end
  170.     end
  171.    
  172.    
  173. end
  174.  
  175.  
  176.  
  177. function rednetControl()
  178.     term.setCursorPos(1, 1);
  179.     term.setBackgroundColor(colors.lightGray);
  180.     term.setTextColor(colors.white);
  181.    
  182.     for i = 1, 20 do
  183.         for j = 1, 26 do
  184.             if (i == 1 or i == 20 or j == 1 or j == 26) then
  185.                 term.setBackgroundColor(colors.gray);
  186.             end
  187.             write(" ");
  188.             term.setBackgroundColor(colors.lightGray);
  189.         end
  190.         if (i ~= 20) then
  191.             print();
  192.         end
  193.     end
  194.    
  195.     term.setCursorPos(3, 3);
  196.     write("Advanced Rednet Console");
  197.    
  198.     term.setBackgroundColor(colors.blue);
  199.     term.setCursorPos(3, 6);
  200.     write("    OPEN Rednet       ");
  201.     term.setCursorPos(3, 8);
  202.     write("    CLOSE Rednet      ");
  203.     term.setCursorPos(3, 10);
  204.     write("  Rednet Broadcast    ");
  205.     term.setCursorPos(3, 12);
  206.     write("    Send Message      ");
  207.     term.setCursorPos(3, 14);
  208.     write("     Get Message      ");
  209.     term.setCursorPos(3, 16);
  210.     write("    Computer ID       ");
  211.     term.setCursorPos(3, 18);
  212.     write("        Exit          ");
  213.    
  214.     local event, button, x, y = os.pullEvent("mouse_click");
  215.    
  216.     if (button == 1) then
  217.         if (y == 6) then
  218.             rednet.open("back");
  219.             showInformationMessage("Rednet Turned On");
  220.             sleep(2);
  221.         end
  222.         if (y == 8) then
  223.             rednet.close();
  224.             showInformationMessage("Rednet Turned Off");
  225.             sleep(2);
  226.         end
  227.         if (y == 10) then
  228.             message = showInputDialog("Broadcast Message");
  229.             showInformationMessage("Broadcasting...", "");
  230.             rednet.broadcast(message);
  231.             sleep(2);
  232.         end
  233.         if (y == 12) then
  234.             id = tonumber(showInputDialog("Target ID"));
  235.             message = showInputDialog("Enter Message");
  236.             showInformationMessage("Sending Message...", "");
  237.             rednet.send(id, message);
  238.             sleep(2);
  239.         end
  240.         if (y == 14) then
  241.             timeout = tonumber(showInputDialog("Enter Timeout"));
  242.             showInformationMessage("Waiting for", "message");
  243.             id, msg = rednet.receive(timeout);
  244.             if (msg == nil) then
  245.                 showErrorMessage("Receive Timeout.");
  246.                 sleep(2);
  247.             else
  248.                 showInformationMessage(id, msg);
  249.                 sleep(5);
  250.             end
  251.         end
  252.         if (y == 16) then
  253.             showInformationMessage("Computer ID", os.getComputerID());
  254.             sleep(5);
  255.         end
  256.        
  257.         if (y == 18) then
  258.             return;
  259.         end
  260.         rednetControl();
  261.     end
  262. end
  263.  
  264. function displayMainMenu()
  265.     term.setBackgroundColor(colors.yellow);
  266.     term.setTextColor(colors.red);
  267.  
  268.     term.setCursorPos(2, 5);
  269.     write("  Current GPS Position  ");
  270.    
  271.     term.setCursorPos(2, 7);
  272.     write("       Mail System      ");
  273.    
  274.     term.setCursorPos(2, 9);
  275.     write("       Enter Chat       ");
  276.    
  277.     term.setCursorPos(2, 11);
  278.     write("    Rednet Console      ");
  279.    
  280.     term.setCursorPos(2, 13);
  281.     write("    System Settings     ");
  282.    
  283.     term.setCursorPos(2, 15);
  284.     write("      Reconnect         ");
  285.    
  286.     term.setCursorPos(2, 17);
  287.     write("       Shutdown         ");
  288.    
  289.     local event, button, x, y = os.pullEvent("mouse_click");
  290.  
  291.     if (button == 1) then
  292.         if (y == 5) then
  293.             showInformationMessage("Getting Location", "");
  294.             if (gps.locate(5) == nil) then
  295.                 showErrorMessage("No GPS Signal.");
  296.                 sleep(2);
  297.             else
  298.                 x, y, z = gps.locate();
  299.                 showInformationMessage("Your Location: ", x .. ", " .. y .. ", " .. z);
  300.                 sleep(5);
  301.             end
  302.         end
  303.        
  304.         if (y == 7) then
  305.         end
  306.        
  307.         if (y == 9) then
  308.             name = showInputDialog("Server Hostname");
  309.             usrname = showInputDialog("Username");
  310.             shell.run("chat", "join", name, usrname);
  311.         end
  312.        
  313.         if (y == 11) then
  314.             showInformationMessage("Please Wait...", "");
  315.             sleep(2);
  316.             rednetControl();
  317.         end
  318.        
  319.         if (y == 13) then
  320.             showInformationMessage("Loading Mail...");
  321.             sleep(2);
  322.             shell.run("mail");
  323.         end
  324.        
  325.         if (y == 15) then
  326.             showInformationMessage("Connecting...", "");
  327.             connected = 0;
  328.             connectToServer();
  329.            
  330.         end
  331.        
  332.         if (y == 17) then
  333.             showInformationMessage("Shutting Down...", "");
  334.             sleep(2);
  335.             os.shutdown();
  336.         end
  337.        
  338.     end
  339.    
  340.     repaint();
  341.     displayMainMenu();
  342.  
  343. end
  344.  
  345.  
  346.  
  347.  
  348. lock();
  349.  
  350. connectToServer();
  351. repaint();
  352. showOSInformation();
  353.  
  354. if (connected == 0) then
  355.     showErrorMessage("No Carrier");
  356.     sleep(5);
  357.     os.reboot();
  358. else
  359.     showInformationMessage("Connected to", serverName);
  360.     sleep(3);
  361. end
  362.  
  363.  
  364. repaint();
  365. displayMainMenu();
Advertisement
Add Comment
Please, Sign In to add comment