Remove the actions module, and move commands into the Command module.

master
rasul 5 years ago
parent 7a8bbe7890
commit fdff6a0f8e

@ -1,127 +0,0 @@
use std::collections::HashMap;
use std::str::FromStr;
use mio::Token;
use crate::actions::look;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::world::{Direction, Exit, Room};
use crate::{try_option_send_error, try_send_error};
/// Look at the room. Provide room name, description, and exits.
pub fn dig(command: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
// find the player
let mut player = try_option_send_error!(token, db.get_connected_player(token));
// get the direction to dig
let direction = match Direction::from_str(&args) {
Ok(d) => d,
Err(e) => {
send_queue.push(token, format!("{}", e), true);
return send_queue;
}
};
// get starting room
let mut start_room = try_option_send_error!(token, db.load_room(player.location));
// make sure exit doesn't already exist
if start_room.exits.contains_key(&direction) {
send_queue.push(token, "Exit already exists", true);
return send_queue;
}
// get starting zone
let mut zone = try_option_send_error!(token, db.load_zone(start_room.zone));
let new_room_id = try_send_error!(token, db.new_area_id());
// create a new, empty room
let mut new_room = Room {
id: new_room_id,
zone: start_room.zone,
name: format!("New Room {}", new_room_id),
description: Vec::new(),
users_visible: true,
exits: HashMap::new(),
};
// add exit from start room to new room
let _ = start_room.exits.insert(
direction,
Exit {
target: new_room.id,
direction,
},
);
// add exit from new room to start room
let _ = new_room.exits.insert(
direction.opposite(),
Exit {
target: start_room.id,
direction: direction.opposite(),
},
);
// add new room to zone
let _ = zone.areas.insert(new_room.id);
// save the new room
if db.save_room(&new_room).is_ok() {
send_queue.push(token, "New room saved\n", false);
} else {
send_queue.push(token, "Unable to save new room", true);
return send_queue;
}
// save the start room
if db.save_room(&start_room).is_ok() {
send_queue.push(token, "Start room saved\n", false);
} else {
send_queue.push(token, "Unable to save start room", true);
return send_queue;
}
// save the zone
if db.save_zone(&zone).is_ok() {
send_queue.push(token, "Zone saved\n", false);
} else {
send_queue.push(token, "Unable to save zone", true);
return send_queue;
}
// move and save the player
player.location = new_room.id;
if db.save_player(&player).is_ok() {
if db.save_connected_player(token, &player).is_ok() {
send_queue.push(token, format!("You dig {}.\n\n", direction.long()), false);
} else {
send_queue.push(token, "Unable to save connected player", true);
}
} else {
send_queue.push(token, "Unable to save player", true);
return send_queue;
}
// inform people about what just took place
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(start_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!("{} digs {}.", player.name, direction.long()),
true,
);
}
}
send_queue.append(&mut look(&command, args, token, db));
send_queue
}

@ -1,11 +0,0 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
pub fn help(command: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
send_queue
}

@ -1,55 +0,0 @@
use colored::Colorize;
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
/// Look at the room. Provide room name, description, and exits.
pub fn look(_: &Command, _: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
// get the player
let player = try_option_send_error!(token, db.get_connected_player(token));
// get the room
let room = try_option_send_error!(token, db.load_room(player.location));
// room name
send_queue.push(token, format!("{}\n", room.name.cyan().to_string()), false);
// room description
send_queue.push(
token,
format!("{}\n", {
let mut s = room.description.join("\n");
s.push_str("\n");
s
}),
false,
);
// exits
send_queue.push(
token,
format!("[ obvious exits: {} ]\n", room.exit_string())
.cyan()
.to_string(),
false,
);
// other people in room
for (neighbor_token, neighbor_player) in try_send_error!(
token,
db.find_connected_players_by_location(player.location)
) {
if neighbor_token != token {
send_queue.push(token, format!("{} is here", neighbor_player.name), false);
}
}
send_queue.push(token, "", true);
send_queue
}

@ -1,13 +0,0 @@
mod dig;
mod help;
mod look;
mod move_room;
mod save;
mod say;
pub use dig::dig;
pub use help::help;
pub use look::look;
pub use move_room::move_room;
pub use save::save;
pub use say::say;

@ -1,84 +0,0 @@
use log::warn;
use mio::Token;
use crate::actions::look;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::world::*;
use crate::{try_option_send_error, try_send_error};
pub fn move_room(command: &Command, _args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let direction: Direction = match command {
Command::N | Command::North => Direction::North,
Command::S | Command::South => Direction::South,
Command::E | Command::East => Direction::East,
Command::W | Command::West => Direction::West,
Command::U | Command::Up => Direction::Up,
Command::D | Command::Down => Direction::Down,
_ => {
warn!("Can't figure out direction: {:?}", command);
return send_queue;
}
};
// find the player
let mut player = try_option_send_error!(token, db.get_connected_player(token));
// get starting room
let start_room = try_option_send_error!(token, db.load_room(player.location));
// get the exit
let exit = if let Some(exit) = start_room.exits.get(&direction) {
exit
} else {
send_queue.push(token, "You can't go that way.", true);
return send_queue;
};
// get the target room
let target_room = try_option_send_error!(token, db.load_room(exit.target));
// move and save the player
player.location = target_room.id;
let _ = try_send_error!(token, db.save_player(&player));
let _ = try_send_error!(token, db.save_connected_player(token, &player));
send_queue.push(token, format!("You leave {}.\n\n", direction.long()), false);
// tell people about leaving
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(start_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!("{} leaves {}.", player.name, direction.long()),
true,
);
}
}
// tell people about entering
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(target_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!(
"{} arrives from the {}.",
player.name,
direction.opposite().long()
),
true,
);
}
}
// look around
send_queue.append(&mut look(&command, String::new(), token, db));
send_queue
}

@ -1,18 +0,0 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
/// Save the player information to disk.
pub fn save(_: &Command, _: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let player = try_option_send_error!(token, db.get_connected_player(token));
let _ = try_send_error!(token, db.save_player(&player));
let _ = try_send_error!(token, db.save_connected_player(token, &player));
send_queue.push(token, "Ok", true);
send_queue
}

@ -1,30 +0,0 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
/// Say something to anyone in the room.
pub fn say(_: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let player = try_option_send_error!(token, db.get_connected_player(token));
for (neighbor_token, _) in try_send_error!(
token,
db.find_connected_players_by_location(player.location)
) {
send_queue.push(
neighbor_token,
if neighbor_token == token {
format!("You say, \"{}\"\n", args)
} else {
format!("{} says, \"{}\"\n", player.name, args)
},
true,
);
}
send_queue
}

@ -3,7 +3,6 @@ use std::default::Default;
use mio::Token; use mio::Token;
use strum_macros::{Display, EnumIter}; use strum_macros::{Display, EnumIter};
use crate::actions;
use crate::command::{CommandSet, Parse, ParserError}; use crate::command::{CommandSet, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
@ -91,18 +90,18 @@ impl Parse for Command {
fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue { fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue {
match self { match self {
Self::N | Self::North => actions::move_room, Self::N | Self::North => Self::dispatch_move_room,
Self::S | Self::South => actions::move_room, Self::S | Self::South => Self::dispatch_move_room,
Self::E | Self::East => actions::move_room, Self::E | Self::East => Self::dispatch_move_room,
Self::W | Self::West => actions::move_room, Self::W | Self::West => Self::dispatch_move_room,
Self::U | Self::Up => actions::move_room, Self::U | Self::Up => Self::dispatch_move_room,
Self::D | Self::Down => actions::move_room, Self::D | Self::Down => Self::dispatch_move_room,
Self::Dig => actions::dig, Self::Dig => Self::dispatch_dig,
Self::Help => actions::help, Self::Help => Self::dispatch_help,
Self::Look => actions::look, Self::Look => Self::dispatch_look,
Self::Quit => Self::dispatch_quit, Self::Quit => Self::dispatch_quit,
Self::Save => actions::save, Self::Save => Self::dispatch_save,
Self::Say => actions::say, Self::Say => Self::dispatch_say,
Self::Set(_) => Self::dispatch_map_subcommand, Self::Set(_) => Self::dispatch_map_subcommand,
Self::Default => Self::dispatch_default, Self::Default => Self::dispatch_default,
} }

@ -0,0 +1,128 @@
use std::collections::HashMap;
use std::str::FromStr;
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::world::{Direction, Exit, Room};
use crate::{try_option_send_error, try_send_error};
impl Command {
/// Look at the room. Provide room name, description, and exits.
pub fn dispatch_dig(command: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
// find the player
let mut player = try_option_send_error!(token, db.get_connected_player(token));
// get the direction to dig
let direction = match Direction::from_str(&args) {
Ok(d) => d,
Err(e) => {
send_queue.push(token, format!("{}", e), true);
return send_queue;
}
};
// get starting room
let mut start_room = try_option_send_error!(token, db.load_room(player.location));
// make sure exit doesn't already exist
if start_room.exits.contains_key(&direction) {
send_queue.push(token, "Exit already exists", true);
return send_queue;
}
// get starting zone
let mut zone = try_option_send_error!(token, db.load_zone(start_room.zone));
let new_room_id = try_send_error!(token, db.new_area_id());
// create a new, empty room
let mut new_room = Room {
id: new_room_id,
zone: start_room.zone,
name: format!("New Room {}", new_room_id),
description: Vec::new(),
users_visible: true,
exits: HashMap::new(),
};
// add exit from start room to new room
let _ = start_room.exits.insert(
direction,
Exit {
target: new_room.id,
direction,
},
);
// add exit from new room to start room
let _ = new_room.exits.insert(
direction.opposite(),
Exit {
target: start_room.id,
direction: direction.opposite(),
},
);
// add new room to zone
let _ = zone.areas.insert(new_room.id);
// save the new room
if db.save_room(&new_room).is_ok() {
send_queue.push(token, "New room saved\n", false);
} else {
send_queue.push(token, "Unable to save new room", true);
return send_queue;
}
// save the start room
if db.save_room(&start_room).is_ok() {
send_queue.push(token, "Start room saved\n", false);
} else {
send_queue.push(token, "Unable to save start room", true);
return send_queue;
}
// save the zone
if db.save_zone(&zone).is_ok() {
send_queue.push(token, "Zone saved\n", false);
} else {
send_queue.push(token, "Unable to save zone", true);
return send_queue;
}
// move and save the player
player.location = new_room.id;
if db.save_player(&player).is_ok() {
if db.save_connected_player(token, &player).is_ok() {
send_queue.push(token, format!("You dig {}.\n\n", direction.long()), false);
} else {
send_queue.push(token, "Unable to save connected player", true);
}
} else {
send_queue.push(token, "Unable to save player", true);
return send_queue;
}
// inform people about what just took place
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(start_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!("{} digs {}.", player.name, direction.long()),
true,
);
}
}
send_queue.append(&mut Self::dispatch_look(&command, args, token, db));
send_queue
}
}

@ -0,0 +1,13 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
impl Command {
pub fn dispatch_help(command: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
send_queue
}
}

@ -0,0 +1,57 @@
use colored::Colorize;
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
impl Command {
/// Look at the room. Provide room name, description, and exits.
pub fn dispatch_look(_: &Command, _: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
// get the player
let player = try_option_send_error!(token, db.get_connected_player(token));
// get the room
let room = try_option_send_error!(token, db.load_room(player.location));
// room name
send_queue.push(token, format!("{}\n", room.name.cyan().to_string()), false);
// room description
send_queue.push(
token,
format!("{}\n", {
let mut s = room.description.join("\n");
s.push_str("\n");
s
}),
false,
);
// exits
send_queue.push(
token,
format!("[ obvious exits: {} ]\n", room.exit_string())
.cyan()
.to_string(),
false,
);
// other people in room
for (neighbor_token, neighbor_player) in try_send_error!(
token,
db.find_connected_players_by_location(player.location)
) {
if neighbor_token != token {
send_queue.push(token, format!("{} is here", neighbor_player.name), false);
}
}
send_queue.push(token, "", true);
send_queue
}
}

@ -0,0 +1,7 @@
mod dig;
mod help;
mod look;
mod move_room;
mod quit;
mod save;
mod say;

@ -0,0 +1,85 @@
use log::warn;
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::world::*;
use crate::{try_option_send_error, try_send_error};
impl Command {
pub fn dispatch_move_room(command: &Command, _args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let direction: Direction = match command {
Command::N | Command::North => Direction::North,
Command::S | Command::South => Direction::South,
Command::E | Command::East => Direction::East,
Command::W | Command::West => Direction::West,
Command::U | Command::Up => Direction::Up,
Command::D | Command::Down => Direction::Down,
_ => {
warn!("Can't figure out direction: {:?}", command);
return send_queue;
}
};
// find the player
let mut player = try_option_send_error!(token, db.get_connected_player(token));
// get starting room
let start_room = try_option_send_error!(token, db.load_room(player.location));
// get the exit
let exit = if let Some(exit) = start_room.exits.get(&direction) {
exit
} else {
send_queue.push(token, "You can't go that way.", true);
return send_queue;
};
// get the target room
let target_room = try_option_send_error!(token, db.load_room(exit.target));
// move and save the player
player.location = target_room.id;
let _ = try_send_error!(token, db.save_player(&player));
let _ = try_send_error!(token, db.save_connected_player(token, &player));
send_queue.push(token, format!("You leave {}.\n\n", direction.long()), false);
// tell people about leaving
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(start_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!("{} leaves {}.", player.name, direction.long()),
true,
);
}
}
// tell people about entering
for (neighbor_token, _) in
try_send_error!(token, db.find_connected_players_by_location(target_room.id))
{
if neighbor_token != token {
send_queue.push(
neighbor_token,
format!(
"{} arrives from the {}.",
player.name,
direction.opposite().long()
),
true,
);
}
}
// look around
send_queue.append(&mut Self::dispatch_look(&command, String::new(), token, db));
send_queue
}
}

@ -0,0 +1,20 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
impl Command {
/// Save the player information to disk.
pub fn dispatch_save(_: &Command, _: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let player = try_option_send_error!(token, db.get_connected_player(token));
let _ = try_send_error!(token, db.save_player(&player));
let _ = try_send_error!(token, db.save_connected_player(token, &player));
send_queue.push(token, "Ok", true);
send_queue
}
}

@ -0,0 +1,32 @@
use mio::Token;
use crate::command::Command;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
impl Command {
/// Say something to anyone in the room.
pub fn dispatch_say(_: &Command, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut send_queue = SendQueue::new();
let player = try_option_send_error!(token, db.get_connected_player(token));
for (neighbor_token, _) in try_send_error!(
token,
db.find_connected_players_by_location(player.location)
) {
send_queue.push(
neighbor_token,
if neighbor_token == token {
format!("You say, \"{}\"\n", args)
} else {
format!("{} says, \"{}\"\n", player.name, args)
},
true,
);
}
send_queue
}
}

@ -1,11 +1,10 @@
mod command; mod command;
mod dispatch;
mod parse; mod parse;
mod parser_error; mod parser_error;
mod quit;
mod set; mod set;
pub use command::Command; pub use command::Command;
pub use parse::Parse; pub use parse::Parse;
pub use parser_error::ParserError; pub use parser_error::ParserError;
pub use quit::*;
pub use set::*; pub use set::*;

@ -2,7 +2,6 @@ use chrono::Utc;
use log::warn; use log::warn;
use mio::Token; use mio::Token;
use crate::actions;
use crate::command::Command; use crate::command::Command;
use crate::game::Game; use crate::game::Game;
use crate::player::Player; use crate::player::Player;
@ -89,7 +88,7 @@ impl Game {
send_queue.push(token, format!("Welcome, {}\n", username), false); send_queue.push(token, format!("Welcome, {}\n", username), false);
send_queue.push(token, "", true); send_queue.push(token, "", true);
send_queue.append(&mut actions::look( send_queue.append(&mut Command::dispatch_look(
&Command::default(), &Command::default(),
String::new(), String::new(),
token, token,
@ -122,7 +121,7 @@ impl Game {
); );
client.state = State::Action; client.state = State::Action;
send_queue.append(&mut actions::look( send_queue.append(&mut Command::dispatch_look(
&Command::default(), &Command::default(),
String::new(), String::new(),
token, token,

@ -1,6 +1,5 @@
//! The Rude Mud //! The Rude Mud
pub mod actions;
pub mod client; pub mod client;
pub mod command; pub mod command;
pub mod config; pub mod config;

@ -1,4 +1,3 @@
mod actions;
mod client; mod client;
mod command; mod command;
mod config; mod config;

Loading…
Cancel
Save