Cleaned code

Refactored code for readability
This commit is contained in:
scorpion9979
2019-06-01 22:49:34 +03:00
committed by Forest Anderson
parent eb80459872
commit 29f230d23e
3 changed files with 17 additions and 9 deletions

View File

@ -18,6 +18,7 @@ pub mod comp;
pub mod figure; pub mod figure;
pub mod inventory; pub mod inventory;
pub mod msg; pub mod msg;
pub mod npc;
pub mod ray; pub mod ray;
pub mod state; pub mod state;
pub mod sys; pub mod sys;
@ -25,7 +26,6 @@ pub mod terrain;
pub mod util; pub mod util;
pub mod vol; pub mod vol;
pub mod volumes; pub mod volumes;
pub mod npc;
/// The networking module containing high-level wrappers of `TcpListener` and `TcpStream` (`PostOffice` and `PostBox` respectively) and data types used by both the server and client. /// The networking module containing high-level wrappers of `TcpListener` and `TcpStream` (`PostOffice` and `PostBox` respectively) and data types used by both the server and client.
/// # Examples /// # Examples

View File

@ -1,26 +1,30 @@
use serde_json;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde_json;
use std::fs::File; use std::fs::File;
use std::io::Error;
pub enum NpcKind { pub enum NpcKind {
Wolf, Wolf,
Pig Pig,
} }
impl NpcKind { impl NpcKind {
fn as_str(&self) -> &'static str { fn as_str(&self) -> &'static str {
match *self { match *self {
NpcKind::Wolf => "wolf", NpcKind::Wolf => "wolf",
NpcKind::Pig => "pig" NpcKind::Pig => "pig",
} }
} }
} }
const npc_names_dir: &str = "common/assets/npc_names.json";
pub fn get_npc_name(npc_type: NpcKind) -> String { pub fn get_npc_name(npc_type: NpcKind) -> String {
let file = File::open("common/assets/npc_names.json").expect("file should open read only"); let npc_names_file =
let json: serde_json::Value = File::open(npc_names_dir).expect(&format!("opening {} in read-only mode", npc_names_dir));
serde_json::from_reader(file).expect("file should be proper JSON"); let npc_names_json: serde_json::Value = serde_json::from_reader(npc_names_file)
let npc_names = json .expect(&format!("reading json contents from {}", npc_names_dir));
let npc_names = npc_names_json
.get(npc_type.as_str()) .get(npc_type.as_str())
.expect("accessing json using NPC type provided as key") .expect("accessing json using NPC type provided as key")
.as_array() .as_array()

View File

@ -3,7 +3,11 @@
//! and provide a handler function. //! and provide a handler function.
use crate::Server; use crate::Server;
use common::{comp, msg::ServerMsg, npc::{NpcKind, get_npc_name}}; use common::{
comp,
msg::ServerMsg,
npc::{get_npc_name, NpcKind},
};
use specs::{Builder, Entity as EcsEntity, Join}; use specs::{Builder, Entity as EcsEntity, Join};
use vek::*; use vek::*;