let p = ig.pieces.as_mut(modperm)
.remove(piece).ok_or(ME::PieceNotFound)?;
let gs = &mut ig.gs;
- let pc = gs.pieces.remove(piece);
+ let pc = gs.pieces.as_mut(modperm).remove(piece);
let desc_html = p.describe_html(Some(Default::default()));
if let Some(pc) = pc { p.delete_hook(&pc, gs); }
(U{ pcs: vec![(piece, PieceUpdateOp::Delete())],
if let (_, true) = pc.pos.clamped(gs.table_size) {
throw!(SpecError::PosOffTable);
}
- let piece = gs.pieces.insert(pc);
+ let piece = gs.pieces.as_mut(modperm).insert(pc);
ig.pieces.as_mut(modperm).insert(piece, p);
updates.push((piece, PieceUpdateOp::Insert(())));
pos[0] += posd[0];
use crate::imports::*;
use std::sync::PoisonError;
+use slotmap::dense as sm;
// ---------- newtypes and type aliases ----------
#[derive(Copy,Clone,Debug)]
pub struct ModifyingPieces(());
+#[derive(Debug,Serialize,Deserialize,Default)]
+#[serde(transparent)]
+pub struct Pieces (pub(in crate::global) ActualPieces);
+type ActualPieces = DenseSlotMap<PieceId,PieceState>;
+
#[derive(Debug,Clone,Deserialize,Serialize)]
#[derive(Eq,PartialEq,Ord,PartialOrd,Hash)]
pub enum ManagementScope {
}
}
undo.push(Box::new(|ig| for &piece in &updated_pieces {
- ig.c.g.gs.pieces[piece].held = Some(oldplayer)
+ (||Some({
+ ig.c.g.gs.pieces.get_mut(piece)?.held = Some(oldplayer);
+ }))();
}));
// Handle gs.log:
(||{
for &piece in &updated_pieces {
- self.c.g.gs.pieces[piece].gen = self.c.g.gs.gen;
+ (||Some({
+ self.c.g.gs.pieces.get_mut(piece)?.gen = self.c.g.gs.gen;
+ }))();
}
let lens = TransparentLens { };
for mut p in gs.pieces.values_mut() {
p.lastclient = Default::default();
}
- gs.pieces.retain(|k,_v| pieces.contains_key(k));
- pieces.retain(|k,_v| gs.pieces.contains_key(k));
+ gs.pieces.0.retain(|k,_v| pieces.contains_key(k));
+ pieces.retain(|k,_v| gs.pieces.0.contains_key(k));
gs
};
}
}
+// ---------- gamestate pieces table ----------
+
+impl Deref for Pieces {
+ type Target = ActualPieces;
+ fn deref(&self) -> &ActualPieces { &self.0 }
+}
+
+impl Pieces {
+ pub fn get_mut(&mut self, piece: PieceId) -> Option<&mut PieceState> {
+ self.0.get_mut(piece)
+ }
+ pub fn values_mut(&mut self) -> sm::ValuesMut<PieceId, PieceState> {
+ self.0.values_mut()
+ }
+ pub fn as_mut(&mut self, _: ModifyingPieces) -> &mut ActualPieces {
+ &mut self.0
+ }
+}
+
+impl ById for Pieces {
+ type Id = PieceId;
+ type Entry = PieceState;
+ type Error = OnlineError;
+ #[throws(OE)]
+ fn byid(&self, piece: PieceId) -> &PieceState {
+ self.get(piece).ok_or(OE::PieceGone)?
+ }
+ #[throws(OE)]
+ fn byid_mut(&mut self, piece: PieceId) -> &mut PieceState {
+ self.get_mut(piece).ok_or(OE::PieceGone)?
+ }
+}
+
+/*impl<'p> IntoIterator for &'p Pieces {
+ type Item = (PieceId, &'p PieceState);
+ type IntoIter = sm::Iter<'p, PieceId, PieceState>;
+ fn into_iter(self) -> Self::IntoIter { (&self.0).into_iter() }
+}*/
+impl<'p> IntoIterator for &'p mut Pieces {
+ type Item = (PieceId, &'p mut PieceState);
+ type IntoIter = sm::IterMut<'p, PieceId, PieceState>;
+ fn into_iter(self) -> Self::IntoIter { (&mut self.0).into_iter() }
+}
+
// ========== background maintenance ==========
// ---------- delayed game save ----------