Browse Source

random formatting and ordering changes

presents
andy 4 years ago
parent
commit
c8b9564009
  1. 4
      src/entity/gateway/entitygateway.rs
  2. 61
      src/entity/gateway/inmemory.rs
  3. 24
      src/entity/gateway/postgres/migrations/V0001__initial.sql
  4. 260
      src/entity/gateway/postgres/models.rs
  5. 102
      src/entity/gateway/postgres/postgres.rs
  6. 2
      src/entity/item/armor.rs
  7. 2
      src/entity/item/esweapon.rs
  8. 4
      src/entity/item/mag.rs
  9. 3
      src/entity/item/mod.rs
  10. 1
      src/entity/item/unit.rs
  11. 5
      src/entity/item/weapon.rs
  12. 44
      src/ship/items/inventory.rs
  13. 25
      src/ship/items/manager.rs
  14. 6
      src/ship/map/enemy.rs
  15. 18
      tests/test_item_modifiers.rs

4
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!();
}

61
src/entity/gateway/inmemory.rs

@ -17,12 +17,12 @@ pub struct InMemoryGateway {
inventories: Arc<Mutex<BTreeMap<CharacterEntityId, InventoryEntity>>>,
banks: Arc<Mutex<BTreeMap<CharacterEntityId, BankEntity>>>,
equips: Arc<Mutex<BTreeMap<CharacterEntityId, EquippedEntity>>>,
mag_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<mag::MagModifier>>>>,
weapon_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<weapon::WeaponModifier>>>>,
esweapon_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<esweapon::ESWeaponModifier>>>>,
armor_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<armor::ArmorModifier>>>>,
shield_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<shield::ShieldModifier>>>>,
unit_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<unit::UnitModifier>>>>,
shield_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<shield::ShieldModifier>>>>,
mag_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<mag::MagModifier>>>>,
tech_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<tech::TechModifier>>>>,
tool_modifiers: Arc<Mutex<BTreeMap<ItemEntityId, Vec<tool::ToolModifier>>>>,
}
@ -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);

24
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
);

260
src/entity/gateway/postgres/models.rs

@ -178,7 +178,6 @@ impl From<SectionID> for PgSectionId {
}
}
#[derive(Debug, sqlx::FromRow)]
pub struct PgCharacter {
pub id: i32,
@ -277,8 +276,6 @@ impl Into<CharacterEntity> 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<weapon::WeaponModifier>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgESWeapon {
esweapon: esweapon::ESWeaponType,
special: Option<esweapon::ESWeaponSpecial>,
name: String,
grind: u8,
wrapping: Option<WrappingPaper>,
}
impl From<esweapon::ESWeapon> 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<esweapon::ESWeapon> 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<esweapon::ESWeaponModifier>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgArmor {
armor: armor::ArmorType,
@ -368,42 +404,6 @@ pub struct PgArmorModifier {
pub modifier: sqlx::types::Json<armor::ArmorModifier>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgShield {
shield: shield::ShieldType,
dfp: u8,
evp: u8,
wrapping: Option<WrappingPaper>,
}
impl From<shield::Shield> for PgShield {
fn from(other: shield::Shield) -> PgShield {
PgShield {
shield: other.shield,
dfp: other.dfp,
evp: other.evp,
wrapping: other.wrapping,
}
}
}
impl Into<shield::Shield> 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<shield::ShieldModifier>,
}
#[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<WrappingPaper>,
}
impl From<tool::Tool> for PgTool {
fn from(other: tool::Tool) -> PgTool {
PgTool {
tool: other.tool,
impl From<shield::Shield> for PgShield {
fn from(other: shield::Shield) -> PgShield {
PgShield {
shield: other.shield,
dfp: other.dfp,
evp: other.evp,
wrapping: other.wrapping,
}
}
}
impl Into<tool::Tool> for PgTool {
fn into(self) -> tool::Tool {
tool::Tool {
tool: self.tool,
impl Into<shield::Shield> 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<tool::ToolModifier>,
pub struct PgShieldModifier {
pub shield: i32,
pub modifier: sqlx::types::Json<shield::ShieldModifier>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgMag {
mag: mag::MagType,
synchro: u8,
color: u8,
wrapping: Option<WrappingPaper>,
}
impl From<mag::Mag> for PgMag {
fn from(other: mag::Mag) -> PgMag {
PgMag {
mag: other.mag,
synchro: other.synchro,
color: other.color,
wrapping: other.wrapping,
}
}
}
impl Into<mag::Mag> 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<WrappingPaper>,
}
impl From<mag::Mag> for PgMag {
fn from(other: mag::Mag) -> PgMag {
PgMag {
mag: other.mag,
synchro: other.synchro,
color: other.color,
wrapping: other.wrapping,
}
}
}
impl Into<mag::Mag> 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<esweapon::ESWeaponSpecial>,
name: String,
grind: u8,
pub struct PgTool {
pub tool: tool::ToolType,
wrapping: Option<WrappingPaper>,
}
impl From<esweapon::ESWeapon> for PgESWeapon {
fn from(other: esweapon::ESWeapon) -> PgESWeapon {
PgESWeapon {
esweapon: other.esweapon,
special: other.special,
name: other.name,
grind: other.grind,
impl From<tool::Tool> for PgTool {
fn from(other: tool::Tool) -> PgTool {
PgTool {
tool: other.tool,
wrapping: other.wrapping,
}
}
}
impl Into<esweapon::ESWeapon> for PgESWeapon {
fn into(self) -> esweapon::ESWeapon {
esweapon::ESWeapon {
esweapon: self.esweapon,
special: self.special,
name: self.name,
grind: self.grind,
impl Into<tool::Tool> 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<esweapon::ESWeaponModifier>,
pub struct PgToolModifier {
pub tool: i32,
pub modifier: sqlx::types::Json<tool::ToolModifier>,
}
#[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<ItemDetail> 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<ItemDetail> 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<PgItemDetail>,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum PgItemLocationDetail {
Inventory {
@ -688,7 +670,6 @@ impl Into<ItemLocation> for PgItemLocationDetail {
}
}
#[derive(Debug, sqlx::FromRow)]
pub struct PgItemLocation {
//pub id: i32,
@ -696,7 +677,6 @@ pub struct PgItemLocation {
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum PgMagModifierDetail {
FeedMag(i32),
@ -740,7 +720,6 @@ pub struct PgMagModifier {
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, sqlx::FromRow)]
pub struct PgItemEntity {
pub id: i32,
@ -764,7 +743,6 @@ impl Into<ItemEntity> for PgItemWithLocation {
}
}
#[derive(Debug, sqlx::FromRow)]
pub struct PgMagModifierWithParameters {
pub mag: i32,
@ -773,7 +751,6 @@ pub struct PgMagModifierWithParameters {
pub cell: Option<sqlx::types::Json<PgTool>>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PgInventoryItemEntity {
@ -799,11 +776,11 @@ pub struct PgEquipped {
pchar: i32,
weapon: Option<i32>,
armor: Option<i32>,
shield: Option<i32>,
unit0: Option<i32>,
unit1: Option<i32>,
unit2: Option<i32>,
unit3: Option<i32>,
shield: Option<i32>,
mag: Option<i32>,
}
@ -812,12 +789,12 @@ impl Into<EquippedEntity> 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),
}
}
}

102
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::<tool::Tool>::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::<tool::Tool>::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?;

2
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,

2
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]);
}
}

4
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<WrappingPaper>,
}
impl Mag {
pub fn baby_mag(skin: u16) -> Mag {
Mag {
@ -1216,4 +1213,3 @@ mod test {
fn test_mag_does_not_level_down() {
}
}

3
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<BankItemEntity>,

1
src/entity/item/unit.rs

@ -343,7 +343,6 @@ pub struct Unit {
pub wrapping: Option<WrappingPaper>,
}
impl Unit {
pub fn as_bytes(&self) -> [u8; 16] {
let mut result = [0; 16];

5
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<WrappingPaper>,
}
impl Weapon {
pub fn new(wep: WeaponType) -> Weapon {
Weapon {
@ -1615,5 +1612,3 @@ impl Weapon {
}
}
}

44
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) => {

25
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?;

6
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,

18
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;

Loading…
Cancel
Save