-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from starknet-id/feat/add_achievement_snq
feat: achievement completed quests
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 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
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)), | ||
} | ||
} |
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