From 55c393b7a875c59c08dfba77e0aa14045db0e050 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 16 Nov 2020 20:46:52 -0400 Subject: [PATCH] move the tech level adjustment to prevent underflow for new FOs. let characters use tech disks --- src/entity/character.rs | 4 ++-- src/ship/items/manager.rs | 9 +++++++-- src/ship/packet/handler/message.rs | 2 +- tests/test_item_use.rs | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/entity/character.rs b/src/entity/character.rs index 00631bb..c10bffd 100644 --- a/src/entity/character.rs +++ b/src/entity/character.rs @@ -173,7 +173,7 @@ impl CharacterTechniques { } pub fn set_tech(&mut self, tech: Technique, level: TechLevel) { - self.techs.insert(tech, level); + self.techs.insert(tech, TechLevel(level.0 - 1)); } // from_bytes @@ -182,7 +182,7 @@ impl CharacterTechniques { self.techs.iter() .fold([0xFF; 20], |mut techlist, (tech, level)| { let index = tech.as_value(); - techlist[index as usize] = level.0 - 1; + techlist[index as usize] = level.0; techlist }) } diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index 3a76284..07c4005 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -2,7 +2,7 @@ use crate::ship::items::ClientItemId; use std::collections::{HashMap, BTreeMap}; use thiserror::Error; use crate::entity::gateway::EntityGateway; -use crate::entity::character::{CharacterEntity, CharacterEntityId}; +use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel}; use crate::entity::item::{ItemDetail, ItemLocation, BankName}; use crate::entity::item::{Meseta, NewItemEntity, ItemEntity, InventoryItemEntity, EquippedEntity, InventoryEntity, BankItemEntity, BankEntity}; use crate::entity::item::tool::{Tool, ToolType}; @@ -490,13 +490,18 @@ impl ItemManager { pub async fn player_consumes_tool(&mut self, entity_gateway: &mut EG, - character: &CharacterEntity, + character: &mut CharacterEntity, item_id: ClientItemId, amount: usize) -> Result { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let used_item = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; let consumed_item = used_item.consume(amount)?; + + if let ItemDetail::TechniqueDisk(tech_disk) = consumed_item.item() { + character.techs.set_tech(tech_disk.tech, TechLevel(tech_disk.level as u8)); + entity_gateway.save_character(character).await?; + }; for entity_id in consumed_item.entity_ids() { entity_gateway.change_item_location(&entity_id, diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index 79ceaa5..f8a9f82 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -261,7 +261,7 @@ where EG: EntityGateway { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; - let item_used_type = item_manager.player_consumes_tool(entity_gateway, &client.character, ClientItemId(player_use_tool.item_id), 1).await?; + let item_used_type = item_manager.player_consumes_tool(entity_gateway, &mut client.character, ClientItemId(player_use_tool.item_id), 1).await?; item_manager.use_item(item_used_type, entity_gateway, &mut client.character).await?; Ok(Box::new(None.into_iter())) diff --git a/tests/test_item_use.rs b/tests/test_item_use.rs index 8a51875..03cb935 100644 --- a/tests/test_item_use.rs +++ b/tests/test_item_use.rs @@ -266,3 +266,20 @@ async fn test_use_materials() { assert!(char.materials.luck == 2); } +#[async_std::test] +pub async fn test_learn_new_tech() {} + +#[async_std::test] +pub async fn test_new_fo_has_foie_1() {} + +#[async_std::test] +pub async fn test_char_cannot_use_lower_level_tech() {} + +#[async_std::test] +pub async fn test_char_cannot_learn_wrong_tech() {} + +#[async_std::test] +pub async fn test_char_cannot_learn_high_level_tech() {} + +#[async_std::test] +pub async fn test_android_cannot_learn_tech() {} \ No newline at end of file