id : vpiece,
face : p.face,
};
- let json = UpdatePayload::PieceUpdate(vpiece, p.mk_update(&pri));
- let json = serde_json::to_string(&json).expect("convert to json");
+ let op = PieceUpdateOp::Modify(p.prep_piecestate(&pri));
let update = PreparedUpdate {
gen,
- u : PreparedPieceUpdate {
+ us : vec![ PreparedUpdateEntry::Piece {
client,
sameclient_cseq : form.cseq,
piece : vpiece,
- json,
- },
+ op,
+ }],
};
let update = Arc::new(update);
// split vie wthing would go here, see also update.piece
pub gen_before_lastclient : Generation,
}
+#[derive(Debug,Serialize)]
+pub struct PreparedPieceState {
+ pub pos : Pos,
+ pub held : Option<PlayerId>,
+ pub svg : String,
+}
+
impl PieceRecord {
pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String {
let pr = self;
defs
}
- pub fn mk_update(&self, pri : &PieceRenderInstructions) -> PieceUpdate {
- PieceUpdate {
+ pub fn prep_piecestate(&self, pri : &PieceRenderInstructions)
+ -> PreparedPieceState {
+ PreparedPieceState {
pos : self.pos,
held : self.held,
- svgs : self.make_defs(pri),
+ svg : self.make_defs(pri),
// xxx want all piece's stuff in the same def
}
}
pub player : PlayerId,
}
-impl Client {
- pub fn transmit_update(&mut self, u : &Update) {
- eprintln!("XXX not transmitting {:?}", &u);
- }
-}
-
#[derive(Debug)]
pub struct PreparedUpdate {
pub gen : Generation,
- pub u : PreparedUpdatePayload,
+ pub us : Vec<PreparedUpdateEntry>,
}
#[derive(Debug)]
-pub enum PreparedUpdatePayload {
- PreparedPieceUpdate {
+pub enum PreparedUpdateEntry {
+ Piece {
client : ClientId,
sameclient_cseq : ClientSequence,
piece : VisiblePieceId,
- json : String,
+ op : PieceUpdateOp<PreparedPieceState>,
+ },
+ Log {
},
}
-pub use PreparedUpdatePayload::*;
-
-impl PreparedUpdatePayload {
- pub fn json_len(&self) -> usize { match self {
- &PreparedPieceUpdate { ref json, .. } => json.len(),
- } }
+impl PreparedUpdateEntry {
+ pub fn json_len(&self) -> usize {
+ use PreparedUpdateEntry::*;
+ match self {
+ Piece { ref op, .. } => {
+ 50 +
+ op.new_state().map(|x| x.svg.len()).unwrap_or(0)
+ },
+ Log { .. } => {
+ todo!("json length of log")
+ }
+ }
+ }
+}
+impl PreparedUpdate {
+ pub fn json_len(&self) -> usize {
+ self.us.iter().map(|u| 20 + u.json_len()).sum()
+ }
}
+#[derive(Debug,Serialize)]
+pub enum PieceUpdateOp<NS> {
+ Delete,
+ Insert(NS),
+ Modify(NS),
+ Move(Pos),
+}
+impl<NS> PieceUpdateOp<NS> {
+ pub fn new_state(&self) -> Option<&NS> {
+ use PieceUpdateOp::*;
+ match self {
+ Delete => None,
+ Insert(ns) => Some(ns),
+ Modify(ns) => Some(ns),
+ Move(_) => None,
+ }
+ }
+}
+
#[derive(Debug,Default)]
pub struct PlayerUpdates {
pub log : StableIndexVecDeque<Arc<PreparedUpdate>,sse::UpdateId>,
ami : Arc<Mutex<Instance>>,
}
-#[derive(Serialize)]
-struct RecordedConfirmation {
- gen : Generation,
- piece : VisiblePieceId,
- cseq : ClientSequence,
+#[derive(Debug,Serialize)]
+enum TransmitUpdate<'u> {
+ Recorded {
+ piece : VisiblePieceId,
+ cseq : ClientSequence,
+ },
+ Piece {
+ piece : VisiblePieceId,
+ op : &'u PieceUpdateOp<PreparedPieceState>,
+ },
+ Log {
+ },
}
#[derive(Error,Debug)]
let next = match pu.log.get(self.to_send) {
Some(next) => next, None => { break }
};
- let next_len = UPDATE_MAX_FRAMING_SIZE + next.u.json_len();
+ let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len();
if next_len > buf.len() { break }
- match &next.u {
- &PreparedPieceUpdate {
- piece, client : uclient, sameclient_cseq : cseq, ..
- } if uclient== self.client => {
- write!(buf, "event: recorded\n\
- data: ")?;
- serde_json::to_writer(&mut buf, &RecordedConfirmation {
- gen : next.gen,
- piece : piece,
- cseq : cseq,
- })?;
- write!(buf, "\n\n")?;
- },
- PreparedPieceUpdate { json, .. } => {
- write!(buf, "id: {}\n\
- data: {}\n\n",
- &self.to_send,
- json)?;
- },
+ write!(buf, "data: [")?;
+ for u in &next.us {
+ let tu = match u {
+ &PreparedUpdateEntry::Piece
+ { piece, client, sameclient_cseq : cseq, .. }
+ if client== self.client => {
+ TransmitUpdate::Recorded { piece, cseq }
+ },
+ &PreparedUpdateEntry::Piece { piece, ref op, .. } => {
+ TransmitUpdate::Piece { piece, op }
+ },
+ PreparedUpdateEntry::Log { } => {
+ TransmitUpdate::Log { }
+ },
+ };
+ serde_json::to_writer(&mut buf, &tu)?;
+ write!(buf,",")?;
}
- self.to_send.try_increment().unwrap();
+ serde_json::to_writer(&mut buf, &next.gen)?;
+ write!(buf, "]\n\
+ id: {}\n\n",
+ &self.to_send)?;
}
+
loop {
let generated = orig_wanted - buf.len();
if generated > 0 {
#[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)]
#[serde(transparent)]
pub struct ClientSequence(u64);
-
+/*
#[derive(Debug,Serialize)]
pub struct Update {
pub gen : Generation,
}
#[derive(Debug,Serialize)]
-pub struct PieceUpdate {
- pub pos : Pos,
- pub held : Option<PlayerId>,
- pub svgs : String,
-}
-
-#[derive(Debug,Serialize)]
-pub enum UpdatePayload {
- NoUpdate,
- ClientSequence(PieceId, ClientSequence),
- PieceDelete(VisiblePieceId),
- PieceInsert(VisiblePieceId, PieceUpdate),
- PieceUpdate(VisiblePieceId, PieceUpdate),
- PieceMove(VisiblePieceId, Pos),
+pub enum PieceUpdate {
}
+*/