Advertisement
Mackan90096

server

Aug 3rd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. local modemSide = "back"
  2. local bridgeSide = "bottom"
  3.  
  4. local glasses = peripheral.wrap(bridgeSide)
  5.  
  6. -- Bar Color, BackgroundColor
  7. local colors = {
  8. ["energy"] = {0x00FF00, 0x0000AA}
  9. }
  10.  
  11. rednet.open(modemSide)
  12.  
  13. glasses.clear()
  14.  
  15. rednet.host("glassMonitor", "server")
  16.  
  17. local gap = 3
  18. local mx = 100
  19. local my = 16
  20.  
  21.  
  22. local offsetY = 160
  23.  
  24. local function outlineBox(xx,yy,ww,hh,color)
  25. if color == nil then
  26. color = 0x000000
  27. end
  28.  
  29. local outlineTable = {}
  30. table.insert(outlineTable, glasses.addBox(xx-1,yy-1,ww+2,1,color,0.5))
  31. table.insert(outlineTable, glasses.addBox(xx-1,yy+hh,ww+2,1,color,0.5))
  32. table.insert(outlineTable, glasses.addBox(xx-1,yy,1,my,color,0.5))
  33. table.insert(outlineTable, glasses.addBox(xx+ww,yy,1,my,color,0.5))
  34.  
  35. return outlineTable
  36. end
  37.  
  38. local function hAlignText(halign,t)
  39. t.setObjectAnchor(halign,"top")
  40. end
  41.  
  42. local function hAlignTexts(halign,textTable)
  43. for i,v in pairs(textTable) do
  44. v.setObjectAnchor(halign,"top")
  45. end
  46. end
  47.  
  48. local function readableNumber(n)
  49. if n >= 10000000000 then return ""..(math.floor((n/1000000000)+0.5)).." G" end
  50. if n >= 1000000000 then return ""..(math.floor((n/100000000)+0.5)/10).." G" end
  51. if n >= 10000000 then return ""..(math.floor((n/1000000)+0.5)).." M" end
  52. if n >= 1000000 then return ""..(math.floor((n/100000)+0.5)/10).." M" end
  53. if n >= 10000 then return ""..(math.floor((n/1000)+0.5)).." k" end
  54. if n >= 1000 then return ""..(math.floor((n/100)+0.5)/10).." k" end
  55. if n >= 100 then return ""..math.floor((n)+0.5).." " end
  56. return ""..(math.floor((n*10)+0.5)/10).." "
  57. end
  58.  
  59. local function splitString(str, pat)
  60. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  61. local fpat = "(.-)" .. pat
  62. local last_end = 1
  63. local s, e, cap = str:find(fpat, 1)
  64. while s do
  65. if s ~= 1 or cap ~= "" then
  66. table.insert(t,cap)
  67. end
  68. last_end = e+1
  69. s, e, cap = str:find(fpat, last_end)
  70. end
  71. if last_end <= #str then
  72. cap = str:sub(last_end)
  73. table.insert(t, cap)
  74. end
  75. return t
  76. end
  77.  
  78. local function fancytext(xx,yy,text,colour,alpha)
  79. if colour == nil then
  80. colour = 0xFFFFFF
  81. end
  82.  
  83. if alpha == nil then
  84. alpha=1
  85. end
  86.  
  87. local t1 = glasses.addText(xx,yy,text,colour)
  88.  
  89. t1.setAlpha(alpha)
  90. t1.setZ(10)
  91. local t2 = glasses.addText(xx+1,yy+1,text,0x000000)
  92. t2.setAlpha(alpha/3)
  93. t2.setZ(5)
  94. return {t1,t2}
  95. end
  96.  
  97. local function drawTitle()
  98. local bannerText = "Base Monitor"
  99.  
  100. hAlignTexts("middle", fancytext((16+mx+gap*2)/2, offsetY+gap+4,bannerText,0xFFFFFF))
  101. outlineBox(gap,offsetY+gap,16+mx+gap,my)
  102. glasses.addGradientBox(gap,offsetY+gap,16+mx+gap,my,0x000000,0.25,0x000000,0.5,1)
  103. end
  104.  
  105. local function percent(minVal, maxVal)
  106. return math.floor((minVal/maxVal)*100)
  107. end
  108.  
  109. local function drawEnergy(currentEnergy, maxEnergy)
  110. local x = 1
  111. local i = 1
  112. local y = 17
  113.  
  114. local text = readableNumber(currentEnergy).."/"..readableNumber(maxEnergy)
  115.  
  116. hAlignTexts("middle", fancytext((16+mx+gap*2)/2, offsetY+gap+4+(my),text,0xFFFFFF))
  117. outlineBox(gap,offsetY+gap+(my), 16+mx+gap,my)
  118. glasses.addGradientBox(gap, offsetY+gap+(my), 16+mx+gap,my,0x000000,0.25,0x000000,0.5,1)
  119.  
  120. local icon = glasses.addIcon(gap,offsetY+gap+my+1,"EnderIO:blockCapacitorBank",3)
  121.  
  122. icon.setZ(5)
  123.  
  124. local energyPercentage = percent(currentEnergy, maxEnergy)
  125.  
  126. local line = glasses.addBox(gap, offsetY+gap+my, (16+gap)+energyPercentage, my, 0x00FF00,0.5)
  127. line.setZ(2)
  128. end
  129.  
  130. local function chatCommand()
  131.  
  132. end
  133.  
  134. local function netEvent(sender, message, protocol)
  135. print("NET EVENT "..sender.." "..message.." "..protocol)
  136. if protocol == "energy" then
  137. local energySplit = splitString(message, "|")
  138. glasses.clear()
  139. drawTitle()
  140. drawEnergy(tonumber(energySplit[1]), tonumber(energySplit[2]))
  141. end
  142. end
  143.  
  144. drawTitle()
  145. glasses.sync()
  146.  
  147. while true do
  148. local event, p1, p2, p3, p4, p5 = os.pullEvent()
  149. if event=="modem_message" then
  150. netEvent(p1,p4["message"], p4["sProtocol"])
  151. elseif event=="glasses_chat_command" then
  152. chatCommand(p1,p2,p3,p4)
  153. end
  154.  
  155. glasses.sync()
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement