Advertisement
johnnic431

Testpower

Aug 12th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. os.pullEvent=os.pullEventRaw
  2. local users={{name="Sylvyrfysh",pin=1324},{name="tester",pin=1111}};
  3. local typedOptions={{key="1",funct=(function() buy(); end)},{key="2",funct=(function() term.clear(); term.setCursorPos(1,1); end)},{key="3",funct=(function() print(cell.getEnergyStored("RF")*100/cell.getMaxEnergyStored("RF").."%"); end)}};
  4.  
  5. local args={...};
  6. local tempuser;
  7.  
  8. local modem=peripheral.wrap("bottom");
  9. local cmodem=peripheral.wrap("back");
  10. cell=peripheral.wrap("cofh_thermalexpansion_energycell_1");
  11.  
  12. term.clear()
  13. term.setCursorPos(1,1);
  14. local function main()
  15.     write("\nSylvyrfysh Inc Power Plant and Sales. Press 1 to buy, 2 to clear screen, and 3 to see how much power is currently available.");
  16. end
  17.  
  18. local function auth()
  19.     while true do
  20.         term.clear();
  21.         term.setCursorPos(1,1);
  22.         write("Username: ");
  23.         unm=read();
  24.         term.write('\n');
  25.         write("PIN: ");
  26.         pss=read("*")
  27.         for t,y in pairs(users) do
  28.             if y.name==unm and y.pin==pss then
  29.                 term.clear();
  30.                 tempuser=unm;
  31.                 return true;
  32.             end
  33.         end
  34.         term.clear();
  35.         term.setCursorPos(1,1);
  36.         write("Auth failed. Retry? y/n");
  37.         key=os.pullEvent("char")[2];
  38.         if not key=="y" or key=="Y" then
  39.             return false;
  40.         end
  41.     end
  42. end
  43.  
  44. function sendf(channel,rChannel,message,mID)
  45.     modem.transmit(channel,rChannel,textutils.serialize({content=message,senderID=os.getComputerID(),chan=channel,rChan=rChannel,messageID=mID or "revolutOS:wirelessAPI"}));
  46.     return true;
  47. end
  48.  
  49. function send(us1,us2,amount)
  50.     if not sendf(21450,21450,{paying=us1,payed=us2,price=amount},"fevAPI") then return false; end
  51.     mess=waitForMessage(21500);
  52.     return mess.content.auth;
  53. end
  54.  
  55. function waitForMessage(channel)
  56.     if not modem.isOpen(channel) then modem.open(channel); end
  57.     while true do
  58.         e={os.pullEvent("modem_message")};
  59.         if e[3]==channel then
  60.             e[5]=textutils.unserialize(e[5]);
  61.             return e[5];
  62.         end
  63.     end
  64. end
  65.  
  66. function buy()
  67.     if not auth() then return; end
  68.     am={50000,100000,200000,400000,1000000,2000000,5000000,10000000};
  69.     fv={200,390,765,1500,3675,7250,21000,40000};
  70.     while true do
  71.         write("Please input the amout of enrgy you would like to buy: (amount in RF)\n");
  72.         print("1: 50k");
  73.         print("2: 100k");
  74.         print("3: 200k");
  75.         print("4: 400k");
  76.         print("5: 1m");
  77.         print("6: 2m");
  78.         print("7: 5m");
  79.         print("8: 10m");
  80.         print("9: Cancel");
  81.         e=os.pullEvent("char")[2];
  82.         if tonumber(e) then
  83.             if tonumber(e)==9 then return; end
  84.             write(am[tonumber(e)].." selected. Price is "..fv[tonumber(e)]..". Is this OK? (y/n): ");
  85.             e=os.pullEvent("char")[2];
  86.             if e=="y" or e=="Y" then
  87.                 if send(tempuser,"Sylvyrfysh",fv[tonumber(e)]) then
  88.                     ticks=am[tonumber(e)]/400;
  89.                     cmodem.transmit(3,3,"on");
  90.                     write("Dispensing for "..(ticks*20).." seconds. ("..am[tonumber(e)].."RF");
  91.                     sleep(ticks*20);
  92.                     cmodem.transmit(3,3,"off");
  93.                 else
  94.                     print("Fev authorization failed or funds were low.");
  95.                 end
  96.             end
  97.         else
  98.             print("Number not received.");
  99.             sleep(5);
  100.         end
  101.         write("Logout? (y/n): ");
  102.         e=os.pullEvent("char")[2];
  103.         if e=="y" or e=="Y" then
  104.             tempuser="";
  105.         end
  106.     end
  107.     tempuser="";
  108. end
  109.  
  110. term.setCursorPos(1,1);
  111. while true do
  112.     main()
  113.     e={os.pullEvent()};
  114.     if e[1]=="char" then
  115.         for r,y in pairs(typedOptions) do
  116.             if y.key==e[2] then
  117.                 y.funct();
  118.             end
  119.         end
  120.     end
  121.     tempuser="";
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement