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