From: Ian Jackson Date: Thu, 18 Mar 2021 23:48:28 +0000 (+0000) Subject: clock: Implement add_ui_operations X-Git-Tag: otter-0.5.0~651 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=b64960c0d0fb76f424fbd51836bec27c243731c6;p=otter.git clock: Implement add_ui_operations Signed-off-by: Ian Jackson --- diff --git a/src/clock.rs b/src/clock.rs index 978650c8..b66e2fa8 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -324,5 +324,71 @@ impl PieceTrait for Clock { Html::lit("the chess clock") } + #[throws(InternalError)] + fn add_ui_operations(&self, upd: &mut Vec, + gs: &GameState, gpc: &GPiece) { + let state: &State = gpc.xdata_exp()?; + + let for_users = || izip!(&USERS, &USERINFOS, &state.users).map( + |(&user, userinfo, ust)| (user, userinfo, ust, + userinfo.idchar.to_ascii_uppercase()) + ); + + for (user, userinfo, _ust, upchar) in for_users() { + if state.current.as_ref().map(|c| c.user) != Some(user) { + upd.push(UoDescription { + kind: UoKind::Piece, + def_key: userinfo.idchar, + opname: format!("start-{}", userinfo.idchar), + desc: Html(if state.current.is_none() { + format!("Start, with player {}", &upchar) + } else { + format!("Make player {} current", &upchar) + }), + wrc: WRC::UpdateSvg, + }); + } + } + + if state.current.is_some() { + upd.push(UoDescription { + kind: UoKind::Piece, + def_key: 'S', + opname: "stop".to_string(), + desc: Html::lit("Stop"), + wrc: WRC::UpdateSvg, + }); + } + if state.current.is_none() { + upd.push(UoDescription { + kind: UoKind::Piece, + def_key: 'R', + opname: "reset".to_string(), + desc: Html::lit("Reset"), + wrc: WRC::UpdateSvg, + }); + } + + for (_user, userinfo, ust, upchar) in for_users() { + if let Some(_gpl) = gs.players.get(ust.player) { + upd.push(UoDescription { + kind: UoKind::Piece, + def_key: upchar, + opname: format!("unclaim-{}", userinfo.idchar), + desc: Html(format!("Clear player {}", &upchar)), + wrc: WRC::UpdateSvg, + }); + } else { + upd.push(UoDescription { + kind: UoKind::Piece, + def_key: upchar, + opname: format!("claim-{}", userinfo.idchar), + desc: Html(format!("Become player {}", &upchar)), + wrc: WRC::UpdateSvg, + }); + } + } + } + fn itemname(&self) -> &str { "chess-clock" } }