Skip to content

Commit

Permalink
Experimental support for nomination only maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Razer2015 committed Dec 7, 2022
1 parent 45b1fda commit 6be2ca3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
36 changes: 30 additions & 6 deletions battlefox/src/mapmanager/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use serde::{Deserialize, Serialize};
/// - map
/// - game mode (Rush, Conquest, ...)
/// - vehicles (None -> Adaptive based on vehicle_threshold, False -> No vehicles, True -> Vehicles)
/// - nom_only (None -> Can be nominated and can randomly show up, False -> Random + nomination, True -> Nomination only)
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MapInPool {
pub map: Map,
pub mode: GameMode,
pub vehicles: Option<bool>
pub vehicles: Option<bool>,
pub nom_only: Option<bool>,
}

impl Display for MapInPool {
Expand Down Expand Up @@ -47,13 +49,14 @@ impl From<Vec<MapInPool>> for MapPool {
}
}
}
impl From<Vec<(Map, GameMode, Option<bool>)>> for MapPool {
fn from(pool: Vec<(Map, GameMode, Option<bool>)>) -> Self {
impl From<Vec<(Map, GameMode, Option<bool>, Option<bool>)>> for MapPool {
fn from(pool: Vec<(Map, GameMode, Option<bool>, Option<bool>)>) -> Self {
Self {
pool: pool.iter().map(|(map, mode, vehicles)| MapInPool {
pool: pool.iter().map(|(map, mode, vehicles, nom_only)| MapInPool {
map: *map,
mode: mode.clone(),
vehicles: *vehicles
vehicles: *vehicles,
nom_only: *nom_only,
}).collect()
}
}
Expand Down Expand Up @@ -135,6 +138,18 @@ impl MapPool {
}
}

/// Returns a new pool, with only the maps that can be randomly drawn.
pub fn random_maps_only(&self) -> Self {
Self {
pool: self
.pool
.iter()
.filter(|mip| mip.nom_only.is_none() || !mip.nom_only.unwrap())
.cloned()
.collect(),
}
}

pub fn to_set(&self) -> HashSet<MapInPool>
{
self.pool.iter().cloned().collect()
Expand Down Expand Up @@ -172,8 +187,9 @@ impl MapPool {
/// Selects at most `n_max` maps from the pool at random.
pub fn choose_random(&self, n_max: usize) -> Self {
let mut rng = thread_rng();
let random_maps_only = self.random_maps_only();
Self {
pool: self
pool: random_maps_only
.pool
.choose_multiple(&mut rng, n_max)
.cloned()
Expand Down Expand Up @@ -231,6 +247,7 @@ mod tests {
map: Map::Metro,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
}],
};

Expand All @@ -240,11 +257,13 @@ mod tests {
map: Map::Metro,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
},
MapInPool {
map: Map::Locker,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
},
],
};
Expand All @@ -254,6 +273,7 @@ mod tests {
map: Map::Locker,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
}],
};

Expand All @@ -269,11 +289,13 @@ mod tests {
map: Map::Metro,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
},
MapInPool {
map: Map::Locker,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
},
],
};
Expand All @@ -283,6 +305,7 @@ mod tests {
map: Map::Metro,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
}],
};

Expand All @@ -291,6 +314,7 @@ mod tests {
map: Map::Locker,
mode: GameMode::Rush,
vehicles: None,
nom_only: None,
}],
};

Expand Down
14 changes: 8 additions & 6 deletions battlefox/src/mapvote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl Inner {
map,
mode: GameMode::Rush,
vehicles,
nom_only: None,
});
self.update_matchers(true);
}
Expand Down Expand Up @@ -608,31 +609,32 @@ impl Mapvote {
if let Some(inner) = &mut *lock {
let not_in_popstate = prefs
.iter()
.filter(|MapInPool { map, mode, vehicles}| !inner.popstate.pool.contains_map(*map))
.filter(|MapInPool { map, mode, vehicles, nom_only}| !inner.popstate.pool.contains_map(*map))
.cloned()
.collect::<Vec<_>>();
// Remove maps which are forbidden by pop state.
prefs.retain(|MapInPool { map, mode, vehicles}| inner.popstate.pool.contains_map(*map));
prefs.retain(|MapInPool { map, mode, vehicles, nom_only}| inner.popstate.pool.contains_map(*map));

let not_in_options = prefs
.iter()
.filter(|MapInPool { map, mode, vehicles}| !inner.alternatives.contains_map(*map))
.filter(|MapInPool { map, mode, vehicles, nom_only}| !inner.alternatives.contains_map(*map))
.cloned()
.collect::<Vec<_>>();
// the maps are in the popstate, but aren't up to be chosen right now.
// Nomination possible.
prefs.retain(|MapInPool { map, mode, vehicles}| inner.alternatives.contains_map(*map));
prefs.retain(|MapInPool { map, mode, vehicles, nom_only}| inner.alternatives.contains_map(*map));

let weight = match player.clone().cases() {
Left(yesvip) => self.vip_vote_weight(),
Right(novip) => Rat::one(),
};

// now, attempt to deduplicate (Ballot:from_iter(..) does that for us)
let alts = prefs.iter().map(|MapInPool { map, mode, vehicles}| MapInPool {
let alts = prefs.iter().map(|MapInPool { map, mode, vehicles, nom_only}| MapInPool {
map: *map,
mode: mode.clone(),
vehicles: *vehicles
vehicles: *vehicles,
nom_only: *nom_only,
});
let (ballot, soft_dups) = match Ballot::from_iter(weight, alts) {
CheckBallotResult::Ok { ballot, soft_dups } => (ballot, soft_dups),
Expand Down

0 comments on commit 6be2ca3

Please sign in to comment.