127 lines
2.6 KiB
Rust
127 lines
2.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashSet;
|
|
|
|
use crate::models::{Position, Tunnel};
|
|
use crate::GeoffreyDatabaseModel;
|
|
|
|
pub mod farm;
|
|
pub mod market;
|
|
pub mod shop;
|
|
pub mod town;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub enum LocationType {
|
|
Base,
|
|
Shop(u64),
|
|
Attraction,
|
|
Town(u64),
|
|
Farm(u64),
|
|
Market(u64),
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub struct Location {
|
|
id: Option<u64>,
|
|
pub name: String,
|
|
pub position: Position,
|
|
owners: HashSet<u64>,
|
|
pub tunnel: Option<Tunnel>,
|
|
loc_type: LocationType,
|
|
}
|
|
|
|
impl Location {
|
|
pub fn new(
|
|
name: &str,
|
|
position: Position,
|
|
owner: u64,
|
|
tunnel: Option<Tunnel>,
|
|
loc_type: LocationType,
|
|
) -> Self {
|
|
let mut owners = HashSet::new();
|
|
owners.insert(owner);
|
|
|
|
Self {
|
|
id: None,
|
|
name: name.to_string(),
|
|
position,
|
|
owners,
|
|
tunnel,
|
|
loc_type,
|
|
}
|
|
}
|
|
|
|
pub fn owners(&self) -> Vec<u64> {
|
|
self.owners.iter().cloned().collect()
|
|
}
|
|
|
|
pub fn add_owner(&mut self, owner: u64) {
|
|
self.owners.insert(owner);
|
|
}
|
|
|
|
pub fn remove_owner(&mut self, owner: u64) {
|
|
self.owners.remove(&owner);
|
|
}
|
|
}
|
|
|
|
impl GeoffreyDatabaseModel for Location {
|
|
fn id(&self) -> Option<u64> {
|
|
self.id
|
|
}
|
|
|
|
fn set_id(&mut self, id: u64) {
|
|
self.id = Some(id)
|
|
}
|
|
|
|
fn tree() -> String {
|
|
"location".to_string()
|
|
}
|
|
|
|
fn check_unique(&self, o: &Self) -> bool {
|
|
o.name.to_lowercase() != self.name.to_lowercase()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::models::locations::{Location, LocationType};
|
|
use crate::models::{Dimension, Position};
|
|
use crate::GeoffreyDatabaseModel;
|
|
|
|
#[test]
|
|
fn test_location_check_unique() {
|
|
let l1 = Location::new(
|
|
"Test",
|
|
Position::new(0, 0, Dimension::Overworld),
|
|
0u64,
|
|
None,
|
|
LocationType::Base,
|
|
);
|
|
let l2 = Location::new(
|
|
"NotTest",
|
|
Position::new(0, 0, Dimension::Overworld),
|
|
0u64,
|
|
None,
|
|
LocationType::Base,
|
|
);
|
|
|
|
assert!(l1.check_unique(&l2));
|
|
|
|
let l1 = Location::new(
|
|
"Test",
|
|
Position::new(0, 0, Dimension::Overworld),
|
|
0u64,
|
|
None,
|
|
LocationType::Base,
|
|
);
|
|
let l2 = Location::new(
|
|
"teSt",
|
|
Position::new(0, 0, Dimension::Overworld),
|
|
0u64,
|
|
None,
|
|
LocationType::Base,
|
|
);
|
|
|
|
assert!(!l1.check_unique(&l2));
|
|
}
|
|
}
|