From 22ba73d7f70c0bbba633f0c41b16bab544de4ebd Mon Sep 17 00:00:00 2001 From: phil Date: Tue, 9 Sep 2025 17:31:07 -0400 Subject: [PATCH] flatten day1 part1 challenge check --- shared/src/advent/challenges/day_one.rs | 143 +++++++++++------------- shared/src/advent/mod.rs | 5 +- web/src/handlers/day.rs | 4 +- 3 files changed, 69 insertions(+), 83 deletions(-) diff --git a/shared/src/advent/challenges/day_one.rs b/shared/src/advent/challenges/day_one.rs index 954a391..56cc7c9 100644 --- a/shared/src/advent/challenges/day_one.rs +++ b/shared/src/advent/challenges/day_one.rs @@ -2,6 +2,7 @@ use crate::OAuthAgentType; use crate::advent::day::Day; use crate::advent::{AdventChallenge, AdventError, ChallengeCheckResponse}; use crate::atrium::safe_check_unknown_record_parse; +use crate::lexicons::codes::advent; use async_trait::async_trait; use atrium_api::types::Collection; use sqlx::PgPool; @@ -30,83 +31,71 @@ impl AdventChallenge for DayOne { did: String, _verification_code: Option, ) -> Result { - match &self.oauth_client { - None => Err(AdventError::ShouldNotHappen( + let client = self + .oauth_client + .as_ref() + .ok_or(AdventError::ShouldNotHappen( "No oauth client. This should not happen".to_string(), - )), - Some(client) => { - match client - .api - .com - .atproto - .repo - .get_record( - atrium_api::com::atproto::repo::get_record::ParametersData { - cid: None, - collection: crate::lexicons::codes::advent::challenge::Day::NSID - .parse() - .unwrap(), - repo: did.parse().unwrap(), - rkey: "1".parse().unwrap(), - } - .into(), - ) - .await - { - Ok(record) => { - //TODO trouble, and make it double - let challenge = self.get_days_challenge(did.clone()).await?; - - match challenge { - None => { - log::error!( - "Could not find a challenge record for day: {} for the user: {}", - self.day(), - did.clone() - ); - Err(AdventError::ShouldNotHappen( - "Could not find a challenge record".to_string(), - )) - } - Some(challenge) => { - let parse_record_result = - safe_check_unknown_record_parse::< - crate::lexicons::codes::advent::challenge::day::RecordData, - >(record.value.clone()); + ))?; - match parse_record_result { - Ok(record_data) => { - match record_data.part_one - == challenge - .verification_code_one - .unwrap_or("".to_string()) - { - true => Ok(ChallengeCheckResponse::Correct), - false => { - Ok(ChallengeCheckResponse::Incorrect(format!( - "The code {} is incorrect", - record_data.part_one - ))) - } - } - } - Err(err) => { - log::error!("Error parsing record: {}", err); - Ok(ChallengeCheckResponse::Incorrect(format!( - "There is a record at the correct location, but it does not seem like it is correct. Try again:\n{err}" - ))) - } - } - } - } - } - Err(err) => { - log::error!("Error getting record: {}", err); - Ok(ChallengeCheckResponse::Incorrect("Does not appear to be a record in your repo in the collection codes.advent.challenge.day with the record key of 1".to_string())) - } + let record_res = client + .api + .com + .atproto + .repo + .get_record( + atrium_api::com::atproto::repo::get_record::ParametersData { + cid: None, + collection: advent::challenge::Day::NSID.parse().unwrap(), + repo: did.parse().unwrap(), + rkey: "1".parse().unwrap(), } + .into(), + ) + .await; + + let record = match record_res { + Ok(r) => r, + Err(e) => { + log::error!("Error getting record: {}", e); + return Ok(ChallengeCheckResponse::Incorrect("Does not appear to be a record in your repo in the collection codes.advent.challenge.day with the record key of 1".to_string())); } - } + }; + + let Some(challenge) = self.get_days_challenge(&did).await? else { + log::error!("Could not find a challenge record for day: 1 for the user: {did:?}"); + return Err(AdventError::ShouldNotHappen( + "Could not find challenge record".to_string(), + )); + }; + + let record_data = match safe_check_unknown_record_parse::( + record.value.clone(), + ) { + Ok(rd) => rd, + Err(e) => { + log::error!("Error parsing record: {e}"); + return Ok(ChallengeCheckResponse::Incorrect(format!( + "There is a record at the correct location, but it does not seem like it is correct. Try again:\n{e}" + ))); + } + }; + + let verification_code = + challenge + .verification_code_one + .ok_or(AdventError::ShouldNotHappen( + "no verification code for day 1 challenge :/".to_string(), + ))?; + + Ok(if record_data.part_one == verification_code { + ChallengeCheckResponse::Correct + } else { + ChallengeCheckResponse::Incorrect(format!( + "The code {} is incorrect", + record_data.part_one + )) + }) } ///TODO this is just a straight copy and paste of part one since it's a proof of concept needs to share code better between the two @@ -128,9 +117,7 @@ impl AdventChallenge for DayOne { .get_record( atrium_api::com::atproto::repo::get_record::ParametersData { cid: None, - collection: crate::lexicons::codes::advent::challenge::Day::NSID - .parse() - .unwrap(), + collection: advent::challenge::Day::NSID.parse().unwrap(), repo: did.parse().unwrap(), rkey: "1".parse().unwrap(), } @@ -140,7 +127,7 @@ impl AdventChallenge for DayOne { { Ok(record) => { //TODO trouble, and make it double - let challenge = self.get_days_challenge(did.clone()).await?; + let challenge = self.get_days_challenge(&did).await?; match challenge { None => { @@ -156,7 +143,7 @@ impl AdventChallenge for DayOne { Some(challenge) => { let parse_record_result = safe_check_unknown_record_parse::< - crate::lexicons::codes::advent::challenge::day::RecordData, + advent::challenge::day::RecordData, >(record.value.clone()); match parse_record_result { diff --git a/shared/src/advent/mod.rs b/shared/src/advent/mod.rs index 516fa2b..6a075ca 100644 --- a/shared/src/advent/mod.rs +++ b/shared/src/advent/mod.rs @@ -186,14 +186,13 @@ pub trait AdventChallenge { async fn get_days_challenge( &self, - did: String, + did: &str, ) -> Result, AdventError> { - let day = self.day(); Ok(sqlx::query_as::<_, ChallengeProgress>( "SELECT * FROM challenges WHERE user_did = $1 AND day = $2", ) .bind(did) - .bind(day as i16) + .bind(self.day() as i16) .fetch_optional(self.pool()) .await?) } diff --git a/web/src/handlers/day.rs b/web/src/handlers/day.rs index bb27bd5..c4a28d8 100644 --- a/web/src/handlers/day.rs +++ b/web/src/handlers/day.rs @@ -83,7 +83,7 @@ pub async fn view_day_handler( .markdown_text_part_one(None) .map(|s| s.to_string()) .unwrap_or_else(|_| "Error loading part one".to_string()), - Some(ref users_did) => match challenge.get_days_challenge(users_did.clone()).await { + Some(ref users_did) => match challenge.get_days_challenge(&users_did).await { Ok(current_challenge) => match current_challenge { None => { let new_code = challenge @@ -186,7 +186,7 @@ async fn get_part_two_text( .markdown_text_part_two(None) .map(|opt| opt.map(|s| s.to_string())) .unwrap_or(None), - Some(users_did) => match challenge.get_days_challenge(users_did.clone()).await { + Some(users_did) => match challenge.get_days_challenge(&users_did).await { Ok(current_challenge) => match current_challenge { None => { if challenge.has_part_two() { -- 2.43.0