diff --git a/src/environment.rs b/src/environment.rs index ba0bb91..cb45478 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -5,122 +5,122 @@ use serde_derive::Deserialize; /// Type of environment for the room #[derive(Debug, Deserialize)] pub enum Environment { - /// Inside a building - Inside, + /// Inside a building + Inside, - /// Out in the wild - Wilderness, + /// Out in the wild + Wilderness, - /// On a road - Road, + /// On a road + Road, - /// Horse load - Horse, + /// Horse load + Horse, - /// In a swamp - Swamp, + /// In a swamp + Swamp, - /// In the water - Water, + /// In the water + Water, - /// A drinking source - Drink, + /// A drinking source + Drink, - /// Weapon shop - Weaponsmith, + /// Weapon shop + Weaponsmith, - /// Armor shop - Armorer, + /// Armor shop + Armorer, - /// Master blacksmith - Blacksmith, + /// Master blacksmith + Blacksmith, - /// Bank - Bank, + /// Bank + Bank, - /// Stables - Stables, + /// Stables + Stables, - /// Place to rent - Rent, + /// Place to rent + Rent, - /// Grocer - Grocer, + /// Grocer + Grocer, - /// Warrior practice - Warrior, + /// Warrior practice + Warrior, - /// Rogue practice - Rogue, + /// Rogue practice + Rogue, - /// Hunter practice - Hunter, + /// Hunter practice + Hunter, - /// Possible PK objective - Pk, + /// Possible PK objective + Pk, - /// Other - Other, + /// Other + Other, } impl From for Environment { - fn from(num: i64) -> Self { - match num { - 20 => Self::Inside, - 21 => Self::Wilderness, - 22 => Self::Road, - 23 => Self::Horse, - 24 => Self::Swamp, - 25 => Self::Water, - 26 => Self::Drink, - 27 => Self::Weaponsmith, - 28 => Self::Armorer, - 29 => Self::Blacksmith, - 30 => Self::Bank, - 31 => Self::Stables, - 32 => Self::Rent, - 33 => Self::Grocer, - 34 => Self::Warrior, - 35 => Self::Rogue, - 36 => Self::Hunter, - 37 => Self::Pk, - _ => Self::Other, - } - } + fn from(num: i64) -> Self { + match num { + 20 => Self::Inside, + 21 => Self::Wilderness, + 22 => Self::Road, + 23 => Self::Horse, + 24 => Self::Swamp, + 25 => Self::Water, + 26 => Self::Drink, + 27 => Self::Weaponsmith, + 28 => Self::Armorer, + 29 => Self::Blacksmith, + 30 => Self::Bank, + 31 => Self::Stables, + 32 => Self::Rent, + 33 => Self::Grocer, + 34 => Self::Warrior, + 35 => Self::Rogue, + 36 => Self::Hunter, + 37 => Self::Pk, + _ => Self::Other, + } + } } impl Environment { - /// Some room environments have a symbol to put in the room on the map - pub fn symbol(&self) -> String { - match self { - Self::Horse => "H", - Self::Drink => "D", - Self::Weaponsmith => "W", - Self::Armorer => "A", - Self::Blacksmith => "B", - Self::Bank => "$", - Self::Stables => "S", - Self::Rent => "R", - Self::Grocer => "G", - Self::Warrior => "W", - Self::Rogue => "R", - Self::Hunter => "H", - Self::Pk => "P", - _ => "", - } - .to_string() - } - - /// Color of the room - pub fn color(&self) -> String { - match self { - Self::Inside => "", - Self::Wilderness => "", - Self::Road => "", - Self::Swamp => "", - Self::Water => "", - _ => "", - } - .to_string() - } + /// Some room environments have a symbol to put in the room on the map + pub fn symbol(&self) -> String { + match self { + Self::Horse => "H", + Self::Drink => "D", + Self::Weaponsmith => "W", + Self::Armorer => "A", + Self::Blacksmith => "B", + Self::Bank => "$", + Self::Stables => "S", + Self::Rent => "R", + Self::Grocer => "G", + Self::Warrior => "W", + Self::Rogue => "R", + Self::Hunter => "H", + Self::Pk => "P", + _ => "", + } + .to_string() + } + + /// Color of the room + pub fn color(&self) -> String { + match self { + Self::Inside => "", + Self::Wilderness => "", + Self::Road => "", + Self::Swamp => "", + Self::Water => "", + _ => "", + } + .to_string() + } } diff --git a/src/main.rs b/src/main.rs index c7dc30b..d893f21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,118 +12,118 @@ use serde_json::Value; use environment::Environment; fn main() -> Result<(), Box> { - let mut buffer = String::new(); - let mut stdin = io::stdin(); - stdin.read_to_string(&mut buffer)?; - - let mudlet_map: Value = serde_json::from_str(&buffer)?; - - let areas = match mudlet_map.get("areas") { - Some(areas) => match areas.as_array() { - Some(areas) => areas, - None => panic!("Areas not an array"), - }, - None => panic!("No areas found"), - }; - - for area in areas { - let id: i64 = match area.get("id") { - Some(id) => match id.as_i64() { - Some(id) => id, - None => continue, - }, - None => continue, - }; - - if id < 0 { - continue; - } - - let mudlet_area: mudlet::Area = serde_json::from_value(area.to_owned())?; - - // hold the tintin version of the rooms - let mut tintin_rooms: BTreeMap = BTreeMap::new(); - - for mudlet_room in mudlet_area.rooms { - // hold the tintin version of room exits - let mut tintin_exits: Vec = Vec::new(); - - for mudlet_exit in mudlet_room.exits { - // map the exit direction - let (dir_num, dir_short) = match mudlet_exit.name.as_str() { - "north" => (1, 'n'), - "east" => (2, 'e'), - "south" => (4, 's'), - "west" => (8, 'w'), - "up" => (16, 'u'), - "down" => (32, 'd'), - _ => panic!("Unknown exit: {}", mudlet_exit.name), - }; - - // create a tintin exit from the mudlet exit - let tintin_exit = tintin::Exit { - vnum: mudlet_exit.exitId, - name: dir_short.into(), - cmd: dir_short.into(), - dir: dir_num, - flags: 0, - data: String::new(), - weight: 1.0, - color: String::new(), - decay: 0.0, - }; - - // add tintin exit to list of tintin exits for the room - tintin_exits.push(tintin_exit); - } - - let userdata = match mudlet_room.userData { - Some(userdata) => userdata, - None => continue, - }; - - let environment = Environment::from(mudlet_room.environment); - - let tintin_room = tintin::Room { - vnum: mudlet_room.id, - flags: 0, - color: environment.color(), - name: mudlet_room.name, - symbol: environment.symbol(), - desc: userdata.description.replace("\n", " ").replace(" ", " "), - area: userdata.zone, - note: String::new(), - terrain: String::new(), - data: String::new(), - weight: 1.0, - id: String::new(), - exits: tintin_exits, - }; - - tintin_rooms.insert(tintin_room.vnum, tintin_room); - } - - for (vnum, room) in tintin_rooms { - println!("R {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}}", vnum, room.flags, room.color, room.name, room.symbol, room.desc, room.area, room.note, room.terrain, room.data, room.weight, room.id); - - for exit in room.exits { - println!( - "E {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}}", - exit.vnum, - exit.name, - exit.cmd, - exit.dir, - exit.flags, - exit.data, - exit.weight, - exit.color, - exit.decay - ); - } - - println!(); - } - } - - Ok(()) + let mut buffer = String::new(); + let mut stdin = io::stdin(); + stdin.read_to_string(&mut buffer)?; + + let mudlet_map: Value = serde_json::from_str(&buffer)?; + + let areas = match mudlet_map.get("areas") { + Some(areas) => match areas.as_array() { + Some(areas) => areas, + None => panic!("Areas not an array"), + }, + None => panic!("No areas found"), + }; + + for area in areas { + let id: i64 = match area.get("id") { + Some(id) => match id.as_i64() { + Some(id) => id, + None => continue, + }, + None => continue, + }; + + if id < 0 { + continue; + } + + let mudlet_area: mudlet::Area = serde_json::from_value(area.to_owned())?; + + // hold the tintin version of the rooms + let mut tintin_rooms: BTreeMap = BTreeMap::new(); + + for mudlet_room in mudlet_area.rooms { + // hold the tintin version of room exits + let mut tintin_exits: Vec = Vec::new(); + + for mudlet_exit in mudlet_room.exits { + // map the exit direction + let (dir_num, dir_short) = match mudlet_exit.name.as_str() { + "north" => (1, 'n'), + "east" => (2, 'e'), + "south" => (4, 's'), + "west" => (8, 'w'), + "up" => (16, 'u'), + "down" => (32, 'd'), + _ => panic!("Unknown exit: {}", mudlet_exit.name), + }; + + // create a tintin exit from the mudlet exit + let tintin_exit = tintin::Exit { + vnum: mudlet_exit.exitId, + name: dir_short.into(), + cmd: dir_short.into(), + dir: dir_num, + flags: 0, + data: String::new(), + weight: 1.0, + color: String::new(), + decay: 0.0, + }; + + // add tintin exit to list of tintin exits for the room + tintin_exits.push(tintin_exit); + } + + let userdata = match mudlet_room.userData { + Some(userdata) => userdata, + None => continue, + }; + + let environment = Environment::from(mudlet_room.environment); + + let tintin_room = tintin::Room { + vnum: mudlet_room.id, + flags: 0, + color: environment.color(), + name: mudlet_room.name, + symbol: environment.symbol(), + desc: userdata.description.replace("\n", " ").replace(" ", " "), + area: userdata.zone, + note: String::new(), + terrain: String::new(), + data: String::new(), + weight: 1.0, + id: String::new(), + exits: tintin_exits, + }; + + tintin_rooms.insert(tintin_room.vnum, tintin_room); + } + + for (vnum, room) in tintin_rooms { + println!("{}", room); + + for exit in room.exits { + println!( + "E {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}}", + exit.vnum, + exit.name, + exit.cmd, + exit.dir, + exit.flags, + exit.data, + exit.weight, + exit.color, + exit.decay + ); + } + + println!(); + } + } + + Ok(()) } diff --git a/src/mudlet/area.rs b/src/mudlet/area.rs index 1a5f51f..9795280 100644 --- a/src/mudlet/area.rs +++ b/src/mudlet/area.rs @@ -7,15 +7,15 @@ use crate::mudlet; #[derive(Debug, Deserialize)] pub struct Area { - /// area id - //id: i64, + /// area id + //id: i64, - /// area name - //name: String, + /// area name + //name: String, - /// rooms - pub rooms: Vec, + /// rooms + pub rooms: Vec, - #[serde(flatten)] - _extra: HashMap, + #[serde(flatten)] + _extra: HashMap, } diff --git a/src/mudlet/exit.rs b/src/mudlet/exit.rs index a36c822..a9ce2dc 100644 --- a/src/mudlet/exit.rs +++ b/src/mudlet/exit.rs @@ -5,12 +5,12 @@ use serde_json::Value; #[derive(Debug, Deserialize)] pub struct Exit { - /// connected room - pub exitId: i64, + /// connected room + pub exitId: i64, - /// direction - pub name: String, + /// direction + pub name: String, - #[serde(flatten)] - _extra: HashMap, + #[serde(flatten)] + _extra: HashMap, } diff --git a/src/mudlet/room.rs b/src/mudlet/room.rs index b5fd2b4..3108cf8 100644 --- a/src/mudlet/room.rs +++ b/src/mudlet/room.rs @@ -4,19 +4,19 @@ use crate::mudlet; #[derive(Debug, Deserialize)] pub struct Room { - /// environment - pub environment: i64, + /// environment + pub environment: i64, - /// exits - pub exits: Vec, + /// exits + pub exits: Vec, - /// room number - pub id: i64, + /// room number + pub id: i64, - /// room name - #[serde(default)] - pub name: String, + /// room name + #[serde(default)] + pub name: String, - /// user data - pub userData: Option, + /// user data + pub userData: Option, } diff --git a/src/mudlet/userdata.rs b/src/mudlet/userdata.rs index 1b0ecc4..e73ac1a 100644 --- a/src/mudlet/userdata.rs +++ b/src/mudlet/userdata.rs @@ -5,13 +5,13 @@ use serde_json::Value; #[derive(Debug, Deserialize)] pub struct UserData { - /// room description - pub description: String, + /// room description + pub description: String, - /// zone name - #[serde(default)] - pub zone: String, + /// zone name + #[serde(default)] + pub zone: String, - #[serde(flatten)] - _extra: HashMap, + #[serde(flatten)] + _extra: HashMap, } diff --git a/src/tintin/exit.rs b/src/tintin/exit.rs index 11f12c9..e8dcbfc 100644 --- a/src/tintin/exit.rs +++ b/src/tintin/exit.rs @@ -1,29 +1,29 @@ #[derive(Debug)] pub struct Exit { - /// vnum - pub vnum: i64, + /// vnum + pub vnum: i64, - /// exit name - pub name: String, + /// exit name + pub name: String, - /// command to use the exit - pub cmd: String, + /// command to use the exit + pub cmd: String, - /// exit direction - pub dir: i8, + /// exit direction + pub dir: i8, - /// exit flags - pub flags: i64, + /// exit flags + pub flags: i64, - /// extra data - pub data: String, + /// extra data + pub data: String, - /// movement cost? - pub weight: f64, + /// movement cost? + pub weight: f64, - /// exit color - pub color: String, + /// exit color + pub color: String, - /// decay? - pub decay: f64, + /// decay? + pub decay: f64, } diff --git a/src/tintin/room.rs b/src/tintin/room.rs index a4866f1..8d7db25 100644 --- a/src/tintin/room.rs +++ b/src/tintin/room.rs @@ -1,43 +1,66 @@ +use std::fmt; + use crate::tintin; #[derive(Debug)] pub struct Room { - /// room number - pub vnum: i64, + /// room number + pub vnum: i64, + + /// flags + pub flags: i64, - /// flags - pub flags: i64, + /// color + pub color: String, - /// color - pub color: String, + /// room name + pub name: String, - /// room name - pub name: String, + /// room symbol + pub symbol: String, - /// room symbol - pub symbol: String, + /// room description + pub desc: String, - /// room description - pub desc: String, + /// area that room is in + pub area: String, - /// area that room is in - pub area: String, + /// notes + pub note: String, - /// notes - pub note: String, + /// type of terrain + pub terrain: String, - /// type of terrain - pub terrain: String, + /// extra data + pub data: String, - /// extra data - pub data: String, + /// movement cost + pub weight: f64, - /// movement cost - pub weight: f64, + /// room id + pub id: String, - /// room id - pub id: String, + /// exits + pub exits: Vec, +} - /// exits - pub exits: Vec, +impl fmt::Display for Room { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "R {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}} {{{}}}", + &self.vnum, + &self.flags, + &self.color, + &self.name, + &self.symbol, + &self.desc, + &self.area, + &self.note, + &self.terrain, + &self.data, + &self.weight, + &self.id + ) + } }