Skip to content

Commit

Permalink
feat: add points on completing quest and achievement
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed Nov 7, 2023
1 parent aa77bc6 commit 29e4464
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 6 deletions.
39 changes: 38 additions & 1 deletion src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub_struct!(Debug, Serialize, Deserialize; QuestDocument {
expiry_timestamp: Option<String>,
mandatory_domain: Option<String>,
expired: Option<bool>,
experience: i64,
});

pub_struct!(Deserialize; CompletedTasks {
Expand Down Expand Up @@ -97,7 +98,7 @@ pub_struct!(Debug, Serialize, Deserialize; AchievementDocument {
done_title: String,
done_desc: String,
verify_type: String,
verify_endpoint: String,
experience:i64,
});

pub_struct!(Debug, Serialize, Deserialize; AchievementCategoryDocument {
Expand All @@ -119,6 +120,42 @@ pub struct UserAchievements {
achievements: Vec<UserAchievement>,
}

pub_struct!(Debug, Serialize, Deserialize ; UserExperienceDocument {
address: String,
timestamp: i64,
experience: i64,
});

#[derive(Debug, Serialize, Deserialize)]
pub struct UserExperience {
address: String,
timestamp: f64,
experience: i64,
}

pub_struct!(Deserialize; Task {
id: u32,
quest_id: u32,
name: String,
href: String,
cta: String,
verify_endpoint: String,
desc: String,
verify_endpoint_type: String,
});

#[derive(Debug, Serialize, Deserialize)]
pub struct TaskDocument {
pub id: u32,
pub quest_id: u32,
name: String,
href: String,
cta: String,
verify_endpoint: String,
desc: String,
verify_endpoint_type: String,
}

pub fn default_category_disabled() -> bool {
false
}
Expand Down
99 changes: 94 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::{AchievementDocument, AppState, CompletedTasks};
use futures::TryStreamExt;
use crate::models::{AchievementDocument, AppState, CompletedTasks, TaskDocument, QuestDocument};
use async_trait::async_trait;
use axum::{
body::Body,
Expand All @@ -17,6 +18,8 @@ use starknet::{
use std::fmt::Write;
use std::result::Result;
use std::str::FromStr;
use chrono::{Utc};


#[macro_export]
macro_rules! pub_struct {
Expand Down Expand Up @@ -118,8 +121,70 @@ impl CompletedTasksTrait for AppState {

let result = completed_tasks_collection
.update_one(filter, update, options)
.await;
result
.await?;

match &result.upserted_id {
Some(id) => {
// lookup from the tasks collection and get quest id
let tasks_collection: Collection<TaskDocument> = self.db.collection("tasks");
let filter = doc! { "id": task_id };
let mut quest_id = 0;
let mut cursor = tasks_collection.find(filter, None).await?;
while let Some(doc) = cursor.try_next().await? {
quest_id = doc.quest_id;
}

// get total tasks for a specific quest id from task collection
let filter = doc! { "quest_id": quest_id };
let mut total_tasks = Vec::new();
let mut cursor = tasks_collection.find(filter, None).await?;
while let Some(doc) = cursor.try_next().await? {
total_tasks.push(doc.id);
}

// get total experience for a specific quest id from quest collection
let quests_collection: Collection<QuestDocument> = self.db.collection("quests");
let filter = doc! { "id": quest_id };
let mut experience: i32 = 0;
let mut cursor = quests_collection.find(filter, None).await?;
while let Some(doc) = cursor.try_next().await? {
experience = doc.experience as i32;
}


// flag value to check if quest completed (initially we assume it is and then check if any task is not completed)
let mut result = true;

// get completed tasks for a specific quest id from completed_tasks collection
for &item in total_tasks.iter() {
let filter = doc! { "address": addr.to_string(),"task_id": item };
match completed_tasks_collection.find(filter, None).await {
Ok(mut cursor) => {
if cursor.try_next().await?.is_none() {
result = false;
break;
}
}
Err(e) => {
result = false
}
}
}

// save the user_exp document in the collection if the quest is completed
if result == true {
// save the user_exp document in the collection
let user_exp_collection = self.db.collection("user_exp");
// add doc with address ,experience and timestamp
let timestamp: f64 = Utc::now().timestamp_millis() as f64;
let document = doc! { "address": addr.to_string(), "experience":experience, "timestamp":timestamp};
user_exp_collection.insert_one(document, None).await?;
}
}
None => {}
}

Ok(result)
}
}

Expand Down Expand Up @@ -162,8 +227,32 @@ impl AchievementsTrait for AppState {

let result = achieved_collection
.update_one(filter, update, options)
.await;
result
.await?;


match &result.upserted_id {
Some(id) => {
// Check if the document was modified
let achievement_collection: Collection<AchievementDocument> = self.db.collection("achievements");
// Define a query using the `doc!` macro.
let query = doc! { "id": achievement_id };
let mut experience: i32 = 0;

let mut cursor = achievement_collection.find(query, None).await?;
// Iterate over the results.
while let Some(doc) = cursor.try_next().await? {
experience = doc.experience as i32;
}

let user_exp_collection = self.db.collection("user_exp");
// add doc with address ,experience and timestamp
let timestamp: f64 = Utc::now().timestamp_millis() as f64;
let document = doc! { "address": addr.to_string(), "experience":experience, "timestamp":timestamp};
let yay = user_exp_collection.insert_one(document, None).await?;
}
None => {}
}
Ok(result)
}

async fn get_achievement(
Expand Down

0 comments on commit 29e4464

Please sign in to comment.