diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2022-12-22 14:49:43 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2022-12-22 14:49:43 -0500 |
commit | cca246746538a5e689168741b2e9e2542613cf8a (patch) | |
tree | fc3e7f7e2d32d346fa3441395f5f43ba5db12e26 /server | |
parent | 22452073063b9211aa2aa38a1b1e2cf2e44cf133 (diff) |
Fix an issue with json serialization of journal entries
Diffstat (limited to 'server')
-rw-r--r-- | server/src/play.rs | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/server/src/play.rs b/server/src/play.rs index 19fa0a0..62b6219 100644 --- a/server/src/play.rs +++ b/server/src/play.rs @@ -9,9 +9,10 @@ use protocol::{ simple_bots::AlwaysPassBiddingBot, }; use rand::random; -use serde::{Deserialize, Serialize, de::DeserializeOwned}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::json; -use sqlx::{query, PgPool}; +use sqlx::{query, query_as, PgPool}; +use tracing::info; use uuid::Uuid; use crate::error::BridgeError; @@ -45,7 +46,10 @@ impl DbJournal { } #[async_trait] -impl<Item: Serialize + DeserializeOwned + Send + 'static> Journal<Item> for DbJournal { +impl<Item> Journal<Item> for DbJournal +where + Item: Clone + Serialize + DeserializeOwned + Send + Sync + Unpin + 'static, +{ async fn append( &mut self, seq: i64, @@ -58,7 +62,7 @@ impl<Item: Serialize + DeserializeOwned + Send + 'static> Journal<Item> for DbJo "#, self.id, seq, - json!(payload), + sqlx::types::Json(payload) as _, ) .execute(&self.db) .await; @@ -75,9 +79,10 @@ impl<Item: Serialize + DeserializeOwned + Send + 'static> Journal<Item> for DbJo } async fn replay(&mut self, seq: i64) -> Result<Vec<Item>, BridgeError> { - let rows = query!( + let rows = query_as!( + ReplayRow::<Item>, r#" - select seq, payload as "payload!: String" from object_journal + select seq, payload as "payload: _" from object_journal where id = $1 and seq >= $2 order by seq "#, @@ -86,9 +91,9 @@ impl<Item: Serialize + DeserializeOwned + Send + 'static> Journal<Item> for DbJo ) .fetch_all(&self.db) .await?; - let mut payloads = vec![]; + let mut payloads: Vec<Item> = vec![]; for v in rows { - payloads.push(serde_json::from_str(&v.payload)?); + payloads.push(v.payload.0); self.seq = v.seq; } Ok(payloads) @@ -185,7 +190,7 @@ impl<J: Journal<TableUpdate>> Table<J> { Ok(()) } - pub async fn play(&mut self, card: Card) -> Result<(), BridgeError> { + pub async fn play(&mut self, _card: Card) -> Result<(), BridgeError> { Ok(()) } @@ -198,6 +203,7 @@ impl<J: Journal<TableUpdate>> Table<J> { } let mut state = TableState::Unknown; for update in log { + info!("Replaying update {update:?}"); match update { TableUpdate::NewDeal(_, _) => todo!(), TableUpdate::ChangeSettings(_) => todo!(), @@ -225,6 +231,12 @@ impl<J: Journal<TableUpdate>> Table<J> { } } +#[derive(Debug)] +struct ReplayRow<Item> { + seq: i64, + payload: sqlx::types::Json<Item>, +} + pub async fn advance_play<J: Journal<TableUpdate>>( table: &mut Table<J>, ) -> Result<(), BridgeError> { @@ -320,8 +332,7 @@ mod test { #[tokio::test] async fn test_advance_play() { - let mut t1 = - Table::new(TestJournal::new()).await.unwrap(); + let mut t1 = Table::new(TestJournal::new()).await.unwrap(); let player = t1.game().unwrap().current_player(); advance_play(&mut t1).await.unwrap(); assert_ne!(player, t1.game().unwrap().current_player()); |