Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TorakTu - Template Server [ 8.26.2013.1531 ]
- -- ComputerCraft
- os.pullEvent = os.pullEventRaw
- local mainName = "Template Server"
- local em = "NONE"
- local sides = {"left","right","top","bottom","back"}
- local mSide = { }
- local mtotal = 0
- local isModem = 0
- local off = 0
- local mon = ""
- local advancedornot = false
- local currPath = shell.resolve(".")
- local timer
- local theSwitch = 0
- local theTime = 0
- local menu_options = 0
- local trans = 0
- local mScaleSize = 0.5
- local setMonScale = 0
- local setlock = 0
- local initLock = false
- local tr = 0
- local serverIdent = ""
- local complexity = 5 -- This is where you define
- -- how complex your serverIdent
- -- should be.
- -- Higher # = more complex
- if currPath == "disk" then currPath = "/disk/" end
- local settings = { }
- settings.isonline = "offline"
- for _,v in pairs(sides) do
- if peripheral.getType(v) == "modem" then
- mtotal = mtotal + 1
- mSide[mtotal] = v
- isModem = 1
- end
- if peripheral.getType(v) == "monitor" then
- em = v
- end
- end
- if isModem == 0 then
- term.clear()
- term.setCursorPos(1,1)
- print(" ")
- print(" WARNING ! No modem attached !")
- print(" ")
- print(" PROGRAM TERMINATED !")
- print(" ")
- return
- end
- local modem = peripheral.wrap(mSide[1])
- if em ~= "NONE" or em == "bottom" or em == "top" or em == "left" or em == "right" or em == "back" then
- mon = peripheral.wrap(em)
- mon.setTextScale(0.5)
- else
- mon = "NONE"
- end
- if term.isColor() then
- advancedornot = true
- end
- function mtprint(theString,x,y,fg,bg)
- if mon ~= "NONE" then
- if advancedornot then
- if f ~= 0 then
- mon.setTextColor(fg)
- term.setTextColor(fg)
- end
- if b ~= 0 then
- mon.setBackgroundColor(bg)
- term.setBackgroundColor(bg)
- end
- end
- mon.setCursorPos(x,y)
- mon.write(theString)
- term.setCursorPos(x,y)
- term.write(theString)
- else
- if advancedornot then
- if f ~= 0 then
- term.setTextColor(fg)
- end
- if b ~= 0 then
- term.setBackgroundColor(bg)
- end
- end
- term.setCursorPos(x,y)
- term.write(theString)
- end
- end
- function clearMe()
- if mon ~= "NONE" then
- if advancedornot then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- end
- mon.clear()
- mon.setCursorPos(1,1)
- term.clear()
- term.setCursorPos(1,1)
- else
- if advancedornot then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- function pscreen()
- if settings.isonline == "online" then
- mtprint(settings.isonline.." ",26,3,colors.green,colors.black)
- else
- mtprint(settings.isonline.." ",26,3,colors.red,colors.black)
- end
- mtprint(tostring(mSide[1]).." ",26,4,colors.cyan,colors.black)
- mtprint(serverIdent.." ",26,5,colors.cyan,colors.black)
- end
- function saveFile( file, data )
- local f = fs.open( file, "w" )
- f.write( data )
- f.close()
- end
- function readFile( file )
- if not fs.exists( file ) then return end
- local f = fs.open( file, "r" )
- local loadInfo = f.readAll()
- f.close()
- return loadInfo
- end
- function emodem()
- if off == 0 then
- settings.isonline = "offline"
- modem.close(1)
- end
- if off == 1 then
- settings.isonline = "online"
- modem.open(1)
- modem.transmit(1,1,serverIdent)
- end
- end
- function mDraw()
- mtprint(" ---------- ",3,7,colors.black,colors.brown)
- mtprint("| Q - QUIT |",3,8,colors.black,colors.brown)
- mtprint(" ---------- ",3,9,colors.black,colors.brown)
- mtprint(" ---------- ",16,7,colors.black,colors.brown)
- mtprint("|E - ENABLE|",16,8,colors.black,colors.brown)
- mtprint(" ---------- ",16,9,colors.black,colors.brown)
- mtprint("R - Reset Server Identity",3,11,colors.red,colors.black)
- end
- function transmissions()
- if trans == 0 then
- tr = 0
- mtprint("0",35,7,colors.gray,colors.gray)
- mtprint("0",35,9,colors.gray,colors.gray)
- end
- if trans == 1 then
- tr = 1
- timer = os.startTimer(0.1)
- mtprint("1",35,7,colors.green,colors.green)
- end
- if trans == 2 then
- tr = 1
- timer = os.startTimer(0.1)
- mtprint("1",35,9,colors.green,colors.green)
- end
- end
- function mainScreen()
- mtprint(mainName,10,1,colors.red,colors.black)
- mtprint("Server Status : ",5,3,colors.white,colors.black)
- mtprint("Modem Side : ",5,4,colors.white,colors.black)
- mtprint("Server Identity : ",5,5,colors.white,colors.black)
- mtprint("Tx : ",30,7,colors.white,colors.black)
- mtprint("Rx : ",30,9,colors.white,colors.black)
- end
- function makeHexString()
- hex = ""
- hexit = ""
- for i = 1, complexity do
- a = math.random(0, 15)
- if a == 10 then hex = "A" end
- if a == 11 then hex = "B" end
- if a == 12 then hex = "C" end
- if a == 13 then hex = "D" end
- if a == 14 then hex = "E" end
- if a == 15 then hex = "F" end
- if a <= 9 then
- hexit = hexit..tostring(a)
- else
- hexit = hexit..hex
- end
- end
- serverIdent = hexit
- end
- function isRandom()
- if not fs.exists("Ident") then
- makeHexString()
- saveFile("Ident", serverIdent)
- else
- serverIdent = readFile("Ident")
- end
- end
- isRandom()
- clearMe()
- mainScreen()
- emodem()
- pscreen()
- mDraw()
- transmissions()
- while true do
- if not initLock then setlock = setlock + 1 end
- if not fs.exists(currPath.."server.lck") and setlock >= 25 then
- saveFile("server.lck", "locked")
- initLock = true
- else
- if not initLock and setlock >= 25 then
- print("The Main TorNet server has a")
- print("running instance. Shut down")
- print("the other server first.")
- return
- end
- end
- if tr >= 1 then
- tr = tr - 1
- else
- if trans > 0 then
- tr = 0
- trans = 0
- transmissions()
- end
- end
- if theTime == 1 then
- timer = os.startTimer(0.1)
- if theSwitch >= 1 then theSwitch = theSwitch - 1 end
- if theSwitch <= 0 then
- theSwitch = 0
- theTime = 0
- end
- end
- if off > 2 then off = 0 end
- if theTime == 0 and theSwitch >= 1 then theSwitch = theSwitch - 1 end
- if theSwitch <= 0 then
- theSwitch = 0
- theTime = 0
- end
- local evt, arg, xPos, yPos, message = os.pullEvent()
- if evt == "timer" then
- if tr > 0 then timer = os.startTimer(0.1) end
- end
- if evt == "modem_message" then
- if settings.isonline == "online" then
- trans = 2
- transmissions()
- if message == serverIdent then
- trans = 1
- transmissions()
- modem.transmit(1,1,serverIdent.."isalreadyonline")
- end
- if message == serverIdent.."isalreadyonline" then
- off = 0
- emodem()
- pscreen()
- clearMe()
- print(" ")
- print("This server is")
- print("already running.")
- print(" ")
- return
- end
- end
- end
- if evt == "char" then
- local c = string.lower(arg)
- if c == "q" then
- if off == 0 then
- break
- else
- end
- end
- if c == "e" then
- if theSwitch == 0 then
- theTime = 1
- theSwitch = 1
- off = off + 1
- if off >= 2 then off = 0 end
- emodem()
- pscreen()
- end
- end
- if c == "r" then
- if off == 0 then
- if fs.exists("Ident") then
- fs.delete("Ident")
- isRandom()
- pscreen()
- else
- isRandom()
- pscreen()
- end
- end
- end
- end
- if evt == "mouse_click" or evt == "monitor_touch" then
- menu_options = 0
- setMonScale = 0
- if evt == "monitor_touch" then setMonScale = 1 end
- if xPos >= 3 + setMonScale and xPos <= 14 + setMonScale and yPos >= 7 and yPos <= 9 then
- menu_options = 1
- end
- if xPos >= 16 + (setMonScale / 2) and xPos <= 27 + setMonScale and yPos >= 7 and yPos <= 9 + (setMonScale / 2) then
- menu_options = 2
- end
- if menu_options == 1 and off == 0 then
- break
- else
- end
- if menu_options == 2 then
- off = off + 1
- if off >= 2 then off = 0 end
- emodem()
- pscreen()
- end
- end
- end
- if fs.exists(currPath.."server.lck") then fs.delete(currPath.."server.lck") end
- clearMe()
- print(" ")
- print("Program Terminated")
- print(" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement