diff --git a/geoffrey_api/src/logging.rs b/geoffrey_api/src/logging.rs index 0a0dc9f..b995fc2 100644 --- a/geoffrey_api/src/logging.rs +++ b/geoffrey_api/src/logging.rs @@ -1,5 +1,4 @@ use log::{LevelFilter, SetLoggerError}; -use serde::{Deserialize, Serialize}; use simple_logger::SimpleLogger; use geoffrey_models::logging::LogLevel; diff --git a/geoffrey_bot/src/bot/commands/mod.rs b/geoffrey_bot/src/bot/commands/mod.rs index 3c6b033..51775e7 100644 --- a/geoffrey_bot/src/bot/commands/mod.rs +++ b/geoffrey_bot/src/bot/commands/mod.rs @@ -21,6 +21,7 @@ pub mod add_item; pub mod add_location; pub mod find; pub mod selling; +pub mod set_portal; #[derive(Debug)] pub enum CommandError { diff --git a/geoffrey_bot/src/bot/commands/set_portal.rs b/geoffrey_bot/src/bot/commands/set_portal.rs new file mode 100644 index 0000000..f18bf72 --- /dev/null +++ b/geoffrey_bot/src/bot/commands/set_portal.rs @@ -0,0 +1,81 @@ +use async_trait::async_trait; +use geoffrey_models::models::locations::Location; +use geoffrey_models::models::Portal; +use reqwest::Method; +use serenity::client::Context; +use serenity::model::interactions::application_command::{ + ApplicationCommand, ApplicationCommandInteraction, ApplicationCommandOptionType, +}; + +use crate::bot::arg_parse::{option_to_i64, option_to_string}; +use crate::bot::commands::{BotCommand, CommandError}; +use geoffrey_models::models::parameters::set_portal_params::SetPortalParams; + +pub struct SetPortalCommand; + +#[async_trait] +impl BotCommand for SetPortalCommand { + type ApiParams = SetPortalParams; + type ApiResp = Location; + + fn command_name() -> String { + "set_portal".to_string() + } + + fn request_type() -> Method { + Method::POST + } + + async fn create_app_command(ctx: &Context) -> Result { + let command = ApplicationCommand::create_global_application_command(&ctx.http, |command| { + command + .name(Self::command_name()) + .description("Set a portal for a location") + .create_option(|option| { + option + .name("loc_name") + .description("Name of the location") + .kind(ApplicationCommandOptionType::String) + .required(true) + }) + .create_option(|option| { + option + .name("x_portal") + .description("X coordinate of the portal in the nether") + .kind(ApplicationCommandOptionType::Integer) + .max_int_value(i32::MAX) + .min_int_value(i32::MIN) + .required(true) + }) + .create_option(|option| { + option + .name("z_portal") + .description("Z coordinate of the portal in the nether") + .kind(ApplicationCommandOptionType::Integer) + .max_int_value(i32::MAX) + .min_int_value(i32::MIN) + .required(true) + }) + }) + .await?; + + Ok(command) + } + + async fn process_arguments( + command_interaction: ApplicationCommandInteraction, + ) -> Result { + let options = command_interaction.data.options; + let loc_name = option_to_string(options.get(0), "name")?; + let x_portal = option_to_i64(options.get(1), "x_portal")?; + let z_portal = option_to_i64(options.get(2), "z_portal")?; + + let portal = Portal::new(x_portal as i32, z_portal as i32); + + Ok(Self::ApiParams::new(loc_name, portal)) + } + + fn build_response(resp: Self::ApiResp) -> String { + format!("{} has been been updated.", resp.name) + } +} \ No newline at end of file diff --git a/geoffrey_bot/src/bot/formatters.rs b/geoffrey_bot/src/bot/formatters.rs index b6c2564..ee79ad2 100644 --- a/geoffrey_bot/src/bot/formatters.rs +++ b/geoffrey_bot/src/bot/formatters.rs @@ -1,5 +1,6 @@ use geoffrey_models::models::locations::Location; use geoffrey_models::models::player::Player; +use geoffrey_models::models::Portal; pub fn display_owners(owners: Vec, limit: usize) -> String { let mut plural = ""; @@ -28,11 +29,21 @@ pub fn display_owners(owners: Vec, limit: usize) -> String { ) } +pub fn display_portal(portal: Portal) -> String { + format!("Portal: {} {} (x={}, z={})", portal.direction(), portal.tunnel_addr(), portal.x, portal.z) +} + pub fn display_loc(loc: Location) -> String { + let portal_str = match loc.portal { + None => "".to_string(), + Some(p) => format!("{}, ", display_portal(p)) + }; + format!( - "**{}**, {}, Owner(s): **{}**", + "**{}**, {}, {}{}", loc.name, loc.position, - display_owners(loc.owners, 3) + portal_str, + display_owners(loc.owners, 3), ) } diff --git a/geoffrey_bot/src/bot/mod.rs b/geoffrey_bot/src/bot/mod.rs index 039a894..6efdc8f 100644 --- a/geoffrey_bot/src/bot/mod.rs +++ b/geoffrey_bot/src/bot/mod.rs @@ -5,6 +5,7 @@ use commands::add_item::AddItemCommand; use commands::add_location::AddLocationCommand; use commands::find::FindCommand; use commands::selling::SellingCommand; +use commands::set_portal::SetPortalCommand; use commands::{BotCommand, CommandError}; pub mod arg_parse; @@ -18,6 +19,7 @@ pub async fn create_commands(ctx: &Context) -> Result, C commands.push(SellingCommand::create_app_command(ctx).await?); commands.push(AddLocationCommand::create_app_command(ctx).await?); commands.push(AddItemCommand::create_app_command(ctx).await?); + commands.push( SetPortalCommand::create_app_command(ctx).await?); Ok(commands) } diff --git a/geoffrey_bot/src/main.rs b/geoffrey_bot/src/main.rs index 94a5b1f..d391888 100644 --- a/geoffrey_bot/src/main.rs +++ b/geoffrey_bot/src/main.rs @@ -25,6 +25,7 @@ use std::path::PathBuf; use structopt::StructOpt; use geoffrey_models::logging::LogLevel; use crate::logging::init_logging; +use crate::bot::commands::set_portal::SetPortalCommand; #[derive(Debug, StructOpt, Clone)] #[structopt(name = "GeoffreyBot", about = "Geoffrey Discord Bot")] @@ -81,6 +82,7 @@ impl EventHandler for Handler { AddLocationCommand::command(geoffrey_ctx, user_id, command.clone()).await } "add_item" => AddItemCommand::command(geoffrey_ctx, user_id, command.clone()).await, + "set_portal" => SetPortalCommand::command(geoffrey_ctx, user_id, command.clone()).await, _ => "not implemented :(".to_string(), }; diff --git a/geoffrey_models/src/models/mod.rs b/geoffrey_models/src/models/mod.rs index 11bd4ca..d4f52e4 100644 --- a/geoffrey_models/src/models/mod.rs +++ b/geoffrey_models/src/models/mod.rs @@ -96,8 +96,8 @@ impl Display for Position { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Portal { - x: i32, - z: i32, + pub x: i32, + pub z: i32, } impl Portal { @@ -118,6 +118,13 @@ impl Portal { Direction::South } } + + pub fn tunnel_addr(&self) -> i32 { + match self.direction() { + Direction::North | Direction::South => self.z.abs(), + Direction::East | Direction::West => self.x.abs() + } + } } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]