From c8b9564009e5d78f03d5afdf26acc98fbafcec57 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 23 Jan 2021 23:39:04 +0000 Subject: [PATCH] random formatting and ordering changes --- src/entity/gateway/entitygateway.rs | 4 +- src/entity/gateway/inmemory.rs | 61 ++-- .../postgres/migrations/V0001__initial.sql | 24 +- src/entity/gateway/postgres/models.rs | 260 ++++++++---------- src/entity/gateway/postgres/postgres.rs | 102 +++---- src/entity/item/armor.rs | 2 - src/entity/item/esweapon.rs | 2 - src/entity/item/mag.rs | 4 - src/entity/item/mod.rs | 3 - src/entity/item/unit.rs | 1 - src/entity/item/weapon.rs | 5 - src/ship/items/inventory.rs | 44 +-- src/ship/items/manager.rs | 25 +- src/ship/map/enemy.rs | 6 - tests/test_item_modifiers.rs | 18 +- 15 files changed, 255 insertions(+), 306 deletions(-) diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index 965ddf2..78d01ee 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -93,11 +93,11 @@ pub trait EntityGateway: Send + Sync + Clone { unimplemented!(); } - async fn add_shield_modifier(&mut self, _item_id: &ItemEntityId, _modifier: shield::ShieldModifier) -> Result<(), GatewayError> { + async fn add_unit_modifier(&mut self, _item_id: &ItemEntityId, _modifier: unit::UnitModifier) -> Result<(), GatewayError> { unimplemented!(); } - async fn add_unit_modifier(&mut self, _item_id: &ItemEntityId, _modifier: unit::UnitModifier) -> Result<(), GatewayError> { + async fn add_shield_modifier(&mut self, _item_id: &ItemEntityId, _modifier: shield::ShieldModifier) -> Result<(), GatewayError> { unimplemented!(); } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 428c5f3..e0fd1f2 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -17,12 +17,12 @@ pub struct InMemoryGateway { inventories: Arc>>, banks: Arc>>, equips: Arc>>, - mag_modifiers: Arc>>>, weapon_modifiers: Arc>>>, esweapon_modifiers: Arc>>>, armor_modifiers: Arc>>>, - shield_modifiers: Arc>>>, unit_modifiers: Arc>>>, + shield_modifiers: Arc>>>, + mag_modifiers: Arc>>>, tech_modifiers: Arc>>>, tool_modifiers: Arc>>>, } @@ -37,12 +37,12 @@ impl InMemoryGateway { inventories: Arc::new(Mutex::new(BTreeMap::new())), banks: Arc::new(Mutex::new(BTreeMap::new())), equips: Arc::new(Mutex::new(BTreeMap::new())), - mag_modifiers: Arc::new(Mutex::new(BTreeMap::new())), weapon_modifiers: Arc::new(Mutex::new(BTreeMap::new())), esweapon_modifiers: Arc::new(Mutex::new(BTreeMap::new())), armor_modifiers: Arc::new(Mutex::new(BTreeMap::new())), - shield_modifiers: Arc::new(Mutex::new(BTreeMap::new())), unit_modifiers: Arc::new(Mutex::new(BTreeMap::new())), + shield_modifiers: Arc::new(Mutex::new(BTreeMap::new())), + mag_modifiers: Arc::new(Mutex::new(BTreeMap::new())), tech_modifiers: Arc::new(Mutex::new(BTreeMap::new())), tool_modifiers: Arc::new(Mutex::new(BTreeMap::new())), } @@ -80,14 +80,6 @@ impl InMemoryGateway { } ItemDetail::Armor(armor) }, - ItemDetail::Shield(mut shield) => { - if let Some(shield_modifiers) = self.shield_modifiers.lock().unwrap().get(&item.id) { - for shield_modifier in shield_modifiers.iter() { - shield.apply_modifier(&shield_modifier); - } - } - ItemDetail::Shield(shield) - }, ItemDetail::Unit(mut unit) => { if let Some(unit_modifiers) = self.unit_modifiers.lock().unwrap().get(&item.id) { for unit_modifier in unit_modifiers.iter() { @@ -96,21 +88,13 @@ impl InMemoryGateway { } ItemDetail::Unit(unit) }, - ItemDetail::TechniqueDisk(mut tech) => { - if let Some(tech_modifiers) = self.tech_modifiers.lock().unwrap().get(&item.id) { - for tech_modifier in tech_modifiers.iter() { - tech.apply_modifier(&tech_modifier); - } - } - ItemDetail::TechniqueDisk(tech) - }, - ItemDetail::Tool(mut tool) => { - if let Some(tool_modifiers) = self.tool_modifiers.lock().unwrap().get(&item.id) { - for tool_modifier in tool_modifiers.iter() { - tool.apply_modifier(&tool_modifier); + ItemDetail::Shield(mut shield) => { + if let Some(shield_modifiers) = self.shield_modifiers.lock().unwrap().get(&item.id) { + for shield_modifier in shield_modifiers.iter() { + shield.apply_modifier(&shield_modifier); } } - ItemDetail::Tool(tool) + ItemDetail::Shield(shield) }, ItemDetail::Mag(mag) => { let mut mag = { @@ -148,9 +132,22 @@ impl InMemoryGateway { } ItemDetail::Mag(mag) }, - // _ => { - // item.item - // } + ItemDetail::TechniqueDisk(mut tech) => { + if let Some(tech_modifiers) = self.tech_modifiers.lock().unwrap().get(&item.id) { + for tech_modifier in tech_modifiers.iter() { + tech.apply_modifier(&tech_modifier); + } + } + ItemDetail::TechniqueDisk(tech) + }, + ItemDetail::Tool(mut tool) => { + if let Some(tool_modifiers) = self.tool_modifiers.lock().unwrap().get(&item.id) { + for tool_modifier in tool_modifiers.iter() { + tool.apply_modifier(&tool_modifier); + } + } + ItemDetail::Tool(tool) + }, }; item }) @@ -354,16 +351,16 @@ impl EntityGateway for InMemoryGateway { Ok(()) } - async fn add_shield_modifier(&mut self, item_id: &ItemEntityId, modifier: shield::ShieldModifier) -> Result<(), GatewayError> { - self.shield_modifiers.lock().unwrap() + async fn add_unit_modifier(&mut self, item_id: &ItemEntityId, modifier: unit::UnitModifier) -> Result<(), GatewayError> { + self.unit_modifiers.lock().unwrap() .entry(*item_id) .or_insert(Vec::new()) .push(modifier); Ok(()) } - async fn add_unit_modifier(&mut self, item_id: &ItemEntityId, modifier: unit::UnitModifier) -> Result<(), GatewayError> { - self.unit_modifiers.lock().unwrap() + async fn add_shield_modifier(&mut self, item_id: &ItemEntityId, modifier: shield::ShieldModifier) -> Result<(), GatewayError> { + self.shield_modifiers.lock().unwrap() .entry(*item_id) .or_insert(Vec::new()) .push(modifier); diff --git a/src/entity/gateway/postgres/migrations/V0001__initial.sql b/src/entity/gateway/postgres/migrations/V0001__initial.sql index 11b9eff..339c424 100644 --- a/src/entity/gateway/postgres/migrations/V0001__initial.sql +++ b/src/entity/gateway/postgres/migrations/V0001__initial.sql @@ -93,14 +93,14 @@ create table weapon_modifier ( created_at timestamptz default current_timestamp not null ); -create table armor_modifier ( - armor integer references item (id) not null, +create table esweapon_modifier ( + esweapon integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); -create table shield_modifier ( - shield integer references item (id) not null, +create table armor_modifier ( + armor integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); @@ -111,26 +111,26 @@ create table unit_modifier ( created_at timestamptz default current_timestamp not null ); -create table tech_modifier ( - tech integer references item (id) not null, +create table shield_modifier ( + shield integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); -create table tool_modifier ( - tool integer references item (id) not null, +create table mag_modifier ( + mag integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); -create table esweapon_modifier ( - esweapon integer references item (id) not null, +create table tech_modifier ( + tech integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); -create table mag_modifier ( - mag integer references item (id) not null, +create table tool_modifier ( + tool integer references item (id) not null, modifier jsonb not null, created_at timestamptz default current_timestamp not null ); diff --git a/src/entity/gateway/postgres/models.rs b/src/entity/gateway/postgres/models.rs index 04409c2..1b83ae7 100644 --- a/src/entity/gateway/postgres/models.rs +++ b/src/entity/gateway/postgres/models.rs @@ -178,7 +178,6 @@ impl From for PgSectionId { } } - #[derive(Debug, sqlx::FromRow)] pub struct PgCharacter { pub id: i32, @@ -277,8 +276,6 @@ impl Into for PgCharacter { pub struct PgGuildCard { } - - #[derive(Debug, Serialize, Deserialize)] pub struct PgWeapon { weapon: weapon::WeaponType, @@ -329,6 +326,45 @@ pub struct PgWeaponModifier { pub modifier: sqlx::types::Json, } +#[derive(Debug, Serialize, Deserialize)] +pub struct PgESWeapon { + esweapon: esweapon::ESWeaponType, + special: Option, + name: String, + grind: u8, + wrapping: Option, +} + +impl From for PgESWeapon { + fn from(other: esweapon::ESWeapon) -> PgESWeapon { + PgESWeapon { + esweapon: other.esweapon, + special: other.special, + name: other.name, + grind: other.grind, + wrapping: other.wrapping, + } + } +} + +impl Into for PgESWeapon { + fn into(self) -> esweapon::ESWeapon { + esweapon::ESWeapon { + esweapon: self.esweapon, + special: self.special, + name: self.name, + grind: self.grind, + wrapping: self.wrapping, + } + } +} + +#[derive(Debug, sqlx::FromRow)] +pub struct PgESWeaponModifier { + pub esweapon: i32, + pub modifier: sqlx::types::Json, +} + #[derive(Debug, Serialize, Deserialize)] pub struct PgArmor { armor: armor::ArmorType, @@ -368,42 +404,6 @@ pub struct PgArmorModifier { pub modifier: sqlx::types::Json, } -#[derive(Debug, Serialize, Deserialize)] -pub struct PgShield { - shield: shield::ShieldType, - dfp: u8, - evp: u8, - wrapping: Option, -} - -impl From for PgShield { - fn from(other: shield::Shield) -> PgShield { - PgShield { - shield: other.shield, - dfp: other.dfp, - evp: other.evp, - wrapping: other.wrapping, - } - } -} - -impl Into for PgShield { - fn into(self) -> shield::Shield { - shield::Shield { - shield: self.shield, - dfp: self.dfp, - evp: self.evp, - wrapping: self.wrapping, - } - } -} - -#[derive(Debug, sqlx::FromRow)] -pub struct PgShieldModifier { - pub shield: i32, - pub modifier: sqlx::types::Json, -} - #[derive(Debug, Serialize, Deserialize)] pub struct PgUnit { unit: unit::UnitType, @@ -438,33 +438,68 @@ pub struct PgUnitModifier { } #[derive(Debug, Serialize, Deserialize)] -pub struct PgTool { - pub tool: tool::ToolType, +pub struct PgShield { + shield: shield::ShieldType, + dfp: u8, + evp: u8, wrapping: Option, } -impl From for PgTool { - fn from(other: tool::Tool) -> PgTool { - PgTool { - tool: other.tool, +impl From for PgShield { + fn from(other: shield::Shield) -> PgShield { + PgShield { + shield: other.shield, + dfp: other.dfp, + evp: other.evp, wrapping: other.wrapping, } } } -impl Into for PgTool { - fn into(self) -> tool::Tool { - tool::Tool { - tool: self.tool, +impl Into for PgShield { + fn into(self) -> shield::Shield { + shield::Shield { + shield: self.shield, + dfp: self.dfp, + evp: self.evp, wrapping: self.wrapping, } } } #[derive(Debug, sqlx::FromRow)] -pub struct PgToolModifier { - pub tool: i32, - pub modifier: sqlx::types::Json, +pub struct PgShieldModifier { + pub shield: i32, + pub modifier: sqlx::types::Json, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct PgMag { + mag: mag::MagType, + synchro: u8, + color: u8, + wrapping: Option, +} + +impl From for PgMag { + fn from(other: mag::Mag) -> PgMag { + PgMag { + mag: other.mag, + synchro: other.synchro, + color: other.color, + wrapping: other.wrapping, + } + } +} + +impl Into for PgMag { + fn into(self) -> mag::Mag { + let mut mag = mag::Mag::baby_mag(self.color as u16); + mag.mag = self.mag; + mag.synchro = self.synchro; + mag.wrapping = self.wrapping; + mag + } } #[derive(Debug, Serialize, Deserialize)] @@ -501,110 +536,58 @@ pub struct PgTechModifier { } #[derive(Debug, Serialize, Deserialize)] -pub struct PgMag { - mag: mag::MagType, - synchro: u8, - color: u8, - wrapping: Option, -} - -impl From for PgMag { - fn from(other: mag::Mag) -> PgMag { - PgMag { - mag: other.mag, - synchro: other.synchro, - color: other.color, - wrapping: other.wrapping, - } - } -} - -impl Into for PgMag { - fn into(self) -> mag::Mag { - /*mag::Mag { - mag: self.mag, - synchro: self.synchro, - color: self.color, - def: 500, - pow: 0, - dex: 0, - mnd: 0, - iq: 0, - photon_blast: [None; 3], - class: CharacterClass::HUmar, - id: SectionID::Viridia, - }*/ - let mut mag = mag::Mag::baby_mag(self.color as u16); - mag.mag = self.mag; - mag.synchro = self.synchro; - mag.wrapping = self.wrapping; - mag - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct PgESWeapon { - esweapon: esweapon::ESWeaponType, - special: Option, - name: String, - grind: u8, +pub struct PgTool { + pub tool: tool::ToolType, wrapping: Option, } -impl From for PgESWeapon { - fn from(other: esweapon::ESWeapon) -> PgESWeapon { - PgESWeapon { - esweapon: other.esweapon, - special: other.special, - name: other.name, - grind: other.grind, +impl From for PgTool { + fn from(other: tool::Tool) -> PgTool { + PgTool { + tool: other.tool, wrapping: other.wrapping, } } } -impl Into for PgESWeapon { - fn into(self) -> esweapon::ESWeapon { - esweapon::ESWeapon { - esweapon: self.esweapon, - special: self.special, - name: self.name, - grind: self.grind, +impl Into for PgTool { + fn into(self) -> tool::Tool { + tool::Tool { + tool: self.tool, wrapping: self.wrapping, } } } #[derive(Debug, sqlx::FromRow)] -pub struct PgESWeaponModifier { - pub esweapon: i32, - pub modifier: sqlx::types::Json, +pub struct PgToolModifier { + pub tool: i32, + pub modifier: sqlx::types::Json, } - #[derive(Debug, Serialize, Deserialize)] pub enum PgItemDetail { Weapon(PgWeapon), + ESWeapon(PgESWeapon), Armor(PgArmor), - Shield(PgShield), Unit(PgUnit), - Tool(PgTool), - TechDisk(PgTechDisk), + Shield(PgShield), Mag(PgMag), - ESWeapon(PgESWeapon), + TechDisk(PgTechDisk), + Tool(PgTool), } impl From for PgItemDetail { fn from(other: ItemDetail) -> PgItemDetail { match other { ItemDetail::Weapon(weapon) => PgItemDetail::Weapon(weapon.into()), + ItemDetail::ESWeapon(esweapon) => PgItemDetail::ESWeapon(esweapon.into()), ItemDetail::Armor(armor) => PgItemDetail::Armor(armor.into()), - ItemDetail::Shield(shield) => PgItemDetail::Shield(shield.into()), ItemDetail::Unit(unit) => PgItemDetail::Unit(unit.into()), - ItemDetail::Tool(tool) => PgItemDetail::Tool(tool.into()), - ItemDetail::TechniqueDisk(tech_disk) => PgItemDetail::TechDisk(tech_disk.into()), + ItemDetail::Shield(shield) => PgItemDetail::Shield(shield.into()), ItemDetail::Mag(mag) => PgItemDetail::Mag(mag.into()), - ItemDetail::ESWeapon(esweapon) => PgItemDetail::ESWeapon(esweapon.into()), + ItemDetail::TechniqueDisk(tech_disk) => PgItemDetail::TechDisk(tech_disk.into()), + ItemDetail::Tool(tool) => PgItemDetail::Tool(tool.into()), } } } @@ -613,13 +596,13 @@ impl Into for PgItemDetail { fn into(self) -> ItemDetail { match self { PgItemDetail::Weapon(weapon) => ItemDetail::Weapon(weapon.into()), + PgItemDetail::ESWeapon(esweapon) => ItemDetail::ESWeapon(esweapon.into()), PgItemDetail::Armor(armor) => ItemDetail::Armor(armor.into()), - PgItemDetail::Shield(shield) => ItemDetail::Shield(shield.into()), PgItemDetail::Unit(unit) => ItemDetail::Unit(unit.into()), - PgItemDetail::Tool(tool) => ItemDetail::Tool(tool.into()), - PgItemDetail::TechDisk(tech_disk) => ItemDetail::TechniqueDisk(tech_disk.into()), + PgItemDetail::Shield(shield) => ItemDetail::Shield(shield.into()), PgItemDetail::Mag(mag) => ItemDetail::Mag(mag.into()), - PgItemDetail::ESWeapon(esweapon) => ItemDetail::ESWeapon(esweapon.into()), + PgItemDetail::TechDisk(tech_disk) => ItemDetail::TechniqueDisk(tech_disk.into()), + PgItemDetail::Tool(tool) => ItemDetail::Tool(tool.into()), } } } @@ -630,7 +613,6 @@ pub struct PgItem { pub item: sqlx::types::Json, } - #[derive(Debug, Serialize, Deserialize)] pub enum PgItemLocationDetail { Inventory { @@ -688,7 +670,6 @@ impl Into for PgItemLocationDetail { } } - #[derive(Debug, sqlx::FromRow)] pub struct PgItemLocation { //pub id: i32, @@ -696,7 +677,6 @@ pub struct PgItemLocation { created_at: chrono::DateTime, } - #[derive(Debug, Serialize, Deserialize)] pub enum PgMagModifierDetail { FeedMag(i32), @@ -740,7 +720,6 @@ pub struct PgMagModifier { created_at: chrono::DateTime, } - #[derive(Debug, sqlx::FromRow)] pub struct PgItemEntity { pub id: i32, @@ -764,7 +743,6 @@ impl Into for PgItemWithLocation { } } - #[derive(Debug, sqlx::FromRow)] pub struct PgMagModifierWithParameters { pub mag: i32, @@ -773,7 +751,6 @@ pub struct PgMagModifierWithParameters { pub cell: Option>, } - #[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] pub enum PgInventoryItemEntity { @@ -799,11 +776,11 @@ pub struct PgEquipped { pchar: i32, weapon: Option, armor: Option, - shield: Option, unit0: Option, unit1: Option, unit2: Option, unit3: Option, + shield: Option, mag: Option, } @@ -812,12 +789,12 @@ impl Into for PgEquipped { EquippedEntity { weapon: self.weapon.map(|i| ItemEntityId(i as u32)), armor: self.armor.map(|i| ItemEntityId(i as u32)), - shield: self.shield.map(|i| ItemEntityId(i as u32)), unit: [self.unit0.map(|i| ItemEntityId(i as u32)), - self.unit1.map(|i| ItemEntityId(i as u32)), - self.unit2.map(|i| ItemEntityId(i as u32)), - self.unit3.map(|i| ItemEntityId(i as u32)), + self.unit1.map(|i| ItemEntityId(i as u32)), + self.unit2.map(|i| ItemEntityId(i as u32)), + self.unit3.map(|i| ItemEntityId(i as u32)), ], + shield: self.shield.map(|i| ItemEntityId(i as u32)), mag: self.mag.map(|i| ItemEntityId(i as u32)), } } @@ -829,13 +806,12 @@ impl From<(CharacterEntityId, EquippedEntity)> for PgEquipped { pchar: char_equips.0.0 as i32, weapon: char_equips.1.weapon.map(|i| i.0 as i32), armor: char_equips.1.armor.map(|i| i.0 as i32), - shield: char_equips.1.shield.map(|i| i.0 as i32), unit0: char_equips.1.unit[0].map(|i| i.0 as i32), unit1: char_equips.1.unit[1].map(|i| i.0 as i32), unit2: char_equips.1.unit[2].map(|i| i.0 as i32), unit3: char_equips.1.unit[3].map(|i| i.0 as i32), + shield: char_equips.1.shield.map(|i| i.0 as i32), mag: char_equips.1.mag.map(|i| i.0 as i32), } } } - diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 14d4435..4453860 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -82,39 +82,6 @@ impl PostgresGateway { ItemDetail::ESWeapon(esweapon) }, - ItemDetail::Mag(mut mag) => { - let q = r#"select mag, modifier, item.item -> 'Tool' as feed, item2.item -> 'Tool' as cell - from mag_modifier - left join item on item.id = cast (modifier ->> 'FeedMag' as integer) - left join item as item2 on item2.id = cast (modifier ->> 'MagCell' as integer) - where mag = $1 order by created_at"#; - let mag_modifiers = sqlx::query_as::<_, PgMagModifierWithParameters>(q) - .bind(id.0 as i32) - .fetch(&self.pool); - - mag_modifiers.for_each(|modifier| { - let PgMagModifierWithParameters {modifier, feed, cell, ..} = modifier.unwrap(); - let modifier: mag::MagModifier = modifier.0.into(); - match modifier { - mag::MagModifier::FeedMag{..} => { - mag.feed(feed.unwrap().tool) - }, - mag::MagModifier::BankMag => { - mag.bank() - }, - mag::MagModifier::MagCell(_) => { - mag.apply_mag_cell(mag::MagCell::try_from(Into::::into(cell.unwrap().0).tool).unwrap()) - }, - mag::MagModifier::OwnerChange(class, section_id) => { - mag.change_owner(class, section_id) - }, - mag::MagModifier::WrapPresent => {mag.apply_modifier(&modifier)}, - mag::MagModifier::UnwrapPresent => {mag.apply_modifier(&modifier)}, - } - }).await; - - ItemDetail::Mag(mag) - }, ItemDetail::Armor(mut armor) => { let q = r#"select armor, modifier from armor_modifier @@ -132,6 +99,23 @@ impl PostgresGateway { ItemDetail::Armor(armor) }, + ItemDetail::Unit(mut unit) => { + let q = r#"select unit, modifier + from unit_modifier + where unit = $1 + order by created_at"#; + let unit_modifiers = sqlx::query_as::<_, PgUnitModifier>(q) + .bind(id.0 as i32) + .fetch(&self.pool); + + unit_modifiers.for_each(|modifier| { + if let Ok(modifier) = modifier { + unit.apply_modifier(&modifier.modifier); + } + }).await; + + ItemDetail::Unit(unit) + }, ItemDetail::Shield(mut shield) => { let q = r#"select shield, modifier from shield_modifier @@ -149,22 +133,38 @@ impl PostgresGateway { ItemDetail::Shield(shield) }, - ItemDetail::Unit(mut unit) => { - let q = r#"select unit, modifier - from unit_modifier - where unit = $1 - order by created_at"#; - let unit_modifiers = sqlx::query_as::<_, PgUnitModifier>(q) - .bind(id.0 as i32) - .fetch(&self.pool); + ItemDetail::Mag(mut mag) => { + let q = r#"select mag, modifier, item.item -> 'Tool' as feed, item2.item -> 'Tool' as cell + from mag_modifier + left join item on item.id = cast (modifier ->> 'FeedMag' as integer) + left join item as item2 on item2.id = cast (modifier ->> 'MagCell' as integer) + where mag = $1 order by created_at"#; + let mag_modifiers = sqlx::query_as::<_, PgMagModifierWithParameters>(q) + .bind(id.0 as i32) + .fetch(&self.pool); - unit_modifiers.for_each(|modifier| { - if let Ok(modifier) = modifier { - unit.apply_modifier(&modifier.modifier); + mag_modifiers.for_each(|modifier| { + let PgMagModifierWithParameters {modifier, feed, cell, ..} = modifier.unwrap(); + let modifier: mag::MagModifier = modifier.0.into(); + match modifier { + mag::MagModifier::FeedMag{..} => { + mag.feed(feed.unwrap().tool) + }, + mag::MagModifier::BankMag => { + mag.bank() + }, + mag::MagModifier::MagCell(_) => { + mag.apply_mag_cell(mag::MagCell::try_from(Into::::into(cell.unwrap().0).tool).unwrap()) + }, + mag::MagModifier::OwnerChange(class, section_id) => { + mag.change_owner(class, section_id) + }, + mag::MagModifier::WrapPresent => {mag.apply_modifier(&modifier)}, + mag::MagModifier::UnwrapPresent => {mag.apply_modifier(&modifier)}, } }).await; - ItemDetail::Unit(unit) + ItemDetail::Mag(mag) }, item @ _ => item }; @@ -502,16 +502,16 @@ impl EntityGateway for PostgresGateway { Ok(()) } - async fn add_shield_modifier(&mut self, item_id: &ItemEntityId, modifier: shield::ShieldModifier) -> Result<(), GatewayError> { - sqlx::query("insert into shield_modifier (shield, modifier) values ($1, $2);") + async fn add_unit_modifier(&mut self, item_id: &ItemEntityId, modifier: unit::UnitModifier) -> Result<(), GatewayError> { + sqlx::query("insert into unit_modifier (unit, modifier) values ($1, $2);") .bind(item_id.0) .bind(sqlx::types::Json(modifier)) .execute(&self.pool).await?; Ok(()) } - async fn add_unit_modifier(&mut self, item_id: &ItemEntityId, modifier: unit::UnitModifier) -> Result<(), GatewayError> { - sqlx::query("insert into unit_modifier (unit, modifier) values ($1, $2);") + async fn add_shield_modifier(&mut self, item_id: &ItemEntityId, modifier: shield::ShieldModifier) -> Result<(), GatewayError> { + sqlx::query("insert into shield_modifier (shield, modifier) values ($1, $2);") .bind(item_id.0) .bind(sqlx::types::Json(modifier)) .execute(&self.pool).await?; @@ -707,16 +707,16 @@ impl EntityGateway for PostgresGateway { } async fn set_character_equips(&mut self, char_id: &CharacterEntityId, equips: &EquippedEntity) -> Result<(), GatewayError> { - sqlx::query(r#"insert into equipped (pchar, weapon, armor, shield, unit0, unit1, unit2, unit3, mag) values ($1, $2, $3, $4, $5, $6, $7, $8, $9) + sqlx::query(r#"insert into equipped (pchar, weapon, armor, unit0, unit1, unit2, unit3, shield, mag) values ($1, $2, $3, $4, $5, $6, $7, $8, $9) on conflict (pchar) do update set weapon=$2, armor=$3, shield=$4, unit0=$5, unit1=$6, unit2=$7, unit3=$8, mag=$9"#) .bind(char_id.0) .bind(equips.weapon.map(|i| i.0 as i32)) .bind(equips.armor.map(|i| i.0 as i32)) - .bind(equips.shield.map(|i| i.0 as i32)) .bind(equips.unit[0].map(|i| i.0 as i32)) .bind(equips.unit[1].map(|i| i.0 as i32)) .bind(equips.unit[2].map(|i| i.0 as i32)) .bind(equips.unit[3].map(|i| i.0 as i32)) + .bind(equips.shield.map(|i| i.0 as i32)) .bind(equips.mag.map(|i| i.0 as i32)) .execute(&self.pool) .await?; diff --git a/src/entity/item/armor.rs b/src/entity/item/armor.rs index 4024600..36a5cea 100644 --- a/src/entity/item/armor.rs +++ b/src/entity/item/armor.rs @@ -288,7 +288,6 @@ impl ArmorType { } } - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum ArmorModifier { AddSlot { @@ -298,7 +297,6 @@ pub enum ArmorModifier { UnwrapPresent, } - #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct Armor { pub armor: ArmorType, diff --git a/src/entity/item/esweapon.rs b/src/entity/item/esweapon.rs index 26671fd..bc68305 100644 --- a/src/entity/item/esweapon.rs +++ b/src/entity/item/esweapon.rs @@ -337,6 +337,4 @@ mod test { let bytes = testweapon.as_bytes(); assert_eq!(bytes, [0x00, 0xA7, 0x0B, 0x69, 0x00, 0x00, 0x83, 0x35, 0x9D, 0x2F, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00]); } - - } diff --git a/src/entity/item/mag.rs b/src/entity/item/mag.rs index a6ba9e4..ef4889c 100644 --- a/src/entity/item/mag.rs +++ b/src/entity/item/mag.rs @@ -56,8 +56,6 @@ lazy_static::lazy_static! { }; } - - #[derive(Debug, Copy, Clone)] pub enum ItemParseError { InvalidMagType, @@ -548,7 +546,6 @@ pub struct Mag { pub wrapping: Option, } - impl Mag { pub fn baby_mag(skin: u16) -> Mag { Mag { @@ -1216,4 +1213,3 @@ mod test { fn test_mag_does_not_level_down() { } } - diff --git a/src/entity/item/mod.rs b/src/entity/item/mod.rs index 0ae6712..a6e70d9 100644 --- a/src/entity/item/mod.rs +++ b/src/entity/item/mod.rs @@ -69,7 +69,6 @@ impl Meseta { } } - #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum ItemType { Weapon(weapon::WeaponType), @@ -279,7 +278,6 @@ impl InventoryEntity { } } - #[derive(Clone, Debug)] pub enum BankItemEntity { Individual(ItemEntity), @@ -314,7 +312,6 @@ impl BankItemEntity { } } - #[derive(Clone, Debug, Default)] pub struct BankEntity { pub items: Vec, diff --git a/src/entity/item/unit.rs b/src/entity/item/unit.rs index 6cf32c1..dac408c 100644 --- a/src/entity/item/unit.rs +++ b/src/entity/item/unit.rs @@ -343,7 +343,6 @@ pub struct Unit { pub wrapping: Option, } - impl Unit { pub fn as_bytes(&self) -> [u8; 16] { let mut result = [0; 16]; diff --git a/src/entity/item/weapon.rs b/src/entity/item/weapon.rs index eeddf4b..94ad8e8 100644 --- a/src/entity/item/weapon.rs +++ b/src/entity/item/weapon.rs @@ -44,7 +44,6 @@ impl WeaponAttribute { } } - #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize, strum_macros::EnumIter)] pub enum WeaponSpecial { Draw = 1, @@ -1420,7 +1419,6 @@ impl WeaponType { } } - #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum TekSpecialModifier { Plus, @@ -1468,7 +1466,6 @@ pub struct Weapon { pub wrapping: Option, } - impl Weapon { pub fn new(wep: WeaponType) -> Weapon { Weapon { @@ -1615,5 +1612,3 @@ impl Weapon { } } } - - diff --git a/src/ship/items/inventory.rs b/src/ship/items/inventory.rs index 5ce2765..e334ffb 100644 --- a/src/ship/items/inventory.rs +++ b/src/ship/items/inventory.rs @@ -3,14 +3,14 @@ use thiserror::Error; use libpso::character::character;//::InventoryItem; use crate::entity::character::CharacterEntityId; use crate::entity::item::{ItemEntityId, ItemDetail, ItemEntity, ItemType, ItemLocation, InventoryEntity, InventoryItemEntity, EquippedEntity}; -use crate::entity::item::tool::Tool; -use crate::entity::item::mag::Mag; use crate::entity::item::weapon::Weapon; use crate::entity::item::esweapon::ESWeapon; use crate::entity::item::armor::Armor; // TODO: cleanup uses -use crate::entity::item::shield::Shield; use crate::entity::item::unit::Unit; +use crate::entity::item::shield::Shield; +use crate::entity::item::mag::Mag; use crate::entity::item::tech::TechniqueDisk; +use crate::entity::item::tool::Tool; use crate::ship::items::{ClientItemId, BankItem, BankItemHandle}; use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem}; @@ -43,44 +43,44 @@ impl IndividualInventoryItem { } } - pub fn mag_mut(&mut self) -> Option<&mut Mag> { - match self.item { - ItemDetail::Mag(ref mut mag) => Some(mag), - _ => None - } - } - pub fn weapon_mut(&mut self) -> Option<&mut Weapon> { match self.item { ItemDetail::Weapon(ref mut weapon) => Some(weapon), _ => None } } - + pub fn esweapon_mut(&mut self) -> Option<&mut ESWeapon> { match self.item { ItemDetail::ESWeapon(ref mut esweapon) => Some(esweapon), _ => None } } - + pub fn armor_mut(&mut self) -> Option<&mut Armor> { match self.item { ItemDetail::Armor(ref mut armor) => Some(armor), _ => None } } - - pub fn shield_mut(&mut self) -> Option<&mut Shield> { + + pub fn unit_mut(&mut self) -> Option<&mut Unit> { match self.item { - ItemDetail::Shield(ref mut shield) => Some(shield), + ItemDetail::Unit(ref mut unit) => Some(unit), _ => None } } - - pub fn unit_mut(&mut self) -> Option<&mut Unit> { + + pub fn shield_mut(&mut self) -> Option<&mut Shield> { match self.item { - ItemDetail::Unit(ref mut unit) => Some(unit), + ItemDetail::Shield(ref mut shield) => Some(shield), + _ => None + } + } + + pub fn mag_mut(&mut self) -> Option<&mut Mag> { + match self.item { + ItemDetail::Mag(ref mut mag) => Some(mag), _ => None } } @@ -237,13 +237,13 @@ impl InventoryItem { InventoryItem::Individual(item) => { match &item.item { ItemDetail::Weapon(w) => w.as_bytes(), + ItemDetail::ESWeapon(e) => e.as_bytes(), ItemDetail::Armor(a) => a.as_bytes(), - ItemDetail::Shield(s) => s.as_bytes(), ItemDetail::Unit(u) => u.as_bytes(), - ItemDetail::Tool(t) => t.as_individual_bytes(), - ItemDetail::TechniqueDisk(d) => d.as_bytes(), + ItemDetail::Shield(s) => s.as_bytes(), ItemDetail::Mag(m) => m.as_bytes(), - ItemDetail::ESWeapon(e) => e.as_bytes(), + ItemDetail::TechniqueDisk(d) => d.as_bytes(), + ItemDetail::Tool(t) => t.as_individual_bytes(), } }, InventoryItem::Stacked(item) => { diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index ac81a48..205b2f6 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -502,7 +502,6 @@ impl ItemManager { 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)?; - println!("consumed some item"); if let ItemDetail::TechniqueDisk(tech_disk) = consumed_item.item() { // TODO: validate tech level in packet is in bounds [1..30] character.techs.set_tech(tech_disk.tech, TechLevel(tech_disk.level as u8)); @@ -645,22 +644,30 @@ impl ItemManager { used_item.unwrap_present(); } else { match used_item.item_type() { - ItemType::Armor(_) => { - let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.armor_mut().ok_or(ItemManagerError::CannotGetMutItem)?; + ItemType::Weapon(_) => { + let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.weapon_mut().ok_or(ItemManagerError::CannotGetMutItem)?; // combining / unsealing? }, ItemType::ESWeapon(_) => { // TODO: wrap srank weapons let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.esweapon_mut().ok_or(ItemManagerError::CannotGetMutItem)?; // combining / unsealing? }, - ItemType::Mag(_) => { - let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.mag_mut().ok_or(ItemManagerError::CannotGetMutItem)?; + ItemType::Armor(_) => { + let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.armor_mut().ok_or(ItemManagerError::CannotGetMutItem)?; + // combining / unsealing? + }, + ItemType::Unit(_) => { + let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.unit_mut().ok_or(ItemManagerError::CannotGetMutItem)?; // combining / unsealing? }, ItemType::Shield(_) => { let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.shield_mut().ok_or(ItemManagerError::CannotGetMutItem)?; // combining / unsealing? }, + ItemType::Mag(_) => { + let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.mag_mut().ok_or(ItemManagerError::CannotGetMutItem)?; + // combining / unsealing? + }, ItemType::TechniqueDisk(_) => { let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.tech_mut().ok_or(ItemManagerError::CannotGetMutItem)?; // combining / unsealing? @@ -769,14 +776,6 @@ impl ItemManager { _ => {}, } }, - ItemType::Unit(_) => { - let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.unit_mut().ok_or(ItemManagerError::CannotGetMutItem)?; - // combining / unsealing? - }, - ItemType::Weapon(_) => { - let _actual_used_item = used_item.individual_mut().ok_or(ItemManagerError::CannotGetMutItem)?.weapon_mut().ok_or(ItemManagerError::CannotGetMutItem)?; - // combining / unsealing? - }, } } entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?; diff --git a/src/ship/map/enemy.rs b/src/ship/map/enemy.rs index a6b397a..da49d27 100644 --- a/src/ship/map/enemy.rs +++ b/src/ship/map/enemy.rs @@ -1,12 +1,9 @@ // TODO: `pub(super) for most of these?` use std::io::{Read}; - use byteorder::{LittleEndian, ReadBytesExt}; use thiserror::Error; - use crate::ship::monster::MonsterType; use crate::ship::room::Episode; - use crate::ship::map::*; #[derive(Debug, Copy, Clone)] @@ -62,7 +59,6 @@ impl RawMapEnemy { } } - #[derive(Error, Debug)] #[error("")] pub enum MapEnemyError { @@ -70,7 +66,6 @@ pub enum MapEnemyError { MapAreaError(#[from] MapAreaError), } - #[derive(Debug, Copy, Clone)] pub struct MapEnemy { pub monster: MonsterType, @@ -194,7 +189,6 @@ impl MapEnemy { _ => return Err(MapEnemyError::UnknownEnemyId(enemy.id)) } }, - MapArea::Tower => { match (enemy, episode) { (RawMapEnemy {id: 97, ..}, _) => MonsterType::DelLily, diff --git a/tests/test_item_modifiers.rs b/tests/test_item_modifiers.rs index 796fb9f..0f770e2 100644 --- a/tests/test_item_modifiers.rs +++ b/tests/test_item_modifiers.rs @@ -27,7 +27,7 @@ async fn test_unwrap_weapon() { Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 30}), None], tekked: false, - wrapping: Some(item::WrappingPaper::Red_Green), + wrapping: Some(item::WrappingPaper::RedGreen), }), location: item::ItemLocation::Inventory{ character_id: char1.id, @@ -43,7 +43,7 @@ async fn test_unwrap_weapon() { let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); - + log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; @@ -79,7 +79,7 @@ async fn test_unwrap_armor() { dfp: 3u8, evp: 2u8, slots: 4u8, - wrapping: Some(item::WrappingPaper::Red_Green), // 5 + wrapping: Some(item::WrappingPaper::RedGreen), // 5 }), location: item::ItemLocation::Inventory{ character_id: char1.id, @@ -96,7 +96,7 @@ async fn test_unwrap_armor() { let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); - + log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; @@ -131,7 +131,7 @@ async fn test_unwrap_shield() { shield: item::shield::ShieldType::CoreShield, dfp: 2u8, evp: 3u8, - wrapping: Some(item::WrappingPaper::Red_Green), // 5 + wrapping: Some(item::WrappingPaper::RedGreen), // 5 }), location: item::ItemLocation::Inventory{ character_id: char1.id, @@ -147,7 +147,7 @@ async fn test_unwrap_shield() { let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); - + log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; @@ -181,7 +181,7 @@ async fn test_unwrap_unit() { item: item::ItemDetail::Unit(item::unit::Unit { unit: item::unit::UnitType::KnightPower, modifier: Some(item::unit::UnitModifier::MinusMinus), - wrapping: Some(item::WrappingPaper::Red_Green), // 5 + wrapping: Some(item::WrappingPaper::RedGreen), // 5 }), location: item::ItemLocation::Inventory{ character_id: char1.id, @@ -197,7 +197,7 @@ async fn test_unwrap_unit() { let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); - + log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; @@ -244,7 +244,7 @@ async fn test_unwrap_mag() { let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); - + log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await;