From 69d9c5aee7adae25acde15dc084f41d65b9f9716 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Mon, 2 Jan 2023 18:14:06 +0300 Subject: [PATCH] make mappack impl work with >3 mappacks --- src/endpoints/levels/getLevels.cr | 7 ++- src/endpoints/packs/getMapPacks.cr | 90 ++++++++++++++++-------------- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/endpoints/levels/getLevels.cr b/src/endpoints/levels/getLevels.cr index 0f3b00b..6c673d4 100644 --- a/src/endpoints/levels/getLevels.cr +++ b/src/endpoints/levels/getLevels.cr @@ -16,12 +16,14 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String { queryParams = [] of String # order by [...] order = "levels.created_at desc" + # joins go here + joins = [] of String page_offset = (params["page"]? || "0").to_i * levels_per_page searchQuery = params["str"]? || "" - if searchQuery != "" && params["type"] != "5" + if searchQuery != "" && params["type"] != "5" && params["type"] != "10" && params["type"] != "19" if searchQuery.to_i? # we do this to get rid of the initial "unlisted = 0" bit can_see_unlisted = true @@ -145,6 +147,7 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String { when "10", "19" # map packs order = "map_pack_links.idx asc" queryParams << "levels.id in (#{searchQuery.split(",").map{|v| v.to_i}.join(",")})" + joins << "left join map_pack_links on map_pack_links.level_id = levels.id" when "11" # rated # todo: order by rate date queryParams << "levels.stars is not null" @@ -166,7 +169,7 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String { where_str = "where (#{queryParams.join(") and (")})" # todo: switch join users to left join to avoid losing levels to the shadow realm after a user vanishes - query_base = "from levels join users on levels.user_id = users.id left join map_pack_links on map_pack_links.level_id = levels.id #{where_str} order by #{order}" + query_base = "from levels join users on levels.user_id = users.id #{joins.join(" ")} #{where_str} order by #{order}" LOG.debug { "query: #{query_base}" } diff --git a/src/endpoints/packs/getMapPacks.cr b/src/endpoints/packs/getMapPacks.cr index 251d58f..a0ea994 100644 --- a/src/endpoints/packs/getMapPacks.cr +++ b/src/endpoints/packs/getMapPacks.cr @@ -11,60 +11,66 @@ CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(body : String): String { LOG.debug { params.inspect } page = params["page"].to_i32 - properties = Hash(Int32, Hash(String, String)).new + map_pack_hash = Hash(Int32, Hash(String, String)).new levels = Hash(Int32, Array(Int32)).new - DATABASE.query("select map_packs.id, map_packs.name, map_packs.stars, map_packs.coins, map_packs.difficulty, map_packs.col1, map_packs.col2, map_pack_links.level_id from map_packs join map_pack_links on map_packs.id = map_pack_links.mappack_id order by map_pack_links.idx limit #{mappacks_per_page} offset #{page * mappacks_per_page} group by map_packs.id") do |rs| - rs.each do - id = rs.read(Int32) - name = rs.read(String) - stars = rs.read(Int32) - coins = rs.read(Int32) - difficulty = rs.read(Int32) - col1 = rs.read(String) - col2 = rs.read(String | Nil) - level_id = rs.read(Int32) + DATABASE.query_each("select map_packs.id, map_packs.name, map_packs.stars, map_packs.coins, map_packs.difficulty, map_packs.col1, map_packs.col2, map_pack_links.level_id from map_packs join map_pack_links on map_packs.id = map_pack_links.mappack_id order by map_packs.id, map_pack_links.idx") do |rs| + id = rs.read(Int32) + name = rs.read(String) + stars = rs.read(Int32) + coins = rs.read(Int32) + difficulty = rs.read(Int32) + col1 = rs.read(String) + col2 = rs.read(String | Nil) + level_id = rs.read(Int32) - if !col2 - col2 = col1 - end + if !col2 + col2 = col1 + end - if !properties.has_key?(id) - properties[id] = { - "name" => name, - "stars" => stars.to_s, - "coins" => coins.to_s, - "difficulty" => difficulty.to_s, - "col1" => col1, - "col2" => col2 - } - end + if !map_pack_hash.has_key?(id) + map_pack_hash[id] = { + "name" => name, + "stars" => stars.to_s, + "coins" => coins.to_s, + "difficulty" => difficulty.to_s, + "col1" => col1, + "col2" => col2 + } + end - if !levels.has_key?(id) - levels[id] = [ level_id ] - else - levels[id] << level_id - end + if !levels.has_key?(id) + levels[id] = [ level_id ] + else + levels[id] << level_id end end map_packs = [] of String hash_data = [] of Tuple(Int32, Int32, Int32) - #"1:".$mappack["ID"].":2:".$mappack["name"].":3:".$mappack["levels"].":4:".$mappack["stars"].":5:".$mappack["coins"].":6:".$mappack["difficulty"].":7:".$mappack["rgbcolors"].":8:".$colors2."|"; - properties.each_key do |id| - map_packs << Format.fmt_hash({ - 1 => id, - 2 => properties[id]["name"], - 3 => levels[id].join(","), - 4 => properties[id]["stars"], - 5 => properties[id]["coins"], - 6 => properties[id]["difficulty"], - 7 => properties[id]["col1"], - 8 => properties[id]["col2"] - }) + min_idx = page * mappacks_per_page + max_idx = min_idx + mappacks_per_page - 1 - hash_data << { id, properties[id]["stars"].to_i32, properties[id]["coins"].to_i32 } + #"1:".$mappack["ID"].":2:".$mappack["name"].":3:".$mappack["levels"].":4:".$mappack["stars"].":5:".$mappack["coins"].":6:".$mappack["difficulty"].":7:".$mappack["rgbcolors"].":8:".$colors2."|"; + idx = 0 + map_pack_hash.each_key do |id| + if idx >= min_idx && idx <= max_idx + map_packs << Format.fmt_hash({ + 1 => id, + 2 => map_pack_hash[id]["name"], + 3 => levels[id].join(","), + 4 => map_pack_hash[id]["stars"], + 5 => map_pack_hash[id]["coins"], + 6 => map_pack_hash[id]["difficulty"], + 7 => map_pack_hash[id]["col1"], + 8 => map_pack_hash[id]["col2"] + }) + + hash_data << { id, map_pack_hash[id]["stars"].to_i32, map_pack_hash[id]["coins"].to_i32 } + end + + idx += 1 end total_count = DATABASE.scalar "select count(*) from map_packs"