Skip to content

Commit e96098b

Browse files
committed
feat: improve judge
1 parent e95d37d commit e96098b

File tree

4 files changed

+71
-81
lines changed

4 files changed

+71
-81
lines changed

lua/leetcode/api/interpreter.lua

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
local log = require("leetcode.logger")
22
local urls = require("leetcode.api.urls")
33

4+
local config = require("leetcode.config")
45
local utils = require("leetcode.api.utils")
5-
local spinner = require("leetcode.logger.spinner")
66

77
local t = require("leetcode.translator")
88

99
---@class lc.Interpreter
1010
local interpreter = {}
1111

12-
local check_state = {
13-
["PENDING"] = "Pending…",
14-
["STARTED"] = "Judging…",
15-
["SUCCESS"] = "Finished",
16-
["FAILURE"] = "Failed", -- CODE: 16
17-
}
18-
1912
---@param item lc.interpreter_response
2013
---
2114
---@return lc.interpreter_response
@@ -49,31 +42,19 @@ end
4942
---@param id string
5043
---@param callback function
5144
function interpreter.listener(id, callback)
52-
local noti = spinner:init(check_state["PENDING"], "points")
53-
5445
local function listen()
5546
interpreter.check(id, function(item, err)
56-
if err then
57-
callback(false)
58-
return noti:stop(err.msg, false)
59-
end
60-
61-
if item.status_code then
47+
if err then -- error
48+
callback(nil, nil, err)
49+
elseif item.status_code then -- got results
6250
item = interpreter:handle_item(item)
63-
noti:stop(item.status_msg, item._.success)
64-
return callback(item)
51+
callback(item)
52+
else -- still judging
53+
local intervals = config.auth.is_premium and { 500, 500 } or { 500, 1000 }
54+
local interval = item.sate == "STARTED" and intervals[2] or intervals[1]
55+
callback(nil, item.state)
56+
vim.defer_fn(listen, interval)
6557
end
66-
67-
local interval = 500
68-
noti:update(check_state[item.state])
69-
if item.state == "PENDING" then
70-
noti:change("points")
71-
elseif item.state == "STARTED" then
72-
noti:change("dot")
73-
interval = 1000
74-
end
75-
76-
vim.defer_fn(listen, interval)
7758
end)
7859
end
7960

@@ -85,32 +66,27 @@ end
8566
---@field typed_code string
8667
---@field data_input string
8768

88-
---@param title_slug string
89-
---@param body lc.Interpret.body|string
90-
---@param callback function
91-
function interpreter.interpret_solution(title_slug, body, callback)
92-
local url = urls.interpret:format(title_slug)
93-
94-
interpreter.fetch(url, {
95-
body = body,
96-
callback = function(res, success)
97-
callback(success)
98-
if success then interpreter.listener(res.interpret_id, callback) end
99-
end,
100-
})
101-
end
102-
103-
---@param title_slug string
69+
---@param submit boolean
70+
---@param q lc.ui.Question
10471
---@param body lc.Interpret.body|string
10572
---@param callback function
106-
function interpreter.submit(title_slug, body, callback)
107-
local url = urls.submit:format(title_slug)
73+
function interpreter.run(submit, q, body, callback)
74+
local url = (submit and urls.submit or urls.interpret):format(q.q.title_slug)
10875

10976
interpreter.fetch(url, {
11077
body = body,
111-
callback = function(res, success)
112-
callback(success)
113-
if success then interpreter.listener(res.submission_id, callback) end
78+
callback = function(res, err)
79+
if err then
80+
if err.status == 429 then
81+
err.msg = "You have attempted to run code too soon"
82+
err.lvl = vim.log.levels.WARN
83+
end
84+
callback(nil, nil, err)
85+
else
86+
q.console.result:clear()
87+
local id = submit and res.submission_id or res.interpret_id
88+
interpreter.listener(id, callback)
89+
end
11490
end,
11591
})
11692
end
@@ -127,17 +103,7 @@ end
127103
function interpreter.fetch(url, opts)
128104
utils.post(url, {
129105
body = opts.body,
130-
callback = function(res, err)
131-
if err then
132-
if err.status == 429 then
133-
err.msg = "You have attempted to run code too soon"
134-
err.lvl = vim.log.levels.WARN
135-
end
136-
opts.callback(log.err(err), false)
137-
else
138-
opts.callback(res, true)
139-
end
140-
end,
106+
callback = opts.callback,
141107
})
142108
end
143109

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local Spinner = require("leetcode.logger.spinner")
2+
3+
---@class lc.Judge : lc.Spinner
4+
local Judge = {}
5+
Judge.__index = Judge
6+
setmetatable(Judge, Spinner)
7+
8+
local check_state = {
9+
["PENDING"] = "Pending…",
10+
["STARTED"] = "Judging…",
11+
["SUCCESS"] = "Finished",
12+
["FAILURE"] = "Failed", -- CODE: 16
13+
}
14+
15+
function Judge:from_state(state)
16+
self:update(check_state[state])
17+
18+
if state == "PENDING" then
19+
self:change("points")
20+
else
21+
self:change("dot")
22+
end
23+
end
24+
25+
function Judge:init()
26+
local spinner = Spinner:init(check_state["PENDING"], "points")
27+
return setmetatable(spinner, Judge)
28+
end
29+
30+
return Judge

lua/leetcode/runner/init.lua

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local log = require("leetcode.logger")
22
local interpreter = require("leetcode.api.interpreter")
33
local config = require("leetcode.config")
4+
local Judge = require("leetcode.logger.spinner.judge")
45

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

41-
local function callback(item)
42-
if type(item) == "boolean" then
43-
if item then
44-
question.console.result:clear()
45-
else
46-
self:stop()
47-
end
48-
else
49-
self:callback(item)
42+
local judge = Judge:init()
43+
local function callback(item, state, err)
44+
if err or item then self:stop() end
45+
46+
if item then
47+
judge:stop(item.status_msg, item._.success)
48+
elseif state then
49+
judge:from_state(state)
50+
elseif err then
51+
judge:stop(err.msg or "Something went wrong", false)
5052
end
51-
end
5253

53-
leetbody:write(vim.json.encode(body), "w")
54-
if not submit then
55-
interpreter.interpret_solution(question.q.title_slug, leetbody:absolute(), callback)
56-
else
57-
interpreter.submit(question.q.title_slug, leetbody:absolute(), callback)
54+
if item then question.console.result:handle(item) end
5855
end
59-
end
6056

61-
---@param item lc.interpreter_response
62-
function Runner:callback(item)
63-
self:stop()
64-
self.question.console.result:handle(item)
57+
leetbody:write(vim.json.encode(body), "w")
58+
interpreter.run(submit, question, leetbody:absolute(), callback)
6559
end
6660

6761
---@param question lc.ui.Question

0 commit comments

Comments
 (0)