nil_core/world/player/
mod.rs1#[cfg(test)]
5mod tests;
6
7use crate::city::City;
8use crate::error::{Error, Result};
9use crate::military::Military;
10use crate::player::{Player, PlayerId, PlayerManager, PlayerStatus};
11use crate::report::ReportId;
12use crate::resources::maintenance::Maintenance;
13use crate::world::World;
14
15impl World {
16 #[inline]
17 pub fn player_manager(&self) -> &PlayerManager {
18 &self.player_manager
19 }
20
21 #[inline]
22 pub fn player(&self, id: &PlayerId) -> Result<&Player> {
23 self.player_manager.player(id)
24 }
25
26 #[inline]
27 pub(crate) fn player_mut(&mut self, id: &PlayerId) -> Result<&mut Player> {
28 self.player_manager.player_mut(id)
29 }
30
31 pub fn players(&self) -> impl Iterator<Item = &Player> {
32 self.player_manager.players()
33 }
34
35 #[inline]
36 pub fn get_player_maintenance(&self, player: &PlayerId) -> Result<Maintenance> {
37 self.get_maintenance(player)
38 }
39
40 pub fn get_player_military(&self, player: &PlayerId) -> Result<Military> {
41 let coords = self.continent.coords_of(player);
42 self.military.intersection(coords)
43 }
44
45 pub fn get_player_reports(&self, player: &PlayerId) -> Vec<ReportId> {
46 self.report.reports_of(player).collect()
47 }
48
49 #[inline]
50 pub fn has_player(&self, id: &PlayerId) -> bool {
51 self.player_manager.has(id)
52 }
53
54 pub fn has_any_active_player(&self) -> bool {
55 self
56 .player_manager
57 .players()
58 .any(Player::is_active)
59 }
60
61 pub fn set_player_status(&mut self, id: &PlayerId, status: PlayerStatus) -> Result<()> {
62 *self
63 .player_manager
64 .player_mut(id)?
65 .status_mut() = status;
66
67 self.emit_player_updated(id.clone());
68
69 Ok(())
70 }
71
72 pub fn spawn_player(&mut self, mut player: Player) -> Result<()> {
73 let id = player.id();
74 if self.has_player(&id) {
75 Err(Error::PlayerAlreadySpawned(id))
76 } else {
77 let (coord, field) = self.find_spawn_point()?;
78 *field = City::builder(coord)
79 .name(&*id)
80 .owner(&id)
81 .build()
82 .into();
83
84 *player.status_mut() = PlayerStatus::Active;
85 self.player_manager.manage(player);
86
87 self.emit_public_city_updated(coord);
88
89 Ok(())
90 }
91 }
92}