Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PKDEX by JustDoesGames --
- local dir = fs.getDir(shell.getRunningProgram())
- local w,h = term.getSize()
- local version = "1.0.0."..fs.getSize(shell.getRunningProgram())
- if dir == "rom/programs/http" then
- version = "PB.1.0.0"
- end
- local debug = false
- local function longestTermInTable(tbl)
- local lt = 0
- for i=1, #tbl do
- if type(tbl[i]) == "table" then
- for ii=1, #tbl[i] do
- lt = math.max(string.len(tbl[i][ii]), lt)
- end
- else
- lt = math.max(string.len(tbl[i]), lt)
- end
- end
- return lt
- end
- local type_colors = {
- Normal = colors.white,
- Fighting = colors.orange,
- Flying = colors.lightBlue,
- Poison = colors.magenta,
- Ground = colors.brown,
- Rock = colors.lightGray,
- Bug = colors.orange,
- Ghost = colors.purple,
- Steel = colors.white,
- Fire = colors.red,
- Water = colors.blue,
- Grass = colors.green,
- Electric = colors.yellow,
- Psychic = colors.magenta,
- Ice = colors.lightBlue,
- Dragon = colors.magenta,
- Dark = colors.gray,
- Fairy = colors.pink,
- }
- pokedex = {}
- currentRegion = ""
- 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,},}
- function c(t) print("[console]"..t) end
- local loadingbar_current = 0
- function loading() term.setTextColor(colors.white) term.setCursorPos(8+loadingbar_current,5) write(string.char(127)) loadingbar_current = loadingbar_current + 1 end
- function connectionFail(report)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- write("Failed to connect to servers: ") sleep(.5)
- term.setTextColor(colors.orange)
- print(report) print("") sleep(.5)
- term.setTextColor(colors.white)
- end
- function grabPkInfo(fl)
- term.setBackgroundColor(colors.black)
- if not http then return connectionFail("Http is disabled.") end
- local all = http.get("https://pokemondb.net/pokedex/all") loading()
- if all == nil then return connectionFail("Failed to connect to site.") end
- local all_short = {}
- repeat until all.readLine() == "<tbody>"
- repeat
- all_short[#all_short+1] = all.readLine()
- if string.gsub(all_short[#all_short], " ","") == "<tr>" or string.gsub(all_short[#all_short], " ","") == "</tr>" then
- all_short[#all_short] = nil --if cycle == 100 then cycle = 0 sleep(.001) else cycle = cycle + 1 end
- end
- until all_short[#all_short] == "</tbody>"
- all_short[#all_short] = nil
- all.close() loading()
- for i=1, #all_short do
- for ii=1, #all_short[i]-1 do
- if all_short[i] == "<tr>" or all_short[i] == "</tr>" then error("Website data was not writen sucessfully.") end
- end
- end
- if debug then
- local loadingCurrent,loadingMax,loadingTrack = 0, math.ceil(#all_short/(w-1)),0
- local _,prey = term.getCursorPos()
- term.setCursorPos(1,prey) write("|")
- term.setCursorPos(w,prey) write("|")
- term.setCursorPos(2,prey)
- end
- local tbl,cycle = {},1
- local types = {"Normal", "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel", "Fire", "Water", "Grass", "Electric", "Psychic", "Ice", "Dragon", "Dark", "Fairy"}
- for i=1, #all_short do
- if string.find(string.gsub(all_short[i], "-",""), "cellnum cellfixed") ~= nil then
- -- Name --
- tbl[#tbl+1] = {}
- local x,y = string.find(all_short[i], "View pokedex for #")
- local tmp1, tmp2, y1 = "", "", y
- repeat
- if tmp1 ~= ">" then tmp1 = string.sub(all_short[i], y, y) y1 = y1+1 end
- tmp2 = string.sub(all_short[i], y, y)
- y = y + 1
- until tmp2 == "<" y = y - 2
- tbl[#tbl][#tbl[#tbl]+1] = string.sub(all_short[i], y1, y)
- -- Type(s) --
- tbl[#tbl][#tbl[#tbl]+1],tmp = {},1
- for ii=1, #types do
- if string.find(all_short[i], string.lower(types[ii])) then
- tbl[#tbl][#tbl[#tbl]][tmp] = types[ii] tmp = tmp + 1
- end
- end
- elseif string.find(string.gsub(all_short[i], "-",""), "cellnum") ~= nil then
- tbl[#tbl][#tbl[#tbl]+1] = string.sub(string.gsub(all_short[i]," ",""), 21,-6)
- elseif string.find(string.gsub(all_short[i], "-",""), "celltotal") ~= nil then
- tbl[#tbl][#tbl[#tbl]+1] = string.sub(string.gsub(all_short[i]," ",""), 23,-6)
- end
- if debug then if loadingTrack == loadingMax then loadingTrack = 0 write(".") else loadingTrack = loadingTrack + 1 end end
- if cycle == math.ceil(#all_short/15) then loading() cycle = 1 else cycle = cycle + 1 end
- end loading()
- local errorcontrol = false
- for i=1, #tbl do
- for ii=1, #tbl[i]-1 do
- if type(tbl[i][ii+1]) ~= "table" then
- 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
- end
- end
- end
- if errorcontrol then sleep(1) c("Press any key to confirm errors...") os.pullEvent("key") sleep(.5) c("Confirmed.") end
- local tmp = {}
- for i=1, #tbl do
- if tbl[i] ~= nil then
- -- Search entire list for pokemon --
- tmp = {}
- for ii=1, #tbl do
- if tbl[i][1] == tbl[ii][1] then tmp[#tmp+1] = ii end
- end
- if #tmp > 1 then
- if tbl[i][1] == "Nidoran?" then
- tbl[tmp[1]][1] = "Nidoran"..string.char(12)
- tbl[tmp[2]][1] = "Nidoran"..string.char(11)
- else
- for i=1, #tmp-1 do
- table.remove(tbl, tmp[#tmp-i+1])
- end
- end
- end
- end
- end loading() sleep(.0001)
- for i=1, #tbl do
- local tmp = {}
- tmp.name = tbl[i][1]
- tmp.types = tbl[i][2]
- tmp.total = tbl[i][3]
- tmp.hp = tbl[i][4]
- tmp.attack = tbl[i][5]
- tmp.defence = tbl[i][6]
- tmp.spAtk = tbl[i][7]
- tmp.spDef = tbl[i][8]
- tmp.speed = tbl[i][9]
- tbl[i] = tmp
- end
- --[[
- local inceptions = {
- "Castform",
- "Deoxys",
- }
- for i=1, #inceptions do
- local t1,t2 = lookup(inceptions[i])
- if t1 then
- table.remove(tbl, t2)
- end
- end
- --]]
- return tbl
- end
- function loadData(fl)
- local data, st, tbl = {}, {}, {}
- --[[
- local file = fs.open(fl, "r")
- local t = 1
- while true do
- data[t] = file.readLine()
- if data[t] == nil then break end
- t = t + 1
- end
- file.close()
- --]]
- data = fl error(textutils.serialize(data))
- local t = ""
- for i=1, string.len(data[1]) do
- if string.sub(data[1], i,i) == " " then
- st[#st+1] = t
- t = ""
- else
- t = t..string.sub(data[1], i,i)
- end
- end
- table.remove(data, 1)
- for i=1, #data do
- t,q = {},""
- for ii=1, string.len(data[i]) do
- if string.sub(data[i], ii,ii) == " " then
- if tostring(tonumber(q)) ~= "nil" then
- t[#t+1] = tonumber(q)
- else
- t[#t+1] = q
- end
- q = ""
- else
- q = q..string.sub(data[i], ii,ii)
- end
- end
- tbl[#tbl+1] = t
- end
- return tbl
- end
- function animatePokeball()
- for i=1, math.ceil(h/2)-1 do
- paintutils.drawLine(1,i+1,w,i+1,colors.red)
- paintutils.drawLine(1,h-i,w,h-i,colors.white)
- sleep(.01)
- end
- sleep(.1)
- paintutils.drawLine(1,math.ceil(h/2),w,math.ceil(h/2),colors.black)
- paintutils.drawLine(1,math.floor(h/2),w,math.floor(h/2),colors.black)
- for i=1, math.ceil(h/2)-2 do
- paintutils.drawLine(1,math.ceil(h/2)-i,w,math.ceil(h/2)-i,colors.black)
- paintutils.drawLine(1,math.ceil(h/2)+i,w,math.ceil(h/2)+i,colors.black)
- sleep(.01)
- end
- end
- function loadPokemonIndex(sel)
- pk = {}
- pk[#pk+1] = "Pokemon #"..sel..": "..pokedex[sel].name
- pk[#pk+1] = ""
- pk[#pk+1] = "Type:"
- for i=1, #pokedex[sel].types do
- pk[#pk+1] = pokedex[sel].types[i]
- end
- pk[#pk+1] = ""
- pk[#pk+1] = "HP - "..pokedex[sel].hp
- pk[#pk+1] = "Attack - "..pokedex[sel].attack
- pk[#pk+1] = "Defence - "..pokedex[sel].defence
- pk[#pk+1] = "Sp. Atk - "..pokedex[sel].spDef
- pk[#pk+1] = "Sp. Def - "..pokedex[sel].spAtk
- pk[#pk+1] = "Speed - "..pokedex[sel].speed
- pk[#pk+1] = ""
- pk[#pk+1] = "Total - "..pokedex[sel].total
- animatePokeball()
- term.setCursorPos(1,2)
- for i=1, #pk do term.setTextColor(type_colors[pk[i]] or colors.white) print(pk[i]) end
- --print("lol")
- os.pullEvent("key")
- animatePokeball()
- end
- function pe(...)
- local events = {...}
- while true do
- a,b,x,y = os.pullEvent()
- for i=1, #events do
- if a == events[i] then return a,b,x,y end
- end
- end
- end
- function controller(direction, selected, scroll, maxscroll)
- if direction == "up" or direction == -1 then
- if selected == 1 then
- selected = #pokedex scroll = maxscroll
- elseif selected <= 3+scroll and scroll ~= 0 then
- selected = selected - 1
- scroll = scroll - 1
- else
- selected = selected - 1
- end
- elseif direction == "down" or direction == 1 then
- if selected == #pokedex then
- selected = 1 scroll = 0
- elseif selected >= h-4+scroll and scroll ~= maxscroll then
- selected = selected + 1
- scroll = scroll + 1
- else
- selected = selected + 1
- end
- end
- return selected, scroll, maxscroll
- end
- function runPokedex()
- local run, updat, selected, scroll, maxscroll = true, true, 1, 0, #pokedex-(h-2)
- local function display()
- if updat then
- term.setBackgroundColor(colors.black)
- term.clear()
- paintutils.drawLine(1,1,w,1,colors.red)
- paintutils.drawLine(1,h,w,h,colors.white)
- --paintutils.drawLine(w-2,h,w,h,colors.gray)
- term.setTextColor(colors.white) term.setBackgroundColor(colors.red)
- term.setCursorPos(w-string.len("v."..version),1) write("v."..version)
- term.setTextColor(colors.black) term.setBackgroundColor(colors.white)
- term.setCursorPos(1,h) write("Press 'q' to exit.")
- term.setTextColor(colors.white)
- updat = false
- end
- for i=1, h-2 do
- term.setCursorPos(1,i+1)
- --if selected == i+scroll then term.setBackgroundColor(colors.lightGray) else term.setBackgroundColor(colors.white) end
- 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
- write(string.sub("000", 1,3-string.len(i+scroll))..i+scroll.." ")
- for i=1, 4-string.len(i+scroll) do write("") end
- write(pokedex[i+scroll].name)
- for i=1, w-string.len(" "..pokedex[i+scroll].name) do
- write(" ")
- end
- end
- end
- local function search()
- local r,text = true,""
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- while r do
- a,i = os.pullEvent("char")
- if type(tonumber(i)) == "number" then
- text = text..tostring(i)
- if string.len(text) == 3 then sleep(.5)
- text = math.min(math.max(tonumber(text),0),#pokedex)
- if selected > text then
- for i=1, selected-text do
- selected, scroll, maxscroll = controller("down", selected, scroll, maxscroll)
- end
- else
- for i=1, math.abs(selected-text) do
- selected, scroll, maxscroll = controller("up", selected, scroll, maxscroll)
- end
- end
- break
- end
- end
- term.setCursorPos((w-2),h) write(text)
- end
- end
- while run do
- display()
- --sleep(.001)
- a,b,x,y = pe("key", "mouse_click", "mouse_drag", "mouse_scroll")
- if a == "key" then
- if b == keys.w or b == keys.up then
- --if selected == 1 then selected = #boot else selected = selected - 1 end
- selected, scroll, maxscroll = controller("up", selected, scroll, maxscroll)
- elseif b == keys.s or b == keys.down then
- --if selected == #boot then selected = 1 else selected = selected + 1 end
- selected, scroll, maxscroll = controller("down", selected, scroll, maxscroll)
- elseif b == keys.enter or b == keys.e then
- loadPokemonIndex(selected) updat = true
- elseif b == keys.q then
- run = false
- end
- elseif a == "mouse_click" or a == "mouse_drag" then
- if y > 1 and y < h then
- if (y-1)+scroll == selected and a == "mouse_click" then loadPokemonIndex(selected) update = true end
- selected = math.min(y-1+scroll, #pokedex)
- elseif x >= w-3 and y == h then
- --search() updat = true
- end
- elseif a == "mouse_scroll" then
- selected, scroll, maxscroll = controller(b, selected, scroll, maxscroll)
- end
- end
- end
- term.clear()
- paintutils.drawImage(boot, 1,1) term.setBackgroundColor(colors.black)
- term.setCursorPos(8,2) print("PokeDex v."..version)
- term.setCursorPos(8,3) print("Just Does Games")
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(8,5) write(" ")
- sleep(.01) print("")
- local fl = dir.."/p.lua"
- pokedex = grabPkInfo() --<<< FOR API USE HERE (Copy the function) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- term.setBackgroundColor(colors.black)
- --[[
- for i=1, #pokedex do
- print("#"..i.." "..pokedex[i][1]) sleep(1)
- end
- --]]
- if debug then
- local sel = 1
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Pkid: "..sel.." | name: "..pokedex[sel].name)
- print("")
- write("Types: ") for i=1, #pokedex[sel].types do write(pokedex[sel].types[i].." ") end print("")
- print("Total: "..pokedex[sel].total)
- print("HP: "..pokedex[sel].hp)
- print("Attack: "..pokedex[sel].attack)
- print("Defence: "..pokedex[sel].defence)
- print("Sp. Atk: "..pokedex[sel].spAtk)
- print("Sp. Def: "..pokedex[sel].spDef)
- print("Speed: "..pokedex[sel].speed) print("")
- a,b = os.pullEvent("key")
- if b == keys.up then sel = math.max(sel - 1,1) elseif b == keys.down then sel = math.min(sel + 1, #pokedex) end
- end
- end
- sleep(.3)
- if pokedex == nil then
- print("Press any key to exit...")
- os.pullEvent("key")
- else
- parallel.waitForAny(runPokedex)
- term.setTextColor(colors.white) term.setBackgroundColor(colors.black) term.clear() term.setCursorPos(1,1)
- end
- --pokedex = loadData(gb)
- --error("Complete!")
- sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement