You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.6 KiB

use std::collections::HashMap;
use std::convert::TryFrom;
use std::error::Error;
use rusqlite::Row;
use serde_derive::{Deserialize, Serialize};
use crate::id::Id;
use crate::try_log;
use crate::world::{Area, AreaType, Direction, Exit, DIRECTION_LIST};
#[derive(Debug, Deserialize, Serialize)]
pub struct Room {
pub id: Id,
pub zone: Id,
pub name: String,
pub description: Vec<String>,
pub users_visible: bool,
pub exits: HashMap<Direction, Exit>,
}
impl Room {
pub fn exit_string(&self) -> String {
if self.exits.is_empty() {
return String::new();
}
DIRECTION_LIST
.iter()
.filter_map(|(_, direction)| {
if self.exits.contains_key(direction) {
Some(direction.short())
} else {
None
}
})
.collect::<Vec<&str>>()
.join(" ")
}
}
impl Area for Room {
fn id(&self) -> Id {
self.id
}
fn parent(&self) -> Id {
self.zone
}
fn name(&self) -> String {
self.name.to_owned()
}
fn visible(&self) -> bool {
self.users_visible
}
fn area_type(&self) -> AreaType {
AreaType::Room
}
}
impl<'a> TryFrom<&Row<'a>> for Room {
type Error = Box<dyn Error>;
fn try_from(row: &Row) -> Result<Self, Self::Error> {
//let orig_id: String = try_log!(row.get("id"), "id");
//let new_id: Uuid = try_log!(Uuid::parse_str(&orig_id), "parse");
Ok(Self {
id: try_log!(row.get("id"), "id"),
zone: try_log!(row.get("zone"), "zone"),
name: try_log!(row.get("name"), "name"),
description: try_log!(row.get::<&str, String>("description"), "description")
.lines()
.map(|s| String::from(s))
.collect(),
users_visible: try_log!(row.get("users_visible"), "users_visible"),
exits: HashMap::new(),
})
}
}