use crate::RoomId;
use anyhow::{Result, bail};
use log::{info, warn};
use crate::player::{Flag, Player};
use crate::view::{StatusAction, View, ViewItem};
use crate::world::AmbleWorld;
pub fn reset_flag(player: &mut Player, flag_name: &str) {
info!("└─ action: ResetFlag(\"{flag_name}\")");
player.reset_flag(flag_name);
}
pub fn advance_flag(player: &mut Player, flag_name: &str) {
info!("└─ action: AdvanceFlag(\"{flag_name}\")");
player.advance_flag(flag_name);
}
pub fn remove_flag(world: &mut AmbleWorld, view: &mut View, flag: &str) {
remove_flag_with_priority(world, view, flag, None);
}
pub(super) fn remove_flag_with_priority(world: &mut AmbleWorld, view: &mut View, flag: &str, priority: Option<isize>) {
let target = Flag::simple(flag, 0);
if world.player.flags.remove(&target) {
info!("└─ action: RemoveFlag(\"{flag}\")");
if let Some(status) = flag.strip_prefix("status:") {
view.push_with_custom_priority(
ViewItem::StatusChange {
action: StatusAction::Remove,
status: status.to_string(),
},
priority,
);
}
} else {
warn!("└─ action: RemoveFlag(\"{flag}\") - flag was not set");
}
}
pub fn award_points(world: &mut AmbleWorld, view: &mut View, amount: isize, reason: &str) {
award_points_with_priority(world, view, amount, reason, None);
}
pub(super) fn award_points_with_priority(
world: &mut AmbleWorld,
view: &mut View,
amount: isize,
reason: &str,
priority: Option<isize>,
) {
world.player.score = world.player.score.saturating_add_signed(amount);
info!("└─ action: AwardPoints({amount}, reason: {reason})");
view.push_with_custom_priority(
ViewItem::PointsAwarded {
amount,
reason: reason.to_string(),
},
priority,
);
}
pub fn add_flag(world: &mut AmbleWorld, view: &mut View, flag: &Flag) {
add_flag_with_priority(world, view, flag, None);
}
pub(super) fn add_flag_with_priority(world: &mut AmbleWorld, view: &mut View, flag: &Flag, priority: Option<isize>) {
if let Some(status) = flag.name().strip_prefix("status:") {
view.push_with_custom_priority(
ViewItem::StatusChange {
action: StatusAction::Apply,
status: status.to_string(),
},
priority,
);
}
world.player.flags.insert(flag.clone());
info!("└─ action: AddFlag(\"{flag}\")");
}
pub fn push_player(world: &mut AmbleWorld, room_id: &RoomId) -> Result<()> {
if world.rooms.contains_key(room_id) {
world.player.move_to_room(room_id.clone());
world.rooms.get_mut(room_id).expect("room_id validated above").visited = true;
info!("└─ action: PushPlayerTo({room_id})");
Ok(())
} else {
bail!("tried to push player to unknown room ({room_id})");
}
}