From 83035c62a3524f39916ab45f4be89cb29bba979e Mon Sep 17 00:00:00 2001 From: CoolestDoggo <40402235+CoolestDoggo@users.noreply.github.com> Date: Sat, 10 Sep 2022 20:37:24 -0400 Subject: [PATCH 1/2] Change map init --- src/modules/commands/maps_init.lua | 78 +++++++++++++++++++----------- 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/src/modules/commands/maps_init.lua b/src/modules/commands/maps_init.lua index 1b903ed..3864c66 100644 --- a/src/modules/commands/maps_init.lua +++ b/src/modules/commands/maps_init.lua @@ -2,41 +2,61 @@ local discordia=require('discordia') local API=require('./../strafes_net.lua') discordia.extensions() API.MAPS={} -for _,game in next,API.GAMES do - if type(tonumber(game)) == 'number' then - local maps = {count=0} - local res,headers = API:GetMaps(game) - local pages = tonumber(headers['Pagination-Count']) - maps.count=maps.count+#res - for _,v in next,res do - maps[v.ID]=v + +local function insert(t, value) + local start, ending, mid, state = 1, #t, 1, 0 + + while start <= ending do + mid = math.floor((start + ending) / 2) + if #value.DisplayName < #t[mid].DisplayName then + ending, state = mid - 1, 0 + else + start, state = mid + 1, 1 end - if pages>1 then - for i=2,pages do - res,headers = API:GetMaps(game,i) - maps.count=maps.count+#res - for _,j in next,res do - maps[j.ID]=j + end + + table.insert(t, mid + state, value.DisplayName) +end + +for _, game in next, API.GAMES do + if type(tonumber(game)) == 'number' then + local count = 0 -- add into the maps table afterwards + local maps = {} + local res, headers = API:GetMaps(game) + local pages = tonumber(headers['Pagination-Count']) + + count = count + #res + + for _, v in next, res do + insert(maps, v) + end + + if pages > 1 then + for i = 2, pages do + res, headers = API:GetMaps(game, i) + count = count + #res + + for _, j in next, res do + insert(maps, j) end end end - setmetatable(maps,{__index=function(self,i) - if i=='count' then return self.count end - if not tonumber(i) then - for ix,v in next,self do - if type(v)=='table' and v.DisplayName:lower():find(i:lower()) then - return v - end - end - elseif tonumber(i) then - for ix,v in next,self do - if type(v)=='table' and v.ID==i then - return v - end + + setmetatable(maps, {__index = function(self, k) + if k=='count' then return self.count end + + -- Just to make sure it goes in the right order + for i = 1, self.count do + local v = self[i] + + if type(v) == 'table' and v.DisplayName:lower():find(k:lower()) then + return v end end end}) - API.MAPS[game]=maps - print('map init done for game:',API.GAMES[game],'count:',API.MAPS[game].count) + + maps.count = count + API.MAPS[game] = maps + print('map init done for game:', API.GAMES[game], 'count:', API.MAPS[game].count) end end \ No newline at end of file -- 2.45.2 From 0a8f14b052882c96740c52def3b0cd1b62da364d Mon Sep 17 00:00:00 2001 From: CoolestDoggo <40402235+CoolestDoggo@users.noreply.github.com> Date: Sat, 10 Sep 2022 20:52:40 -0400 Subject: [PATCH 2/2] Fix insert --- src/modules/commands/maps_init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/commands/maps_init.lua b/src/modules/commands/maps_init.lua index 3864c66..bacf0f3 100644 --- a/src/modules/commands/maps_init.lua +++ b/src/modules/commands/maps_init.lua @@ -15,7 +15,7 @@ local function insert(t, value) end end - table.insert(t, mid + state, value.DisplayName) + table.insert(t, mid + state, value) end for _, game in next, API.GAMES do -- 2.45.2