Item durability is now persisted

This commit is contained in:
Sam 2022-06-06 21:52:27 -04:00
parent a07e042fa2
commit c586db8feb
5 changed files with 44 additions and 10 deletions

View File

@ -1223,6 +1223,22 @@ impl Item {
}
}
pub fn persistence_durability(&self) -> Option<i32> {
self.durability.map(|x| x.min(i32::MAX as u32) as i32)
}
pub fn persistence_set_durability(&mut self, value: Option<i32>) {
// If changes have been made so that item no longer needs to track durability,
// set to None
if !self.has_durability() {
self.durability = None;
} else {
// Set durability to persisted value, and if item previously had no durability,
// set to Some(0) so that durability will be tracked
self.durability = Some(value.map_or(0, |x| x.max(0) as u32));
}
}
#[cfg(test)]
pub fn create_test_item_from_kind(kind: ItemKind) -> Self {
let ability_map = &AbilityMap::load().read();

View File

@ -0,0 +1 @@
ALTER TABLE item ADD durability INTEGER;

View File

@ -70,13 +70,15 @@ pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, Persi
parent_container_item_id,
item_definition_id,
stack_size,
position
position,
durability
) AS (
SELECT item_id,
parent_container_item_id,
item_definition_id,
stack_size,
position
position,
durability
FROM item
WHERE parent_container_item_id = ?1
UNION ALL
@ -84,7 +86,8 @@ pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, Persi
item.parent_container_item_id,
item.item_definition_id,
item.stack_size,
item.position
item.position,
item.durability
FROM item, items_tree
WHERE item.parent_container_item_id = items_tree.item_id
)
@ -100,6 +103,7 @@ pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, Persi
item_definition_id: row.get(2)?,
stack_size: row.get(3)?,
position: row.get(4)?,
durability: row.get(5)?,
})
})?
.filter_map(Result::ok)
@ -390,6 +394,7 @@ pub fn create_character(
parent_container_item_id: WORLD_PSEUDO_CONTAINER_ID,
item_definition_id: CHARACTER_PSEUDO_CONTAINER_DEF_ID.to_owned(),
position: character_id.to_string(),
durability: None,
},
Item {
stack_size: 1,
@ -397,6 +402,7 @@ pub fn create_character(
parent_container_item_id: character_id,
item_definition_id: INVENTORY_PSEUDO_CONTAINER_DEF_ID.to_owned(),
position: INVENTORY_PSEUDO_CONTAINER_POSITION.to_owned(),
durability: None,
},
Item {
stack_size: 1,
@ -404,6 +410,7 @@ pub fn create_character(
parent_container_item_id: character_id,
item_definition_id: LOADOUT_PSEUDO_CONTAINER_DEF_ID.to_owned(),
position: LOADOUT_PSEUDO_CONTAINER_POSITION.to_owned(),
durability: None,
},
];
@ -413,8 +420,9 @@ pub fn create_character(
parent_container_item_id,
item_definition_id,
stack_size,
position)
VALUES (?1, ?2, ?3, ?4, ?5)",
position,
durability)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
for pseudo_container in pseudo_containers {
@ -424,6 +432,7 @@ pub fn create_character(
&pseudo_container.item_definition_id,
&pseudo_container.stack_size,
&pseudo_container.position,
&pseudo_container.durability,
])?;
}
drop(stmt);
@ -521,8 +530,9 @@ pub fn create_character(
parent_container_item_id,
item_definition_id,
stack_size,
position)
VALUES (?1, ?2, ?3, ?4, ?5)",
position,
durability)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
for item in inserts {
@ -532,6 +542,7 @@ pub fn create_character(
&item.model.item_definition_id,
&item.model.stack_size,
&item.model.position,
&item.model.durability,
])?;
}
drop(stmt);
@ -1048,8 +1059,9 @@ pub fn update(
parent_container_item_id,
item_definition_id,
stack_size,
position)
VALUES (?1, ?2, ?3, ?4, ?5)",
position,
durability)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
for item in upserted_items.iter() {
@ -1059,6 +1071,7 @@ pub fn update(
&item.item_definition_id,
&item.stack_size,
&item.position,
&item.durability,
])?;
}
}

View File

@ -176,6 +176,7 @@ pub fn convert_items_to_database_items(
} else {
1
},
durability: item.persistence_durability(),
},
// Continue to remember the atomic, in case we detect an error later and want
// to roll back to preserve liveness.
@ -359,6 +360,7 @@ pub fn convert_inventory_from_database_items(
item_indices.insert(db_item.item_id, i);
let mut item = get_item_from_asset(db_item.item_definition_id.as_str())?;
item.persistence_set_durability(db_item.durability);
// NOTE: Since this is freshly loaded, the atomic is *unique.*
let comp = item.get_item_id_for_database();
@ -459,7 +461,8 @@ pub fn convert_loadout_from_database_items(
for (i, db_item) in database_items.iter().enumerate() {
item_indices.insert(db_item.item_id, i);
let item = get_item_from_asset(db_item.item_definition_id.as_str())?;
let mut item = get_item_from_asset(db_item.item_definition_id.as_str())?;
item.persistence_set_durability(db_item.durability);
// NOTE: item id is currently *unique*, so we can store the ID safely.
let comp = item.get_item_id_for_database();

View File

@ -12,6 +12,7 @@ pub struct Item {
pub item_definition_id: String,
pub stack_size: i32,
pub position: String,
pub durability: Option<i32>,
}
pub struct Body {