mirror of
https://github.com/RustySamovar/RustySamovar.git
synced 2024-11-24 11:23:33 +00:00
Inventory subsystem: WIP
This commit is contained in:
parent
c1cedcb189
commit
5ca48a5693
83
src/subsystems/inventory_subsystem/inventory_subsystem.rs
Normal file
83
src/subsystems/inventory_subsystem/inventory_subsystem.rs
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
use std::sync::{Arc, mpsc};
|
||||||
|
|
||||||
|
use crate::server::IpcMessage;
|
||||||
|
use crate::{DatabaseManager, JsonManager};
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
use packet_processor::*;
|
||||||
|
|
||||||
|
pub struct InventorySubsystem {
|
||||||
|
packets_to_send_tx: mpsc::Sender<IpcMessage>,
|
||||||
|
db: Arc<DatabaseManager>,
|
||||||
|
jm: Arc<JsonManager>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InventorySubsystem {
|
||||||
|
pub fn new(jm: Arc<JsonManager>, db: Arc<DatabaseManager>, packets_to_send_tx: mpsc::Sender<IpcMessage>) -> Self {
|
||||||
|
Self {
|
||||||
|
packets_to_send_tx: packets_to_send_tx.clone(),
|
||||||
|
db: db.clone(),
|
||||||
|
jm: jm.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_item(&self, user_id: u32, metadata: &proto::PacketHead, item_id: u32, count: u32, reason: &proto::ActionReasonType, inform_user: bool) {
|
||||||
|
let (item, is_new) = if self.jm.is_item_weapon(item_id) || self.jm.is_item_reliquary(item_id) {
|
||||||
|
assert!(count == 1);
|
||||||
|
(self.db.add_equip(user_id, item_id).unwrap(), false) // TODO: is new equip considered a new item?
|
||||||
|
} else {
|
||||||
|
let old_count = self.db.get_item_count_by_item_id(user_id, item_id);
|
||||||
|
|
||||||
|
(self.db.add_stackable(user_id, item_id, count as i32).unwrap(), old_count == 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
if inform_user {
|
||||||
|
build_and_send!(self, user_id, metadata, ItemAddHintNotify {
|
||||||
|
item_list: vec![build!(ItemHint {
|
||||||
|
item_id: item_id,
|
||||||
|
count: count,
|
||||||
|
is_new: is_new,
|
||||||
|
})],
|
||||||
|
reason: *reason as u32,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
build_and_send!(self, user_id, metadata, StoreItemChangeNotify {
|
||||||
|
store_type: proto::StoreType::StorePack as i32, // TODO: hardcoded!
|
||||||
|
item_list: vec![item],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sub_item(&self, user_id: u32, metadata: &proto::PacketHead, item_id: u32, count: u32, reason: &proto::ActionReasonType) {
|
||||||
|
let old_amount = self.db.get_item_count_by_item_id(user_id, item_id);
|
||||||
|
|
||||||
|
assert!(old_amount >= count);
|
||||||
|
|
||||||
|
let (new_amount, item) = if self.jm.is_item_weapon(item_id) || self.jm.is_item_reliquary(item_id) {
|
||||||
|
panic!("You can't 'substract' a weapon or reliquary {}!", item_id)
|
||||||
|
} else {
|
||||||
|
let item = if old_amount > count {
|
||||||
|
// Just "add" a negative amount of items
|
||||||
|
self.db.add_stackable(user_id, item_id, -(count as i32)).unwrap()
|
||||||
|
} else {
|
||||||
|
self.db.remove_item_by_item_id(user_id, item_id).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
(old_amount - count, item)
|
||||||
|
};
|
||||||
|
|
||||||
|
if new_amount > 0 {
|
||||||
|
assert!(item.detail != None);
|
||||||
|
|
||||||
|
build_and_send!(self, user_id, metadata, StoreItemChangeNotify {
|
||||||
|
store_type: proto::StoreType::StorePack as i32,
|
||||||
|
item_list: vec![item],
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
build_and_send!(self, user_id, metadata, StoreItemDelNotify {
|
||||||
|
store_type: proto::StoreType::StorePack as i32, // TODO: hardcoded!
|
||||||
|
guid_list: vec![item.guid],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
src/subsystems/inventory_subsystem/mod.rs
Normal file
3
src/subsystems/inventory_subsystem/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod inventory_subsystem;
|
||||||
|
|
||||||
|
pub use self::inventory_subsystem::InventorySubsystem;
|
@ -1,6 +1,8 @@
|
|||||||
pub mod entity_subsystem;
|
pub mod entity_subsystem;
|
||||||
|
pub mod inventory_subsystem;
|
||||||
pub mod misc;
|
pub mod misc;
|
||||||
|
|
||||||
pub use self::entity_subsystem::EntitySubsystem;
|
pub use self::entity_subsystem::EntitySubsystem;
|
||||||
|
pub use self::inventory_subsystem::InventorySubsystem;
|
||||||
pub use self::misc::NpcSubsystem;
|
pub use self::misc::NpcSubsystem;
|
||||||
pub use self::misc::ShopSubsystem;
|
pub use self::misc::ShopSubsystem;
|
Loading…
Reference in New Issue
Block a user