Automatically select team with least players

This commit is contained in:
Sofia 2026-07-22 01:48:30 +03:00
parent 05bf6401e0
commit f3a5a18400

View File

@ -326,6 +326,28 @@ impl Game {
character: None, character: None,
}); });
self.run_deferred(move |s| s.signals().on_new_player().emit(id)); self.run_deferred(move |s| s.signals().on_new_player().emit(id));
let team_with_least = self
.get_teams()
.iter_shared()
.enumerate()
.min_by(|(team_idx_a, _), (team_idx_b, _)| {
let a = self
.players
.iter()
.filter(|p| p.data.team == *team_idx_a as u8)
.count();
let b = self
.players
.iter()
.filter(|p| p.data.team == *team_idx_b as u8)
.count();
a.cmp(&b)
})
.map(|(team_idx, _)| team_idx)
.unwrap_or(0);
self.try_set_player_team(id, team_with_least as u8, true);
id id
} }