comando matar

This commit is contained in:
serxoz 2022-09-15 17:10:11 +02:00
parent 01fac69f0b
commit 2be6c80cbe
5 changed files with 51 additions and 0 deletions

1
Cargo.lock generated
View File

@ -194,6 +194,7 @@ version = "0.1.0"
dependencies = [
"diesel",
"dotenv",
"rand",
"uuid",
]

View File

@ -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"

1
TODO.md Normal file
View File

@ -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.

View File

@ -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<Player, diesel::result::Error> {
@ -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
}

View File

@ -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."),
}