Skip to content

Commit

Permalink
Announce first player to complete an achievement (#2)
Browse files Browse the repository at this point in the history
* Announce first player to complete an achievement

* PR Fixes

* serde default field
  • Loading branch information
Mew2K authored Jul 13, 2024
1 parent 30cb878 commit 0fd2de4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/database/models/achievement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub struct Achievement {
pub description: String,
#[serde(rename = "category")]
pub metadata: Option<AchievementMetadata>,
pub agent: Agent
pub agent: Agent,
#[serde(default)]
pub first_completion: Option<String>
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion src/http/achievements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async fn add_achievement(
name,
description,
metadata,
agent
agent,
first_completion: None
};
state.database.save::<Achievement>(&new_achievement).await;
return Ok(JsonResponder::from(new_achievement, Status::Ok));
Expand Down
2 changes: 1 addition & 1 deletion src/socket/player/player_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct DisconnectPlayerData {
pub reason: String
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PlayerAchievementData {
pub player: SimplePlayer,
Expand Down
27 changes: 20 additions & 7 deletions src/socket/socket_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{collections::HashMap};


use futures::future::join_all;
use mongodb::bson::doc;
use mongodb::options::UpdateOptions;
use rocket::serde::{json::{serde_json, Value}, DeserializeOwned};

use uuid::Uuid;
Expand Down Expand Up @@ -655,19 +657,30 @@ impl SocketRouter {
}

async fn on_achievement_complete(&mut self, data: PlayerAchievementData) -> Result<(), SocketError> {
// confirm the achievement exists
let achievement_exists = Database::find_by_id(&self.server.api_state.database.achievements, data.achievement_id.as_str()).await.is_some();
if !achievement_exists {
let achievement_id = data.achievement_id.as_str();
let database = &self.server.api_state.database;
let collection = &database.achievements;

if let Some(mut achievement) = Database::find_by_id(collection, achievement_id).await {
if achievement.first_completion.is_none() {
achievement.first_completion = Some(data.player.id.clone());
database.save(&achievement).await;
}
} else {
return Ok(());
}

let mut player = unwrap_helper::return_default!(
self.server.api_state.player_cache.get(&self.server.api_state.database, data.player.name.as_str()).await,
Ok(())
);
self.server.api_state.player_cache.get(database, data.player.name.as_str()).await,
Ok(())
);

player.stats.achievements.insert(data.achievement_id.clone(), AchievementData {
completion_time: data.completion_time
});
self.server.api_state.player_cache.set(&self.server.api_state.database, &player.name, &player, true).await;

self.server.api_state.player_cache.set(database, &player.name, &player, true).await;

Ok(())
}

Expand Down

0 comments on commit 0fd2de4

Please sign in to comment.