Advertisement
jztmanyl

Untitled

Jul 5th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.92 KB | None | 0 0
  1. local isMain = true;
  2. local id = "Memebase";
  3. local UpdateIntervalSeconds = 15;
  4. local ItemsFullPercentAt = 256;
  5. local FluidFullPercentAt = 64000;
  6.  
  7. local VersionInfo = "Base Monitor 2.0";
  8.  
  9. local titleTextColor = colors.blue;
  10. local titleBackColor = colors.white;
  11. local tankTextColor = colors.white;
  12. local tankBackColor = colors.red;
  13. local chestTextColor = colors.white;
  14. local chestBackColor = colors.purple;
  15. local cacheTextColor = colors.white;
  16. local cacheBackColor = colors.cyan;
  17. local powerTextColor = colors.black;
  18. local powerBackColor = colors.orange;
  19.  
  20. local whitelist = "";
  21. local blacklist = "";
  22. local groups = {"Ingot", "Ore", "Pulverized", "Dust", "Crystal"};
  23.  
  24.  
  25. local NameLen = 18;
  26. local mainChannel = 2;
  27.  
  28. -- Above are values you can change
  29.  
  30. print("Starting "..VersionInfo);
  31. local peripherals = peripheral.getNames();
  32. local mon;
  33. local wmod;
  34. local x,y;
  35. local CurColumn = 0;
  36. local MaxColumn;
  37. local ColumnWidth;
  38. local CurLine = 2;
  39. local ContentData = {};
  40.  
  41. function padString(sText, iLen)
  42.     local iTextLen = string.len(sText);
  43.     -- Too short, pad
  44.     if (iTextLen < iLen) then
  45.         local iDiff = iLen - iTextLen;
  46.         return(sText..string.rep(" ",iDiff));
  47.     end
  48.     -- Too long, trim
  49.     if (iTextLen > iLen) then
  50.         return(string.sub(sText,1,iLen));
  51.     end
  52.     -- Exact length
  53.     return(sText);
  54. end
  55.  
  56. function prepmonitor(objectmon)
  57.     mon = peripheral.wrap(objectmon);
  58.     if (mon.isColor() == false) then
  59.         titleTextColor = colors.black;
  60.         titleBackColor = colors.white;
  61.         tankTextColor = colors.black;
  62.         tankBackColor = colors.white;
  63.         chestTextColor = colors.black;
  64.         chestBackColor = colors.white;
  65.         cacheTextColor = colors.black;
  66.         cacheBackColor = colors.white;
  67.         powerTextColor = colors.black;
  68.         powerBackColor = colors.white;
  69.     end
  70. end
  71.  
  72. function updateTable(strName,strAmount,timestamp,strLegend)
  73.     if string.find(strName, "item.") then
  74.         print(strName);
  75.     end
  76.     if string.find(strName, "item.projectred") then
  77.             strName = "Electrotine";
  78.     end
  79.     if string.find(strName, "item.mob_ingredient") then
  80.             strName = "Nebulous Heart";
  81.     end
  82.    
  83.     local itemGroup = "No group";
  84.     if (type(groups) == "table") then
  85.         for l,filter in pairs(groups) do
  86.             if(string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  87.                 itemGroup = filter;
  88.             end
  89.         end
  90.     end
  91.     local isWhitelisted = true;
  92.     if (type(whitelist) == "table") then
  93.         isWhitelisted = false;
  94.         for l,filter in pairs(whitelist) do
  95.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  96.                 isWhitelisted = true;
  97.             end
  98.         end
  99.     end
  100.     if (isWhitelisted == false) then
  101.         return;
  102.     end
  103.     if (type(blacklist) == "table") then
  104.         for l,filter in pairs(blacklist) do
  105.             if (string.find(string.lower(strName),string.lower(filter)) ~= nil) then
  106.                 return;
  107.             end
  108.         end
  109.     end
  110.    
  111.     if (ContentData[id] == nil) then
  112.         ContentData[id] = {};
  113.     end
  114.     if (ContentData[id][timestamp] == nil) then
  115.         ContentData[id][timestamp] = {};
  116.     end
  117.     if (ContentData[id][timestamp][itemGroup] == nil) then
  118.         ContentData[id][timestamp][itemGroup] = {};
  119.     end
  120.     if (ContentData[id][timestamp][itemGroup][strName] == nil) then
  121.         ContentData[id][timestamp][itemGroup][strName] = {};
  122.     end
  123.     if (ContentData[id][timestamp][itemGroup][strName]["count"] == nil) then
  124.         ContentData[id][timestamp][itemGroup][strName]["legend"] = strLegend;
  125.         ContentData[id][timestamp][itemGroup][strName]["count"] = strAmount;
  126.     else
  127.         ContentData[id][timestamp][itemGroup][strName]["legend"] = strLegend;
  128.         if not (ContentData[id][timestamp][itemGroup][strName]["count"] == strAmount) then
  129.             ContentData[id][timestamp][itemGroup][strName]["count"] = strAmount;
  130.             ContentData[id][timestamp][itemGroup][strName]["legend"] = "?";
  131.         end
  132.     end
  133. end
  134.  
  135. --[[
  136. local titleTextColor = colors.red;
  137. local titleBackColor = colors.white;
  138.  
  139. local tankTextColor = colors.white; +? (double)
  140. local tankBackColor = colors.red; +? (double)
  141.  
  142. local chestTextColor = colors.white; (ME)
  143. local chestBackColor = colors.purple; (ME)
  144.  
  145. local cacheTextColor = colors.white; $ (Black Hole)
  146. local cacheBackColor = colors.cyan; $ (Black Hole)
  147.  
  148. local powerTextColor = colors.black; * (Capacitor)
  149. local powerBackColor = colors.orange; * (Capacitor)
  150. ]]--
  151.  
  152. function printmon(strName,strAmount,strMax,strLegend)
  153.     if(strAmount<50000) then
  154.         return false;
  155.     end
  156.     local textColor;
  157.     local backColor;
  158.     local FullPercentAt = ItemsFullPercentAt;
  159.     if (strLegend == "#") then
  160.         textColor = chestTextColor;
  161.         backColor = chestBackColor;
  162.         strLegend = "";
  163.     end
  164.     if (strLegend == "+") then
  165.         textColor = tankTextColor;
  166.         backColor = tankBackColor;
  167.         FullPercentAt = FluidFullPercentAt;
  168.         strLegend = "";
  169.     end
  170.     if (strLegend == "*") then
  171.         textColor = powerTextColor;
  172.         backColor = powerBackColor;
  173.         strLegend = "";
  174.     end
  175.     if (strLegend == "$") then
  176.         textColor = cacheTextColor;
  177.         backColor = cacheBackColor;
  178.         strLegend = "";
  179.     end
  180.     if (strLegend == "?") then
  181.         textColor = tankTextColor;
  182.         backColor = tankBackColor;
  183.         strLegend = "";
  184.     end
  185.     local line = string.format("%s  %3i%s",padString(strName,NameLen+1),strAmount,padString(strLegend,1));
  186.     if (strAmount >= 1000000) then
  187.         line = string.format("%s  %.2fM%s",padString(strName,NameLen),strAmount/1000000,padString(strLegend,1));
  188.     elseif (strAmount >= 1000) then
  189.         line = string.format("%s  %.1fK%s",padString(strName,NameLen),strAmount/1000,padString(strLegend,1));
  190.     end
  191.  
  192.     local ColPadding = 0;
  193.     if (CurColumn > 0) then
  194.         ColPadding = 1;
  195.     end
  196.     local CurX = math.floor((CurColumn*ColumnWidth))+math.floor(CurColumn*ColPadding)+1;
  197.     if (CurColumn == 0) then
  198.         --  print("CurX:"..CurX);
  199.     end
  200.     mon.setCursorPos(CurX,CurLine);
  201.     --local percent = strAmount / strMax * 100;
  202.     mon.setBackgroundColor(backColor);
  203.     local percent = strAmount / FullPercentAt * 100;
  204.     if (percent > 100) then percent = 100; end
  205.     local barlength = math.floor(percent / 100 * (string.len(line)+2));
  206.     --if (CurColumn == 0) then
  207.     --  barlength = barlength + 1;
  208.     --end
  209.  
  210.     if (string.len(line) > barlength) then
  211.         local msg = string.sub(line,1,barlength);
  212.         mon.setTextColor(textColor);
  213.         mon.write(msg);
  214.         --[[if (percent == 0) then
  215.             mon.setBackgroundColor();
  216.         else
  217.             mon.setBackgroundColor(colors.black);
  218.         end--]]
  219.         mon.setBackgroundColor(colors.black);
  220.         mon.setTextColor(backColor);
  221.         mon.write(string.sub(line,barlength+1,-2))
  222.     else
  223.         local spaces = barlength - string.len(line);
  224.         mon.write(line);
  225.         mon.write(string.rep(" ",spaces));
  226.     end
  227.  
  228.     mon.setTextColor(colors.white);
  229.     CurColumn = CurColumn + 1;
  230.     if (CurColumn > MaxColumn) then
  231.         CurColumn = 0;
  232.         CurLine = CurLine + 1;
  233.     end
  234.     return true;
  235. end
  236.  
  237. -- Find a monitor
  238. function findMonitor()
  239.     for i,name in pairs(peripherals) do
  240.         for j,method in pairs(peripheral.getMethods(name)) do
  241.             if (method == 'getCursorPos') then
  242.                 prepmonitor(name);
  243.             end
  244.         end
  245.     end
  246. end
  247.  
  248. -- Find a wireless modem
  249. function findWirelessModem()
  250.     local foundWireless = false;
  251.     for i,name in pairs(peripherals) do
  252.         for j,method in pairs(peripheral.getMethods(name)) do
  253.             if (method == 'isWireless') then
  254.                 wmod = peripheral.wrap(name);
  255.                 if (wmod.isWireless()) then
  256.                     wmod.closeAll();
  257.                     foundWireless = true;
  258.                     break;
  259.                 else
  260.                     wmod = {};
  261.                 end
  262.             end
  263.         end
  264.         if (foundWireless) then
  265.             break;
  266.         end
  267.     end
  268. end
  269.  
  270. function collectLocalInfo()
  271.     local timestamp = os.clock();
  272.     for i,name in pairs(peripherals) do
  273.         local p = peripheral.wrap(name);
  274.         local displayNames = {};
  275.         if (p.getTankInfo ~= nil) then
  276.             print("Processing Tank");
  277.             local iteminfo = p.getTankInfo();
  278.             if (iteminfo ~= nil) then
  279.                 local displayname = "Empty";
  280.                 local amount = 0;
  281.                 if (iteminfo[1].contents) then
  282.                     displayname = iteminfo[1].contents.rawName;
  283.                     amount = iteminfo[1].contents.amount;
  284.                     amount = math.floor(amount/1000);
  285.                 end
  286.                 if (amount ~= 0) then
  287.                     updateTable(displayname,amount,timestamp,"+");
  288.                 end
  289.             end
  290.         end
  291.         if (p.getStoredItems ~= nil) then
  292.             print("Processing Cache");
  293.             local iteminfo = p.getStoredItems();
  294.             if (iteminfo) then
  295.                 local displayname = iteminfo.display_name;
  296.                 updateTable(displayname,iteminfo.qty,timestamp,"$");
  297.             end
  298.         end
  299.         if (p.getEnergyStored ~= nil) then
  300.             print("Processing Energy");
  301.             local energy = p.getEnergyStored();
  302.             if (energy ~= nil) then
  303.                 updateTable("Energy",energy,timestamp,"*");
  304.             end
  305.         end
  306.         if (p.getInventorySize ~= nil) then
  307.             print("Processing Chest");
  308.             local chestSize = p.getInventorySize();
  309.             if (chestSize ~= nil) then
  310.                 local items = {};
  311.                 for j=1,chestSize,1 do
  312.                     local iteminfo = p.getStackInSlot(j);
  313.                     if (iteminfo) then
  314.                         displayname = iteminfo.display_name;
  315.                         if (displayname) then
  316.                             if (not items[displayname]) then
  317.                                 items[displayname] = iteminfo.qty;
  318.                             else
  319.                                 items[displayname] = items[displayname] + iteminfo.qty;
  320.                             end
  321.                         end
  322.                     end
  323.                 end
  324.                 local k = 0;
  325.                 for key,val in pairs(items) do
  326.                     k = k + 1;
  327.                     updateTable(key,val,timestamp,"#");
  328.                 end
  329.             end
  330.         end
  331.         if (p.getMetadata ~= nil) then
  332.             local metadata = p.getMetadata();
  333.             if (metadata ~= nil) then
  334.                 if (metadata.displayName ~= nil) then
  335.                     if string.find(metadata.displayName, "Black Hole Controller") then
  336.                         local itemarray = p.list();
  337.                         if (itemarray ~= nil) then
  338.                             print("Processing Black Hole Controller");
  339.                             for j=1,table.getn(itemarray),1 do
  340.                                 if (itemarray[j].count > 0) then
  341.                                     local amount = itemarray[j].count;
  342.                                     local fingerprint = itemarray[j].name;
  343.                                     local displayName = itemarray[j].displayName;
  344.                                     updateTable(displayName,amount,timestamp,"$");
  345.                                 end
  346.                             end
  347.                         end
  348.                     end
  349.                 end
  350.             end
  351.         end
  352.         if (p.listAvailableItems ~= nil) then
  353.             print("Processing ME System");
  354.             local itemarray = p.listAvailableItems();
  355.             if (itemarray ~= nil) then
  356.                 for j=1,table.getn(itemarray),1 do
  357.                     if (itemarray[j].count > 0) then
  358.                         local amount = itemarray[j].count;
  359.                         local fingerprint = itemarray[j].name;
  360.                         local displayName = itemarray[j].displayName;
  361.                         updateTable(displayName,amount,timestamp,"#");
  362.                     end
  363.                 end
  364.             end
  365.         end
  366.         if (p.listItems ~= nil) then
  367.             print("Processing ME Bridge");
  368.             local itemarray = p.listItems();
  369.             if (itemarray ~= nil) then
  370.                 for j=1,table.getn(itemarray),1 do
  371.                     if (itemarray[j].amount > 0) then
  372.                         local amount = itemarray[j].amount;
  373.                         local fingerprint = itemarray[j].name;
  374.                         local displayName = itemarray[j].displayName;
  375.                         updateTable(displayName,amount,timestamp,"#");
  376.                     end
  377.                 end
  378.             end
  379.         end
  380.         print("Done");
  381.     end
  382. end
  383.  
  384. function updateMonitor()
  385.     x,y = mon.getSize();
  386.     ColumnWidth = NameLen + 10;
  387.     MaxColumn = math.floor(x / (ColumnWidth))-1;
  388.     mon.setBackgroundColor(colors.black);
  389.     mon.clear();
  390.     CurColumn = 0;
  391.     CurLine = 1;
  392.     mon.setCursorPos(1,1);
  393.     mon.setTextColor(colors.white);
  394.     mon.setTextScale(0.5);
  395.     mon.write(VersionInfo);
  396.     -- Sort by Base names
  397.     local sortedSources = {};
  398.     for n in pairs(ContentData) do
  399.         table.insert(sortedSources, n);
  400.     end
  401.     table.sort(sortedSources);
  402.     local name = "";
  403.     for i,source in ipairs(sortedSources) do
  404.         if (name ~= source) then
  405.             name = source;
  406.             CurColumn = 0;
  407.             mon.setTextColor(titleTextColor);
  408.             mon.setBackgroundColor(titleBackColor);
  409.             mon.setCursorPos(1,CurLine+1);
  410.             mon.write(padString("Contents for "..name,x-1));
  411.             CurLine = CurLine + 3;
  412.         end
  413.         sortedTimestamps = {};
  414.         for timestamp in pairs(ContentData[source]) do
  415.             table.insert(sortedTimestamps,timestamp);
  416.         end
  417.         table.sort(sortedTimestamps);
  418.         latest = nil;
  419.         for j=table.maxn(sortedTimestamps),1,-1 do
  420.             if (latest == nil) then
  421.                 latest = sortedTimestamps[j];
  422.             else
  423.                 ContentData[source][sortedTimestamps[j]] = nil;
  424.             end
  425.         end
  426.        
  427.         for itemgroup in pairs(ContentData[source][latest]) do
  428.             CurColumn = 0;
  429.             mon.setTextColor(titleTextColor);
  430.             mon.setBackgroundColor(titleBackColor);
  431.             mon.setCursorPos(1,CurLine-1);
  432.             mon.write(padString(itemgroup, x-1));
  433.             for itemname in pairs(ContentData[source][latest][itemgroup]) do
  434.                 count = ContentData[source][latest][itemgroup][itemname]["count"];
  435.                 if(count ~= nil) then
  436.                     legend = ContentData[source][latest][itemgroup][itemname]["legend"];
  437.                     max = 0;
  438.                     printmon(itemname,count,max,legend);
  439.                 end
  440.             end
  441.             CurLine = CurLine + 2;
  442.         end
  443.     end
  444. end
  445. -- This is the main section of the script
  446.  
  447. findMonitor();
  448. findWirelessModem();
  449.  
  450. if (isMain == true) then
  451.     if (type(wmod.isWireless) == "function") then
  452.         wmod.open(mainChannel);
  453.     else
  454.         print("You don't have a wireless modem, and this is set as the main computer");
  455.     end
  456. end
  457.  
  458. ContentData = {};
  459.  
  460. local timerUpdate = os.startTimer(UpdateIntervalSeconds);
  461. local updateCount = 0;
  462. local wirelessEventCount = 0;
  463. -- Perform Initial Collection and Update the Monitor
  464. collectLocalInfo();
  465. updateMonitor();
  466.  
  467. -- Main program loop
  468. while true do  
  469.     local event, param1, param2, param3, param4, param5 = os.pullEvent();
  470.     print("Received event:"..event);
  471.     if (event == "timer") then
  472.         if (param1 == timerUpdate) then
  473.             collectLocalInfo();
  474.             if (wmod) then
  475.                 if (isMain == false) then
  476.                     wmod.transmit(mainChannel,1,ContentData);
  477.                 end
  478.             end
  479.             updateMonitor();
  480.             wirelessEventCount = 0;
  481.             timerUpdate = os.startTimer(UpdateIntervalSeconds);
  482.         end
  483.     end
  484.     if (event == "modem_message") then
  485.         if (isMain == true) then
  486.             wirelessEventCount = wirelessEventCount + 1;
  487.             for source,data in pairs(param4) do
  488.                 if (ContentData[source] == nil) then
  489.                     ContentData[source] = {};
  490.                 end
  491.                 ContentData[source] = param4[source];
  492.             end
  493.             if (wirelessEventCount >= 10) then
  494.                 timerUpdate = os.startTimer(1);
  495.             end
  496.         end
  497.     end
  498.     if (event == "monitor_touch") or (event == "monitor_resize") then
  499.         print("Updating the Monitor");
  500.         collectLocalInfo();
  501.         updateMonitor();
  502.     end
  503.     if (event == "peripheral") or (event == "peripheral_detach") then
  504.         print("Updating the peripheral list");
  505.         peripherals = peripheral.getNames();
  506.     end
  507. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement