2020-06-16 01:00:32 +00:00
|
|
|
//! Database operations related to character data
|
|
|
|
//!
|
2020-09-17 23:02:14 +00:00
|
|
|
//! Methods in this module should remain private to the persistence module -
|
|
|
|
//! database updates and loading are communicated via requests to the
|
|
|
|
//! [`CharacterLoader`] and [`CharacterUpdater`] while results/responses are
|
|
|
|
//! polled and handled each server tick.
|
2021-04-13 22:05:47 +00:00
|
|
|
extern crate rusqlite;
|
2020-05-09 15:41:25 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
use super::{error::PersistenceError, models::*};
|
2020-09-17 23:02:14 +00:00
|
|
|
use crate::{
|
2021-11-27 04:19:46 +00:00
|
|
|
comp::{self, Inventory},
|
2020-09-17 23:02:14 +00:00
|
|
|
persistence::{
|
|
|
|
character::conversions::{
|
2021-11-27 04:19:46 +00:00
|
|
|
convert_active_abilities_from_database, convert_active_abilities_to_database,
|
2020-09-17 23:02:14 +00:00
|
|
|
convert_body_from_database, convert_body_to_database_json,
|
|
|
|
convert_character_from_database, convert_inventory_from_database_items,
|
|
|
|
convert_items_to_database_items, convert_loadout_from_database_items,
|
2021-04-14 15:35:34 +00:00
|
|
|
convert_skill_groups_to_database, convert_skill_set_from_database,
|
2021-10-22 15:47:06 +00:00
|
|
|
convert_stats_from_database, convert_waypoint_from_database_json,
|
|
|
|
convert_waypoint_to_database_json,
|
2020-09-17 23:02:14 +00:00
|
|
|
},
|
2020-11-14 19:32:39 +00:00
|
|
|
character_loader::{CharacterCreationResult, CharacterDataResult, CharacterListResult},
|
2021-07-28 22:36:41 +00:00
|
|
|
character_updater::PetPersistenceData,
|
2021-04-13 22:05:47 +00:00
|
|
|
error::PersistenceError::DatabaseError,
|
2021-12-13 00:13:33 +00:00
|
|
|
EditableComponents, PersistedComponents,
|
2020-06-01 21:34:52 +00:00
|
|
|
},
|
2020-05-09 15:41:25 +00:00
|
|
|
};
|
2022-09-16 02:38:13 +00:00
|
|
|
use common::{
|
|
|
|
character::{CharacterId, CharacterItem, MAX_CHARACTERS_PER_PLAYER},
|
|
|
|
event::UpdateCharacterMetadata,
|
|
|
|
};
|
2020-09-17 23:02:14 +00:00
|
|
|
use core::ops::Range;
|
2021-05-02 15:08:39 +00:00
|
|
|
use rusqlite::{types::Value, Connection, ToSql, Transaction, NO_PARAMS};
|
2021-07-28 22:36:41 +00:00
|
|
|
use std::{num::NonZeroU64, rc::Rc};
|
|
|
|
use tracing::{debug, error, trace, warn};
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
/// Private module for very tightly coupled database conversion methods. In
|
|
|
|
/// general, these have many invariants that need to be maintained when they're
|
|
|
|
/// called--do not assume it's safe to make these public!
|
|
|
|
mod conversions;
|
|
|
|
|
|
|
|
pub(crate) type EntityId = i64;
|
|
|
|
|
|
|
|
const CHARACTER_PSEUDO_CONTAINER_DEF_ID: &str = "veloren.core.pseudo_containers.character";
|
|
|
|
const INVENTORY_PSEUDO_CONTAINER_DEF_ID: &str = "veloren.core.pseudo_containers.inventory";
|
|
|
|
const LOADOUT_PSEUDO_CONTAINER_DEF_ID: &str = "veloren.core.pseudo_containers.loadout";
|
|
|
|
const INVENTORY_PSEUDO_CONTAINER_POSITION: &str = "inventory";
|
|
|
|
const LOADOUT_PSEUDO_CONTAINER_POSITION: &str = "loadout";
|
|
|
|
const WORLD_PSEUDO_CONTAINER_ID: EntityId = 1;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct CharacterContainers {
|
|
|
|
inventory_container_id: EntityId,
|
|
|
|
loadout_container_id: EntityId,
|
2020-06-02 08:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
/// Load the inventory/loadout
|
|
|
|
///
|
|
|
|
/// Loading is done recursively to ensure that each is topologically sorted in
|
2022-05-17 23:05:43 +00:00
|
|
|
/// the sense required by convert_inventory_from_database_items.
|
|
|
|
///
|
|
|
|
/// For items with components, the parent item must sorted so that its
|
|
|
|
/// components are after the parent item.
|
2021-07-28 21:11:57 +00:00
|
|
|
pub fn load_items(connection: &Connection, root: i64) -> Result<Vec<Item>, PersistenceError> {
|
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
|
|
|
WITH RECURSIVE
|
|
|
|
items_tree (
|
|
|
|
item_id,
|
|
|
|
parent_container_item_id,
|
|
|
|
item_definition_id,
|
|
|
|
stack_size,
|
|
|
|
position
|
|
|
|
) AS (
|
|
|
|
SELECT item_id,
|
|
|
|
parent_container_item_id,
|
|
|
|
item_definition_id,
|
|
|
|
stack_size,
|
|
|
|
position
|
|
|
|
FROM item
|
|
|
|
WHERE parent_container_item_id = ?1
|
|
|
|
UNION ALL
|
|
|
|
SELECT item.item_id,
|
|
|
|
item.parent_container_item_id,
|
|
|
|
item.item_definition_id,
|
|
|
|
item.stack_size,
|
|
|
|
item.position
|
|
|
|
FROM item, items_tree
|
|
|
|
WHERE item.parent_container_item_id = items_tree.item_id
|
|
|
|
)
|
|
|
|
SELECT *
|
|
|
|
FROM items_tree",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let items = stmt
|
|
|
|
.query_map(&[root], |row| {
|
|
|
|
Ok(Item {
|
|
|
|
item_id: row.get(0)?,
|
|
|
|
parent_container_item_id: row.get(1)?,
|
|
|
|
item_definition_id: row.get(2)?,
|
|
|
|
stack_size: row.get(3)?,
|
|
|
|
position: row.get(4)?,
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.collect::<Vec<Item>>();
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-02-18 22:22:15 +00:00
|
|
|
Ok(items)
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:53 +00:00
|
|
|
/// Load stored data for a character.
|
|
|
|
///
|
|
|
|
/// After first logging in, and after a character is selected, we fetch this
|
|
|
|
/// data for the purpose of inserting their persisted data for the entity.
|
2020-09-17 23:02:14 +00:00
|
|
|
pub fn load_character_data(
|
|
|
|
requesting_player_uuid: String,
|
|
|
|
char_id: CharacterId,
|
2021-05-02 15:08:39 +00:00
|
|
|
connection: &Connection,
|
2020-09-17 23:02:14 +00:00
|
|
|
) -> CharacterDataResult {
|
|
|
|
let character_containers = get_pseudo_containers(connection, char_id)?;
|
2021-07-28 21:11:57 +00:00
|
|
|
let inventory_items = load_items(connection, character_containers.inventory_container_id)?;
|
|
|
|
let loadout_items = load_items(connection, character_containers.loadout_container_id)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
SELECT c.character_id,
|
|
|
|
c.alias,
|
|
|
|
c.waypoint,
|
|
|
|
b.variant,
|
|
|
|
b.body_data
|
|
|
|
FROM character c
|
|
|
|
JOIN body b ON (c.character_id = b.body_id)
|
|
|
|
WHERE c.player_uuid = ?1
|
|
|
|
AND c.character_id = ?2",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let (body_data, character_data) = stmt.query_row(
|
|
|
|
&[requesting_player_uuid.clone(), char_id.to_string()],
|
|
|
|
|row| {
|
|
|
|
let character_data = Character {
|
|
|
|
character_id: row.get(0)?,
|
|
|
|
player_uuid: requesting_player_uuid,
|
|
|
|
alias: row.get(1)?,
|
|
|
|
waypoint: row.get(2)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
let body_data = Body {
|
|
|
|
body_id: row.get(0)?,
|
|
|
|
variant: row.get(3)?,
|
|
|
|
body_data: row.get(4)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((body_data, character_data))
|
|
|
|
},
|
|
|
|
)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2022-02-20 10:10:18 +00:00
|
|
|
let (char_waypoint, char_map_marker) = match character_data
|
|
|
|
.waypoint
|
|
|
|
.as_ref()
|
|
|
|
.map(|x| convert_waypoint_from_database_json(x))
|
|
|
|
{
|
|
|
|
Some(Ok(w)) => w,
|
|
|
|
Some(Err(e)) => {
|
|
|
|
warn!(
|
|
|
|
"Error reading waypoint from database for character ID
|
2021-04-13 22:05:47 +00:00
|
|
|
{}, error: {}",
|
2022-02-20 10:10:18 +00:00
|
|
|
char_id, e
|
|
|
|
);
|
|
|
|
(None, None)
|
|
|
|
},
|
|
|
|
None => (None, None),
|
|
|
|
};
|
2020-11-03 00:12:49 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
SELECT skill_group_kind,
|
2021-10-21 14:53:46 +00:00
|
|
|
earned_exp,
|
2021-11-14 19:58:29 +00:00
|
|
|
spent_exp,
|
2021-11-14 08:24:03 +00:00
|
|
|
skills,
|
|
|
|
hash_val
|
2021-04-13 22:05:47 +00:00
|
|
|
FROM skill_group
|
|
|
|
WHERE entity_id = ?1",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let skill_group_data = stmt
|
|
|
|
.query_map(&[char_id], |row| {
|
|
|
|
Ok(SkillGroup {
|
|
|
|
entity_id: char_id,
|
|
|
|
skill_group_kind: row.get(0)?,
|
2021-10-13 14:44:28 +00:00
|
|
|
earned_exp: row.get(1)?,
|
2021-11-14 19:58:29 +00:00
|
|
|
spent_exp: row.get(2)?,
|
|
|
|
skills: row.get(3)?,
|
|
|
|
hash_val: row.get(4)?,
|
2021-04-13 22:05:47 +00:00
|
|
|
})
|
|
|
|
})?
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.collect::<Vec<SkillGroup>>();
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = connection.prepare_cached("
|
|
|
|
SELECT p.pet_id,
|
|
|
|
p.name,
|
|
|
|
b.variant,
|
|
|
|
b.body_data
|
|
|
|
FROM pet p
|
|
|
|
JOIN body b ON (p.pet_id = b.body_id)
|
|
|
|
WHERE p.character_id = ?1",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let db_pets = stmt
|
|
|
|
.query_map(&[char_id], |row| {
|
|
|
|
Ok(Pet {
|
|
|
|
database_id: row.get(0)?,
|
|
|
|
name: row.get(1)?,
|
|
|
|
body_variant: row.get(2)?,
|
|
|
|
body_data: row.get(3)?,
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.collect::<Vec<Pet>>();
|
|
|
|
|
|
|
|
// Re-construct the pet components for the player's pets, including
|
|
|
|
// de-serializing the pets' bodies and creating their Pet and Stats
|
|
|
|
// components
|
|
|
|
let pets = db_pets
|
|
|
|
.iter()
|
|
|
|
.filter_map(|db_pet| {
|
|
|
|
if let Ok(pet_body) =
|
|
|
|
convert_body_from_database(&db_pet.body_variant, &db_pet.body_data)
|
|
|
|
{
|
|
|
|
let pet = comp::Pet::new_from_database(
|
|
|
|
NonZeroU64::new(db_pet.database_id as u64).unwrap(),
|
|
|
|
);
|
|
|
|
let pet_stats = comp::Stats::new(db_pet.name.to_owned());
|
|
|
|
Some((pet, pet_body, pet_stats))
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Failed to deserialize pet_id: {} for character_id {}",
|
|
|
|
db_pet.database_id, char_id
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<(comp::Pet, comp::Body, comp::Stats)>>();
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
|
|
|
SELECT ability_sets
|
|
|
|
FROM ability_set
|
|
|
|
WHERE entity_id = ?1",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let ability_set_data = stmt.query_row(&[char_id], |row| {
|
|
|
|
Ok(AbilitySets {
|
|
|
|
entity_id: char_id,
|
|
|
|
ability_sets: row.get(0)?,
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2022-09-16 00:11:10 +00:00
|
|
|
let (skill_set, skill_set_persistence_load_error) =
|
|
|
|
convert_skill_set_from_database(&skill_group_data);
|
|
|
|
Ok((
|
|
|
|
PersistedComponents {
|
|
|
|
body: convert_body_from_database(&body_data.variant, &body_data.body_data)?,
|
|
|
|
stats: convert_stats_from_database(character_data.alias),
|
|
|
|
skill_set,
|
|
|
|
inventory: convert_inventory_from_database_items(
|
|
|
|
character_containers.inventory_container_id,
|
|
|
|
&inventory_items,
|
|
|
|
character_containers.loadout_container_id,
|
|
|
|
&loadout_items,
|
|
|
|
)?,
|
|
|
|
waypoint: char_waypoint,
|
|
|
|
pets,
|
|
|
|
active_abilities: convert_active_abilities_from_database(&ability_set_data),
|
|
|
|
map_marker: char_map_marker,
|
|
|
|
},
|
2022-09-16 02:38:13 +00:00
|
|
|
UpdateCharacterMetadata {
|
|
|
|
skill_set_persistence_load_error,
|
|
|
|
},
|
2022-09-16 00:11:10 +00:00
|
|
|
))
|
2020-05-11 10:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a list of characters belonging to the player. This data is a small
|
|
|
|
/// subset of the character's data, and is used to render the character and
|
|
|
|
/// their level in the character list.
|
|
|
|
///
|
|
|
|
/// In the event that a join fails, for a character (i.e. they lack an entry for
|
|
|
|
/// stats, body, etc...) the character is skipped, and no entry will be
|
|
|
|
/// returned.
|
2021-05-02 15:08:39 +00:00
|
|
|
pub fn load_character_list(player_uuid_: &str, connection: &Connection) -> CharacterListResult {
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
|
|
|
SELECT character_id,
|
2021-12-13 00:55:25 +00:00
|
|
|
alias
|
|
|
|
FROM character
|
2021-07-28 21:11:57 +00:00
|
|
|
WHERE player_uuid = ?1
|
|
|
|
ORDER BY character_id",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let characters = stmt
|
|
|
|
.query_map(&[player_uuid_], |row| {
|
|
|
|
Ok(Character {
|
|
|
|
character_id: row.get(0)?,
|
|
|
|
alias: row.get(1)?,
|
|
|
|
player_uuid: player_uuid_.to_owned(),
|
|
|
|
waypoint: None, // Not used for character select
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.map(|x| x.unwrap())
|
|
|
|
.collect::<Vec<Character>>();
|
|
|
|
drop(stmt);
|
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
characters
|
2020-09-17 23:02:14 +00:00
|
|
|
.iter()
|
2021-01-01 22:39:36 +00:00
|
|
|
.map(|character_data| {
|
2021-07-11 18:41:52 +00:00
|
|
|
let char = convert_character_from_database(character_data);
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
|
|
|
SELECT body_id,
|
|
|
|
variant,
|
|
|
|
body_data
|
|
|
|
FROM body
|
|
|
|
WHERE body_id = ?1",
|
|
|
|
)?;
|
|
|
|
let db_body = stmt.query_row(&[char.id], |row| {
|
|
|
|
Ok(Body {
|
|
|
|
body_id: row.get(0)?,
|
|
|
|
variant: row.get(1)?,
|
|
|
|
body_data: row.get(2)?,
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
drop(stmt);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
let char_body = convert_body_from_database(&db_body.variant, &db_body.body_data)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
let loadout_container_id = get_pseudo_container_id(
|
|
|
|
connection,
|
|
|
|
character_data.character_id,
|
|
|
|
LOADOUT_PSEUDO_CONTAINER_POSITION,
|
|
|
|
)?;
|
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let loadout_items = load_items(connection, loadout_container_id)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-02-18 22:22:15 +00:00
|
|
|
let loadout =
|
2021-04-14 21:30:53 +00:00
|
|
|
convert_loadout_from_database_items(loadout_container_id, &loadout_items)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
Ok(CharacterItem {
|
|
|
|
character: char,
|
|
|
|
body: char_body,
|
2022-09-22 20:08:39 +00:00
|
|
|
inventory: Inventory::with_loadout(loadout, char_body),
|
2020-07-08 07:04:28 +00:00
|
|
|
})
|
2020-09-17 23:02:14 +00:00
|
|
|
})
|
|
|
|
.collect()
|
2020-05-09 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
pub fn create_character(
|
2020-05-09 15:41:25 +00:00
|
|
|
uuid: &str,
|
2020-06-16 01:00:32 +00:00
|
|
|
character_alias: &str,
|
2020-09-17 23:02:14 +00:00
|
|
|
persisted_components: PersistedComponents,
|
2021-11-27 04:19:46 +00:00
|
|
|
transaction: &mut Transaction,
|
2020-11-14 19:32:39 +00:00
|
|
|
) -> CharacterCreationResult {
|
2021-11-27 04:19:46 +00:00
|
|
|
check_character_limit(uuid, transaction)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2022-01-14 17:45:54 +00:00
|
|
|
let PersistedComponents {
|
|
|
|
body,
|
|
|
|
stats: _,
|
|
|
|
skill_set,
|
|
|
|
inventory,
|
|
|
|
waypoint,
|
|
|
|
pets: _,
|
|
|
|
active_abilities,
|
2022-02-20 10:10:18 +00:00
|
|
|
map_marker,
|
2022-01-14 17:45:54 +00:00
|
|
|
} = persisted_components;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Fetch new entity IDs for character, inventory and loadout
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut new_entity_ids = get_new_entity_ids(transaction, |next_id| next_id + 3)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Create pseudo-container items for character
|
|
|
|
let character_id = new_entity_ids.next().unwrap();
|
|
|
|
let inventory_container_id = new_entity_ids.next().unwrap();
|
|
|
|
let loadout_container_id = new_entity_ids.next().unwrap();
|
2020-11-16 18:49:00 +00:00
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
let pseudo_containers = vec![
|
|
|
|
Item {
|
|
|
|
stack_size: 1,
|
|
|
|
item_id: character_id,
|
|
|
|
parent_container_item_id: WORLD_PSEUDO_CONTAINER_ID,
|
|
|
|
item_definition_id: CHARACTER_PSEUDO_CONTAINER_DEF_ID.to_owned(),
|
2020-11-16 18:49:00 +00:00
|
|
|
position: character_id.to_string(),
|
2020-09-17 23:02:14 +00:00
|
|
|
},
|
|
|
|
Item {
|
|
|
|
stack_size: 1,
|
|
|
|
item_id: inventory_container_id,
|
|
|
|
parent_container_item_id: character_id,
|
|
|
|
item_definition_id: INVENTORY_PSEUDO_CONTAINER_DEF_ID.to_owned(),
|
|
|
|
position: INVENTORY_PSEUDO_CONTAINER_POSITION.to_owned(),
|
|
|
|
},
|
|
|
|
Item {
|
|
|
|
stack_size: 1,
|
|
|
|
item_id: loadout_container_id,
|
|
|
|
parent_container_item_id: character_id,
|
|
|
|
item_definition_id: LOADOUT_PSEUDO_CONTAINER_DEF_ID.to_owned(),
|
|
|
|
position: LOADOUT_PSEUDO_CONTAINER_POSITION.to_owned(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-07-28 21:11:57 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
INSERT INTO item (item_id,
|
|
|
|
parent_container_item_id,
|
|
|
|
item_definition_id,
|
|
|
|
stack_size,
|
|
|
|
position)
|
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
for pseudo_container in pseudo_containers {
|
|
|
|
stmt.execute(&[
|
|
|
|
&pseudo_container.item_id as &dyn ToSql,
|
|
|
|
&pseudo_container.parent_container_item_id,
|
|
|
|
&pseudo_container.item_definition_id,
|
|
|
|
&pseudo_container.stack_size,
|
|
|
|
&pseudo_container.position,
|
|
|
|
])?;
|
2020-09-17 23:02:14 +00:00
|
|
|
}
|
2021-04-13 22:05:47 +00:00
|
|
|
drop(stmt);
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-07-28 21:11:57 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
INSERT INTO body (body_id,
|
|
|
|
variant,
|
|
|
|
body_data)
|
2021-07-28 21:11:57 +00:00
|
|
|
VALUES (?1, ?2, ?3)",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
let (body_variant, body_json) = convert_body_to_database_json(&body)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
stmt.execute(&[
|
|
|
|
&character_id as &dyn ToSql,
|
2021-07-28 22:36:41 +00:00
|
|
|
&body_variant.to_string(),
|
|
|
|
&body_json,
|
2021-04-13 22:05:47 +00:00
|
|
|
])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-07-28 21:11:57 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
INSERT INTO character (character_id,
|
|
|
|
player_uuid,
|
|
|
|
alias,
|
|
|
|
waypoint)
|
2021-07-28 21:11:57 +00:00
|
|
|
VALUES (?1, ?2, ?3, ?4)",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
stmt.execute(&[
|
|
|
|
&character_id as &dyn ToSql,
|
|
|
|
&uuid,
|
|
|
|
&character_alias,
|
2022-02-20 10:10:18 +00:00
|
|
|
&convert_waypoint_to_database_json(waypoint, map_marker),
|
2021-04-13 22:05:47 +00:00
|
|
|
])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
2021-12-02 05:47:28 +00:00
|
|
|
let db_skill_groups = convert_skill_groups_to_database(character_id, skill_set.skill_groups());
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-07-28 21:11:57 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
INSERT INTO skill_group (entity_id,
|
|
|
|
skill_group_kind,
|
2021-11-14 08:24:03 +00:00
|
|
|
earned_exp,
|
2021-11-14 19:58:29 +00:00
|
|
|
spent_exp,
|
2021-11-14 08:24:03 +00:00
|
|
|
skills,
|
|
|
|
hash_val)
|
2021-11-14 19:58:29 +00:00
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
2021-07-28 21:11:57 +00:00
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
for skill_group in db_skill_groups {
|
|
|
|
stmt.execute(&[
|
|
|
|
&character_id as &dyn ToSql,
|
|
|
|
&skill_group.skill_group_kind,
|
2021-10-13 14:44:28 +00:00
|
|
|
&skill_group.earned_exp,
|
2021-11-14 19:58:29 +00:00
|
|
|
&skill_group.spent_exp,
|
2021-11-14 08:24:03 +00:00
|
|
|
&skill_group.skills,
|
|
|
|
&skill_group.hash_val,
|
2021-04-13 22:05:47 +00:00
|
|
|
])?;
|
2020-12-13 17:21:51 +00:00
|
|
|
}
|
2021-04-13 22:05:47 +00:00
|
|
|
drop(stmt);
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let ability_sets = convert_active_abilities_to_database(character_id, &active_abilities);
|
|
|
|
|
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-12-22 11:57:07 +00:00
|
|
|
INSERT INTO ability_set (entity_id,
|
2021-11-27 04:19:46 +00:00
|
|
|
ability_sets)
|
|
|
|
VALUES (?1, ?2)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
stmt.execute(&[
|
|
|
|
&character_id as &dyn ToSql,
|
|
|
|
&ability_sets.ability_sets as &dyn ToSql,
|
|
|
|
])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
// Insert default inventory and loadout item records
|
|
|
|
let mut inserts = Vec::new();
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
get_new_entity_ids(transaction, |mut next_id| {
|
2021-02-18 22:22:15 +00:00
|
|
|
let inserts_ = convert_items_to_database_items(
|
2020-09-17 23:02:14 +00:00
|
|
|
loadout_container_id,
|
|
|
|
&inventory,
|
|
|
|
inventory_container_id,
|
|
|
|
&mut next_id,
|
|
|
|
);
|
|
|
|
inserts = inserts_;
|
|
|
|
next_id
|
2020-05-09 15:41:25 +00:00
|
|
|
})?;
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-07-28 21:11:57 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
INSERT INTO item (item_id,
|
|
|
|
parent_container_item_id,
|
|
|
|
item_definition_id,
|
|
|
|
stack_size,
|
|
|
|
position)
|
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
for item in inserts {
|
|
|
|
stmt.execute(&[
|
|
|
|
&item.model.item_id as &dyn ToSql,
|
|
|
|
&item.model.parent_container_item_id,
|
|
|
|
&item.model.item_definition_id,
|
|
|
|
&item.model.stack_size,
|
|
|
|
&item.model.position,
|
|
|
|
])?;
|
2020-09-17 23:02:14 +00:00
|
|
|
}
|
2021-04-13 22:05:47 +00:00
|
|
|
drop(stmt);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
load_character_list(uuid, transaction).map(|list| (character_id, list))
|
2020-05-09 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 00:13:33 +00:00
|
|
|
pub fn edit_character(
|
|
|
|
editable_components: EditableComponents,
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
character_id: CharacterId,
|
|
|
|
uuid: &str,
|
|
|
|
character_alias: &str,
|
|
|
|
) -> CharacterCreationResult {
|
|
|
|
let (body,) = editable_components;
|
2021-12-15 18:50:39 +00:00
|
|
|
let mut char_list = load_character_list(uuid, transaction);
|
|
|
|
|
|
|
|
if let Ok(char_list) = &mut char_list {
|
|
|
|
if let Some(char) = char_list
|
|
|
|
.iter_mut()
|
|
|
|
.find(|c| c.character.id == Some(character_id))
|
|
|
|
{
|
2022-07-15 12:08:04 +00:00
|
|
|
if let (comp::Body::Humanoid(new), comp::Body::Humanoid(old)) = (body, char.body) {
|
2021-12-15 18:50:39 +00:00
|
|
|
if new.species != old.species || new.body_type != old.body_type {
|
2021-12-20 11:33:51 +00:00
|
|
|
warn!(
|
|
|
|
"Character edit rejected due to failed validation - Character ID: {} \
|
|
|
|
Alias: {}",
|
|
|
|
character_id, character_alias
|
|
|
|
);
|
2021-12-15 18:50:39 +00:00
|
|
|
return Err(PersistenceError::CharacterDataError);
|
|
|
|
} else {
|
|
|
|
char.body = body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 00:13:33 +00:00
|
|
|
|
|
|
|
let mut stmt = transaction
|
2021-12-13 00:55:25 +00:00
|
|
|
.prepare_cached("UPDATE body SET variant = ?1, body_data = ?2 WHERE body_id = ?3")?;
|
2021-12-13 00:13:33 +00:00
|
|
|
|
|
|
|
let (body_variant, body_data) = convert_body_to_database_json(&body)?;
|
|
|
|
stmt.execute(&[
|
|
|
|
&body_variant.to_string(),
|
|
|
|
&body_data,
|
|
|
|
&character_id as &dyn ToSql,
|
|
|
|
])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
|
|
|
let mut stmt =
|
|
|
|
transaction.prepare_cached("UPDATE character SET alias = ?1 WHERE character_id = ?2")?;
|
|
|
|
|
|
|
|
stmt.execute(&[&character_alias, &character_id as &dyn ToSql])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
2021-12-15 18:50:39 +00:00
|
|
|
char_list.map(|list| (character_id, list))
|
2021-12-13 00:13:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:53 +00:00
|
|
|
/// Delete a character. Returns the updated character list.
|
2020-09-17 23:02:14 +00:00
|
|
|
pub fn delete_character(
|
|
|
|
requesting_player_uuid: &str,
|
|
|
|
char_id: CharacterId,
|
2021-07-28 21:11:57 +00:00
|
|
|
transaction: &mut Transaction,
|
2020-09-17 23:02:14 +00:00
|
|
|
) -> CharacterListResult {
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
SELECT COUNT(1)
|
|
|
|
FROM character
|
|
|
|
WHERE character_id = ?1
|
2021-07-28 21:11:57 +00:00
|
|
|
AND player_uuid = ?2",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
let result = stmt.query_row(&[&char_id as &dyn ToSql, &requesting_player_uuid], |row| {
|
|
|
|
let y: i64 = row.get(0)?;
|
|
|
|
Ok(y)
|
|
|
|
})?;
|
|
|
|
drop(stmt);
|
2020-05-09 15:41:25 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
if result != 1 {
|
|
|
|
return Err(PersistenceError::OtherError(
|
|
|
|
"Requested character to delete does not belong to the requesting player".to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete skill groups
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-04-13 22:05:47 +00:00
|
|
|
"
|
|
|
|
DELETE
|
|
|
|
FROM skill_group
|
|
|
|
WHERE entity_id = ?1",
|
|
|
|
)?;
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
stmt.execute(&[&char_id])?;
|
|
|
|
drop(stmt);
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2021-09-06 12:59:48 +00:00
|
|
|
let pet_ids = get_pet_ids(char_id, transaction)?
|
|
|
|
.iter()
|
|
|
|
.map(|x| Value::from(*x))
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
if !pet_ids.is_empty() {
|
|
|
|
delete_pets(transaction, char_id, Rc::new(pet_ids))?;
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
// Delete ability sets
|
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
|
|
|
DELETE
|
|
|
|
FROM ability_set
|
|
|
|
WHERE entity_id = ?1",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
stmt.execute(&[&char_id])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
// Delete character
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-04-13 22:05:47 +00:00
|
|
|
"
|
|
|
|
DELETE
|
|
|
|
FROM character
|
|
|
|
WHERE character_id = ?1",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
stmt.execute(&[&char_id])?;
|
|
|
|
drop(stmt);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Delete body
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2021-04-13 22:05:47 +00:00
|
|
|
"
|
|
|
|
DELETE
|
|
|
|
FROM body
|
|
|
|
WHERE body_id = ?1",
|
|
|
|
)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
stmt.execute(&[&char_id])?;
|
|
|
|
drop(stmt);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Delete all items, recursively walking all containers starting from the
|
|
|
|
// "character" pseudo-container that is the root for all items owned by
|
|
|
|
// a character.
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2020-09-17 23:02:14 +00:00
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
WITH RECURSIVE
|
|
|
|
parents AS (
|
|
|
|
SELECT item_id
|
|
|
|
FROM item
|
|
|
|
WHERE item.item_id = ?1 -- Item with character id is the character pseudo-container
|
|
|
|
UNION ALL
|
|
|
|
SELECT item.item_id
|
|
|
|
FROM item,
|
|
|
|
parents
|
|
|
|
WHERE item.parent_container_item_id = parents.item_id
|
|
|
|
)
|
|
|
|
DELETE
|
2020-09-17 23:02:14 +00:00
|
|
|
FROM item
|
2021-04-13 22:05:47 +00:00
|
|
|
WHERE EXISTS (SELECT 1 FROM parents WHERE parents.item_id = item.item_id)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let deleted_item_count = stmt.execute(&[&char_id])?;
|
|
|
|
drop(stmt);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
if deleted_item_count < 3 {
|
|
|
|
return Err(PersistenceError::OtherError(format!(
|
2020-09-17 23:02:14 +00:00
|
|
|
"Error deleting from item table for char_id {} (expected at least 3 deletions, found \
|
|
|
|
{})",
|
2021-04-13 22:05:47 +00:00
|
|
|
char_id, deleted_item_count
|
2020-09-17 23:02:14 +00:00
|
|
|
)));
|
2020-08-06 22:08:54 +00:00
|
|
|
}
|
2020-05-09 15:41:25 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
load_character_list(requesting_player_uuid, transaction)
|
2020-05-09 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:16:23 +00:00
|
|
|
/// Before creating a character, we ensure that the limit on the number of
|
|
|
|
/// characters has not been exceeded
|
2021-04-13 22:05:47 +00:00
|
|
|
pub fn check_character_limit(
|
|
|
|
uuid: &str,
|
2021-07-28 21:11:57 +00:00
|
|
|
transaction: &mut Transaction,
|
2021-04-13 22:05:47 +00:00
|
|
|
) -> Result<(), PersistenceError> {
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
SELECT COUNT(1)
|
|
|
|
FROM character
|
2021-07-28 21:11:57 +00:00
|
|
|
WHERE player_uuid = ?1",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
#[allow(clippy::needless_question_mark)]
|
|
|
|
let character_count: i64 = stmt.query_row(&[&uuid], |row| Ok(row.get(0)?))?;
|
|
|
|
drop(stmt);
|
|
|
|
|
|
|
|
if character_count < MAX_CHARACTERS_PER_PLAYER as i64 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(PersistenceError::CharacterLimitReached)
|
2020-05-09 15:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
/// NOTE: This relies heavily on serializability to work correctly.
|
2020-06-02 08:16:23 +00:00
|
|
|
///
|
2020-09-17 23:02:14 +00:00
|
|
|
/// The count function takes the starting entity id, and returns the desired
|
|
|
|
/// count of new entity IDs.
|
|
|
|
///
|
|
|
|
/// These are then inserted into the entities table.
|
|
|
|
fn get_new_entity_ids(
|
2021-07-28 21:11:57 +00:00
|
|
|
transaction: &mut Transaction,
|
2020-09-17 23:02:14 +00:00
|
|
|
mut max: impl FnMut(i64) -> i64,
|
2021-04-13 22:05:47 +00:00
|
|
|
) -> Result<Range<EntityId>, PersistenceError> {
|
2020-09-17 23:02:14 +00:00
|
|
|
// The sqlite_sequence table is used here to avoid reusing entity IDs for
|
2021-04-13 22:05:47 +00:00
|
|
|
// deleted entities. This table always contains the highest used ID for
|
|
|
|
// each AUTOINCREMENT column in a SQLite database.
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
2020-09-17 23:02:14 +00:00
|
|
|
"
|
|
|
|
SELECT seq + 1 AS entity_id
|
|
|
|
FROM sqlite_sequence
|
2021-04-13 22:05:47 +00:00
|
|
|
WHERE name = 'entity'",
|
|
|
|
)?;
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
#[allow(clippy::needless_question_mark)]
|
|
|
|
let next_entity_id = stmt.query_row(NO_PARAMS, |row| Ok(row.get(0)?))?;
|
2020-09-17 23:02:14 +00:00
|
|
|
let max_entity_id = max(next_entity_id);
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
// Create a new range of IDs and insert them into the entity table
|
|
|
|
let new_ids: Range<EntityId> = next_entity_id..max_entity_id;
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached("INSERT INTO entity (entity_id) VALUES (?1)")?;
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
// SQLite has no bulk insert
|
2021-04-13 22:05:47 +00:00
|
|
|
for i in new_ids.clone() {
|
|
|
|
stmt.execute(&[i])?;
|
2020-06-01 21:34:52 +00:00
|
|
|
}
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2020-09-20 04:57:26 +00:00
|
|
|
trace!(
|
2020-09-17 23:02:14 +00:00
|
|
|
"Created {} new persistence entity_ids: {}",
|
|
|
|
new_ids.end - new_ids.start,
|
|
|
|
new_ids
|
|
|
|
.clone()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
);
|
|
|
|
Ok(new_ids)
|
2020-06-01 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
/// Fetches the pseudo_container IDs for a character
|
|
|
|
fn get_pseudo_containers(
|
2021-05-02 15:08:39 +00:00
|
|
|
connection: &Connection,
|
2020-09-17 23:02:14 +00:00
|
|
|
character_id: CharacterId,
|
2021-04-13 22:05:47 +00:00
|
|
|
) -> Result<CharacterContainers, PersistenceError> {
|
2020-09-17 23:02:14 +00:00
|
|
|
let character_containers = CharacterContainers {
|
|
|
|
loadout_container_id: get_pseudo_container_id(
|
|
|
|
connection,
|
|
|
|
character_id,
|
|
|
|
LOADOUT_PSEUDO_CONTAINER_POSITION,
|
|
|
|
)?,
|
|
|
|
inventory_container_id: get_pseudo_container_id(
|
|
|
|
connection,
|
|
|
|
character_id,
|
|
|
|
INVENTORY_PSEUDO_CONTAINER_POSITION,
|
|
|
|
)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(character_containers)
|
|
|
|
}
|
2020-06-01 21:34:52 +00:00
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
fn get_pseudo_container_id(
|
2021-05-02 15:08:39 +00:00
|
|
|
connection: &Connection,
|
2020-09-17 23:02:14 +00:00
|
|
|
character_id: CharacterId,
|
|
|
|
pseudo_container_position: &str,
|
2021-04-13 22:05:47 +00:00
|
|
|
) -> Result<EntityId, PersistenceError> {
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = connection.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
SELECT item_id
|
|
|
|
FROM item
|
|
|
|
WHERE parent_container_item_id = ?1
|
|
|
|
AND position = ?2",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
#[allow(clippy::needless_question_mark)]
|
|
|
|
let res = stmt.query_row(
|
|
|
|
&[
|
|
|
|
character_id.to_string(),
|
|
|
|
pseudo_container_position.to_string(),
|
|
|
|
],
|
|
|
|
|row| Ok(row.get(0)?),
|
|
|
|
);
|
|
|
|
|
|
|
|
match res {
|
2020-09-17 23:02:14 +00:00
|
|
|
Ok(id) => Ok(id),
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
?e,
|
|
|
|
?character_id,
|
|
|
|
?pseudo_container_position,
|
|
|
|
"Failed to retrieve pseudo container ID"
|
|
|
|
);
|
|
|
|
Err(DatabaseError(e))
|
|
|
|
},
|
2020-06-04 11:44:33 +00:00
|
|
|
}
|
2020-06-01 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
/// Stores new pets in the database, and removes pets from the database that the
|
|
|
|
/// player no longer has. Currently there are no actual updates to pet data
|
|
|
|
/// since we don't store any updatable data about pets in the database.
|
|
|
|
fn update_pets(
|
|
|
|
char_id: CharacterId,
|
|
|
|
pets: Vec<PetPersistenceData>,
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
) -> Result<(), PersistenceError> {
|
|
|
|
debug!("Updating {} pets for character {}", pets.len(), char_id);
|
|
|
|
|
|
|
|
let db_pets = get_pet_ids(char_id, transaction)?;
|
|
|
|
if !db_pets.is_empty() {
|
|
|
|
let dead_pet_ids = Rc::new(
|
|
|
|
db_pets
|
|
|
|
.iter()
|
|
|
|
.filter(|pet_id| {
|
|
|
|
!pets.iter().any(|(pet, _, _)| {
|
|
|
|
pet.get_database_id()
|
|
|
|
.load()
|
|
|
|
.map_or(false, |x| x.get() == **pet_id as u64)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map(|x| Value::from(*x))
|
|
|
|
.collect::<Vec<Value>>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if !dead_pet_ids.is_empty() {
|
|
|
|
delete_pets(transaction, char_id, dead_pet_ids)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pet, body, stats) in pets
|
|
|
|
.iter()
|
|
|
|
.filter(|(pet, _, _)| pet.get_database_id().load().is_none())
|
|
|
|
{
|
|
|
|
let pet_entity_id = get_new_entity_ids(transaction, |next_id| next_id + 1)?.start;
|
|
|
|
|
|
|
|
let (body_variant, body_json) = convert_body_to_database_json(body)?;
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = transaction.prepare_cached("
|
|
|
|
INSERT
|
|
|
|
INTO body (
|
|
|
|
body_id,
|
|
|
|
variant,
|
|
|
|
body_data)
|
|
|
|
VALUES (?1, ?2, ?3)"
|
|
|
|
)?;
|
|
|
|
|
|
|
|
stmt.execute(&[
|
|
|
|
&pet_entity_id as &dyn ToSql,
|
|
|
|
&body_variant.to_string(),
|
|
|
|
&body_json,
|
|
|
|
])?;
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = transaction.prepare_cached("
|
|
|
|
INSERT
|
|
|
|
INTO pet (
|
|
|
|
pet_id,
|
|
|
|
character_id,
|
|
|
|
name)
|
|
|
|
VALUES (?1, ?2, ?3)",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
stmt.execute(&[&pet_entity_id as &dyn ToSql, &char_id, &stats.name])?;
|
|
|
|
drop(stmt);
|
|
|
|
|
|
|
|
pet.get_database_id()
|
|
|
|
.store(NonZeroU64::new(pet_entity_id as u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_pet_ids(char_id: i64, transaction: &mut Transaction) -> Result<Vec<i64>, PersistenceError> {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = transaction.prepare_cached("
|
|
|
|
SELECT pet_id
|
|
|
|
FROM pet
|
|
|
|
WHERE character_id = ?1
|
|
|
|
")?;
|
|
|
|
|
|
|
|
#[allow(clippy::needless_question_mark)]
|
|
|
|
let db_pets = stmt
|
|
|
|
.query_map(&[&char_id], |row| Ok(row.get(0)?))?
|
|
|
|
.map(|x| x.unwrap())
|
|
|
|
.collect::<Vec<i64>>();
|
|
|
|
drop(stmt);
|
|
|
|
Ok(db_pets)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_pets(
|
|
|
|
transaction: &mut Transaction,
|
|
|
|
char_id: CharacterId,
|
|
|
|
pet_ids: Rc<Vec<Value>>,
|
|
|
|
) -> Result<(), PersistenceError> {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = transaction.prepare_cached("
|
|
|
|
DELETE
|
|
|
|
FROM pet
|
|
|
|
WHERE pet_id IN rarray(?1)"
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let delete_count = stmt.execute(&[&pet_ids])?;
|
|
|
|
drop(stmt);
|
|
|
|
debug!("Deleted {} pets for character id {}", delete_count, char_id);
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let mut stmt = transaction.prepare_cached("
|
2021-12-13 00:55:25 +00:00
|
|
|
DELETE
|
2021-07-28 22:36:41 +00:00
|
|
|
FROM body
|
|
|
|
WHERE body_id IN rarray(?1)"
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let delete_count = stmt.execute(&[&pet_ids])?;
|
|
|
|
debug!(
|
|
|
|
"Deleted {} pet bodies for character id {}",
|
|
|
|
delete_count, char_id
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-20 10:10:18 +00:00
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-09-17 23:02:14 +00:00
|
|
|
pub fn update(
|
|
|
|
char_id: CharacterId,
|
2021-04-14 15:35:34 +00:00
|
|
|
char_skill_set: comp::SkillSet,
|
2022-07-15 12:08:04 +00:00
|
|
|
inventory: Inventory,
|
2021-07-28 22:36:41 +00:00
|
|
|
pets: Vec<PetPersistenceData>,
|
2020-11-16 18:49:00 +00:00
|
|
|
char_waypoint: Option<comp::Waypoint>,
|
2021-11-27 04:19:46 +00:00
|
|
|
active_abilities: comp::ability::ActiveAbilities,
|
2022-02-20 10:10:18 +00:00
|
|
|
map_marker: Option<comp::MapMarker>,
|
2021-07-28 21:11:57 +00:00
|
|
|
transaction: &mut Transaction,
|
2021-04-13 22:05:47 +00:00
|
|
|
) -> Result<(), PersistenceError> {
|
2021-07-28 22:36:41 +00:00
|
|
|
// Run pet persistence
|
|
|
|
update_pets(char_id, pets, transaction)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
let pseudo_containers = get_pseudo_containers(transaction, char_id)?;
|
2020-09-17 23:02:14 +00:00
|
|
|
let mut upserts = Vec::new();
|
2021-04-13 22:05:47 +00:00
|
|
|
// First, get all the entity IDs for any new items, and identify which
|
|
|
|
// slots to upsert and which ones to delete.
|
2021-07-28 21:11:57 +00:00
|
|
|
get_new_entity_ids(transaction, |mut next_id| {
|
2021-02-18 22:22:15 +00:00
|
|
|
let upserts_ = convert_items_to_database_items(
|
2020-09-17 23:02:14 +00:00
|
|
|
pseudo_containers.loadout_container_id,
|
|
|
|
&inventory,
|
|
|
|
pseudo_containers.inventory_container_id,
|
|
|
|
&mut next_id,
|
|
|
|
);
|
|
|
|
upserts = upserts_;
|
|
|
|
next_id
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Next, delete any slots we aren't upserting.
|
2020-09-20 04:57:26 +00:00
|
|
|
trace!("Deleting items for character_id {}", char_id);
|
2021-04-13 22:05:47 +00:00
|
|
|
let mut existing_item_ids: Vec<_> = vec![
|
|
|
|
Value::from(pseudo_containers.inventory_container_id),
|
|
|
|
Value::from(pseudo_containers.loadout_container_id),
|
2021-02-18 22:22:15 +00:00
|
|
|
];
|
2021-07-28 21:11:57 +00:00
|
|
|
for it in load_items(transaction, pseudo_containers.inventory_container_id)? {
|
2021-04-13 22:05:47 +00:00
|
|
|
existing_item_ids.push(Value::from(it.item_id));
|
2021-02-18 22:22:15 +00:00
|
|
|
}
|
2021-07-28 21:11:57 +00:00
|
|
|
for it in load_items(transaction, pseudo_containers.loadout_container_id)? {
|
2021-04-13 22:05:47 +00:00
|
|
|
existing_item_ids.push(Value::from(it.item_id));
|
2021-02-18 22:22:15 +00:00
|
|
|
}
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
let non_upserted_items = upserts
|
|
|
|
.iter()
|
|
|
|
.map(|item_pair| Value::from(item_pair.model.item_id))
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
DELETE
|
|
|
|
FROM item
|
|
|
|
WHERE parent_container_item_id
|
|
|
|
IN rarray(?1)
|
2021-07-28 21:11:57 +00:00
|
|
|
AND item_id NOT IN rarray(?2)",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
let delete_count = stmt.execute(&[Rc::new(existing_item_ids), Rc::new(non_upserted_items)])?;
|
2020-09-20 04:57:26 +00:00
|
|
|
trace!("Deleted {} items", delete_count);
|
2020-09-17 23:02:14 +00:00
|
|
|
|
|
|
|
// Upsert items
|
|
|
|
let expected_upsert_count = upserts.len();
|
|
|
|
if expected_upsert_count > 0 {
|
2021-04-13 22:05:47 +00:00
|
|
|
let (upserted_items, _): (Vec<_>, Vec<_>) = upserts
|
2020-09-17 23:02:14 +00:00
|
|
|
.into_iter()
|
2021-02-18 22:22:15 +00:00
|
|
|
.map(|model_pair| {
|
|
|
|
debug_assert_eq!(
|
|
|
|
model_pair.model.item_id,
|
|
|
|
model_pair.comp.load().unwrap().get() as i64
|
|
|
|
);
|
|
|
|
(model_pair.model, model_pair.comp)
|
|
|
|
})
|
2020-09-17 23:02:14 +00:00
|
|
|
.unzip();
|
2020-09-20 04:57:26 +00:00
|
|
|
trace!(
|
2020-09-17 23:02:14 +00:00
|
|
|
"Upserting items {:?} for character_id {}",
|
2020-09-20 04:57:26 +00:00
|
|
|
upserted_items,
|
|
|
|
char_id
|
2020-09-17 23:02:14 +00:00
|
|
|
);
|
|
|
|
|
2021-02-18 22:22:15 +00:00
|
|
|
// When moving inventory items around, foreign key constraints on
|
2021-04-13 22:05:47 +00:00
|
|
|
// `parent_container_item_id` can be temporarily violated by one
|
|
|
|
// upsert, but restored by another upsert. Deferred constraints
|
|
|
|
// allow SQLite to check this when committing the transaction.
|
|
|
|
// The `defer_foreign_keys` pragma treats the foreign key
|
|
|
|
// constraints as deferred for the next transaction (it turns itself
|
2021-02-18 22:22:15 +00:00
|
|
|
// off at the commit boundary). https://sqlite.org/foreignkeys.html#fk_deferred
|
2021-07-28 21:11:57 +00:00
|
|
|
transaction.pragma_update(None, "defer_foreign_keys", &"ON".to_string())?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
REPLACE
|
|
|
|
INTO item (item_id,
|
|
|
|
parent_container_item_id,
|
|
|
|
item_definition_id,
|
|
|
|
stack_size,
|
|
|
|
position)
|
2021-07-28 21:11:57 +00:00
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
for item in upserted_items.iter() {
|
|
|
|
stmt.execute(&[
|
|
|
|
&item.item_id as &dyn ToSql,
|
|
|
|
&item.parent_container_item_id,
|
|
|
|
&item.item_definition_id,
|
|
|
|
&item.stack_size,
|
|
|
|
&item.position,
|
|
|
|
])?;
|
2020-06-01 21:34:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-12-02 05:47:28 +00:00
|
|
|
let db_skill_groups = convert_skill_groups_to_database(char_id, char_skill_set.skill_groups());
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
REPLACE
|
|
|
|
INTO skill_group (entity_id,
|
|
|
|
skill_group_kind,
|
2021-11-14 08:24:03 +00:00
|
|
|
earned_exp,
|
2021-11-14 19:58:29 +00:00
|
|
|
spent_exp,
|
2021-11-14 08:24:03 +00:00
|
|
|
skills,
|
|
|
|
hash_val)
|
2021-11-14 19:58:29 +00:00
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
2021-07-28 21:11:57 +00:00
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
for skill_group in db_skill_groups {
|
|
|
|
stmt.execute(&[
|
|
|
|
&skill_group.entity_id as &dyn ToSql,
|
|
|
|
&skill_group.skill_group_kind,
|
2021-10-13 14:44:28 +00:00
|
|
|
&skill_group.earned_exp,
|
2021-11-14 19:58:29 +00:00
|
|
|
&skill_group.spent_exp,
|
2021-11-14 08:24:03 +00:00
|
|
|
&skill_group.skills,
|
|
|
|
&skill_group.hash_val,
|
2021-04-13 22:05:47 +00:00
|
|
|
])?;
|
|
|
|
}
|
2020-12-13 17:21:51 +00:00
|
|
|
|
2022-02-20 10:10:18 +00:00
|
|
|
let db_waypoint = convert_waypoint_to_database_json(char_waypoint, map_marker);
|
2021-04-13 22:05:47 +00:00
|
|
|
|
2021-07-28 21:11:57 +00:00
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
2021-04-13 22:05:47 +00:00
|
|
|
UPDATE character
|
|
|
|
SET waypoint = ?1
|
|
|
|
WHERE character_id = ?2
|
2021-07-28 21:11:57 +00:00
|
|
|
",
|
|
|
|
)?;
|
2021-04-13 22:05:47 +00:00
|
|
|
|
|
|
|
let waypoint_count = stmt.execute(&[&db_waypoint as &dyn ToSql, &char_id])?;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-01-01 22:39:36 +00:00
|
|
|
if waypoint_count != 1 {
|
2021-04-13 22:05:47 +00:00
|
|
|
return Err(PersistenceError::OtherError(format!(
|
2021-01-01 22:39:36 +00:00
|
|
|
"Error updating character table for char_id {}",
|
2020-09-17 23:02:14 +00:00
|
|
|
char_id
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:19:46 +00:00
|
|
|
let ability_sets = convert_active_abilities_to_database(char_id, &active_abilities);
|
|
|
|
|
|
|
|
let mut stmt = transaction.prepare_cached(
|
|
|
|
"
|
|
|
|
UPDATE ability_set
|
|
|
|
SET ability_sets = ?1
|
|
|
|
WHERE entity_id = ?2
|
|
|
|
",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let ability_sets_count = stmt.execute(&[
|
|
|
|
&ability_sets.ability_sets as &dyn ToSql,
|
|
|
|
&char_id as &dyn ToSql,
|
|
|
|
])?;
|
|
|
|
|
|
|
|
if ability_sets_count != 1 {
|
|
|
|
return Err(PersistenceError::OtherError(format!(
|
|
|
|
"Error updating ability_set table for char_id {}",
|
2022-02-20 10:10:18 +00:00
|
|
|
char_id,
|
2021-11-27 04:19:46 +00:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
Ok(())
|
2020-06-01 21:34:52 +00:00
|
|
|
}
|