Skip to content

Commit

Permalink
Merge pull request #8 from Jupeyy/pr_name_check
Browse files Browse the repository at this point in the history
Fix player name change check should ignore own player
  • Loading branch information
Jupeyy authored Jan 3, 2025
2 parents ef8d3b2 + 0c245fd commit bdfefe3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 3 additions & 2 deletions game/vanilla/src/entities/character/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ pub mod player {
pub fn contains_key(&self, id: &PlayerId) -> bool {
self.players.get(id).is_some()
}
pub fn any_with_name(&self, name: &str) -> bool {
pub fn any_with_name(&self, except_id: Option<PlayerId>, name: &str) -> bool {
self.players
.values()
.iter()
.filter_map(|(id, p)| (Some(*id) != except_id).then_some(p))
.any(|p| p.player_info.player_info.name.as_str() == name)
}

Expand Down
20 changes: 11 additions & 9 deletions game/vanilla/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,15 +1428,20 @@ pub mod state {
}
}

fn chech_player_info(&self, mut info: NetworkCharacterInfo) -> NetworkCharacterInfo {
fn chech_player_info(
&self,
mut info: NetworkCharacterInfo,
player_id: Option<PlayerId>,
) -> NetworkCharacterInfo {
// check if the name is already in use
let name_exists = |name: &str| {
self.game.stages.values().any(|s| {
s.world
.characters
.values()
.iter()
.filter_map(|(id, c)| (Some(*id) != player_id).then_some(c))
.any(|c| c.player_info.player_info.name.as_str() == name)
}) || self.game.spectator_players.any_with_name(name)
}) || self.game.spectator_players.any_with_name(player_id, name)
};
let mut name = info.name.clone();
if name_exists(name.as_str()) {
Expand Down Expand Up @@ -1917,7 +1922,7 @@ pub mod state {
let player_id = self.id_generator.next_id();
let stage_0_id = self.stage_0_id;

let character_info = self.chech_player_info(client_player_info.info.clone());
let character_info = self.chech_player_info(client_player_info.info.clone(), None);

self.game
.stages
Expand Down Expand Up @@ -2071,7 +2076,7 @@ pub mod state {
version: NonZeroU64,
) {
let old_info = &mut None;
let new_info = self.chech_player_info(info.clone());
let new_info = self.chech_player_info(info.clone(), Some(*id));
let mut stage_id = self.stage_0_id;
if let Some(player) = self.game.players.player(id) {
stage_id = player.stage_id();
Expand Down Expand Up @@ -2138,10 +2143,7 @@ pub mod state {
old_info
.take()
.and_then(|(old_name, old_skin, old_skin_info)| {
(old_name != new_info.name
|| old_skin != new_info.skin
|| old_skin_info != new_info.skin_info)
.then_some((old_name, old_skin, old_skin_info))
(old_name != new_info.name).then_some((old_name, old_skin, old_skin_info))
})
{
let stage = self.game.stages.get(&stage_id).unwrap();
Expand Down

0 comments on commit bdfefe3

Please sign in to comment.