fix print order

This commit is contained in:
Barrett Ruth 2025-10-03 14:41:32 -04:00
parent 1520939d4b
commit 34ef7bafd6
2 changed files with 34 additions and 62 deletions

View file

@ -36,9 +36,8 @@ function M.get_platforms()
return result return result
end end
---Get list of contests for a specific platform ---@param platform string
---@param platform string Platform identifier (e.g. "codeforces", "atcoder") ---@param refresh? boolean
---@param refresh? boolean Whether to skip caching and append new contests
---@return cp.ContestItem[] ---@return cp.ContestItem[]
function M.get_platform_contests(platform, refresh) function M.get_platform_contests(platform, refresh)
logger.log( logger.log(
@ -48,24 +47,21 @@ function M.get_platform_contests(platform, refresh)
) )
cache.load() cache.load()
local picker_contests = cache.get_contest_summaries(platform) local picker_contests = cache.get_contest_summaries(platform)
if refresh or vim.tbl_isempty(picker_contests) then if refresh or vim.tbl_isempty(picker_contests) then
logger.log(('Cache miss on %s contests'):format(platform)) logger.log(('Cache miss on %s contests'):format(platform))
local contests = scraper.scrape_contest_list(platform) local contests = scraper.scrape_contest_list(platform) -- sync
cache.set_contest_summaries(platform, contests) cache.set_contest_summaries(platform, contests)
picker_contests = cache.get_contest_summaries(platform) -- <-- reload after write
end end
logger.log( logger.log(
('Loaded %s %s contests.'):format(#picker_contests, constants.PLATFORM_DISPLAY_NAMES[platform]), ('Loaded %d %s contests.'):format(#picker_contests, constants.PLATFORM_DISPLAY_NAMES[platform]),
vim.log.levels.INFO, vim.log.levels.INFO,
true true
) )
picker_contests = cache.get_contest_summaries(platform)
return picker_contests return picker_contests
end end

View file

@ -57,6 +57,33 @@ function M.setup_contest(platform, contest_id, language, problem_id)
state.set_contest_id(contest_id) state.set_contest_id(contest_id)
cache.load() cache.load()
local function proceed(contest_data)
local problems = contest_data.problems
local pid = problems[(problem_id and contest_data.index_map[problem_id] or 1)].id
M.setup_problem(pid, language)
local cached_len = #vim.tbl_filter(function(p)
return cache.get_test_cases(platform, contest_id, p.id) ~= nil
end, problems)
if cached_len ~= #problems then
scraper.scrape_all_tests(platform, contest_id, function(ev)
local cached_tests = {}
for i, t in ipairs(ev.tests) do
cached_tests[i] = { index = i, input = t.input, expected = t.expected }
end
cache.set_test_cases(
platform,
contest_id,
ev.problem_id,
cached_tests,
ev.timeout_ms or 0,
ev.memory_mb or 0
)
end)
end
end
local contest_data = cache.get_contest_data(platform, contest_id) local contest_data = cache.get_contest_data(platform, contest_id)
if not contest_data or not contest_data.problems then if not contest_data or not contest_data.problems then
logger.log('Fetching contests problems...', vim.log.levels.INFO, true) logger.log('Fetching contests problems...', vim.log.levels.INFO, true)
@ -64,63 +91,12 @@ function M.setup_contest(platform, contest_id, language, problem_id)
local problems = result.problems or {} local problems = result.problems or {}
cache.set_contest_data(platform, contest_id, problems, result.name, result.display_name) cache.set_contest_data(platform, contest_id, problems, result.name, result.display_name)
logger.log(('Found %d problems for %s contest %s.'):format(#problems, platform, contest_id)) logger.log(('Found %d problems for %s contest %s.'):format(#problems, platform, contest_id))
proceed(cache.get_contest_data(platform, contest_id))
contest_data = cache.get_contest_data(platform, contest_id)
local pid = contest_data.problems[problem_id and contest_data.index_map[problem_id] or 1].id
M.setup_problem(pid, language)
local cached_len = #vim.tbl_filter(function(p)
return cache.get_test_cases(platform, contest_id, p.id) ~= nil
end, problems)
if cached_len < #problems then
scraper.scrape_all_tests(platform, contest_id, function(ev)
if not ev or not ev.tests or not ev.problem_id then
return
end
local cached_tests = {}
for i, t in ipairs(ev.tests) do
cached_tests[i] = { index = i, input = t.input, expected = t.expected }
end
cache.set_test_cases(
platform,
contest_id,
ev.problem_id,
cached_tests,
ev.timeout_ms or 0,
ev.memory_mb or 0
)
end)
end
end) end)
return return
end end
local problems = contest_data.problems proceed(contest_data)
local pid = problems[(problem_id and contest_data.index_map[problem_id] or 1)].id
M.setup_problem(pid, language)
local cached_len = #vim.tbl_filter(function(p)
return cache.get_test_cases(platform, contest_id, p.id) ~= nil
end, problems)
if cached_len < #problems then
scraper.scrape_all_tests(platform, contest_id, function(ev)
if not ev or not ev.tests or not ev.problem_id then
return
end
local cached_tests = {}
for i, t in ipairs(ev.tests) do
cached_tests[i] = { index = i, input = t.input, expected = t.expected }
end
cache.set_test_cases(
platform,
contest_id,
ev.problem_id,
cached_tests,
ev.timeout_ms or 0,
ev.memory_mb or 0
)
end)
end
end end
---@param problem_id string ---@param problem_id string