summaryrefslogtreecommitdiff
path: root/server/src/play.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-11-16 09:10:41 -0500
committerKjetil Orbekk <kj@orbekk.com>2022-11-16 09:10:41 -0500
commit8fbf71b667d8b02777361adb7189939bd2d6fd02 (patch)
treefcadeede5218622d84aac467f1a692f4b106c3fd /server/src/play.rs
parentb114fe7940e77090861ac9ba60f4d0b8caec8978 (diff)
Generate and display table in the webapp
Diffstat (limited to 'server/src/play.rs')
-rw-r--r--server/src/play.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/server/src/play.rs b/server/src/play.rs
index 2632b24..01f4847 100644
--- a/server/src/play.rs
+++ b/server/src/play.rs
@@ -76,6 +76,10 @@ where
}
impl<J: Journal> Table<J> {
+ pub fn game(&self) -> &GameState { &self.game }
+}
+
+impl<J: Journal> Table<J> {
pub async fn new(mut journal: J) -> Result<Self, BridgeError> {
let game = GameState::Bidding {
dealer: Player::East,
@@ -88,7 +92,16 @@ impl<J: Journal> Table<J> {
pub async fn replay(mut journal: J) -> Result<Self, BridgeError> {
let games = journal.replay(0).await?;
if games.is_empty() {
- return Err(BridgeError::Internal(format!("empty journal")));
+ return Err(BridgeError::NotFound("table journal missing".to_string()));
+ }
+ let game = serde_json::from_value(games[games.len() - 1].clone())?;
+ Ok(Table { journal, game } )
+ }
+
+ pub async fn new_or_replay(mut journal: J) -> Result<Self, BridgeError> {
+ let games = journal.replay(0).await?;
+ if games.is_empty() {
+ return Self::new(journal).await;
}
let game = serde_json::from_value(games[games.len() - 1].clone())?;
Ok(Table { journal, game } )