Skip to content

Commit

Permalink
feat: improve judge
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 20, 2024
1 parent e95d37d commit e96098b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 81 deletions.
88 changes: 27 additions & 61 deletions lua/leetcode/api/interpreter.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
local log = require("leetcode.logger")
local urls = require("leetcode.api.urls")

local config = require("leetcode.config")
local utils = require("leetcode.api.utils")
local spinner = require("leetcode.logger.spinner")

local t = require("leetcode.translator")

---@class lc.Interpreter
local interpreter = {}

local check_state = {
["PENDING"] = "Pending…",
["STARTED"] = "Judging…",
["SUCCESS"] = "Finished",
["FAILURE"] = "Failed", -- CODE: 16
}

---@param item lc.interpreter_response
---
---@return lc.interpreter_response
Expand Down Expand Up @@ -49,31 +42,19 @@ end
---@param id string
---@param callback function
function interpreter.listener(id, callback)
local noti = spinner:init(check_state["PENDING"], "points")

local function listen()
interpreter.check(id, function(item, err)
if err then
callback(false)
return noti:stop(err.msg, false)
end

if item.status_code then
if err then -- error
callback(nil, nil, err)
elseif item.status_code then -- got results
item = interpreter:handle_item(item)
noti:stop(item.status_msg, item._.success)
return callback(item)
callback(item)
else -- still judging
local intervals = config.auth.is_premium and { 500, 500 } or { 500, 1000 }
local interval = item.sate == "STARTED" and intervals[2] or intervals[1]
callback(nil, item.state)
vim.defer_fn(listen, interval)
end

local interval = 500
noti:update(check_state[item.state])
if item.state == "PENDING" then
noti:change("points")
elseif item.state == "STARTED" then
noti:change("dot")
interval = 1000
end

vim.defer_fn(listen, interval)
end)
end

Expand All @@ -85,32 +66,27 @@ end
---@field typed_code string
---@field data_input string

---@param title_slug string
---@param body lc.Interpret.body|string
---@param callback function
function interpreter.interpret_solution(title_slug, body, callback)
local url = urls.interpret:format(title_slug)

interpreter.fetch(url, {
body = body,
callback = function(res, success)
callback(success)
if success then interpreter.listener(res.interpret_id, callback) end
end,
})
end

---@param title_slug string
---@param submit boolean
---@param q lc.ui.Question
---@param body lc.Interpret.body|string
---@param callback function
function interpreter.submit(title_slug, body, callback)
local url = urls.submit:format(title_slug)
function interpreter.run(submit, q, body, callback)
local url = (submit and urls.submit or urls.interpret):format(q.q.title_slug)

interpreter.fetch(url, {
body = body,
callback = function(res, success)
callback(success)
if success then interpreter.listener(res.submission_id, callback) end
callback = function(res, err)
if err then
if err.status == 429 then
err.msg = "You have attempted to run code too soon"
err.lvl = vim.log.levels.WARN
end
callback(nil, nil, err)
else
q.console.result:clear()
local id = submit and res.submission_id or res.interpret_id
interpreter.listener(id, callback)
end
end,
})
end
Expand All @@ -127,17 +103,7 @@ end
function interpreter.fetch(url, opts)
utils.post(url, {
body = opts.body,
callback = function(res, err)
if err then
if err.status == 429 then
err.msg = "You have attempted to run code too soon"
err.lvl = vim.log.levels.WARN
end
opts.callback(log.err(err), false)
else
opts.callback(res, true)
end
end,
callback = opts.callback,
})
end

Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions lua/leetcode/logger/spinner/judge.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local Spinner = require("leetcode.logger.spinner")

---@class lc.Judge : lc.Spinner
local Judge = {}
Judge.__index = Judge
setmetatable(Judge, Spinner)

local check_state = {
["PENDING"] = "Pending…",
["STARTED"] = "Judging…",
["SUCCESS"] = "Finished",
["FAILURE"] = "Failed", -- CODE: 16
}

function Judge:from_state(state)
self:update(check_state[state])

if state == "PENDING" then
self:change("points")
else
self:change("dot")
end
end

function Judge:init()
local spinner = Spinner:init(check_state["PENDING"], "points")
return setmetatable(spinner, Judge)
end

return Judge
34 changes: 14 additions & 20 deletions lua/leetcode/runner/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local log = require("leetcode.logger")
local interpreter = require("leetcode.api.interpreter")
local config = require("leetcode.config")
local Judge = require("leetcode.logger.spinner.judge")

---@type Path
local leetbody = config.storage.cache:joinpath("body")
Expand Down Expand Up @@ -38,30 +39,23 @@ function Runner:handle(submit)
data_input = not submit and table.concat(question.console.testcase:content(), "\n") or nil,
}

local function callback(item)
if type(item) == "boolean" then
if item then
question.console.result:clear()
else
self:stop()
end
else
self:callback(item)
local judge = Judge:init()
local function callback(item, state, err)
if err or item then self:stop() end

if item then
judge:stop(item.status_msg, item._.success)
elseif state then
judge:from_state(state)
elseif err then
judge:stop(err.msg or "Something went wrong", false)
end
end

leetbody:write(vim.json.encode(body), "w")
if not submit then
interpreter.interpret_solution(question.q.title_slug, leetbody:absolute(), callback)
else
interpreter.submit(question.q.title_slug, leetbody:absolute(), callback)
if item then question.console.result:handle(item) end
end
end

---@param item lc.interpreter_response
function Runner:callback(item)
self:stop()
self.question.console.result:handle(item)
leetbody:write(vim.json.encode(body), "w")
interpreter.run(submit, question, leetbody:absolute(), callback)
end

---@param question lc.ui.Question
Expand Down

0 comments on commit e96098b

Please sign in to comment.