algohub_server/routes/
problem.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::time::Duration;

use eval_stack::{compile::Language, config::JudgeOptions, judge::JudgeStatus};
use rocket::{
    serde::json::Json,
    tokio::{
        fs::{create_dir_all, File},
        io::AsyncWriteExt,
    },
    State,
};
use serde::{Deserialize, Serialize};
use surrealdb::{engine::remote::ws::Client, Surreal};

use crate::{
    models::{
        error::Error,
        problem::{Mode, Problem, ProblemDetail, Sample},
        response::Response,
        OwnedCredentials,
    },
    utils::{problem, session},
    Result,
};

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct ProblemData<'r> {
    pub id: &'r str,
    pub token: &'r str,

    pub title: &'r str,
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<String>,
    pub samples: Vec<Sample>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hint: Option<String>,

    // pub owner: Thing,
    pub time_limit: u64,
    pub memory_limit: u64,
    pub test_cases: Vec<Sample>,

    pub categories: Vec<String>,
    pub tags: Vec<String>,

    pub mode: Mode,
    pub private: bool,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(crate = "rocket::serde")]
pub struct ProblemResponse {
    pub id: String,
}

#[post("/create", data = "<problem>")]
pub async fn create(
    db: &State<Surreal<Client>>,
    problem: Json<ProblemData<'_>>,
) -> Result<ProblemResponse> {
    if !session::verify(db, problem.id, problem.token).await {
        return Err(Error::Unauthorized(Json("Invalid token".into())));
    }

    let problem = problem::create(db, problem.into_inner())
        .await
        .map_err(|e| Error::ServerError(Json(e.to_string().into())))?
        .ok_or(Error::ServerError(Json(
            "Failed to create problem, please try again later.".into(),
        )))?;

    Ok(Json(Response {
        success: true,
        message: "Problem created successfully".to_string(),
        data: Some(ProblemResponse {
            id: problem.id.unwrap().id.to_string(),
        }),
    }))
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Authenticate<'r> {
    pub id: Option<&'r str>,
    pub token: Option<&'r str>,
}

#[post("/get/<id>", data = "<auth>")]
pub async fn get(
    db: &State<Surreal<Client>>,
    id: &str,
    auth: Json<Authenticate<'_>>,
) -> Result<ProblemDetail> {
    let problem = problem::get::<ProblemDetail>(db, id)
        .await
        .map_err(|e| Error::ServerError(Json(e.to_string().into())))?
        .ok_or(Error::NotFound(Json(
            "Problem with specified id not found".into(),
        )))?;

    let has_permission = if problem.private {
        if auth.id.is_none() || auth.token.is_none() {
            false
        } else {
            if !session::verify(db, auth.id.unwrap(), auth.token.unwrap()).await {
                false
            } else {
                auth.id.unwrap() == problem.owner.id.to_string()
            }
        }
    } else {
        true
    };

    if !has_permission {
        return Err(Error::Unauthorized(Json(
            "You have no permission to access this problem".into(),
        )));
    }

    Ok(Json(Response {
        success: true,
        message: "Problem found".to_string(),
        data: Some(problem),
    }))
}

#[derive(Serialize, Deserialize, Default)]
#[serde(crate = "rocket::serde")]
pub struct ProblemFilter {
    pub limit: Option<u8>,
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct ListProblem {
    pub auth: OwnedCredentials,
    pub filter: Option<ProblemFilter>,
}

#[post("/list", data = "<data>")]
pub async fn list(
    db: &State<Surreal<Client>>,
    data: Json<ListProblem>,
) -> Result<Vec<ProblemDetail>> {
    if !session::verify(db, &data.auth.id, &data.auth.token).await {
        return Err(Error::Unauthorized(Json("Invalid token".into())));
    }

    let data = data.into_inner();
    let problems = problem::list(db, &data.auth.id, data.filter)
        .await
        .map_err(|e| Error::ServerError(Json(e.to_string().into())))?;
    println!("{:?}", problems);

    Ok(Json(Response {
        success: true,
        message: "Problems found".to_string(),
        data: Some(problems),
    }))
}

#[derive(Serialize, Deserialize)]
pub struct SubmitData<'r> {
    pub id: &'r str,
    pub token: &'r str,
    pub language: &'r str,
    pub code: String,
}

#[derive(Serialize, Deserialize)]
pub struct SubmitResponse {
    pub status: String,
}

#[post("/submit/<id>", data = "<data>")]
pub async fn submit(
    db: &State<Surreal<Client>>,
    id: &str,
    data: Json<SubmitData<'_>>,
) -> Result<SubmitResponse> {
    if !session::verify(db, data.id, data.token).await {
        return Err(Error::Unauthorized(Json("Invalid token".into())));
    }

    let problem = problem::get::<Problem>(db, id)
        .await
        .map_err(|e| Error::ServerError(Json(e.to_string().into())))?
        .ok_or(Error::NotFound(Json(
            "Problem with specified id not found".into(),
        )))?;

    let options = JudgeOptions {
        time_limit: Duration::from_secs(problem.time_limit),
        memory_limit: problem.memory_limit,
    };

    let workspace = std::env::current_dir()
        .unwrap()
        .join("content")
        .join(data.id)
        .join("workspace")
        .join(uuid::Uuid::new_v4().to_string());

    if !workspace.exists() {
        create_dir_all(&workspace)
            .await
            .map_err(|e| Error::ServerError(Json(e.to_string().into())))?;
    }

    let language = match data.language {
        "rust" => Language::Rust,
        "c" => Language::C,
        "cpp" => Language::CPP,
        "python" => Language::Python,
        _ => Language::Rust,
    };

    let source_file_path = workspace.join("source.code");
    File::create(&source_file_path)
        .await
        .map_err(|e| Error::ServerError(Json(e.to_string().into())))?
        .write_all(data.code.as_bytes())
        .await
        .unwrap();

    println!("{:?}", problem.test_cases);
    let result = eval_stack::case::run_test_cases(
        language,
        workspace,
        source_file_path,
        options,
        problem
            .test_cases
            .iter()
            .map(|s| (s.input.clone(), s.output.clone()))
            .collect(),
        true,
    )
    .await
    .map_err(|e| Error::ServerError(Json(e.to_string().into())))?;
    println!("{:?}", result);

    let err = result
        .iter()
        .find(|&r| !matches!(r.status, JudgeStatus::Accepted));

    if let Some(_e) = err {
        Ok(Json(Response {
            success: true,
            message: "Unaccepted".to_string(),
            data: Some(SubmitResponse {
                status: "Wrong Answer".to_string(),
            }),
        }))
    } else {
        Ok(Json(Response {
            success: true,
            message: "Accepted".to_string(),
            data: Some(SubmitResponse {
                status: "Accepted".to_string(),
            }),
        }))
    }
}

pub fn routes() -> Vec<rocket::Route> {
    use rocket::routes;
    routes![create, get, submit, list]
}