Advertisement
Alakazard12

Grief Report Server

Dec 15th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. local sides = {"top", "bottom", "left", "right", "front", "back"}
  2. for i,v in pairs(sides) do
  3.     rednet.open(v)
  4. end
  5.  
  6. local rf = "/files"
  7. local clients = {}
  8.  
  9. local stx, sty = term.getSize()
  10.  
  11. function clear()
  12.     term.clear()
  13.     term.setCursorPos(1, 1)
  14. end
  15.  
  16. function drawlist(title, listr, num)
  17.     clear()
  18.     term.setTextColor(colors.lime)
  19.     local list = {}
  20.     print(title.."\n")
  21.     for i,v in pairs(listr) do
  22.         list[i] = v
  23.     end
  24.     local start = 1
  25.     local last = #list
  26.     if num > sty - 4 then
  27.         start = num - (sty - 4)
  28.     end
  29.     local cnt = 0
  30.     for i = start, #list do
  31.         if cnt > sty - 4 then
  32.             last = last - 1
  33.             cnt = cnt - 1
  34.         end
  35.         cnt = cnt + 1
  36.     end
  37.     for m = start, last do
  38.         local tor = true
  39.         if tor == true then
  40.             if m == num then
  41.                 term.setTextColor(colors.white)
  42.                 write("[")
  43.                 term.setTextColor(colors.lime)
  44.                 write("-")
  45.                 term.setTextColor(colors.white)
  46.                 write("]")
  47.                 term.setTextColor(colors.lime)
  48.                 print(" "..list[m])
  49.             else
  50.                 term.setTextColor(colors.white)
  51.                 print("[ ] "..list[m])
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. function getKey()
  58.     local rt = nil
  59.     function lop()
  60.         local e, a1 = os.pullEvent("key")
  61.         if a1 == 200 then
  62.             rt = "up"
  63.         elseif a1 == 208 then
  64.             rt = "down"
  65.         elseif a1 == 28 then
  66.             rt = "enter"
  67.         else
  68.             lop()
  69.         end
  70.     end
  71.     lop()
  72.     repeat sleep(0) until rt ~= nil
  73.     return rt
  74. end
  75.  
  76. function ch(title, list)
  77.     clear()
  78.     local ind = 1
  79.     local lcopy = list
  80.     drawlist(title, lcopy, ind)
  81.     local rt = nil
  82.     cycle = function()
  83.         local ke = getKey()
  84.         if ke == "up" then
  85.             if ind > 1 then
  86.                 ind = ind - 1
  87.             else
  88.                 ind = #list
  89.             end
  90.             drawlist(title, lcopy, ind)
  91.             cycle()
  92.         elseif ke == "down" then
  93.             if ind < #list then
  94.                 ind = ind + 1
  95.             else
  96.                 ind = 1
  97.             end
  98.             drawlist(title, lcopy, ind)
  99.             cycle()
  100.         elseif ke == "enter" then
  101.             rt = ind
  102.         end
  103.     end
  104.     cycle()
  105.     repeat sleep(0) until rt ~= nil
  106.     return ind
  107. end
  108.  
  109. if not fs.exists(rf) then
  110.     fs.makeDir(rf)
  111. end
  112.  
  113. local function a1()
  114.     while true do
  115.         local tr = ch("Grief reports", {"View reports", "Delete reports"})
  116.         if tr == 1 then
  117.             while true do
  118.                 local lst = fs.list(rf)
  119.                 table.insert(lst, 1, "Back")
  120.                 local yu = ch("Choose a report to view", lst)
  121.                 if yu ~= 1 then
  122.                     shell.run("edit", rf.."/"..lst[yu])
  123.                 else
  124.                     break
  125.                 end
  126.             end
  127.         elseif tr == 2 then
  128.             while true do
  129.                 local lst = fs.list(rf)
  130.                 table.insert(lst, 1, "Back")
  131.                 local yu = ch("Choose a report to delete", lst)
  132.                 if yu ~= 1 then
  133.                     local conf = ch("Are you sure you want to delete this report?", {"Yes", "No"})
  134.                     if conf == 1 then
  135.                         fs.delete(rf.."/"..lst[yu])
  136.                     end
  137.                 else
  138.                     break
  139.                 end
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. local function a2()
  146.     while true do
  147.         local id, msg = rednet.receive()
  148.         local isi = false
  149.         for i,v in pairs(clients) do
  150.             if v == id then
  151.                 isi = true
  152.             end
  153.         end
  154.         if isi == true then
  155.             local ty = fs.open(rf.."/"..math.random(1, 10000)+math.random(1, 1000)-math.random(1, 100), "w")
  156.             ty.write(msg)
  157.             ty.close()
  158.         end
  159.     end
  160. end
  161.  
  162. parallel.waitForAny(a1, a2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement