Skip to content

Commit

Permalink
Remove manual saves from guilds
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Aug 14, 2024
1 parent 65ac0ba commit 88234fe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 121 deletions.
76 changes: 25 additions & 51 deletions src/map/map/guild/finish_guild_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use eolib::{
},
};

use crate::{character::Character, SETTINGS};
use crate::SETTINGS;

use super::super::Map;

Expand All @@ -18,37 +18,31 @@ impl Map {
guild_tag: String,
guild_name: String,
) {
let mut guild_characters: Vec<Character> = Vec::with_capacity(SETTINGS.guild.min_players);

{
let character = match self.characters.get_mut(&player_id) {
Some(character) => character,
None => return,
};

character.remove_item(1, SETTINGS.guild.create_cost);
character.guild_tag = Some(guild_tag.clone());
character.guild_name = Some(guild_name.clone());
character.guild_rank_string = Some(SETTINGS.guild.default_leader_rank_name.clone());
character.guild_rank = Some(1);

guild_characters.push(character.to_owned());

self.world.add_guild_member(player_id, guild_tag.clone());
let character = match self.characters.get_mut(&player_id) {
Some(character) => character,
None => return,
};

if let Some(player) = character.player.as_ref() {
player.send(
PacketAction::Create,
PacketFamily::Guild,
&GuildCreateServerPacket {
leader_player_id: player_id,
guild_tag: guild_tag.clone(),
guild_name: guild_name.clone(),
rank_name: SETTINGS.guild.default_leader_rank_name.clone(),
gold_amount: character.get_item_amount(1),
},
);
}
character.remove_item(1, SETTINGS.guild.create_cost);
character.guild_tag = Some(guild_tag.clone());
character.guild_name = Some(guild_name.clone());
character.guild_rank_string = Some(SETTINGS.guild.default_leader_rank_name.clone());
character.guild_rank = Some(1);

self.world.add_guild_member(player_id, guild_tag.clone());

if let Some(player) = character.player.as_ref() {
player.send(
PacketAction::Create,
PacketFamily::Guild,
&GuildCreateServerPacket {
leader_player_id: player_id,
guild_tag: guild_tag.clone(),
guild_name: guild_name.clone(),
rank_name: SETTINGS.guild.default_leader_rank_name.clone(),
gold_amount: character.get_item_amount(1),
},
);
}

let packet = GuildAgreeServerPacket {
Expand Down Expand Up @@ -82,31 +76,11 @@ impl Map {
character.guild_rank_string = Some(SETTINGS.guild.default_new_member_rank_name.clone());
character.guild_rank = Some(9);

guild_characters.push(character.to_owned());

self.world.add_guild_member(*player_id, guild_tag.clone());

if let Some(player) = character.player.as_ref() {
player.send_buf(PacketAction::Agree, PacketFamily::Guild, buf.clone());
}
}

let pool = self.pool.clone();

tokio::spawn(async move {
let mut conn = match pool.get_conn().await {
Ok(conn) => conn,
Err(e) => {
error!("Error getting connection from pool: {}", e);
return;
}
};

for character in guild_characters.iter_mut() {
character.save(&mut conn).await.unwrap_or_else(|e| {
error!("Error saving character: {}", e);
});
}
});
}
}
19 changes: 0 additions & 19 deletions src/map/map/guild/join_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,5 @@ impl Map {
},
);
}

// TODO: This is potentially unsafe if character changes before being saved..
// Possible duping hazard?
let mut character = character.to_owned();
let pool = self.pool.clone();

tokio::spawn(async move {
let mut conn = match pool.get_conn().await {
Ok(conn) => conn,
Err(e) => {
error!("Error getting connection from pool: {}", e);
return;
}
};

if let Err(e) = character.save(&mut conn).await {
error!("Error saving character: {}", e);
}
});
}
}
17 changes: 0 additions & 17 deletions src/map/map/guild/kick_from_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,5 @@ impl Map {
&GuildKickServerPacket::default(),
);
}

let mut character = character.to_owned();
let pool = self.pool.clone();

tokio::spawn(async move {
let mut conn = match pool.get_conn().await {
Ok(conn) => conn,
Err(e) => {
error!("Error getting connection from pool: {}", e);
return;
}
};

if let Err(e) = character.save(&mut conn).await {
error!("Error saving character: {}", e);
}
});
}
}
17 changes: 0 additions & 17 deletions src/map/map/guild/leave_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,5 @@ impl Map {
character.guild_rank_string = None;

self.world.remove_guild_member(player_id, guild_tag);

let mut character = character.to_owned();
let pool = self.pool.clone();

tokio::spawn(async move {
let mut conn = match pool.get_conn().await {
Ok(conn) => conn,
Err(e) => {
error!("Error getting connection from pool: {}", e);
return;
}
};

if let Err(e) = character.save(&mut conn).await {
error!("Error saving character: {}", e);
}
});
}
}
17 changes: 0 additions & 17 deletions src/map/map/guild/update_guild_rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,5 @@ impl Map {
&GuildAcceptServerPacket { rank },
);
}

let mut character = character.to_owned();
let pool = self.pool.clone();

tokio::spawn(async move {
let mut conn = match pool.get_conn().await {
Ok(conn) => conn,
Err(e) => {
error!("Error getting connection from pool: {}", e);
return;
}
};

if let Err(e) = character.save(&mut conn).await {
error!("Error saving character: {}", e);
}
});
}
}

0 comments on commit 88234fe

Please sign in to comment.