2022-01-14 17:45:54 +00:00
|
|
|
use crate::persistence::{character_updater::CharacterUpdater, PersistedComponents};
|
2021-12-13 00:13:33 +00:00
|
|
|
use common::{
|
|
|
|
character::CharacterId,
|
2023-03-31 13:24:14 +00:00
|
|
|
comp::{
|
|
|
|
inventory::loadout_builder::LoadoutBuilder, Body, Inventory, Item, SkillSet, Stats,
|
|
|
|
Waypoint,
|
|
|
|
},
|
2021-04-14 15:35:34 +00:00
|
|
|
};
|
2021-05-02 15:08:39 +00:00
|
|
|
use specs::{Entity, WriteExpect};
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2021-07-29 22:38:35 +00:00
|
|
|
const VALID_STARTER_ITEMS: &[[Option<&str>; 2]] = &[
|
|
|
|
[None, None], // Not used with an unmodified client but should still be allowed (zesterer)
|
|
|
|
[Some("common.items.weapons.hammer.starter_hammer"), None],
|
|
|
|
[Some("common.items.weapons.bow.starter"), None],
|
|
|
|
[Some("common.items.weapons.axe.starter_axe"), None],
|
|
|
|
[Some("common.items.weapons.staff.starter_staff"), None],
|
|
|
|
[Some("common.items.weapons.sword.starter"), None],
|
|
|
|
[
|
|
|
|
Some("common.items.weapons.sword_1h.starter"),
|
|
|
|
Some("common.items.weapons.sword_1h.starter"),
|
|
|
|
],
|
2021-03-09 23:25:36 +00:00
|
|
|
];
|
|
|
|
|
2021-07-29 22:38:35 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CreationError {
|
|
|
|
InvalidWeapon,
|
|
|
|
InvalidBody,
|
|
|
|
}
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
pub fn create_character(
|
|
|
|
entity: Entity,
|
|
|
|
player_uuid: String,
|
|
|
|
character_alias: String,
|
2021-07-29 22:38:35 +00:00
|
|
|
character_mainhand: Option<String>,
|
|
|
|
character_offhand: Option<String>,
|
2020-09-17 23:02:14 +00:00
|
|
|
body: Body,
|
2021-05-02 15:08:39 +00:00
|
|
|
character_updater: &mut WriteExpect<'_, CharacterUpdater>,
|
2023-03-31 13:24:14 +00:00
|
|
|
waypoint: Option<Waypoint>,
|
2021-07-29 22:38:35 +00:00
|
|
|
) -> Result<(), CreationError> {
|
2021-03-09 23:25:36 +00:00
|
|
|
// quick fix whitelist validation for now; eventually replace the
|
|
|
|
// `Option<String>` with an index into a server-provided list of starter
|
|
|
|
// items, and replace `comp::body::Body` with `comp::body::humanoid::Body`
|
|
|
|
// throughout the messages involved
|
|
|
|
if !matches!(body, Body::Humanoid(_)) {
|
2021-07-29 22:38:35 +00:00
|
|
|
return Err(CreationError::InvalidBody);
|
2021-03-09 23:25:36 +00:00
|
|
|
}
|
2021-07-29 22:38:35 +00:00
|
|
|
if !VALID_STARTER_ITEMS.contains(&[character_mainhand.as_deref(), character_offhand.as_deref()])
|
|
|
|
{
|
|
|
|
return Err(CreationError::InvalidWeapon);
|
|
|
|
};
|
|
|
|
// The client sends None if a weapon hand is empty
|
2021-07-28 16:19:10 +00:00
|
|
|
let loadout = LoadoutBuilder::empty()
|
2020-09-17 23:02:14 +00:00
|
|
|
.defaults()
|
2021-07-29 22:38:35 +00:00
|
|
|
.active_mainhand(character_mainhand.map(|x| Item::new_from_asset_expect(&x)))
|
|
|
|
.active_offhand(character_offhand.map(|x| Item::new_from_asset_expect(&x)))
|
2020-09-17 23:02:14 +00:00
|
|
|
.build();
|
2022-05-29 23:53:00 +00:00
|
|
|
let mut inventory = Inventory::with_loadout_humanoid(loadout);
|
2023-03-29 23:11:59 +00:00
|
|
|
let stats = Stats::new(character_alias.to_string(), body);
|
2021-07-29 22:38:35 +00:00
|
|
|
let skill_set = SkillSet::default();
|
2021-01-08 19:12:09 +00:00
|
|
|
// Default items for new characters
|
2021-04-09 07:34:58 +00:00
|
|
|
inventory
|
|
|
|
.push(Item::new_from_asset_expect(
|
|
|
|
"common.items.consumable.potion_minor",
|
|
|
|
))
|
|
|
|
.expect("Inventory has at least 2 slots left!");
|
|
|
|
inventory
|
|
|
|
.push(Item::new_from_asset_expect("common.items.food.cheese"))
|
|
|
|
.expect("Inventory has at least 1 slot left!");
|
2021-01-08 19:12:09 +00:00
|
|
|
|
2022-02-20 10:10:18 +00:00
|
|
|
let map_marker = None;
|
2020-09-17 23:02:14 +00:00
|
|
|
|
2022-01-14 17:45:54 +00:00
|
|
|
character_updater.create_character(entity, player_uuid, character_alias, PersistedComponents {
|
|
|
|
body,
|
|
|
|
stats,
|
|
|
|
skill_set,
|
|
|
|
inventory,
|
|
|
|
waypoint,
|
|
|
|
pets: Vec::new(),
|
|
|
|
active_abilities: Default::default(),
|
2022-02-20 10:10:18 +00:00
|
|
|
map_marker,
|
2022-01-14 17:45:54 +00:00
|
|
|
});
|
2021-07-29 22:38:35 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-13 00:13:33 +00:00
|
|
|
pub fn edit_character(
|
|
|
|
entity: Entity,
|
|
|
|
player_uuid: String,
|
|
|
|
id: CharacterId,
|
|
|
|
character_alias: String,
|
|
|
|
body: Body,
|
|
|
|
character_updater: &mut WriteExpect<'_, CharacterUpdater>,
|
|
|
|
) -> Result<(), CreationError> {
|
|
|
|
if !matches!(body, Body::Humanoid(_)) {
|
|
|
|
return Err(CreationError::InvalidBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
character_updater.edit_character(entity, player_uuid, id, character_alias, (body,));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:38:35 +00:00
|
|
|
// Error handling
|
|
|
|
impl core::fmt::Display for CreationError {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
CreationError::InvalidWeapon => write!(
|
|
|
|
f,
|
|
|
|
"Invalid weapon.\nServer and client might be partially incompatible."
|
|
|
|
),
|
|
|
|
CreationError::InvalidBody => write!(
|
|
|
|
f,
|
|
|
|
"Invalid Body.\nServer and client might be partially incompatible"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 23:02:14 +00:00
|
|
|
}
|