Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions server/data/src/services/character/character.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use std::ops::{Add, Div};

use proto95::shared::char::{CharStatFlags, CharStatPartial};
use proto95::{
id::ItemId,
shared::char::{CharStatFlags, CharStatPartial},
};
use shroom_net::packet::CondOption;

use crate::{entities::character::Model, services::helper::intentory::inv::InventorySet};
use crate::{
entities::character::Model,
services::{
helper::intentory::inv::{InventoryExt, InventorySet, InventoryType},
meta::meta_service::ItemMeta,
model::item::{EquipItem, StackItem},
},
};

#[derive(Debug, Clone)]
pub struct Character {
Expand Down Expand Up @@ -49,13 +59,39 @@ impl Character {
self.char_stat_flags.insert(CharStatFlags::Mp);
}

pub fn update_mesos(&mut self, mesos: i32) -> bool {
if self.model.mesos + mesos < 0 {
return false;
pub fn update_mesos(&mut self, mesos: i32) -> anyhow::Result<bool> {
if self.model.mesos.checked_add(mesos).is_none() {
return Ok(false);
}
self.model.mesos = self.model.mesos.saturating_add(mesos);
self.char_stat_flags.insert(CharStatFlags::Money);
true
Ok(true)
}

pub fn update_inventory(
&mut self,
item_id: ItemId,
itype: InventoryType,
item_meta: ItemMeta,
quantity: usize,
) -> anyhow::Result<bool> {
if InventoryType::is_equip(&itype) {
let eq_inv = self.inventory.get_equipped_inventory_mut(itype)?;
let equip_item = EquipItem::from_item_id(item_id, &item_meta).into();
Comment thread
lee-aaron marked this conversation as resolved.
Ok(eq_inv
.get_inner_mut()
.try_add(equip_item)
.map(|_| true)
.unwrap_or(false))
} else {
let stack_inv = self.inventory.get_stack_inventory_mut(itype)?;
let stack_item = StackItem::from_item_id(item_id, quantity as u16).into();
Ok(stack_inv
.get_inner_mut()
.try_add(stack_item)
.map(|_| true)
.unwrap_or(false))
}
}

pub fn get_char_partial(&mut self) -> CharStatPartial {
Expand Down
39 changes: 27 additions & 12 deletions server/data/src/services/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ use proto95::{
use ractor::{Actor, ActorProcessingErr, ActorRef, RpcReplyPort};
use shroom_net::net::service::{packet_buffer::PacketBuffer, server_sess::SharedSessionHandle};

use crate::services::helper::intentory::inv::InventoryType;

use super::{
character::Character,
data::character::CharacterID,
helper::pool::{drop::DropLeaveParam, reactor::Reactor, user::User, Drop, Mob, Npc, Pool},
helper::pool::{
drop::{DropLeaveParam, DropTypeValue},
reactor::Reactor,
user::User,
Drop, Mob, Npc, Pool,
},
meta::{
fh_tree::FhTree,
meta_service::{FieldMeta, MetaService},
Expand All @@ -28,7 +35,7 @@ use super::{

#[derive(Debug)]
pub struct FieldData {
_meta: &'static MetaService,
meta: &'static MetaService,
field_meta: FieldMeta,
field_fh: &'static FhTree,
drop_pool: Pool<Drop>,
Expand Down Expand Up @@ -105,7 +112,7 @@ impl FieldData {
});

Self {
_meta: meta,
meta: meta,
field_meta,
field_fh: fh_meta,
drop_pool: Pool::new(meta),
Expand Down Expand Up @@ -240,15 +247,23 @@ impl FieldData {
Ok(())
}

// TODO: handle various drop items
pub fn handle_pickup(&self, item: DropId, char: &mut Character) -> anyhow::Result<()> {
match self.drop_pool.is_money(item) {
Some(m) => {
char.update_mesos(m.try_into().unwrap());
}
None => {}
};
Ok(())
pub fn handle_pickup(&self, item: DropId, char: &mut Character) -> anyhow::Result<bool> {
match self.drop_pool.get_item(item) {
Some(v) => match v.value {
DropTypeValue::Item(i) => {
let (item, itype) = match self.meta.get_item_data(i) {
Copy link
Copy Markdown
Contributor Author

@lee-aaron lee-aaron Apr 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is messy. any ideas on how to fix? i assume i'd need to expand later on for other kinds of items?

Some(v) => (v, InventoryType::Use),
None => match self.meta.get_eq_data(i) {
Some(v) => (v, InventoryType::Equip),
None => return Ok(false),
},
};
Ok(char.update_inventory(i, itype, item, v.quantity)?)
}
DropTypeValue::Mesos(m) => Ok(char.update_mesos(m as i32)?),
},
None => Ok(false),
}
}

pub async fn attack_mob(
Expand Down
17 changes: 17 additions & 0 deletions server/data/src/services/helper/intentory/inv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl<const CAP: usize> InventoryExt<CAP> for EquippedInventory<CAP> {
fn get_inner_mut(&mut self) -> &mut Inventory<CAP, Self::Item> {
&mut self.0
}

}

impl<const CAP: usize> EquippedInventory<CAP> {
Expand Down Expand Up @@ -182,6 +183,14 @@ impl<const CAP: usize> EquipInventory<CAP> {
pub fn items_mut(&mut self) -> impl Iterator<Item = &mut EquipItemSlot> {
self.0.items_mut()
}

pub fn get_inner(&self) -> &Inventory<CAP, EquipItemSlot> {
&self.0
}

pub fn get_inner_mut(&mut self) -> &mut Inventory<CAP, EquipItemSlot> {
&mut self.0
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -242,6 +251,14 @@ impl<const CAP: usize> StackInventory<CAP> {
pub fn items_mut(&mut self) -> impl Iterator<Item = &mut StackItemSlot> {
self.0.items_mut()
}

pub fn get_inner(&self) -> &Inventory<CAP, StackItemSlot> {
&self.0
}

pub fn get_inner_mut(&mut self) -> &mut Inventory<CAP, StackItemSlot> {
&mut self.0
}
}

#[derive(Debug, Clone, Copy, TryFromPrimitive)]
Expand Down
14 changes: 4 additions & 10 deletions server/data/src/services/helper/pool/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::services::{

use super::{next_id, Pool, PoolItem};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Drop {
pub owner: DropOwner,
pub pos: Vec2,
Expand All @@ -30,7 +30,7 @@ pub struct Drop {
pub quantity: usize,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum DropTypeValue {
Mesos(u32),
Item(ItemId),
Expand Down Expand Up @@ -108,18 +108,12 @@ impl PoolItem for Drop {
}

impl Pool<Drop> {
pub fn is_money(&self, item: DropId) -> Option<u32> {
pub fn get_item(&self, item: DropId) -> Option<Drop> {
let pool = match self.items.read() {
Ok(map) => map,
Err(_) => return None,
};
match pool.get(&item) {
Some(i) => match i.value {
DropTypeValue::Item(_) => None,
DropTypeValue::Mesos(m) => Some(m),
},
None => None,
}
pool.get(&item).cloned()
}

pub fn add_mob_drops(
Expand Down
16 changes: 10 additions & 6 deletions server/game/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,16 @@ impl GameHandler {
) -> GameResult<CharStatChangedResp> {
dbg!(&req);

self.field
let remove_drop = self
.field
.handle_pickup(req.drop_id, &mut self.session.char)?;
self.field.remove_drop(
req.drop_id,
DropLeaveParam::UserPickup(self.session.char.model.id as u32),
)?;
if remove_drop {
self.field.remove_drop(
req.drop_id,
DropLeaveParam::UserPickup(self.session.char.model.id as u32),
)?;
}

Ok(CharStatChangedResp {
excl: true,
stats: PartialFlag {
Expand All @@ -535,7 +539,7 @@ impl GameHandler {
&mut self,
req: UserDropMoneyReq,
) -> GameResult<CharStatChangedResp> {
let ok = self.session.char.update_mesos((req.money as i32).neg());
let ok = self.session.char.update_mesos((req.money as i32).neg())?;
if ok {
self.field.add_drop(Drop {
owner: proto95::game::drop::DropOwner::User(self.session.char.model.id as u32),
Expand Down