feat: race countdown support and language version selection (#346)

## Problem

Two gaps in the plugin: (1) contest pickers have no way to know whether
a contest supports countdown (race), so the UI can't surface that
affordance; (2) `:CP submit` hardcodes a single language ID per platform
with no way to choose C++ standard or override the platform ID.

## Solution

**Race countdown** (`4e709c8`): Add `supports_countdown` boolean to
`ContestListResult` and wire it through CSES/USACO scrapers, cache, race
module, and pickers.

**Language version selection** (`b90ac67`): Add `LANGUAGE_VERSIONS` and
`DEFAULT_VERSIONS` tables in `constants.lua`. Config gains `version` and
`submit_id` fields validated at setup time. `submit.lua` resolves the
effective config to a platform ID (priority: `submit_id` > `version` >
default). Helpdocs add `*cp-submit-language*` section. Tests cover
`LANGUAGE_IDS` completeness.
This commit is contained in:
Barrett Ruth 2026-03-06 18:18:21 -05:00 committed by GitHub
parent 592f977296
commit 58b6840815
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 265 additions and 46 deletions

View file

@ -9,15 +9,24 @@ local race_state = {
timer = nil,
platform = nil,
contest_id = nil,
contest_name = nil,
language = nil,
start_time = nil,
}
local function format_countdown(seconds)
local h = math.floor(seconds / 3600)
local d = math.floor(seconds / 86400)
local h = math.floor((seconds % 86400) / 3600)
local m = math.floor((seconds % 3600) / 60)
local s = seconds % 60
return string.format('%02d:%02d:%02d', h, m, s)
if d > 0 then
return string.format('%dd %dh %dm %ds', d, h, m, s)
elseif h > 0 then
return string.format('%dh %dm %ds', h, m, s)
elseif m > 0 then
return string.format('%dm %ds', m, s)
end
return string.format('%ds', s)
end
function M.start(platform, contest_id, language)
@ -35,20 +44,42 @@ function M.start(platform, contest_id, language)
end
cache.load()
local display = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
local cached_countdown = cache.get_supports_countdown(platform)
if cached_countdown == false then
logger.log(('%s does not support :CP race'):format(display), { level = vim.log.levels.ERROR })
return
end
local start_time = cache.get_contest_start_time(platform, contest_id)
if not start_time then
logger.log('Fetching contest list...', { level = vim.log.levels.INFO, override = true })
local contests = scraper.scrape_contest_list(platform)
if contests and #contests > 0 then
cache.set_contest_summaries(platform, contests)
start_time = cache.get_contest_start_time(platform, contest_id)
logger.log(
'Fetching contest list...',
{ level = vim.log.levels.INFO, override = true, sync = true }
)
local result = scraper.scrape_contest_list(platform)
if result then
local sc = result.supports_countdown
if sc == false then
cache.set_contest_summaries(platform, result.contests or {}, { supports_countdown = false })
logger.log(
('%s does not support :CP race'):format(display),
{ level = vim.log.levels.ERROR }
)
return
end
if result.contests and #result.contests > 0 then
cache.set_contest_summaries(platform, result.contests, { supports_countdown = sc })
start_time = cache.get_contest_start_time(platform, contest_id)
end
end
end
if not start_time then
logger.log(
('No start time found for %s contest %s'):format(
('No start time found for %s contest "%s"'):format(
constants.PLATFORM_DISPLAY_NAMES[platform] or platform,
contest_id
),
@ -69,18 +100,10 @@ function M.start(platform, contest_id, language)
race_state.platform = platform
race_state.contest_id = contest_id
race_state.contest_name = cache.get_contest_display_name(platform, contest_id) or contest_id
race_state.language = language
race_state.start_time = start_time
logger.log(
('Race started for %s %s — %s remaining'):format(
constants.PLATFORM_DISPLAY_NAMES[platform] or platform,
contest_id,
format_countdown(remaining)
),
{ level = vim.log.levels.INFO, override = true }
)
local timer = vim.uv.new_timer()
race_state.timer = timer
timer:start(
@ -97,17 +120,14 @@ function M.start(platform, contest_id, language)
local l = race_state.language
race_state.platform = nil
race_state.contest_id = nil
race_state.contest_name = nil
race_state.language = nil
race_state.start_time = nil
logger.log('Contest started!', { level = vim.log.levels.INFO, override = true })
require('cp.setup').setup_contest(p, c, nil, l)
else
vim.notify(
('[cp.nvim] %s %s — %s'):format(
constants.PLATFORM_DISPLAY_NAMES[race_state.platform] or race_state.platform,
race_state.contest_id,
format_countdown(r)
),
('[cp.nvim] %s starts in %s'):format(race_state.contest_name, format_countdown(r)),
vim.log.levels.INFO
)
end
@ -126,6 +146,7 @@ function M.stop()
race_state.timer = nil
race_state.platform = nil
race_state.contest_id = nil
race_state.contest_name = nil
race_state.language = nil
race_state.start_time = nil
logger.log('Race cancelled', { level = vim.log.levels.INFO, override = true })