You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.6 KiB
Rust
152 lines
3.6 KiB
Rust
use knowfoolery_shared::models::*;
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct AuthResponse {
|
|
pub token: String,
|
|
pub player: PlayerResponse,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct PlayerResponse {
|
|
pub id: String,
|
|
pub email: String,
|
|
pub name: String,
|
|
pub role: String,
|
|
}
|
|
|
|
impl From<&Player> for PlayerResponse {
|
|
fn from(p: &Player) -> Self {
|
|
Self {
|
|
id: p.id.clone(),
|
|
email: p.email.clone(),
|
|
name: p.name.clone(),
|
|
role: p.role.as_str().to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SessionResponse {
|
|
pub id: String,
|
|
pub status: String,
|
|
pub score: i32,
|
|
pub questions_answered: i32,
|
|
pub correct_answers: i32,
|
|
pub time_remaining: i64,
|
|
}
|
|
|
|
impl SessionResponse {
|
|
pub fn from_session(s: &Session, time_remaining: i64) -> Self {
|
|
Self {
|
|
id: s.id.clone(),
|
|
status: s.status.as_str().to_string(),
|
|
score: s.score,
|
|
questions_answered: s.questions_answered,
|
|
correct_answers: s.correct_answers,
|
|
time_remaining,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct QuestionResponse {
|
|
pub id: String,
|
|
pub theme: String,
|
|
pub text: String,
|
|
pub session_question_id: String,
|
|
pub attempts_used: i32,
|
|
pub attempts_remaining: i32,
|
|
pub hint_used: bool,
|
|
pub time_remaining: i64,
|
|
pub score: i32,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct HintResponse {
|
|
pub hint: String,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct NextQuestionResponse {
|
|
pub game_over: bool,
|
|
pub question: Option<QuestionResponse>,
|
|
pub session: SessionResponse,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct StartSessionResponse {
|
|
pub session: SessionResponse,
|
|
pub question: QuestionResponse,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct LeaderboardResponse {
|
|
pub rank: i32,
|
|
pub player_name: String,
|
|
pub score: i32,
|
|
pub questions_answered: i32,
|
|
pub correct_answers: i32,
|
|
pub success_rate: f64,
|
|
pub duration_seconds: i64,
|
|
}
|
|
|
|
impl From<LeaderboardEntry> for LeaderboardResponse {
|
|
fn from(entry: LeaderboardEntry) -> Self {
|
|
Self {
|
|
rank: entry.rank,
|
|
player_name: entry.player_name,
|
|
score: entry.score,
|
|
questions_answered: entry.questions_answered,
|
|
correct_answers: entry.correct_answers,
|
|
success_rate: entry.success_rate,
|
|
duration_seconds: entry.duration_seconds,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct AdminQuestionResponse {
|
|
pub id: String,
|
|
pub theme: String,
|
|
pub text: String,
|
|
pub answer: String,
|
|
pub hint: Option<String>,
|
|
pub difficulty: Option<String>,
|
|
}
|
|
|
|
impl From<&Question> for AdminQuestionResponse {
|
|
fn from(q: &Question) -> Self {
|
|
Self {
|
|
id: q.id.clone(),
|
|
theme: q.theme.clone(),
|
|
text: q.text.clone(),
|
|
answer: q.answer.clone(),
|
|
hint: q.hint.clone(),
|
|
difficulty: q.difficulty.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ErrorResponse {
|
|
pub error: ErrorBody,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ErrorBody {
|
|
pub code: String,
|
|
pub message: String,
|
|
}
|