-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,87 @@ | ||
use std::collections::HashMap; | ||
use crate::model::generated::prelude::Answer; | ||
use crate::model::question::Question; | ||
use tracing::debug; | ||
use crate::model::question::{Question, QuestionType}; | ||
|
||
pub async fn judge_subjectives(questions: Vec<Question>, answer: HashMap<String, String>) { | ||
// let mut score = 0; | ||
// for question in questions { | ||
// if let Some(ans) = answer.get(&question.id) { | ||
// if ans == question.answer { | ||
// score += 1; | ||
// } | ||
// } | ||
// } | ||
// score | ||
pub async fn judge_subjectives(questions: Vec<Question>, answer: HashMap<String, String>) -> i32 { | ||
let mut score = 0; | ||
let mut full_score = 0; | ||
|
||
for question in questions { | ||
if let Some(ans) = question.answer { | ||
full_score += ans.all_points; | ||
|
||
match question.r#type { | ||
QuestionType::MultipleChoice => { | ||
let point = judge_multiple_choice(&ans.answer, answer.get(&question.id.to_string()).unwrap()); | ||
match point { | ||
PointType::All => { | ||
score += ans.all_points; | ||
} | ||
PointType::Sub => { | ||
if let Some(sub) = ans.sub_points { | ||
score += sub; | ||
} | ||
} | ||
PointType::None => {} | ||
} | ||
} | ||
QuestionType::SingleChoice => { | ||
let point = judge_single_choice(&ans.answer, answer.get(&question.id.to_string()).unwrap()); | ||
match point { | ||
PointType::All => { | ||
score += ans.all_points; | ||
} | ||
PointType::None => {} | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
score | ||
} | ||
|
||
pub fn judge_multiple_choice(answer: &str, user: &str) -> PointType { | ||
let answer: Vec<String> = serde_json::from_str(answer).unwrap(); | ||
let user: Vec<String> = serde_json::from_str(user).unwrap(); | ||
debug!("answer: {:?}, user: {:?}", answer, user); | ||
let mut missing_flag = false; | ||
let mut wrong_flag = false; | ||
|
||
for ans in &answer { | ||
if !user.contains(ans) { | ||
missing_flag = true; | ||
break; | ||
} | ||
} | ||
|
||
for u in &user { | ||
if !answer.contains(u) { | ||
wrong_flag = true; | ||
break; | ||
} | ||
} | ||
|
||
if wrong_flag { | ||
PointType::None | ||
} else if missing_flag { | ||
PointType::Sub | ||
} else { | ||
PointType::All | ||
} | ||
} | ||
|
||
pub fn judge_single_choice(answer: &str, user: &str) -> PointType { | ||
if answer == user { | ||
PointType::All | ||
} else { | ||
PointType::None | ||
} | ||
} | ||
|
||
pub enum PointType { | ||
All, | ||
Sub, | ||
None, | ||
} |