From 2be6c80cbe33e77b3dc95400b1e821ce95bc8b74 Mon Sep 17 00:00:00 2001 From: serxoz Date: Thu, 15 Sep 2022 17:10:11 +0200 Subject: [PATCH] comando matar --- Cargo.lock | 1 + Cargo.toml | 1 + TODO.md | 1 + src/player.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/rlib.rs | 1 + 5 files changed, 51 insertions(+) create mode 100644 TODO.md diff --git a/Cargo.lock b/Cargo.lock index 10b622b..07b6942 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,6 +194,7 @@ version = "0.1.0" dependencies = [ "diesel", "dotenv", + "rand", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index ec331fc..3e4b3e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] diesel = { version = "2.0.0", features = ["mysql"] } dotenv = "0.15.0" +rand = "0.8.5" [dependencies.uuid] version = "1.1.2" diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..a352a2a --- /dev/null +++ b/TODO.md @@ -0,0 +1 @@ +- Revisar instanciado dos xogadores. Durante as probas do combate pareceume que non se instanciaban ben cando se usaba un xogador que xa existía. diff --git a/src/player.rs b/src/player.rs index 167969b..610706c 100644 --- a/src/player.rs +++ b/src/player.rs @@ -2,6 +2,8 @@ use crate::objects::{get_objects, get_object_here, update_object_location}; use crate::models::Player; use crate::database::establish_connection; use diesel::prelude::*; +use crate::npc::get_npc_here; +use rand::Rng; // get_player le o xogador polo nick pub fn get_player(nome: String) -> Result { @@ -117,3 +119,48 @@ pub fn execute_drop(player: &mut Player, nombre: &str) -> String { output } + +// execute_kill intenta matar un npc +pub fn execute_kill(player: &mut Player, nombre: &str) -> String { + let output: String; + let location = &player.location; + let npc = get_npc_here(location.to_string(), nombre.to_string()); + + if npc.is_ok() { + // non hai iniciativa, comeza o xogador + let mut malo = npc.unwrap(); + while malo.hp >= 0 && player.hp >= 0 { + // turno do xogador: + let tohit = rand::thread_rng().gen_range(1..20) + player.ba; + if tohit >= malo.ca { + let damage = rand::thread_rng().gen_range(1..6); + malo.hp = malo.hp - damage; + println!("Golpeas a {} por {} de daño, le quedan {} pg.", malo.nombre, damage, malo.hp); + } else { + println!("No consigues golpear a {}.", malo.nombre); + } + + // turno do malo + let tohit = rand::thread_rng().gen_range(1..20) + malo.ba; + if tohit >= player.ca { + let damage = rand::thread_rng().gen_range(1..6); + player.hp = player.hp - damage; + println!("{} te golpea por {} de daño, te quedan {} pg.", malo.nombre, damage, player.hp); + } else { + println!("{} no consigue herirte.", malo.nombre); + } + } + + if malo.hp <= 0 { + output = format!("{} ha muerto!", malo.nombre); + } else if player.hp <= 0 { + output = format!("{} ha muerto!", player.nombre); + } else { + output = String::from("No se que ha pasado. Alguien debería estar muerto.") + } + } else { + output = String::from("No encuentro lo que quieres matar."); + } + + output +} diff --git a/src/rlib.rs b/src/rlib.rs index 7cfb44e..b76ca24 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -104,6 +104,7 @@ pub fn update_state(player: &mut Player, command: &Command) -> String { "inventario" => output = execute_inventory(player), "coger" => output = execute_pick(player, command.noun.as_str()), "tirar" => output = execute_drop(player, command.noun.as_str()), + "matar" => output = execute_kill(player, command.noun.as_str()), _ => output = format!("No se como hacer eso."), }