Changed how durability was persisted so that a more general system can be used for future item properties.

This commit is contained in:
Sam 2022-07-18 22:08:03 -04:00
parent 0966753699
commit 011b6c3feb
8 changed files with 64 additions and 25 deletions

BIN
assets/voxygen/voxel/sprite/repair_bench/repair_bench-0.vox (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -3788,6 +3788,17 @@ CookingPot: Some((
],
wind_sway: 0.0,
)),
// Repair Bench
RepairBench: Some((
variations: [
(
model: "voxygen.voxel.sprite.repair_bench.repair_bench-0",
offset: (-7.0, -8.0, 0.0),
lod_axes: (0.0, 0.0, 0.0),
),
],
wind_sway: 0.0,
)),
// Ensnaring Vines
EnsnaringVines: Some((
variations: [

View File

@ -1228,11 +1228,7 @@ 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>) {
pub fn persistence_set_durability(&mut self, value: Option<u32>) {
// If changes have been made so that item no longer needs to track durability,
// set to None
if !self.has_durability() {
@ -1240,7 +1236,7 @@ impl Item {
} 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));
self.durability = Some(value.unwrap_or(0));
}
}

View File

@ -1 +1 @@
ALTER TABLE item ADD durability INTEGER;
ALTER TABLE item ADD properties TEXT NOT NULL DEFAULT '{}';

View File

@ -71,14 +71,14 @@ pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, Persi
item_definition_id,
stack_size,
position,
durability
properties
) AS (
SELECT item_id,
parent_container_item_id,
item_definition_id,
stack_size,
position,
durability
properties
FROM item
WHERE parent_container_item_id = ?1
UNION ALL
@ -87,7 +87,7 @@ pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, Persi
item.item_definition_id,
item.stack_size,
item.position,
item.durability
item.properties
FROM item, items_tree
WHERE item.parent_container_item_id = items_tree.item_id
)
@ -103,7 +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)?,
properties: row.get(5)?,
})
})?
.filter_map(Result::ok)
@ -394,7 +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,
properties: String::new(),
},
Item {
stack_size: 1,
@ -402,7 +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,
properties: String::new(),
},
Item {
stack_size: 1,
@ -410,7 +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,
properties: String::new(),
},
];
@ -421,7 +421,7 @@ pub fn create_character(
item_definition_id,
stack_size,
position,
durability)
properties)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
@ -432,7 +432,7 @@ pub fn create_character(
&pseudo_container.item_definition_id,
&pseudo_container.stack_size,
&pseudo_container.position,
&pseudo_container.durability,
&pseudo_container.properties,
])?;
}
drop(stmt);
@ -531,7 +531,7 @@ pub fn create_character(
item_definition_id,
stack_size,
position,
durability)
properties)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
@ -542,7 +542,7 @@ pub fn create_character(
&item.model.item_definition_id,
&item.model.stack_size,
&item.model.position,
&item.model.durability,
&item.model.properties,
])?;
}
drop(stmt);
@ -1060,7 +1060,7 @@ pub fn update(
item_definition_id,
stack_size,
position,
durability)
properties)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
)?;
@ -1071,7 +1071,7 @@ pub fn update(
&item.item_definition_id,
&item.stack_size,
&item.position,
&item.durability,
&item.properties,
])?;
}
}

View File

@ -5,7 +5,10 @@ use crate::persistence::{
use crate::persistence::{
error::PersistenceError,
json_models::{self, CharacterPosition, DatabaseAbilitySet, GenericBody, HumanoidBody},
json_models::{
self, CharacterPosition, DatabaseAbilitySet, DatabaseItemProperties, GenericBody,
HumanoidBody,
},
};
use common::{
character::CharacterId,
@ -165,6 +168,8 @@ pub fn convert_items_to_database_items(
bfs_queue.push_back((format!("component_{}", i), Some(component), item_id));
}
let item_properties = json_models::item_properties_to_db_model(item);
let upsert = ItemModelPair {
model: Item {
item_definition_id: String::from(item.persistence_item_id()),
@ -176,7 +181,8 @@ pub fn convert_items_to_database_items(
} else {
1
},
durability: item.persistence_durability(),
properties: serde_json::to_string(&item_properties)
.expect("We probably want to crash if this gets triggered?"),
},
// Continue to remember the atomic, in case we detect an error later and want
// to roll back to preserve liveness.
@ -360,7 +366,10 @@ 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);
let item_properties =
serde_json::de::from_str::<DatabaseItemProperties>(&db_item.properties)
.expect("We probably want to crash if this gets triggered?");
json_models::apply_db_item_properties(&mut item, &item_properties);
// NOTE: Since this is freshly loaded, the atomic is *unique.*
let comp = item.get_item_id_for_database();
@ -462,7 +471,10 @@ pub fn convert_loadout_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);
let item_properties =
serde_json::de::from_str::<DatabaseItemProperties>(&db_item.properties)
.expect("We probably want to crash if this gets triggered?");
json_models::apply_db_item_properties(&mut item, &item_properties);
// NOTE: item id is currently *unique*, so we can store the ID safely.
let comp = item.get_item_id_for_database();

View File

@ -285,3 +285,20 @@ pub fn active_abilities_from_db_model(
.collect::<HashMap<_, _>>();
comp::ability::ActiveAbilities::new(ability_sets)
}
#[derive(Serialize, Deserialize)]
pub struct DatabaseItemProperties {
#[serde(skip_serializing_if = "Option::is_none")]
durability: Option<u32>,
}
pub fn item_properties_to_db_model(item: &comp::Item) -> DatabaseItemProperties {
DatabaseItemProperties {
durability: item.durability(),
}
}
pub fn apply_db_item_properties(item: &mut comp::Item, properties: &DatabaseItemProperties) {
let DatabaseItemProperties { durability } = properties;
item.persistence_set_durability(*durability);
}

View File

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