Skip to content

Commit

Permalink
Extend map metadata with name
Browse files Browse the repository at this point in the history
This is needed so maps can be displayed in menu during map selection.

Relates to DigitalExtinction#70.
  • Loading branch information
Indy2222 committed Nov 9, 2022
1 parent 02d8f0f commit 7058f7e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assets/maps/huge.tar
Git LFS file not shown
2 changes: 1 addition & 1 deletion crates/map/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mod test {
#[test]
fn test_store_load() {
let bounds = MapBounds::new(Vec2::new(1000., 2000.));
let mut map = Map::empty(MapMetadata::new(bounds, Player::Player4));
let mut map = Map::empty(MapMetadata::new("Test Map".into(), bounds, Player::Player4));

let bases = [
(Vec2::new(-400., -900.), Player::Player1),
Expand Down
7 changes: 6 additions & 1 deletion crates/map/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod test {
#[test]
fn test_map() {
let mut map = Map::empty(MapMetadata::new(
"Test Map".into(),
MapBounds::new(Vec2::new(1000., 1000.)),
Player::Player3,
));
Expand Down Expand Up @@ -141,7 +142,11 @@ mod test {
));

let map = Map::new(
MapMetadata::new(MapBounds::new(Vec2::new(5., 5.)), Player::Player4),
MapMetadata::new(
"Test Map".into(),
MapBounds::new(Vec2::new(5., 5.)),
Player::Player4,
),
content,
);

Expand Down
28 changes: 26 additions & 2 deletions crates/map/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use thiserror::Error;

use crate::size::{MapBounds, MapBoundsValidationError};

pub const MAX_MAP_NAME_LEN: usize = 16;

/// General information about a map. It does not hold full content of the map
/// (i.e. location of objects on the map).
#[derive(Serialize, Deserialize)]
pub struct MapMetadata {
name: String,
bounds: MapBounds,
max_player: Player,
}
Expand All @@ -17,6 +20,8 @@ impl MapMetadata {
///
/// # Arguments
///
/// * `name` - name of the map.
///
/// * `bounds` - bounds of the map.
///
/// * `max_player` - maximum number of players which can play on the map.
Expand All @@ -26,8 +31,12 @@ impl MapMetadata {
/// # Panics
///
/// Panics if any of the map parameters is invalid.
pub fn new(bounds: MapBounds, max_player: Player) -> Self {
let map = Self { bounds, max_player };
pub fn new(name: String, bounds: MapBounds, max_player: Player) -> Self {
let map = Self {
name,
bounds,
max_player,
};
map.validate().unwrap();
map
}
Expand All @@ -41,6 +50,19 @@ impl MapMetadata {
}

pub(crate) fn validate(&self) -> Result<(), MapMetadataValidationError> {
if self.name.is_empty() {
return Err(MapMetadataValidationError::MapName(
"map name is empty".into(),
));
}
if self.name.len() > MAX_MAP_NAME_LEN {
return Err(MapMetadataValidationError::MapName(format!(
"map name too long: {} > {}",
self.name.len(),
MAX_MAP_NAME_LEN
)));
}

if let Err(error) = self.bounds.validate() {
return Err(MapMetadataValidationError::MapBounds { source: error });
}
Expand All @@ -55,6 +77,8 @@ impl MapMetadata {

#[derive(Error, Debug)]
pub enum MapMetadataValidationError {
#[error("invalid map name: {0}")]
MapName(String),
#[error("invalid map bounds")]
MapBounds { source: MapBoundsValidationError },
#[error("map has to have at least 2 players, got {0}")]
Expand Down

0 comments on commit 7058f7e

Please sign in to comment.