Skip to content

Commit

Permalink
Merge pull request #116 from starknet-id/feat/add_achievement_snq
Browse files Browse the repository at this point in the history
feat: achievement completed quests
  • Loading branch information
Th0rgal authored Oct 28, 2023
2 parents f45c47f + b264397 commit 6f1b694
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/endpoints/achievements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod fetch;
pub mod fetch_buildings;
pub mod verify_achieved_quests;
pub mod verify_avnu;
pub mod verify_briq;
pub mod verify_default;
Expand Down
65 changes: 65 additions & 0 deletions src/endpoints/achievements/verify_achieved_quests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::sync::Arc;

use crate::{
models::{AppState, VerifyAchievementQuery},
utils::{get_error, AchievementsTrait},
};
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use serde_json::json;
use starknet::core::types::FieldElement;

pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
) -> impl IntoResponse {
let addr = query.addr;
if addr == FieldElement::ZERO {
return get_error("Please connect your wallet first".to_string());
}

let achievement_id = query.id;
if !(20..=22).contains(&achievement_id) {
return get_error("Invalid achievement id".to_string());
}

let url = format!(
"{}/get_completed_quests?addr={}",
state.conf.variables.api_link, addr
);
let client = reqwest::Client::new();
match client.get(&url).send().await {
Ok(response) => match response.json::<Vec<u32>>().await {
Ok(quests) => {
println!("quests: {:?}", quests);
if quests.is_empty() {
return get_error("You have not completed any quests.".to_string());
}

if (achievement_id == 20 && quests.len() >= 5)
|| (achievement_id == 21 && quests.len() >= 10)
|| (achievement_id == 22 && quests.len() >= 20)
{
match state
.upsert_completed_achievement(addr, achievement_id)
.await
{
Ok(_) => (StatusCode::OK, Json(json!({"achieved": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
}
} else {
get_error("You have not completed enough quests.".to_string())
}
}
Err(e) => get_error(format!(
"Failed to get JSON response from Starkscan api: {}",
e
)),
},
Err(e) => get_error(format!("Failed to fetch Starkscan api: {}", e)),
}
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ async fn main() {
"/achievements/verify_has_domain",
get(endpoints::achievements::verify_has_domain::handler),
)
.route(
"/achievements/verify_achieved_quests",
get(endpoints::achievements::verify_achieved_quests::handler),
)
.route(
"/achievements/fetch",
get(endpoints::achievements::fetch::handler),
Expand Down

0 comments on commit 6f1b694

Please sign in to comment.