Browse Source

player can (un)equip things now

pbs
andy 4 years ago
parent
commit
48abf4533f
  1. 76
      src/bin/main.rs
  2. 42
      src/ship/items/manager.rs
  3. 29
      src/ship/packet/handler/message.rs
  4. 7
      src/ship/ship.rs

76
src/bin/main.rs

@ -194,6 +194,82 @@ fn main() {
name: item::BankName("".to_string()), name: item::BankName("".to_string()),
} }
}).await; }).await;
entity_gateway.create_item(
NewItemEntity {
item: ItemDetail::Armor(
item::armor::Armor {
armor: item::armor::ArmorType::Frame,
dfp: 0,
evp: 0,
slots: 4,
modifiers: Vec::new(),
}
),
location: ItemLocation::Inventory {
character_id: character.id,
slot: 5,
equipped: true,
}
}
).await;
entity_gateway.create_item(
NewItemEntity {
item: ItemDetail::Shield(
item::shield::Shield {
shield: item::shield::ShieldType::Barrier,
dfp: 0,
evp: 0,
}
),
location: ItemLocation::Inventory {
character_id: character.id,
slot: 6,
equipped: true,
}
}
).await;
entity_gateway.create_item(
NewItemEntity {
item: ItemDetail::Unit(
item::unit::Unit {
unit: item::unit::UnitType::KnightPower,
modifier: Some(item::unit::UnitModifier::Plus),
}
),
location: ItemLocation::Inventory {
character_id: character.id,
slot: 7,
equipped: true,
}
}
).await;
entity_gateway.create_item(
NewItemEntity {
item: ItemDetail::Unit(
item::unit::Unit {
unit: item::unit::UnitType::KnightPower,
modifier: Some(item::unit::UnitModifier::PlusPlus),
}
),
location: ItemLocation::Inventory {
character_id: character.id,
slot: 8,
equipped: true,
}
}
).await;
entity_gateway.create_item(
NewItemEntity {
item: ItemDetail::Mag(
item::mag::Mag::baby_mag(5)
),
location: ItemLocation::Inventory {
character_id: character.id,
slot: 9,
equipped: true,
}
}
).await;
} }
info!("[patch] starting server"); info!("[patch] starting server");

42
src/ship/items/manager.rs

@ -42,7 +42,9 @@ pub enum ItemManagerError {
UseItemError(#[from] use_tool::UseItemError), UseItemError(#[from] use_tool::UseItemError),
CouldNotBuyItem, CouldNotBuyItem,
CouldNotAddBoughtItemToInventory, CouldNotAddBoughtItemToInventory,
ItemIdNotInInventory(ClientItemId)
ItemIdNotInInventory(ClientItemId),
CannotGetMutItem,
CannotGetIndividualItem,
} }
@ -893,4 +895,42 @@ impl ItemManager {
}; };
Ok(inventory_item) Ok(inventory_item)
} }
pub async fn player_equips_item<EG: EntityGateway>(&mut self,
entity_gateway: &mut EG,
character: &CharacterEntity,
item_id: ClientItemId)
-> Result<(), ItemManagerError> {
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let mut inventory_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
let inventory_item = inventory_item_handle.item_mut().ok_or(ItemManagerError::CannotGetMutItem)?.individual().ok_or(ItemManagerError::CannotGetIndividualItem)?;
inventory_item.equipped = true;
entity_gateway.change_item_location(&inventory_item.entity_id, ItemLocation::Inventory{
character_id: character.id,
slot: character.slot as usize,
equipped: true,
}).await;
entity_gateway.save_character(character).await;
Ok(())
}
pub async fn player_unequips_item<EG: EntityGateway>(&mut self,
entity_gateway: &mut EG,
character: &CharacterEntity,
item_id: ClientItemId)
-> Result<(), ItemManagerError> {
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let mut inventory_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
let inventory_item = inventory_item_handle.item_mut().ok_or(ItemManagerError::CannotGetMutItem)?.individual().ok_or(ItemManagerError::CannotGetIndividualItem)?;
inventory_item.equipped = false;
entity_gateway.change_item_location(&inventory_item.entity_id, ItemLocation::Inventory{
character_id: character.id,
slot: character.slot as usize,
equipped: false,
}).await;
entity_gateway.save_character(character).await;
Ok(())
}
} }

29
src/ship/packet/handler/message.rs

@ -307,3 +307,32 @@ where
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone())))) (client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone()))))
}))) })))
} }
pub async fn player_equips_item<EG>(id: ClientId,
pkt: &PlayerEquipItem,
entity_gateway: &mut EG,
clients: &Clients,
item_manager: &mut ItemManager)
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
EG: EntityGateway
{
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
item_manager.player_equips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id)).await?;
Ok(Box::new(None.into_iter()))
}
pub async fn player_unequips_item<EG>(id: ClientId,
pkt: &PlayerUnequipItem,
entity_gateway: &mut EG,
clients: &Clients,
item_manager: &mut ItemManager)
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
EG: EntityGateway
{
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
item_manager.player_unequips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id)).await?;
Ok(Box::new(None.into_iter()))
}

7
src/ship/ship.rs

@ -393,6 +393,13 @@ impl<EG: EntityGateway> ShipServerState<EG> {
GameMessage::PlayerFeedMag(player_feed_mag) => { GameMessage::PlayerFeedMag(player_feed_mag) => {
handler::message::player_feed_mag(id, &player_feed_mag, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.item_manager).await handler::message::player_feed_mag(id, &player_feed_mag, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.item_manager).await
}, },
GameMessage::PlayerEquipItem(player_equip_item) => {
handler::message::player_equips_item(id, &player_equip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await
},
GameMessage::PlayerUnequipItem(player_unequip_item) => {
handler::message::player_unequips_item(id, &player_unequip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await
},
_ => { _ => {
let cmsg = msg.clone(); let cmsg = msg.clone();
Ok(Box::new(self.client_location.get_client_neighbors(id).unwrap().into_iter() Ok(Box::new(self.client_location.get_client_neighbors(id).unwrap().into_iter()

Loading…
Cancel
Save