From 9f122659278d553a630e13d1d2a42801fc669c0e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 16 Oct 2020 23:20:03 +0100 Subject: [PATCH] wip timezone Signed-off-by: Ian Jackson --- src/bin/otter.rs | 11 +++++++++-- src/cmdlistener.rs | 13 +++++++++---- src/commands.rs | 8 +++++++- src/spec.rs | 2 ++ src/tz.rs | 10 +++++----- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index e51b0449..cf28f2d2 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -361,9 +361,16 @@ fn setup_table(ma: &MainOpts, chan: &mut ConnForGame, Err(anyhow!("duplicate player nick {:?} in spec", &pspec.nick))?; } st.new = true; + let timezone = pspec.timezone.as_ref().or( + spec.timezone.as_ref() + ).cloned(); + // ^ todo use client program timezone? if !st.old { - insns.push(MgmtGameInstruction::AddPlayer(PlayerState { - nick: pspec.nick.clone() + insns.push(MgmtGameInstruction::AddPlayer(MgmtPlayerState { + timezone, + st: PlayerState { + nick: pspec.nick.clone(), + }, })); } } diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index 7e84e8ce..9a7f7d32 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -159,15 +159,20 @@ fn execute_game_insn(cs: &CommandStream, } Insn::AddPlayer(pl) => { - if ig.gs.players.values().any(|p| p.nick == pl.nick) { + if ig.gs.players.values().any(|p| p.nick == pl.st.nick) { Err(ME::AlreadyExists)?; } let logentry = LogEntry { html: Html(format!("{} added a player: {}", &who, - htmlescape::encode_minimal(&pl.nick))), + htmlescape::encode_minimal(&pl.st.nick))), }; - let tz = Timezone::default_todo(); - let (player, logentry) = ig.player_new(pl, tz, logentry)?; + let timezone = pl.timezone.as_ref().map(String::as_str) + .unwrap_or(""); + let tz = match Timezone::from_str(timezone) { + Ok(tz) => tz, + Err(x) => x, // x is of type ! + }; + let (player, logentry) = ig.player_new(pl.st, tz, logentry)?; (U{ pcs: vec![], log: vec![ logentry ], raw: None }, diff --git a/src/commands.rs b/src/commands.rs index f39e038a..dbf2fd4b 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -39,13 +39,19 @@ pub enum MgmtGameInstruction { AddPieces(PiecesSpec), DeletePiece(PieceId), - AddPlayer(PlayerState), + AddPlayer(MgmtPlayerState), RemovePlayer(PlayerId), ResetPlayerAccesses { players: Vec }, ReportPlayerAccesses { players: Vec }, SetFixedPlayerAccess { player: PlayerId, token: RawToken }, } +#[derive(Debug,Serialize,Deserialize)] +pub struct MgmtPlayerState { + pub timezone: Option, + #[serde(flatten)] pub st: PlayerState, +} + #[derive(Debug,Serialize,Deserialize)] pub enum MgmtGameResponse { Fine, diff --git a/src/spec.rs b/src/spec.rs index 5380d0fa..5002452e 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -56,11 +56,13 @@ display_as_debug!{SpecError} #[derive(Debug,Serialize,Deserialize)] pub struct TableSpec { pub players : Vec, + pub timezone: Option, // default for player timezones } #[derive(Debug,Serialize,Deserialize)] pub struct PlayerSpec { pub nick: String, + pub timezone: Option, #[serde(flatten)] pub access: Option>, } diff --git a/src/tz.rs b/src/tz.rs index a2c4f655..04c7e20d 100644 --- a/src/tz.rs +++ b/src/tz.rs @@ -9,10 +9,10 @@ use parking_lot::{RwLock, const_rwlock}; #[derive(SerializeDisplay)] #[derive(DeserializeFromStr)] #[derive(Clone,Debug)] -pub struct Timezone (Arc); +pub struct Timezone (Arc); #[derive(Clone,Debug,Default)] -struct ChronoTz { +struct TzInfo { name: String, ctz: Option, } @@ -60,11 +60,11 @@ impl FromStr for Timezone { let name = name.to_string(); match chrono_tz::Tz::from_str(&name) { Ok(ctz) => { - Arc::new(ChronoTz { name, ctz: Some(ctz) }) + Arc::new(TzInfo { name, ctz: Some(ctz) }) }, Err(emsg) => { error!("Error loading timezone {:?}: {}, using UTC", name, emsg); - Arc::new(ChronoTz { name, ctz: None }) + Arc::new(TzInfo { name, ctz: None }) }, } }; @@ -77,6 +77,6 @@ impl FromStr for Timezone { impl Default for Timezone { fn default() -> Self { - Timezone(Arc::new(ChronoTz { ctz: None, name: default() })) + Timezone(Arc::new(TzInfo { ctz: None, name: default() })) } } -- 2.30.2