irongit

A Neovim plugin enabling you to solve LeetCode problems.

leetcode.nvim/lua/leetcode/api/interpreter.lua
111 lines3.1 KBLua
1local log = require("leetcode.logger")
2local urls = require("leetcode.api.urls")
3
4local config = require("leetcode.config")
5local utils = require("leetcode.api.utils")
6
7local t = require("leetcode.translator")
8
9---@class lc.Interpreter
10local interpreter = {}
11
12---@param item lc.interpreter_response
13---
14---@return lc.interpreter_response
15function interpreter:handle_item(item)
16 local success = false
17 if item.status_code == 10 then
18 success = item.compare_result:match("^[1]+$") and true or false
19 item.status_msg = success and "Accepted" or "Wrong Answer"
20 end
21
22 local submission = false
23 if item.submission_id then
24 submission = not item.submission_id:find("runcode") and true or false
25 end
26 local hl = success and "leetcode_ok" or "leetcode_error"
27
28 if item.status_code == 15 and item.invalid_testcase then
29 item.status_msg = "Invalid Testcase"
30 end
31
32 item._ = {
33 title = "" .. t(item.status_msg),
34 hl = hl,
35 success = success,
36 submission = submission,
37 }
38
39 return item
40end
41
42---@param id string
43---@param callback function
44function interpreter.listener(id, callback)
45 local function listen()
46 interpreter.check(id, function(item, err)
47 if err then -- error
48 callback(nil, nil, err)
49 elseif item.status_code then -- got results
50 item = interpreter:handle_item(item)
51 callback(item)
52 else -- still judging
53 local intervals = config.auth.is_premium and { 450, 450 } or { 450, 900 }
54 local interval = item.sate == "STARTED" and intervals[2] or intervals[1]
55 callback(nil, item.state)
56 vim.defer_fn(listen, interval)
57 end
58 end)
59 end
60
61 listen()
62end
63
64---@class lc.Interpret.body
65---@field question lc.question_res
66---@field typed_code string
67---@field data_input string
68
69---@param submit boolean
70---@param q lc.ui.Question
71---@param body lc.Interpret.body|string
72---@param callback function
73function interpreter.run(submit, q, body, callback)
74 local url = (submit and urls.submit or urls.interpret):format(q.q.title_slug)
75
76 interpreter.fetch(url, {
77 body = body,
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 local id = submit and res.submission_id or res.interpret_id
87 q.console.testcase:snapshot(id, res)
88 q.console.result:clear()
89 interpreter.listener(id, callback)
90 end
91 end,
92 })
93end
94
95---@param id string
96---@param cb function
97---
98---@return lc.Interpreter.Response
99function interpreter.check(id, cb)
100 local url = urls.check:format(id)
101 utils.get(url, { callback = cb })
102end
103
104function interpreter.fetch(url, opts)
105 utils.post(url, {
106 body = opts.body,
107 callback = opts.callback,
108 })
109end
110
111return interpreter