diff --git a/Cargo.lock b/Cargo.lock index 0d5dedf..7db13a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,88 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + [[package]] name = "reentrada" version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" diff --git a/Cargo.toml b/Cargo.toml index 5171771..b3971cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "1.0.144", features = ["derive"]} +serde_json = "1.0.85" diff --git a/locations/0.json b/locations/0.json new file mode 100644 index 0000000..7960130 --- /dev/null +++ b/locations/0.json @@ -0,0 +1,10 @@ +{ + "tag": "0", + "descripcion": "Pasillo oscuro, luz pulsante.", + "salidas": { + "norte": "1", + "este": "", + "sur": "", + "oeste": "" + } +} diff --git a/locations/1.json b/locations/1.json new file mode 100644 index 0000000..ff7acd2 --- /dev/null +++ b/locations/1.json @@ -0,0 +1,10 @@ +{ + "tag": "1", + "descripcion": "Puente de mando. Humo denso.", + "salidas": { + "norte": "", + "este": "2", + "sur": "0", + "oeste": "" + } +} diff --git a/locations/2.json b/locations/2.json new file mode 100644 index 0000000..2cb1280 --- /dev/null +++ b/locations/2.json @@ -0,0 +1,10 @@ +{ + "tag": "2", + "descripcion": "Vainas criogénicas. Sonido de alarma.", + "salidas": { + "norte": "", + "este": "", + "sur": "", + "oeste": "1" + } +} diff --git a/src/main.rs b/src/main.rs index afeec2a..31cadbb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() { let mut command = rlib::Command::new(); let mut output: String; - let mut player = Player{name: "manolo".to_string(), location: 0}; + let mut player = Player{name: "manolo".to_string(), location: "0".to_string()}; // main loop while command.verb != "salir" { diff --git a/src/player.rs b/src/player.rs index 00936e4..1e0f6d9 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,4 @@ pub struct Player { pub name: String, - pub location: usize, + pub location: String, } diff --git a/src/rlib.rs b/src/rlib.rs index ad285ed..a9845c3 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,5 +1,5 @@ use std::io::{self, Write}; -use crate::rlocation::{Location, execute_go, execute_look}; +use crate::rlocation::execute_go; use crate::player::Player; pub struct Command { @@ -50,7 +50,11 @@ pub fn update_state(player: &mut Player, command: &Command) -> String { match command.verb.as_str() { "salir" => output = format!("Saliendo.\nGracias por jugar! :D"), "mirar" => output = format!("Está muy oscuro, no puedes ver nada excepto la luz pulsante."), - "ir" => output = execute_go(player, command.noun.to_string()), + "ir" => output = execute_go(player, command.noun.as_str()), + "n" => output = execute_go(player, "n"), + "e" => output = execute_go(player, "e"), + "s" => output = execute_go(player, "s"), + "o" => output = execute_go(player, "o"), _ => output = format!("No se como hacer eso."), } diff --git a/src/rlocation.rs b/src/rlocation.rs index 4e7f437..531f3a4 100644 --- a/src/rlocation.rs +++ b/src/rlocation.rs @@ -1,43 +1,78 @@ use crate::player::Player; +use serde::Deserialize; +use std::fs::File; +use std::io::Read; -#[derive(Debug)] -pub struct Location<'a> { - descripcion: &'a str, - tag: &'a str, +#[derive(Debug, Deserialize)] +#[allow(dead_code)] // evita os warnings de que non se le o campo +pub struct Location { + tag: String, // tamén é o nome do arquivo, mentras non se implementa bbdd + descripcion: String, + salidas: Salidas, } -const LOCATIONS: &'static [Location] = &[ - Location {descripcion: "Pasillo principal.", tag: "pasillo"}, - Location {descripcion: "Sala de máquinas.", tag: "maquinas"}, - Location {descripcion: "Vainas criogénicas.", tag: "vainas"} -]; +#[derive(Debug, Deserialize)] +#[allow(dead_code)] // evita os warnings de que non se le o campo +pub struct Salidas { + norte: String, + este: String, + sur: String, + oeste: String, +} -pub fn execute_look(tag: String) -> String { +pub fn execute_look(_tag: String) -> String { let salida = ""; return salida.to_string(); } -pub fn execute_go(player: &mut Player, tag: String) -> String { - let salida; +pub fn execute_go(player: &mut Player, coord: &str) -> String { + let output: String; - if tag != "" { - for (pos, e) in LOCATIONS.iter().enumerate() { - // println!("Element at position {}: {:?}", pos, e.tag); - if tag == e.tag { - if pos == player.location { - salida = "No te puedes acercar mucho más..."; - } else { - player.location = pos; - salida = e.descripcion; - } - return salida.to_string(); + if coord != "" { + let tag: String; + let player_location = &player.location; + let current_location = get_location(player_location.to_string()); + + match coord { + "n" => tag = current_location.salidas.norte, + "e" => tag = current_location.salidas.este, + "s" => tag = current_location.salidas.sur, + "o" => tag = current_location.salidas.oeste, + "norte" => tag = current_location.salidas.norte, + "este" => tag = current_location.salidas.este, + "sur" => tag = current_location.salidas.sur, + "oeste" => tag = current_location.salidas.oeste, + _ => return String::from("No existe esa dirección.") + }; + if tag.as_str() != "" { + let location = get_location(tag); + if location.tag == player.location { + output = String::from("No te puedes acercar mucho más..."); + } else { + player.location = location.tag; + output = location.descripcion; } + } else { + output = String::from("No existe esa salida."); } - salida = "No encuentro ese lugar..." } else { - salida = "No entiendo a donde quieres ir..."; + output = String::from("No entiendo a donde quieres ir. Debes elegir una coordenada correspondiente a las salidas de esta sala."); } - return salida.to_string(); + return output.to_string(); +} + +// get_location le o arquivo de localización e devolve o json +fn get_location(tag: String) -> Location { + let dir = "locations"; + let path = format!("{}/{}.json", dir, tag); + + let mut file = File::open(path).expect("Localización non atopada."); + let mut data = String::new(); + file.read_to_string(&mut data).expect("Error durante a lectura da localización."); + + let location: Location = serde_json::from_str(&data).expect("JSON was not well-formatted"); + + location }