Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for i,v in pairs(pt) do
- local split = mysplit(v,",")
- playerlist[split[1]] = {["day"]=split[2],["time"]=split[3]}
- --printdebug("Read player "..split[1]..", "..split[2]..", "..split[3])
- end
- end
- function sense()
- printdebug(key.." "..message.." "..value)
- local players = mysplit(message,"!")
- if players[1]=="No-one" then return end
- for k,v in pairs(players) do
- playerlist[v] = {["day"]=os.day(),["time"]=os.time()}
- local colour = 0xFFFF00
- local notecommand = "random.orb#0.5"
- if searchlist(v,blacklist) then
- notecommand = "nuclearcontrol:alarm-default#1"
- colour = 0xFF0000
- end
- if not searchlist(v,whitelist) then
- rednet.broadcast("note "..notecommand,"dgserver")
- addtoticker("Sensor "..sender.."/"..pid..": "..v, colour)
- end
- end
- --updateticker()
- writeplayerlist()
- end
- function daystominutes(days)
- return days*20
- end
- function textsetalpha(t,a)
- --printdebug("Alpha "..a)
- t.setAlpha(a)
- end
- function updateticker()
- local s = false
- if refreshticker then
- local yy = -20
- for i,t in ipairs(ticker) do
- t["text"].setY(yy)
- yy = yy - 10
- end
- refreshticker = false
- s = true
- end
- local i = 1
- if #ticker > 0 then s = true end
- while i <= #ticker do
- -- too old? Eastbourne
- if ticker[i]["timestamp"] >= tickerttl then
- ticker[i]["text"].delete()
- table.remove(ticker,i)
- else
- local a = 0.11+((tickerttl-ticker[i]["timestamp"])*0.89/tickerttl)
- textsetalpha(ticker[i]["text"], a)
- ticker[i]["timestamp"] = ticker[i]["timestamp"] + 1
- i = i + 1
- end
- end
- if s then glass.sync() end
- end
- function addtoticker(text,colour)
- if colour==nil then colour=0xFFFFFF end
- table.insert(ticker, 1, {["timestamp"]=0, ["text"]=glass.addText(-10,-20,text,colour)})
- haligntext("right", ticker[1]["text"])
- saligntext("right", "bottom", ticker[1]["text"])
- refreshticker = true
- end
- function draw()
- if protocol=="ibarrel" then
- split = mysplit(liquidname,";")
- icontex = split[1]
- icondmg = split[2]
- else
- icontex = blockidms[id][1]
- icondmg = blockidms[id][2]
- end
- if texts[key] == nil then
- -- add to the bottom
- boxes[key] = glass.addBox(x+16+gap,y,mx,my,0xFFFFFF,0.0)
- texts[key] = fancytext(x+20+gap,y+4,message)
- gauges[key] = {}
- outlines[key] = outlinebox(x+16+gap,y,mx,my)
- for i,v in pairs(outlinebox(gap,y,16,16)) do table.insert(outlines[key],v) end
- table.insert(outlines[key],glass.addGradientBox(gap,y,16,16,0x000000,0.25,0x000000,0.5,1))
- if protocol=="estor" or protocol=="eout" or protocol=="eheat" or protocol=="eoutspeed" or protocol=="istor" or protocol=="ibarrel" or protocol == "fout" then
- for i=1,mx,2 do
- local line = glass.addBox(x+20+i,y,1,my,0x000000,0.2)
- line.setZ(2)
- table.insert(outlines[key],line)
- end
- end
- icons[key] = glass.addIcon(x,y,icontex,icondmg)
- icons[key].setZ(10)
- y = y + my + gap
- -- sort them into key order
- reorder()
- else
- for ii,vv in ipairs(texts[key]) do
- vv.setText(message)
- end
- end
- --printdebug(key.." "..message.." "..value)
- --printdebug("Key: "..key)
- --printdebug("Message: "..message)
- --printdebug("Svalue: "..svalue)
- --printdebug("Value: "..value)
- printdebug(protocol..": "..message)
- bx = boxes[key].getX()
- bw = boxes[key].getWidth()
- bpw = bw*(value/100)
- bxp = bx + bpw
- by = boxes[key].getY()
- bh = boxes[key].getHeight()
- for i,v in pairs(gauges[key]) do
- v.delete()
- end
- gauges[key] = {}
- if protocol=="estor" or protocol=="eout" or protocol=="eoutspeed" or protocol=="eheat" or protocol=="istor" or protocol=="ibarrel" then
- if (value>0) then
- table.insert(gauges[key],glass.addGradientBox(bx,by,bpw,bh,pcols[protocol][2],0.7,pcols[protocol][1],0.7,1))
- end
- if (value<100) then
- table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x111111,0.7,0x333333,0.7,1))
- end
- if protocol=="ibarrel" and icons[key] ~= nil then
- local nx = icons[key].getX()
- local ny = icons[key].getY()
- local oldicon = icons[key]
- icons[key] = glass.addIcon(nx,ny,icontex,icondmg)
- oldicon.delete()
- end
- elseif protocol=="aecells" then
- for ii,vv in ipairs(texts[key]) do
- vv.setText("")
- end
- for i=1,value do
- cw = (bw/value)
- cellcol = 0x333333
- if string.sub(message,i,i)=="g" then cellcol = 0x66FF66
- elseif string.sub(message,i,i)=="o" then cellcol = 0xFF9933
- elseif string.sub(message,i,i)=="r" then cellcol = 0xFF0000 end
- table.insert(gauges[key],glass.addGradientBox(bx+((i-1)*cw),by,cw,bh,cellcol,0.5,cellcol,0.7,2))
- end
- elseif protocol=="tank" or protocol=="fout" then
- if (value>0) then
- table.insert(gauges[key],glass.addLiquid(bx,by,bpw,bh,liquidname))
- gauges[key][1].setAlpha(0.7)
- end
- if (value<100) then table.insert(gauges[key],glass.addGradientBox(bxp,by,bw-bpw,bh,0x000000,0.25,0x000000,0.5,1)) end
- end
- glass.sync()
- end
- function serialised(array)
- local s = ""
- for _,v in pairs(array) do
- s = s .. "," .. v
- end
- return string.sub(s, 2)
- end
- function sendlist(sender,listname,list)
- rednet.send(sender,listname.." "..serialised(list),"dgserver")
- end
- function broadcastlist(listname,list)
- rednet.broadcast(listname.." "..serialised(list),"dgserver")
- end
- -- initialise
- term.clear()
- term.setCursorPos(1,1)
- print("Deadlock989's DeadGlass server "..version.." running!")
- if (os.getComputerLabel()==nil) then
- print("Setting computer label to 'dgserver'.")
- os.setComputerLabel("dgserver")
- end
- print("Loading whitelist ...")
- whitelist = filetoarray("whitelist.txt")
- print("Loading blacklist ...")
- blacklist = filetoarray("blacklist.txt")
- print("Loading detected players list ...")
- readplayerlist()
- modemfound = false
- tgbfound = false
- monitorfound = false
- sides = peripheral.getNames()
- for i=1, #sides do
- thistype = peripheral.getType(sides[i])
- if thistype=="modem" then
- print("Attaching to modem on the "..sides[i].." ...")
- rednet.open(sides[i])
- modemfound = true
- end
- if thistype=="monitor" then
- print("Attaching to monitor on the "..sides[i].." ...")
- mymonitor = peripheral.wrap(sides[i])
- monitorfound = true
- mymonitor.clear()
- mymonitor.setCursorPos(1,1)
- end
- if not tgbfound and thistype=="openperipheral_bridge" then
- print("Attaching to TGB on the "..sides[i].." ...")
- glass = peripheral.wrap(sides[i])
- tgbfound = true
- -- workaround text bug?
- end
- end
- if not modemfound then error("No modem attached. Aborting.") end
- if not tgbfound then error("No Terminal Glasses Bridge attached. Aborting.") end
- rednet.host("dgserver","dgserver")
- glass.clear()
- drawtitle()
- print("Running ...")
- -- network events
- function netevent(p1,p2,p3)
- sender = p1
- message = p2
- protocol = p3
- psplit = mysplit(protocol,"/")
- protocol = psplit[1]
- if not searchlist(protocol,knownprotocols) then return end
- pid = psplit[2]
- if protocol=="handshake" then
- printdebug("Received handshake request from client "..sender)
- sendlist(sender,"whitelist",whitelist)
- sendlist(sender,"blacklist",blacklist)
- return
- end
- id = ""
- value = 0
- split = mysplit(message,"|")
- message = split[1]
- id = split[2]
- value = tonumber(split[3])
- svalue = split[3]
- liquidname = split[4]
- key = protocol.."/"..blockidms[id][1].."/"..sender.."/"..pid
- timestamp[key] = os.clock()
- -- new info?
- if lastmessage[key]==nil or lastmessage[key]~=message then
- lastmessage[key] = message
- if protocol == "sensor" then sense()
- else draw() end
- end
- end
- function listcommand(array,file,commands)
- local colour = 0x00FF00
- if commands[1]=="blacklist" then
- colour = 0xFF0000
- end
- if commands[2]==nil and commands[3]==nil then
- if #array==0 then addtoticker("List is empty")
- else
- addtoticker(commands[1]:gsub("^%l", string.upper)..":")
- for i,v in ipairs(array) do addtoticker(v, colour) end
- end
- --updateticker()
- elseif commands[3]==nil or (commands[2]~="add" and commands[2]~="remove") then
- addtoticker("Syntax: $$"..commands[1].." [add/remove playername]")
- --updateticker()
- elseif commands[2]=="add" then
- local found = false
- if commands[1]=="whitelist" and searchlist(commands[3],blacklist) then
- addtoticker("Player is in blacklist")
- return array
- elseif commands[1]=="blacklist" and searchlist(commands[3],whitelist) then
- addtoticker("Player is in whitelist")
- return array
- end
- for i,v in ipairs(array) do
- if v==commands[3] then
- found = true
- break
- end
- end
- if found then
- addtoticker("Player "..commands[3].." is already in list")
- --updateticker()
- else
- table.insert(array,commands[3])
- arraytofile(array,file)
- addtoticker("Player "..commands[3].." added")
- --updateticker()
- broadcastlist(commands[1],array)
- end
- elseif commands[2]=="remove" then
- local i = 1
- local found = false
- while i <= #array do
- if array[i]==commands[3] then
- table.remove(array,i)
- found = true
- break
- end
- i = i + 1
- end
- if found then
- arraytofile(array,file)
- addtoticker("Player "..commands[3].." removed")
- --updateticker()
- broadcastlist(commands[1],array)
- else
- addtoticker("Player "..commands[3].." not found in list")
- --updateticker()
- end
- end
- return array
- end
- -- chat commands
- function chatcommand(side,player,key,command)
- printdebug("Command received: "..command)
- commands = mysplit(command," ")
- if command=="reset" then
- redraw()
- elseif command=="small" then
- mx=50
- redraw()
- elseif command=="medium" then
- mx=100
- redraw()
- elseif command=="large" then
- mx=200
- redraw()
- elseif command=="reboot" then
- os.reboot()
- elseif command=="players" then
- for k,v in pairs(playerlist) do
- local colour = 0xFFFF00
- if searchlist(v, whitelist) then colour = 0x00FF00
- elseif searchlist(v, blacklist) then colour = 0xFF0000
- end
- addtoticker(k..": "..timedifftoenglish(estimatetimediff(v["day"],v["time"])))
- end
- elseif commands[1]=="redstone" then
- if commands[2]==nil or (commands[2]~="on" and commands[2]~="off") then
- addtoticker("Syntax: $$redstone on/off [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"dgserver")
- elseif commands[1]=="credstone" then
- if commands[2]==nil or not searchlist(commands[2],colourlist) or commands[3]==nil or (commands[3]~="on" and commands[3]~="off") then
- addtoticker("Syntax: $$credstone colour on/off [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"dgserver")
- elseif commands[1]=="lamps" then
- if commands[2]==nil or string.find(commands[2],"^%x%x%x%x%x%x$")==nil then
- addtoticker("Syntax: $$lamps FFFFFF [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"dgserver")
- elseif commands[1]=="note" then
- if commands[2]==nil or string.find(commands[2],"#")==nil then
- addtoticker("Syntax: $$note notename#pitch [clientname]")
- return
- end
- printdebug("Broadcasting ...")
- rednet.broadcast(command,"dgserver")
- elseif commands[1]=="whitelist" then
- whitelist = listcommand(whitelist,"whitelist.txt",commands)
- elseif commands[1]=="blacklist" then
- blacklist = listcommand(blacklist,"blacklist.txt",commands)
- elseif commands[1]=="brrods" then
- if commands[2]==nil or string.find(commands[2],"^%d%d?%d?$")==nil or tonumber(commands[2])<0 or tonumber(commands[2])>100 then
- addtoticker("Syntax: $$brrods 0-100 [clientname]")
- return
- end
- rednet.broadcast(command,"dgserver")
- elseif commands[1]=="brflow" then
- 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
- addtoticker("Syntax: $$brrods 0-2000 [clientname]")
- return
- end
- rednet.broadcast(command,"dgserver")
- end
- end
- function getkeyset(t)
- ks = {}
- for k,v in pairs(t) do
- table.insert(ks,k)
- end
- return ks
- end
- function timerlistener(p1)
- if p1==heartbeat then
- heartbeat = os.startTimer(hbtime)
- --printdebug("*Heartbeat*")
- updateticker()
- hbcount = hbcount + 1
- if (hbcount%displaycheckcount==0) then
- -- kill dead client displays
- local k = 1
- for i,k in ipairs(getkeyset(timestamp)) do
- if (os.clock()-timestamp[k])>=decaytime then
- printdebug("Removing "..k)
- if boxes[k] ~= nil then
- for ii,vv in ipairs(gauges[k]) do
- vv.delete()
- end
- for ii,vv in ipairs(outlines[k]) do
- vv.delete()
- end
- for ii,vv in ipairs(texts[k]) do
- vv.delete()
- end
- boxes[k].delete()
- icons[k].delete()
- end
- gauges[k] = nil
- outlines[k] = nil
- texts[k] = nil
- boxes[k] = nil
- icons[k] = nil
- timestamp[k] = nil
- lastmessage[k] = nil
- end
- end
- reorder()
- glass.sync()
- end
- end
- end
- function eventlistener()
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- if event=="rednet_message" then netevent(p1,p2,p3)
- elseif event=="timer" then timerlistener(p1)
- elseif event=="glasses_chat_command" then chatcommand(p1,p2,p3,p4) end
- end
- -- main loop
- heartbeat = os.startTimer(hbtime)
- while true do
- eventlistener()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement