Advertisement
JustDoesGames

PkDex

May 27th, 2020
1,698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.80 KB | None | 0 0
  1. -- PKDEX by JustDoesGames --
  2. local dir = fs.getDir(shell.getRunningProgram())
  3. local w,h = term.getSize()
  4. local version = "1.0.0."..fs.getSize(shell.getRunningProgram())
  5. if dir == "rom/programs/http" then
  6.     version = "PB.1.0.0"
  7. end
  8. local debug = false
  9.  
  10. local function longestTermInTable(tbl)
  11.     local lt = 0
  12.     for i=1, #tbl do
  13.         if type(tbl[i]) == "table" then
  14.             for ii=1, #tbl[i] do
  15.                 lt = math.max(string.len(tbl[i][ii]), lt)
  16.             end
  17.         else
  18.             lt = math.max(string.len(tbl[i]), lt)
  19.         end
  20.     end
  21.     return lt
  22. end
  23.  
  24. local type_colors = {
  25.     Normal = colors.white,
  26.     Fighting = colors.orange,
  27.     Flying = colors.lightBlue,
  28.     Poison = colors.magenta,
  29.     Ground = colors.brown,
  30.     Rock = colors.lightGray,
  31.     Bug = colors.orange,
  32.     Ghost = colors.purple,
  33.     Steel = colors.white,
  34.     Fire  = colors.red,
  35.     Water = colors.blue,
  36.     Grass = colors.green,
  37.     Electric = colors.yellow,
  38.     Psychic = colors.magenta,
  39.     Ice = colors.lightBlue,
  40.     Dragon = colors.magenta,
  41.     Dark = colors.gray,
  42.     Fairy = colors.pink,
  43. }
  44. pokedex = {}
  45.  
  46. currentRegion = ""
  47.  
  48. local boot = {{},{0,16384,16384,16384,16384,16384,},{0,16384,16384,16384,16384,16384,},{0,128,128,1,128,128,},{0,1,1,1,1,1,},{0,1,1,1,1,1,},}
  49.  
  50. function c(t) print("[console]"..t) end
  51. local loadingbar_current = 0
  52. function loading() term.setTextColor(colors.white) term.setCursorPos(8+loadingbar_current,5) write(string.char(127)) loadingbar_current = loadingbar_current + 1 end
  53.  
  54. function connectionFail(report)
  55.     term.setBackgroundColor(colors.black)
  56.     term.setTextColor(colors.white)
  57.     term.clear()
  58.     term.setCursorPos(1,1)
  59.     write("Failed to connect to servers: ") sleep(.5)
  60.     term.setTextColor(colors.orange)
  61.     print(report) print("") sleep(.5)
  62.     term.setTextColor(colors.white)
  63. end
  64.  
  65. function grabPkInfo(fl)
  66.     term.setBackgroundColor(colors.black)
  67.     if not http then return connectionFail("Http is disabled.") end
  68.     local all = http.get("https://pokemondb.net/pokedex/all") loading()
  69.     if all == nil then return connectionFail("Failed to connect to site.") end
  70.     local all_short = {}
  71.     repeat until all.readLine() == "<tbody>"
  72.     repeat
  73.         all_short[#all_short+1] = all.readLine()
  74.         if string.gsub(all_short[#all_short], " ","") == "<tr>" or string.gsub(all_short[#all_short], " ","") == "</tr>" then
  75.             all_short[#all_short] = nil --if cycle == 100 then cycle = 0 sleep(.001) else cycle = cycle + 1 end
  76.         end
  77.     until all_short[#all_short] == "</tbody>"
  78.     all_short[#all_short] = nil
  79.     all.close() loading()
  80.    
  81.     for i=1, #all_short do
  82.         for ii=1, #all_short[i]-1 do
  83.             if all_short[i] == "<tr>" or all_short[i] == "</tr>" then error("Website data was not writen sucessfully.") end
  84.         end
  85.     end
  86.    
  87.     if debug then
  88.         local loadingCurrent,loadingMax,loadingTrack = 0, math.ceil(#all_short/(w-1)),0
  89.         local _,prey = term.getCursorPos()
  90.         term.setCursorPos(1,prey) write("|")
  91.         term.setCursorPos(w,prey) write("|")
  92.         term.setCursorPos(2,prey)
  93.     end
  94.    
  95.     local tbl,cycle = {},1
  96.     local types = {"Normal", "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel", "Fire", "Water", "Grass", "Electric", "Psychic", "Ice", "Dragon", "Dark", "Fairy"}
  97.     for i=1, #all_short do
  98.         if string.find(string.gsub(all_short[i], "-",""), "cellnum cellfixed") ~= nil then
  99.             -- Name --
  100.             tbl[#tbl+1] = {}
  101.             local x,y = string.find(all_short[i], "View pokedex for #")
  102.             local tmp1, tmp2, y1 = "", "", y
  103.             repeat
  104.                 if tmp1 ~= ">" then tmp1 = string.sub(all_short[i], y, y) y1 = y1+1 end
  105.                 tmp2 = string.sub(all_short[i], y, y)
  106.                 y = y + 1
  107.             until tmp2 == "<" y = y - 2
  108.             tbl[#tbl][#tbl[#tbl]+1] = string.sub(all_short[i], y1, y)
  109.            
  110.             -- Type(s) --
  111.             tbl[#tbl][#tbl[#tbl]+1],tmp = {},1
  112.             for ii=1, #types do
  113.                 if string.find(all_short[i], string.lower(types[ii])) then
  114.                     tbl[#tbl][#tbl[#tbl]][tmp] = types[ii] tmp = tmp + 1
  115.                 end
  116.             end
  117.            
  118.         elseif string.find(string.gsub(all_short[i], "-",""), "cellnum") ~= nil then
  119.            
  120.             tbl[#tbl][#tbl[#tbl]+1] = string.sub(string.gsub(all_short[i]," ",""), 21,-6)
  121.            
  122.         elseif string.find(string.gsub(all_short[i], "-",""), "celltotal") ~= nil then
  123.            
  124.             tbl[#tbl][#tbl[#tbl]+1] = string.sub(string.gsub(all_short[i]," ",""), 23,-6)
  125.            
  126.         end
  127.        
  128.         if debug then if loadingTrack == loadingMax then loadingTrack = 0 write(".") else loadingTrack = loadingTrack + 1 end end
  129.         if cycle == math.ceil(#all_short/15) then loading() cycle = 1 else cycle = cycle + 1 end
  130.     end loading()
  131.    
  132.     local errorcontrol = false
  133.     for i=1, #tbl do
  134.         for ii=1, #tbl[i]-1 do
  135.             if type(tbl[i][ii+1]) ~= "table" then
  136.                 if string.find(tbl[i][ii+1], ">") ~= nil or string.find(tbl[i][ii+1], "<") ~= nil then c("ERROR:: Pk #"..i.." | Name: "..tbl[i][1].." | got "..tbl[i][ii+1].." on id "..ii) errorcontrol = true end
  137.             end
  138.         end
  139.     end
  140.     if errorcontrol then sleep(1) c("Press any key to confirm errors...") os.pullEvent("key") sleep(.5) c("Confirmed.") end
  141.    
  142.     local tmp = {}
  143.     for i=1, #tbl do
  144.         if tbl[i] ~= nil then
  145.             -- Search entire list for pokemon --
  146.             tmp = {}
  147.             for ii=1, #tbl do
  148.                 if tbl[i][1] == tbl[ii][1] then tmp[#tmp+1] = ii end
  149.             end
  150.             if #tmp > 1 then
  151.                 if tbl[i][1] == "Nidoran?" then
  152.                     tbl[tmp[1]][1] = "Nidoran"..string.char(12)
  153.                     tbl[tmp[2]][1] = "Nidoran"..string.char(11)
  154.                 else
  155.                     for i=1, #tmp-1 do
  156.                         table.remove(tbl, tmp[#tmp-i+1])
  157.                     end
  158.                 end
  159.             end
  160.         end
  161.     end loading() sleep(.0001)
  162.    
  163.     for i=1, #tbl do
  164.         local tmp = {}
  165.         tmp.name = tbl[i][1]
  166.         tmp.types = tbl[i][2]
  167.         tmp.total = tbl[i][3]
  168.         tmp.hp = tbl[i][4]
  169.         tmp.attack = tbl[i][5]
  170.         tmp.defence = tbl[i][6]
  171.         tmp.spAtk = tbl[i][7]
  172.         tmp.spDef = tbl[i][8]
  173.         tmp.speed = tbl[i][9]
  174.         tbl[i] = tmp
  175.     end
  176.     --[[
  177.     local inceptions = {
  178.         "Castform",
  179.         "Deoxys",
  180.     }
  181.     for i=1, #inceptions do
  182.         local t1,t2 = lookup(inceptions[i])
  183.         if t1 then
  184.             table.remove(tbl, t2)
  185.         end
  186.     end
  187.     --]]
  188.     return tbl
  189. end
  190.  
  191. function loadData(fl)
  192.     local data, st, tbl = {}, {}, {}
  193.     --[[
  194.     local file = fs.open(fl, "r")
  195.     local t = 1
  196.     while true do
  197.         data[t] = file.readLine()
  198.         if data[t] == nil then break end
  199.         t = t + 1
  200.     end
  201.     file.close()
  202.     --]]
  203.     data = fl error(textutils.serialize(data))
  204.     local t = ""
  205.     for i=1, string.len(data[1]) do
  206.         if string.sub(data[1], i,i) == " " then
  207.             st[#st+1] = t
  208.             t = ""
  209.         else
  210.             t = t..string.sub(data[1], i,i)
  211.         end
  212.     end
  213.     table.remove(data, 1)
  214.    
  215.     for i=1, #data do
  216.         t,q = {},""
  217.         for ii=1, string.len(data[i]) do
  218.             if string.sub(data[i], ii,ii) == " " then
  219.                 if tostring(tonumber(q)) ~= "nil" then
  220.                     t[#t+1] = tonumber(q)
  221.                 else
  222.                     t[#t+1] = q
  223.                 end
  224.                 q = ""
  225.             else
  226.                 q = q..string.sub(data[i], ii,ii)
  227.             end
  228.         end
  229.         tbl[#tbl+1] = t
  230.     end
  231.     return tbl
  232. end
  233.  
  234. function animatePokeball()
  235.     for i=1, math.ceil(h/2)-1 do
  236.         paintutils.drawLine(1,i+1,w,i+1,colors.red)
  237.         paintutils.drawLine(1,h-i,w,h-i,colors.white)
  238.         sleep(.01)
  239.     end
  240.     sleep(.1)
  241.     paintutils.drawLine(1,math.ceil(h/2),w,math.ceil(h/2),colors.black)
  242.     paintutils.drawLine(1,math.floor(h/2),w,math.floor(h/2),colors.black)
  243.     for i=1, math.ceil(h/2)-2 do
  244.         paintutils.drawLine(1,math.ceil(h/2)-i,w,math.ceil(h/2)-i,colors.black)
  245.         paintutils.drawLine(1,math.ceil(h/2)+i,w,math.ceil(h/2)+i,colors.black)
  246.         sleep(.01)
  247.     end
  248. end
  249.  
  250. function loadPokemonIndex(sel)
  251.     pk = {}
  252.     pk[#pk+1] = "Pokemon #"..sel..": "..pokedex[sel].name
  253.     pk[#pk+1] = ""
  254.     pk[#pk+1] = "Type:"
  255.     for i=1, #pokedex[sel].types do
  256.         pk[#pk+1] = pokedex[sel].types[i]
  257.     end
  258.     pk[#pk+1] = ""
  259.     pk[#pk+1] = "HP - "..pokedex[sel].hp
  260.     pk[#pk+1] = "Attack - "..pokedex[sel].attack
  261.     pk[#pk+1] = "Defence - "..pokedex[sel].defence
  262.     pk[#pk+1] = "Sp. Atk - "..pokedex[sel].spDef
  263.     pk[#pk+1] = "Sp. Def - "..pokedex[sel].spAtk
  264.     pk[#pk+1] = "Speed - "..pokedex[sel].speed
  265.     pk[#pk+1] = ""
  266.     pk[#pk+1] = "Total - "..pokedex[sel].total
  267.    
  268.     animatePokeball()
  269.    
  270.     term.setCursorPos(1,2)
  271.     for i=1, #pk do term.setTextColor(type_colors[pk[i]] or colors.white) print(pk[i]) end
  272.     --print("lol")
  273.     os.pullEvent("key")
  274.     animatePokeball()
  275. end
  276.  
  277. function pe(...)
  278.     local events = {...}
  279.     while true do
  280.         a,b,x,y = os.pullEvent()
  281.         for i=1, #events do
  282.             if a == events[i] then return a,b,x,y end
  283.         end
  284.     end
  285. end
  286.  
  287. function controller(direction, selected, scroll, maxscroll)
  288.     if direction == "up" or direction == -1 then
  289.         if selected == 1 then
  290.             selected = #pokedex scroll = maxscroll
  291.         elseif selected <= 3+scroll and scroll ~= 0 then
  292.             selected = selected - 1
  293.             scroll = scroll - 1
  294.         else
  295.             selected = selected - 1
  296.         end
  297.     elseif direction == "down" or direction == 1 then
  298.         if selected == #pokedex then
  299.             selected = 1 scroll = 0
  300.         elseif selected >= h-4+scroll and scroll ~= maxscroll then
  301.             selected = selected + 1
  302.             scroll = scroll + 1
  303.         else
  304.             selected = selected + 1
  305.         end
  306.     end
  307.     return selected, scroll, maxscroll
  308. end
  309.  
  310.  
  311.  
  312.  
  313. function runPokedex()
  314.     local run, updat, selected, scroll, maxscroll = true, true, 1, 0, #pokedex-(h-2)
  315.     local function display()
  316.         if updat then
  317.             term.setBackgroundColor(colors.black)
  318.             term.clear()
  319.             paintutils.drawLine(1,1,w,1,colors.red)
  320.             paintutils.drawLine(1,h,w,h,colors.white)
  321.             --paintutils.drawLine(w-2,h,w,h,colors.gray)
  322.             term.setTextColor(colors.white) term.setBackgroundColor(colors.red)
  323.             term.setCursorPos(w-string.len("v."..version),1) write("v."..version)
  324.             term.setTextColor(colors.black) term.setBackgroundColor(colors.white)
  325.             term.setCursorPos(1,h) write("Press 'q' to exit.")
  326.             term.setTextColor(colors.white)
  327.             updat = false
  328.         end
  329.         for i=1, h-2 do
  330.             term.setCursorPos(1,i+1)
  331.             --if selected == i+scroll then term.setBackgroundColor(colors.lightGray) else term.setBackgroundColor(colors.white) end
  332.             if selected == i+scroll then term.setBackgroundColor(colors.gray) term.setTextColor(type_colors[pokedex[selected].types[1]] or colors.white) write("> ") else term.setBackgroundColor(colors.black) term.setTextColor(colors.gray) write("  ") end
  333.             write(string.sub("000", 1,3-string.len(i+scroll))..i+scroll.." ")
  334.             for i=1, 4-string.len(i+scroll) do write("") end
  335.             write(pokedex[i+scroll].name)
  336.             for i=1, w-string.len("      "..pokedex[i+scroll].name) do
  337.                 write(" ")
  338.             end
  339.         end
  340.     end
  341.    
  342.     local function search()
  343.         local r,text = true,""
  344.         term.setBackgroundColor(colors.gray)
  345.         term.setTextColor(colors.white)
  346.         while r do
  347.             a,i = os.pullEvent("char")
  348.             if type(tonumber(i)) == "number" then
  349.                 text = text..tostring(i)
  350.                 if string.len(text) == 3 then sleep(.5)
  351.                     text = math.min(math.max(tonumber(text),0),#pokedex)
  352.                    
  353.                     if selected > text then
  354.                         for i=1, selected-text do
  355.                             selected, scroll, maxscroll = controller("down", selected, scroll, maxscroll)
  356.                         end
  357.                     else
  358.                         for i=1, math.abs(selected-text) do
  359.                             selected, scroll, maxscroll = controller("up", selected, scroll, maxscroll)
  360.                         end
  361.                     end
  362.                     break
  363.                 end
  364.             end
  365.             term.setCursorPos((w-2),h) write(text)
  366.         end
  367.     end
  368.    
  369.     while run do
  370.         display()
  371.         --sleep(.001)
  372.         a,b,x,y = pe("key", "mouse_click", "mouse_drag", "mouse_scroll")
  373.         if a == "key" then
  374.             if b == keys.w or b == keys.up then
  375.                 --if selected == 1 then selected = #boot else selected = selected - 1 end
  376.                 selected, scroll, maxscroll = controller("up", selected, scroll, maxscroll)
  377.             elseif b == keys.s or b == keys.down then
  378.                 --if selected == #boot then selected = 1 else selected = selected + 1 end
  379.                 selected, scroll, maxscroll = controller("down", selected, scroll, maxscroll)
  380.             elseif b == keys.enter or b == keys.e then
  381.                 loadPokemonIndex(selected) updat = true
  382.             elseif b == keys.q then
  383.                 run = false
  384.             end
  385.         elseif a == "mouse_click" or a == "mouse_drag" then
  386.             if y > 1 and y < h then
  387.                 if (y-1)+scroll == selected and a == "mouse_click" then loadPokemonIndex(selected) update = true end
  388.                 selected = math.min(y-1+scroll, #pokedex)
  389.             elseif x >= w-3 and y == h then
  390.                 --search() updat = true
  391.             end
  392.         elseif a == "mouse_scroll" then
  393.             selected, scroll, maxscroll = controller(b, selected, scroll, maxscroll)
  394.         end
  395.     end
  396. end
  397.  
  398.  
  399. term.clear()
  400. paintutils.drawImage(boot, 1,1) term.setBackgroundColor(colors.black)
  401. term.setCursorPos(8,2) print("PokeDex v."..version)
  402. term.setCursorPos(8,3) print("Just Does Games")
  403. term.setBackgroundColor(colors.gray)
  404. term.setCursorPos(8,5) write("                  ")
  405. sleep(.01) print("")
  406.  
  407. local fl = dir.."/p.lua"
  408. pokedex = grabPkInfo() --<<< FOR API USE HERE (Copy the function) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  409. term.setBackgroundColor(colors.black)
  410. --[[
  411. for i=1, #pokedex do
  412.     print("#"..i.." "..pokedex[i][1]) sleep(1)
  413. end
  414. --]]
  415. if debug then
  416.     local sel = 1
  417.     while true do
  418.         term.clear()
  419.         term.setCursorPos(1,1)
  420.         print("Pkid: "..sel.." | name: "..pokedex[sel].name)
  421.         print("")
  422.         write("Types: ") for i=1, #pokedex[sel].types do write(pokedex[sel].types[i].." ") end print("")
  423.         print("Total: "..pokedex[sel].total)
  424.         print("HP: "..pokedex[sel].hp)
  425.         print("Attack: "..pokedex[sel].attack)
  426.         print("Defence: "..pokedex[sel].defence)
  427.         print("Sp. Atk: "..pokedex[sel].spAtk)
  428.         print("Sp. Def: "..pokedex[sel].spDef)
  429.         print("Speed: "..pokedex[sel].speed) print("")
  430.        
  431.         a,b = os.pullEvent("key")
  432.         if b == keys.up then sel = math.max(sel - 1,1) elseif b == keys.down then sel = math.min(sel + 1, #pokedex) end
  433.     end
  434. end
  435. sleep(.3)
  436. if pokedex == nil then
  437.     print("Press any key to exit...")
  438.     os.pullEvent("key")
  439. else
  440.     parallel.waitForAny(runPokedex)
  441.     term.setTextColor(colors.white) term.setBackgroundColor(colors.black) term.clear() term.setCursorPos(1,1)
  442. end
  443. --pokedex = loadData(gb)
  444. --error("Complete!")
  445. sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement