Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit d5ee9bf

Browse files
committed
add contest/contestquestion handlers to cache mod
1 parent 69cf804 commit d5ee9bf

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

β€Žsrc/cache/mod.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,30 @@ impl Cache {
106106
Ok(ps)
107107
}
108108

109+
// TODO: get rid of this
110+
pub fn push_problem(&self, p: Problem) -> Result<(), Error> {
111+
diesel::replace_into(problems)
112+
.values(&vec![p])
113+
.execute(&self.conn()?)?;
114+
Ok(())
115+
}
116+
117+
/// TODO: implement caching
118+
/// Get contest
119+
pub async fn get_contest(&self, contest: &str) -> Result<Contest, Error> {
120+
let ctest = self.0
121+
.get_contest_info(contest)
122+
.await?
123+
.json()
124+
.await?;
125+
let ctest = parser::contest(ctest).ok_or(Error::NoneError)?;
126+
Ok(ctest)
127+
}
128+
109129
/// Get problem
110130
pub fn get_problem(&self, rfid: i32) -> Result<Problem, Error> {
111131
let p: Problem = problems.filter(fid.eq(rfid)).first(&self.conn()?)?;
112-
if p.category != "algorithms" {
132+
if p.category != "algorithms" && p.category != "contest"{
113133
return Err(Error::FeatureError(
114134
"Not support database and shell questions for now".to_string(),
115135
));
@@ -191,6 +211,17 @@ impl Cache {
191211
Ok(rdesc)
192212
}
193213

214+
// TODO: we can probably use this for all questions in general, actually
215+
/// Get contest question
216+
pub async fn get_contest_qnp(&self, problem: &str) -> Result<(Problem,Question), Error> {
217+
let graphql_res = self.0
218+
.get_contest_question_detail(problem)
219+
.await?
220+
.json()
221+
.await?;
222+
parser::graphql_problem_and_question(graphql_res).ok_or(Error::NoneError)
223+
}
224+
194225
pub async fn get_tagged_questions(self, rslug: &str) -> Result<Vec<String>, Error> {
195226
trace!("Geting {} questions...", &rslug);
196227
let ids: Vec<String>;
@@ -231,6 +262,7 @@ impl Cache {
231262
run: Run,
232263
rfid: i32,
233264
testcase: Option<String>,
265+
contest: Option<&str>
234266
) -> Result<(HashMap<&'static str, String>, [String; 2]), Error> {
235267
trace!("pre run code...");
236268
use crate::helper::{code_path, test_cases_path};
@@ -277,11 +309,21 @@ impl Cache {
277309
json.insert("name", p.name.to_string());
278310
json.insert("data_input", testcase);
279311

312+
// TODO: make this less ugly
313+
let make_url = |s: &str| {
314+
if let Some(c) = contest {
315+
let s = format!("{}_contest", s);
316+
conf.sys.urls.get(&s).map(|u| u.replace("$contest", &c))
317+
} else {
318+
conf.sys.urls.get(s).map(|u| u.to_owned())
319+
}.ok_or(Error::NoneError)
320+
};
321+
280322
let url = match run {
281-
Run::Test => conf.sys.urls.get("test").ok_or(Error::NoneError)?.replace("$slug", &p.slug),
323+
Run::Test => make_url("test")?.replace("$slug", &p.slug),
282324
Run::Submit => {
283325
json.insert("judge_type", "large".to_string());
284-
conf.sys.urls.get("submit").ok_or(Error::NoneError)?.replace("$slug", &p.slug)
326+
make_url("submit")?.replace("$slug", &p.slug)
285327
}
286328
};
287329

@@ -317,9 +359,10 @@ impl Cache {
317359
rfid: i32,
318360
run: Run,
319361
testcase: Option<String>,
362+
contest: Option<&str>
320363
) -> Result<VerifyResult, Error> {
321364
trace!("Exec problem filter β€”β€” Test or Submit");
322-
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, testcase).await?;
365+
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, testcase, contest).await?;
323366
trace!("Pre run code result {:?}, {:?}, {:?}", json, url, refer);
324367

325368
let run_res: RunCode = self

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /