LegoStax

CCWiki Browser

Nov 24th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.50 KB | None | 0 0
  1. -- CCWiki Database Browser
  2. local running = true
  3. local PCODE = "zzqM8062"
  4. local BUFFER = {}
  5. local PDATA = { -- this is the default database, more data will be added to the Pastebin as needed. Nothing more will be added here.
  6.     [1] = {
  7.         n = "computer",
  8.         d = "Computer^^Recipe:^S = stone, R = redstone, G = glass pane^=======^|S|S|S|^=======^|S|R|S|^=======^|S|G|S|^=======",
  9.     },
  10.     [2] = {
  11.         n = "advanced computer",
  12.         d = "Advanced Computer^^Recipe:^G = gold ingot, R = redstone, P = glass pane^=======^|G|G|G|^=======^|G|R|G|^=======^|G|P|G|^=======",
  13.     },
  14.     [3] = {
  15.         n = "wired modem",
  16.         d = "Wired Modem^^Recipe:^S = stone, R = redstone^=======^|S|S|S|^=======^|S|R|S|^=======^|S|S|S|^=======",
  17.     },
  18.     [4] = {
  19.         n = "networking cable",
  20.         d = "Networking Cable^^Recipe:^S = stone, R = redstone^=======^| |S| |^=======^|S|R|S|^=======^| |S| |^=======",
  21.     },
  22.     [5] = {
  23.         n = "wireless modem",
  24.         d = "Wireless Modem^^Recipe:^S = stone, E = ender pearl^=======^|S|S|S|^=======^|S|E|S|^=======^|S|S|S|^=======",
  25.     },
  26.     [6] = {
  27.         n = "disk drive",
  28.         d = "Disk Drive^^Recipe:^S = stone, R = redstone^=======^|S|S|S|^=======^|S|R|S|^=======^|S|R|S|^=======",
  29.     },
  30.     [7] = {
  31.         n = "floppy disk",
  32.         d = "Floppy Disk^^Recipe:^R = redstone, P = paper^=======^| |R| |^=======^| |P| |^=======^| | | |^=======",
  33.     },
  34.     [8] = {
  35.         n = "turtle",
  36.         d = "Turtle^^Recipe:^I = iron ingot, C = computer, H = chest^=======^|I|I|I|^=======^|I|C|I|^=======^|I|H|I|^=======",
  37.     },
  38.     [9] = {
  39.         n = "monitor",
  40.         d = "Monitor^^Recipe:^S = stone, G = glass pane^=======^|S|S|S|^=======^|S|G|S|^=======^|S|S|S|^=======",
  41.     },
  42.     [10] = {
  43.         n = "printer",
  44.         d = "Printer^^Recipe:^S = stone, R = redstone, I = ink sac^=======^|S|S|S|^=======^|S|R|S|^=======^|S|I|S|^=======",
  45.     },
  46.     [11] = {
  47.         n = "advanced monitor",
  48.         d = "Advanced Monitor^^Recipe:^G = gold ingot, P = glass pane^=======^|G|G|G|^=======^|G|P|G|^=======^|G|G|G|^=======",
  49.     },
  50.     [12] = {
  51.         n = "advanced turtle",
  52.         d = "Advanced Turtle^^Recipe:^G = gold ingot, A = advanced computer, C = chest^=======^|G|G|G|^=======^|G|A|G|^=======^|G|C|G|^=======",
  53.     },
  54.     [13] = {
  55.         n = "pocket computer",
  56.         d = "Pocket Computer^^Recipe:^S = stone, G = golden apple, P = glass pane^=======^|S|S|S|^=======^|S|G|S|^=======^|S|P|S|^=======",
  57.     },
  58.     [14] = {
  59.         n = "advanced pocket computer",
  60.         d = "Advanced Pocket Computer^^Recipe:^I = gold ingot, G = golden apple, P = glass pane^=======^|I|I|I|^=======^|I|G|I|^=======^|I|P|I|^=======",
  61.     },
  62.     [15] = {
  63.         n = "wireless pocket computer",
  64.         d = "Wireless Pocket Computer^^Recipe:^W = wireless modem, P = pocket computer^=======^| | | |^=======^| |W| |^=======^| |P| |^=======",
  65.     },
  66.     [16] = {
  67.         n = "golden apple",
  68.         d = "Golden Apple^^Recipe:^G = block of gold, A = apple^=======^|G|G|G|^=======^|G|A|G|^=======^|G|G|G|^=======^^",
  69.     },
  70. }
  71. local w,h = term.getSize()
  72. local choice = 1
  73. local searchmode = false
  74. local curresults = ""
  75. local input = ""
  76. local xread = 2
  77. local scrollpos = 1
  78. local pospossible = 1
  79.  
  80. if not term.isColor() or not term.isColour() then
  81.     running = false
  82.     print("Colorful display required")
  83. end
  84.  
  85. local function printPos(msg,x,y,bg,fg)
  86.     if bg then term.setBackgroundColor(bg) end
  87.     if fg then term.setTextColor(fg) end
  88.     term.setCursorPos(x,y)
  89.     term.write(msg)
  90. end
  91.  
  92. function download(url,fn)
  93.     if not http then
  94.         os.queueEvent("ccwiki_error","HTTP API disabled. Please re-enable it")
  95.     else
  96.         local response = http.get(url)
  97.         if response then
  98.             local data = response.readAll()
  99.             response.close()
  100.             local f = fs.open(fn, "w")
  101.             f.write(data)
  102.             f.close()
  103.         else
  104.             os.queueEvent("ccwiki_error","No response from Pastebin servers.")
  105.         end
  106.     end
  107. end
  108.  
  109. local function clearViewport(c)
  110.     term.setBackgroundColor(c)
  111.     for y = 2,h-1 do
  112.         term.setCursorPos(1,y)
  113.         term.clearLine()
  114.     end
  115. end
  116.  
  117. local function calcScroll()
  118.     scrollpos = 1
  119.     pospossible = #BUFFER-(h-3)
  120.     if pospossible < 1 then pospossible = 1 end
  121. end
  122.  
  123. local function drawHeader()
  124.     printPos(" CC Wiki",1,1,colors.gray,colors.white)
  125.     for x = 9,w-1 do printPos(" ",x,1) end
  126.     printPos("X",w,1,colors.red,colors.white)
  127. end
  128.  
  129. local function drawFooter()
  130.     printPos(" Search",1,h,colors.gray,colors.lightGray)
  131.     for x = 8,w do printPos(" ",x,h) end
  132. end
  133.  
  134. local function drawFocus()
  135.     clearViewport(colors.white)
  136.     if xread > 2 then
  137.         -- Draw selected choice bar
  138.         term.setBackgroundColor(colors.lightGray)
  139.         term.setCursorPos(1,choice+1)
  140.         term.clearLine()
  141.     end
  142.     term.setTextColor(colors.black)
  143.     term.setCursorPos(1,2)
  144.     for i = scrollpos,#BUFFER do
  145.         if i > #BUFFER then break end
  146.         term.write(BUFFER[i])
  147.         local x,y = term.getCursorPos()
  148.         if y+1 > h-1 then break end
  149.         term.setCursorPos(1,y+1)
  150.     end
  151. end
  152.  
  153. local function drawScreen()
  154.     term.setBackgroundColor(colors.white)
  155.     term.clear()
  156.     drawHeader()
  157.     drawFooter()
  158.     drawFocus()
  159. end
  160.  
  161. local function writeBuffer(data)
  162.     BUFFER = {}
  163.     local add = ""
  164.     for i = 1,data:len() do
  165.         if string.sub(data,i,i) == "^" then
  166.             table.insert(BUFFER,add)
  167.             add = ""
  168.         else
  169.             add = add .. string.sub(data,i,i)
  170.         end
  171.         if add:len() == w or i == data:len() then
  172.             table.insert(BUFFER,add)
  173.             add = ""
  174.         end
  175.     end
  176.     calcScroll()
  177.     drawFocus()
  178. end
  179.  
  180. local function writeError(e)
  181.     printPos(e,2,3,colors.white,colors.red)
  182. end
  183.  
  184. local function searchDatabase(data)
  185.     local list = ""
  186.     for i = 1,#PDATA do
  187.         if data == string.sub(PDATA[i].n, 1, data:len()) then
  188.             list = list .. PDATA[i].n .. "^"
  189.         end
  190.     end
  191.     return list
  192. end
  193.  
  194. local function displayResult(keyword)
  195.     for i = 1,#PDATA do
  196.         if PDATA[i].n == keyword then
  197.             writeBuffer(PDATA[i].d)
  198.         end
  199.     end
  200. end
  201.  
  202. local function indexDatabase()
  203.     local b = ""
  204.     for i = 1,#PDATA do
  205.         b = b .. PDATA[i].n .. "^"
  206.     end
  207.     writeBuffer(b)
  208.     while true do
  209.         local e = {os.pullEvent()}
  210.         if e[1] == "mouse_click" and e[2] == 1 then
  211.             if e[3] == 51 and e[4] == 1 then
  212.                 running = false
  213.                 break
  214.             elseif e[4] == 1 then
  215.                 writeBuffer("^ Start typing to search^^ Click the title bar to view the index")
  216.                 break
  217.             else
  218.                 displayResult(BUFFER[e[4]-1])
  219.                 break
  220.             end
  221.         end
  222.     end
  223. end
  224.  
  225. -- Initialize
  226. if fs.exists(".ccwiki/latest") then -- overwrite backup
  227.     local f = fs.open(".ccwiki/latest", "r")
  228.     local d = f.readAll()
  229.     f.close()
  230.     local f = fs.open(".ccwiki/backup", "w")
  231.     f.write(d)
  232.     f.close()
  233. end
  234. download("http://pastebin.com/raw.php?i="..PCODE, ".ccwiki/latest") -- download database file
  235. local f = fs.open(".ccwiki/latest", "r")
  236. PDATA = textutils.unserialize(f.readAll()) -- load database file
  237. f.close()
  238.  
  239. local function main()
  240.     writeBuffer("^ Start typing to search^^ Click the title bar to view the index")
  241.     drawScreen()
  242.     while running do
  243.         local e = {os.pullEvent()}
  244.         if e[1] == "mouse_click" and e[2] == 1 then
  245.             if e[3] == w and e[4] == 1 then
  246.                 running = false
  247.                 break
  248.             elseif e[4] == 1 then
  249.                 indexDatabase()
  250.             end
  251.         elseif e[1] == "mouse_scroll" then
  252.             if e[2] == 1 and scrollpos < pospossible then
  253.                 scrollpos = scrollpos+1
  254.                 drawFocus()
  255.             elseif e[2] == -1 and scrollpos > 1 then
  256.                 scrollpos = scrollpos-1
  257.                 drawFocus()
  258.             end
  259.         elseif e[1] == "ccwiki_error" then
  260.             writeError(e[2])
  261.         elseif e[1] == "char" then
  262.             if input == "" then
  263.                 term.setBackgroundColor(colors.gray)
  264.                 term.setCursorPos(1,h)
  265.                 term.clearLine()
  266.             end
  267.             input = input .. e[2]
  268.             printPos(e[2],xread,h,colors.gray,colors.lightGray)
  269.             xread = xread+1
  270.             -- search with current input and display results
  271.             curresults = searchDatabase(input)
  272.             writeBuffer(curresults)
  273.         elseif e[1] == "key" then
  274.             if e[2] == keys.enter then
  275.                 -- select current option
  276.                 input = ""
  277.                 xread = 2
  278.                 drawFooter()
  279.                 -- display searched wiki article
  280.                 displayResult(BUFFER[choice])
  281.                 choice = 1
  282.             elseif e[2] == keys.backspace and xread > 2 then
  283.                 curresults = searchDatabase(input)
  284.                 writeBuffer(curresults)
  285.                 input = string.sub(input,1,input:len()-1)
  286.                 xread = xread-1
  287.                 term.setBackgroundColor(colors.gray)
  288.                 term.setCursorPos(xread,h)
  289.                 term.write("  ")
  290.                 term.setCursorPos(xread,h)
  291.                 if xread == 2 then
  292.                     choice = 1
  293.                     writeBuffer("^ Start typing to search^^ Click the title bar to view the index")
  294.                     drawFooter()
  295.                 end
  296.             elseif e[2] == keys.up and choice > 1 then
  297.                 choice = choice-1
  298.                 writeBuffer(curresults)
  299.             elseif e[2] == keys.down and choice < #BUFFER-1 then
  300.                 choice = choice+1
  301.                 writeBuffer(curresults)
  302.             end
  303.         elseif e[1] == "term_resize" then
  304.             drawScreen()
  305.         end
  306.     end
  307. end
  308. main()
  309. term.setBackgroundColor(colors.black)
  310. term.clear()
  311. term.setCursorPos(1,1)
  312. coroutine.yield()
Advertisement
Add Comment
Please, Sign In to add comment