Advertisement
se7enek

Part 2

Jun 18th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. for i,v in pairs(pt) do
  2. local split = mysplit(v,",")
  3. playerlist[split[1]] = {["day"]=split[2],["time"]=split[3]}
  4. --printdebug("Read player "..split[1]..", "..split[2]..", "..split[3])
  5. end
  6. end
  7.  
  8. function sense()
  9. printdebug(key.." "..message.." "..value)
  10. local players = mysplit(message,"!")
  11. if players[1]=="No-one" then return end
  12. for k,v in pairs(players) do
  13. playerlist[v] = {["day"]=os.day(),["time"]=os.time()}
  14. local colour = 0xFFFF00
  15. local notecommand = "random.orb#0.5"
  16. if searchlist(v,blacklist) then
  17. notecommand = "nuclearcontrol:alarm-default#1"
  18. colour = 0xFF0000
  19. end
  20. if not searchlist(v,whitelist) then
  21. rednet.broadcast("note "..notecommand,"dgserver")
  22. addtoticker("Sensor "..sender.."/"..pid..": "..v, colour)
  23. end
  24. end
  25. --updateticker()
  26. writeplayerlist()
  27. end
  28.  
  29. function daystominutes(days)
  30. return days*20
  31. end
  32.  
  33. function textsetalpha(t,a)
  34. --printdebug("Alpha "..a)
  35. t.setAlpha(a)
  36. end
  37.  
  38. function updateticker()
  39. local s = false
  40. if refreshticker then
  41. local yy = -20
  42. for i,t in ipairs(ticker) do
  43. t["text"].setY(yy)
  44. yy = yy - 10
  45. end
  46. refreshticker = false
  47. s = true
  48. end
  49. local i = 1
  50. if #ticker > 0 then s = true end
  51. while i <= #ticker do
  52. -- too old? Eastbourne
  53. if ticker[i]["timestamp"] >= tickerttl then
  54. ticker[i]["text"].delete()
  55. table.remove(ticker,i)
  56. else
  57. local a = 0.11+((tickerttl-ticker[i]["timestamp"])*0.89/tickerttl)
  58. textsetalpha(ticker[i]["text"], a)
  59. ticker[i]["timestamp"] = ticker[i]["timestamp"] + 1
  60. i = i + 1
  61. end
  62. end
  63. if s then glass.sync() end
  64. end
  65.  
  66. function addtoticker(text,colour)
  67. if colour==nil then colour=0xFFFFFF end
  68. table.insert(ticker, 1, {["timestamp"]=0, ["text"]=glass.addText(-10,-20,text,colour)})
  69. haligntext("right", ticker[1]["text"])
  70. saligntext("right", "bottom", ticker[1]["text"])
  71. refreshticker = true
  72. end
  73.  
  74. function draw()
  75. if protocol=="ibarrel" then
  76. split = mysplit(liquidname,";")
  77. icontex = split[1]
  78. icondmg = split[2]
  79. else
  80. icontex = blockidms[id][1]
  81. icondmg = blockidms[id][2]
  82. end
  83. if texts[key] == nil then
  84. -- add to the bottom
  85. boxes[key] = glass.addBox(x+16+gap,y,mx,my,0xFFFFFF,0.0)
  86. texts[key] = fancytext(x+20+gap,y+4,message)
  87. gauges[key] = {}
  88. outlines[key] = outlinebox(x+16+gap,y,mx,my)
  89. for i,v in pairs(outlinebox(gap,y,16,16)) do table.insert(outlines[key],v) end
  90. table.insert(outlines[key],glass.addGradientBox(gap,y,16,16,0x000000,0.25,0x000000,0.5,1))
  91. if protocol=="estor" or protocol=="eout" or protocol=="eheat" or protocol=="eoutspeed" or protocol=="istor" or protocol=="ibarrel" or protocol == "fout" then
  92. for i=1,mx,2 do
  93. local line = glass.addBox(x+20+i,y,1,my,0x000000,0.2)
  94. line.setZ(2)
  95. table.insert(outlines[key],line)
  96. end
  97. end
  98. icons[key] = glass.addIcon(x,y,icontex,icondmg)
  99. icons[key].setZ(10)
  100. y = y + my + gap
  101. -- sort them into key order
  102. reorder()
  103. else
  104. for ii,vv in ipairs(texts[key]) do
  105. vv.setText(message)
  106. end
  107. end
  108.  
  109. --printdebug(key.." "..message.." "..value)
  110. --printdebug("Key: "..key)
  111. --printdebug("Message: "..message)
  112. --printdebug("Svalue: "..svalue)
  113. --printdebug("Value: "..value)
  114. printdebug(protocol..": "..message)
  115.  
  116. bx = boxes[key].getX()
  117. bw = boxes[key].getWidth()
  118. bpw = bw*(value/100)
  119. bxp = bx + bpw
  120. by = boxes[key].getY()
  121. bh = boxes[key].getHeight()
  122. for i,v in pairs(gauges[key]) do
  123. v.delete()
  124. end
  125. gauges[key] = {}
  126.  
  127. if protocol=="estor" or protocol=="eout" or protocol=="eoutspeed" or protocol=="eheat" or protocol=="istor" or protocol=="ibarrel" then
  128. if (value>0) then
  129. table.insert(gauges[key],glass.addGradientBox(bx,by,bpw,bh,pcols[protocol][2],0.7,pcols[protocol][1],0.7,1))
  130. end
  131. if (value<100) then
  132. table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x111111,0.7,0x333333,0.7,1))
  133. end
  134. if protocol=="ibarrel" and icons[key] ~= nil then
  135. local nx = icons[key].getX()
  136. local ny = icons[key].getY()
  137. local oldicon = icons[key]
  138. icons[key] = glass.addIcon(nx,ny,icontex,icondmg)
  139. oldicon.delete()
  140. end
  141. elseif protocol=="aecells" then
  142. for ii,vv in ipairs(texts[key]) do
  143. vv.setText("")
  144. end
  145. for i=1,value do
  146. cw = (bw/value)
  147. cellcol = 0x333333
  148. if string.sub(message,i,i)=="g" then cellcol = 0x66FF66
  149. elseif string.sub(message,i,i)=="o" then cellcol = 0xFF9933
  150. elseif string.sub(message,i,i)=="r" then cellcol = 0xFF0000 end
  151. table.insert(gauges[key],glass.addGradientBox(bx+((i-1)*cw),by,cw,bh,cellcol,0.5,cellcol,0.7,2))
  152. end
  153. elseif protocol=="tank" or protocol=="fout" then
  154. if (value>0) then
  155. table.insert(gauges[key],glass.addLiquid(bx,by,bpw,bh,liquidname))
  156. gauges[key][1].setAlpha(0.7)
  157. end
  158. if (value<100) then table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x000000,0.25,0x000000,0.5,1)) end
  159. end
  160.  
  161. glass.sync()
  162.  
  163. end
  164.  
  165. function serialised(array)
  166. local s = ""
  167. for _,v in pairs(array) do
  168. s = s .. "," .. v
  169. end
  170. return string.sub(s, 2)
  171. end
  172.  
  173. function sendlist(sender,listname,list)
  174. rednet.send(sender,listname.." "..serialised(list),"dgserver")
  175. end
  176.  
  177. function broadcastlist(listname,list)
  178. rednet.broadcast(listname.." "..serialised(list),"dgserver")
  179. end
  180.  
  181.  
  182. -- initialise
  183.  
  184. term.clear()
  185. term.setCursorPos(1,1)
  186. print("Deadlock989's DeadGlass server "..version.." running!")
  187. if (os.getComputerLabel()==nil) then
  188. print("Setting computer label to 'dgserver'.")
  189. os.setComputerLabel("dgserver")
  190. end
  191. print("Loading whitelist ...")
  192. whitelist = filetoarray("whitelist.txt")
  193. print("Loading blacklist ...")
  194. blacklist = filetoarray("blacklist.txt")
  195. print("Loading detected players list ...")
  196. readplayerlist()
  197.  
  198. modemfound = false
  199. tgbfound = false
  200. monitorfound = false
  201. sides = peripheral.getNames()
  202. for i=1, #sides do
  203. thistype = peripheral.getType(sides[i])
  204. if thistype=="modem" then
  205. print("Attaching to modem on the "..sides[i].." ...")
  206. rednet.open(sides[i])
  207. modemfound = true
  208. end
  209. if thistype=="monitor" then
  210. print("Attaching to monitor on the "..sides[i].." ...")
  211. mymonitor = peripheral.wrap(sides[i])
  212. monitorfound = true
  213. mymonitor.clear()
  214. mymonitor.setCursorPos(1,1)
  215. end
  216. if not tgbfound and thistype=="openperipheral_bridge" then
  217. print("Attaching to TGB on the "..sides[i].." ...")
  218. glass = peripheral.wrap(sides[i])
  219. tgbfound = true
  220. -- workaround text bug?
  221. end
  222. end
  223.  
  224. if not modemfound then error("No modem attached. Aborting.") end
  225. if not tgbfound then error("No Terminal Glasses Bridge attached. Aborting.") end
  226.  
  227. rednet.host("dgserver","dgserver")
  228. glass.clear()
  229. drawtitle()
  230. print("Running ...")
  231.  
  232. -- network events
  233. function netevent(p1,p2,p3)
  234. sender = p1
  235. message = p2
  236. protocol = p3
  237. psplit = mysplit(protocol,"/")
  238. protocol = psplit[1]
  239. if not searchlist(protocol,knownprotocols) then return end
  240. pid = psplit[2]
  241.  
  242. if protocol=="handshake" then
  243. printdebug("Received handshake request from client "..sender)
  244. sendlist(sender,"whitelist",whitelist)
  245. sendlist(sender,"blacklist",blacklist)
  246. return
  247. end
  248.  
  249. id = ""
  250. value = 0
  251. split = mysplit(message,"|")
  252. message = split[1]
  253. id = split[2]
  254. value = tonumber(split[3])
  255. svalue = split[3]
  256. liquidname = split[4]
  257.  
  258. key = protocol.."/"..blockidms[id][1].."/"..sender.."/"..pid
  259. timestamp[key] = os.clock()
  260.  
  261. -- new info?
  262. if lastmessage[key]==nil or lastmessage[key]~=message then
  263. lastmessage[key] = message
  264. if protocol == "sensor" then sense()
  265. else draw() end
  266. end
  267. end
  268.  
  269. function listcommand(array,file,commands)
  270. local colour = 0x00FF00
  271. if commands[1]=="blacklist" then
  272. colour = 0xFF0000
  273. end
  274. if commands[2]==nil and commands[3]==nil then
  275. if #array==0 then addtoticker("List is empty")
  276. else
  277. addtoticker(commands[1]:gsub("^%l", string.upper)..":")
  278. for i,v in ipairs(array) do addtoticker(v, colour) end
  279. end
  280. --updateticker()
  281. elseif commands[3]==nil or (commands[2]~="add" and commands[2]~="remove") then
  282. addtoticker("Syntax: $$"..commands[1].." [add/remove playername]")
  283. --updateticker()
  284. elseif commands[2]=="add" then
  285. local found = false
  286. if commands[1]=="whitelist" and searchlist(commands[3],blacklist) then
  287. addtoticker("Player is in blacklist")
  288. return array
  289. elseif commands[1]=="blacklist" and searchlist(commands[3],whitelist) then
  290. addtoticker("Player is in whitelist")
  291. return array
  292. end
  293. for i,v in ipairs(array) do
  294. if v==commands[3] then
  295. found = true
  296. break
  297. end
  298. end
  299. if found then
  300. addtoticker("Player "..commands[3].." is already in list")
  301. --updateticker()
  302. else
  303. table.insert(array,commands[3])
  304. arraytofile(array,file)
  305. addtoticker("Player "..commands[3].." added")
  306. --updateticker()
  307. broadcastlist(commands[1],array)
  308. end
  309. elseif commands[2]=="remove" then
  310. local i = 1
  311. local found = false
  312. while i <= #array do
  313. if array[i]==commands[3] then
  314. table.remove(array,i)
  315. found = true
  316. break
  317. end
  318. i = i + 1
  319. end
  320. if found then
  321. arraytofile(array,file)
  322. addtoticker("Player "..commands[3].." removed")
  323. --updateticker()
  324. broadcastlist(commands[1],array)
  325. else
  326. addtoticker("Player "..commands[3].." not found in list")
  327. --updateticker()
  328. end
  329. end
  330. return array
  331. end
  332.  
  333. -- chat commands
  334. function chatcommand(side,player,key,command)
  335. printdebug("Command received: "..command)
  336. commands = mysplit(command," ")
  337. if command=="reset" then
  338. redraw()
  339. elseif command=="small" then
  340. mx=50
  341. redraw()
  342. elseif command=="medium" then
  343. mx=100
  344. redraw()
  345. elseif command=="large" then
  346. mx=200
  347. redraw()
  348. elseif command=="reboot" then
  349. os.reboot()
  350. elseif command=="players" then
  351. for k,v in pairs(playerlist) do
  352. local colour = 0xFFFF00
  353. if searchlist(v, whitelist) then colour = 0x00FF00
  354. elseif searchlist(v, blacklist) then colour = 0xFF0000
  355. end
  356. addtoticker(k..": "..timedifftoenglish(estimatetimediff(v["day"],v["time"])))
  357. end
  358. elseif commands[1]=="redstone" then
  359. if commands[2]==nil or (commands[2]~="on" and commands[2]~="off") then
  360. addtoticker("Syntax: $$redstone on/off [clientname]")
  361. return
  362. end
  363. printdebug("Broadcasting ...")
  364. rednet.broadcast(command,"dgserver")
  365. elseif commands[1]=="credstone" then
  366. if commands[2]==nil or not searchlist(commands[2],colourlist) or commands[3]==nil or (commands[3]~="on" and commands[3]~="off") then
  367. addtoticker("Syntax: $$credstone colour on/off [clientname]")
  368. return
  369. end
  370. printdebug("Broadcasting ...")
  371. rednet.broadcast(command,"dgserver")
  372. elseif commands[1]=="lamps" then
  373. if commands[2]==nil or string.find(commands[2],"^%x%x%x%x%x%x$")==nil then
  374. addtoticker("Syntax: $$lamps FFFFFF [clientname]")
  375. return
  376. end
  377. printdebug("Broadcasting ...")
  378. rednet.broadcast(command,"dgserver")
  379. elseif commands[1]=="note" then
  380. if commands[2]==nil or string.find(commands[2],"#")==nil then
  381. addtoticker("Syntax: $$note notename#pitch [clientname]")
  382. return
  383. end
  384. printdebug("Broadcasting ...")
  385. rednet.broadcast(command,"dgserver")
  386. elseif commands[1]=="whitelist" then
  387. whitelist = listcommand(whitelist,"whitelist.txt",commands)
  388. elseif commands[1]=="blacklist" then
  389. blacklist = listcommand(blacklist,"blacklist.txt",commands)
  390. elseif commands[1]=="brrods" then
  391. if commands[2]==nil or string.find(commands[2],"^%d%d?%d?$")==nil or tonumber(commands[2])<0 or tonumber(commands[2])>100 then
  392. addtoticker("Syntax: $$brrods 0-100 [clientname]")
  393. return
  394. end
  395. rednet.broadcast(command,"dgserver")
  396. elseif commands[1]=="brflow" then
  397. if commands[2]==nil or string.find(commands[2],"^%d%d?%d?%d?$")==nil or tonumber(commands[2])<0 or tonumber(commands[2])>2000 then
  398. addtoticker("Syntax: $$brrods 0-2000 [clientname]")
  399. return
  400. end
  401. rednet.broadcast(command,"dgserver")
  402. end
  403. end
  404.  
  405. function getkeyset(t)
  406. ks = {}
  407. for k,v in pairs(t) do
  408. table.insert(ks,k)
  409. end
  410. return ks
  411. end
  412.  
  413. function timerlistener(p1)
  414. if p1==heartbeat then
  415. heartbeat = os.startTimer(hbtime)
  416. --printdebug("*Heartbeat*")
  417. updateticker()
  418. hbcount = hbcount + 1
  419. if (hbcount%displaycheckcount==0) then
  420. -- kill dead client displays
  421. local k = 1
  422. for i,k in ipairs(getkeyset(timestamp)) do
  423. if (os.clock()-timestamp[k])>=decaytime then
  424. printdebug("Removing "..k)
  425. if boxes[k] ~= nil then
  426. for ii,vv in ipairs(gauges[k]) do
  427. vv.delete()
  428. end
  429. for ii,vv in ipairs(outlines[k]) do
  430. vv.delete()
  431. end
  432. for ii,vv in ipairs(texts[k]) do
  433. vv.delete()
  434. end
  435. boxes[k].delete()
  436. icons[k].delete()
  437. end
  438. gauges[k] = nil
  439. outlines[k] = nil
  440. texts[k] = nil
  441. boxes[k] = nil
  442. icons[k] = nil
  443. timestamp[k] = nil
  444. lastmessage[k] = nil
  445. end
  446. end
  447. reorder()
  448. glass.sync()
  449. end
  450. end
  451. end
  452.  
  453. function eventlistener()
  454. local event, p1, p2, p3, p4, p5 = os.pullEvent()
  455. if event=="rednet_message" then netevent(p1,p2,p3)
  456. elseif event=="timer" then timerlistener(p1)
  457. elseif event=="glasses_chat_command" then chatcommand(p1,p2,p3,p4) end
  458. end
  459.  
  460. -- main loop
  461. heartbeat = os.startTimer(hbtime)
  462. while true do
  463. eventlistener()
  464. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement