From 52e20a058bd65415203bbce44fb27ddcf4d43972 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Sun, 7 May 2023 15:21:13 +0300 Subject: [PATCH] feat(client): use chrono for game and action times --- Cargo.lock | 144 ++++++++++++++++++++++++++- client/Cargo.toml | 3 + client/src/api/game/mod.rs | 14 +-- client/src/plugins/menu/ui/browse.rs | 87 ++++++++-------- 4 files changed, 189 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1282c54..5e10973 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,7 +56,7 @@ dependencies = [ "arrayvec", "once_cell", "paste", - "windows", + "windows 0.44.0", ] [[package]] @@ -1143,6 +1143,31 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "serde", + "time", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "chrono-humanize" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32dce1ea1988dbdf9f9815ff11425828523bd2a134ec0805d2ac8af26ee6096e" +dependencies = [ + "chrono", +] + [[package]] name = "clipboard-win" version = "4.5.0" @@ -1280,6 +1305,50 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "cxx" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn 2.0.15", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "d3d12" version = "0.6.0" @@ -1638,7 +1707,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -1716,7 +1785,7 @@ dependencies = [ "log", "thiserror", "winapi", - "windows", + "windows 0.44.0", ] [[package]] @@ -1895,6 +1964,30 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows 0.48.0", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "idna" version = "0.3.0" @@ -2029,6 +2122,8 @@ dependencies = [ "bevy", "bevy_editor_pls", "bevy_egui", + "chrono", + "chrono-humanize", "fastrand", "futures-lite", "reqwest", @@ -2059,6 +2154,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.2" @@ -2164,7 +2268,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] @@ -2818,6 +2922,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" + [[package]] name = "security-framework" version = "2.8.2" @@ -3095,6 +3205,17 @@ dependencies = [ "weezl", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -3356,6 +3477,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3629,6 +3756,15 @@ dependencies = [ "windows-targets 0.42.2", ] +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-implement" version = "0.44.0" diff --git a/client/Cargo.toml b/client/Cargo.toml index 712ce25..051f42a 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -20,6 +20,9 @@ serde_json = "1.0.96" fastrand = "1.9.0" +chrono = { version = "0.4.24", features = [ "serde" ] } +chrono-humanize = "0.2.2" + [dependencies.serde] version = "1.0.160" default-features = false diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs index e24137b..738ff06 100644 --- a/client/src/api/game/mod.rs +++ b/client/src/api/game/mod.rs @@ -49,8 +49,8 @@ pub enum Command { #[derive(Deserialize, Serialize, Clone)] pub struct Action { pub id: String, - pub created_at: String, - pub updated_at: String, + pub created_at: chrono::DateTime, + pub updated_at: chrono::DateTime, pub game_id: String, pub invoker: String, pub target: String, @@ -69,8 +69,8 @@ impl Action { ) -> Self { Self { id: "".to_string(), - created_at: "".to_string(), - updated_at: "".to_string(), + created_at: chrono::Utc::now(), + updated_at: chrono::Utc::now(), game_id: game_id.to_string(), invoker: invoker.to_string(), target: target.to_string(), @@ -92,9 +92,9 @@ pub enum GameState { #[derive(Deserialize, Clone)] pub struct Game { pub id: String, - pub created_at: String, - pub updated_at: String, - pub ended_at: String, + pub created_at: chrono::DateTime, + pub updated_at: chrono::DateTime, + pub ended_at: chrono::DateTime, pub host_id: String, pub host: Option, pub guest_id: String, diff --git a/client/src/plugins/menu/ui/browse.rs b/client/src/plugins/menu/ui/browse.rs index 683c715..7496788 100644 --- a/client/src/plugins/menu/ui/browse.rs +++ b/client/src/plugins/menu/ui/browse.rs @@ -13,7 +13,7 @@ use crate::{plugins::{ GameCreateCallEvent, BrowseState, GameFormingCallEvent, GameMyGamesCallEvent, GameJoinCallEvent, menu::MenuState -}, api::game::GameState, Global, AppState}; +}, api::game::{GameState, Game}, Global, AppState}; use super::{MenuData, MenuUIState}; @@ -79,54 +79,26 @@ pub fn view( egui::ScrollArea::vertical().show(ui, |ui| match data.browse_state { BrowseState::Forming => { for game in &data.forming_games { - egui::Frame::none() - .fill(egui::Color32::BLACK) - .rounding(4.) - .outer_margin(4.) - .inner_margin(4.) - .show(ui, |ui| { - ui.horizontal(|ui| { - ui.label(format!( - "Host: {}", - game.host.as_ref().unwrap().username - )); - - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if game.host_id == global.user.as_ref().unwrap().id { - ui.add_enabled(false, egui::Button::new("Host")); - } else if ui.button("Join").clicked() { - join_ev_w.send(GameJoinCallEvent { - game_id: game.id.clone(), - }); - } - }); + game_row(ui, game, |ui| { + if game.host_id == global.user.as_ref().unwrap().id { + ui.add_enabled(false, egui::Button::new("Host")); + } else if ui.button("Join").clicked() { + join_ev_w.send(GameJoinCallEvent { + game_id: game.id.clone(), }); - }); + } + }); } } BrowseState::InProgress => { let mut games = data.my_games.clone(); games.retain(|g| g.state == GameState::InProgress); for game in &games { - egui::Frame::none() - .fill(egui::Color32::BLACK) - .rounding(4.) - .outer_margin(4.) - .inner_margin(4.) - .show(ui, |ui| { - ui.horizontal(|ui| { - ui.label(format!( - "Host: {}", - game.host.as_ref().unwrap().username - )); - - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.button("Enter").clicked() { - enter_game(&mut commands, &mut global, &game.id); - } - }); - }); - }); + game_row(ui, game, |ui| { + if ui.button("Enter").clicked() { + enter_game(&mut commands, &mut global, &game.id); + } + }); } } BrowseState::Finished => { @@ -139,12 +111,7 @@ pub fn view( .outer_margin(4.) .inner_margin(4.) .show(ui, |ui| { - ui.horizontal(|ui| { - ui.label(format!( - "Host: {}", - game.host.as_ref().unwrap().username - )); - }); + game_row(ui, game, |_| {}); }); } } @@ -153,6 +120,30 @@ pub fn view( }); } +fn game_row(ui: &mut egui::Ui, game: &Game, buttons: impl FnOnce(&mut egui::Ui)) { + egui::Frame::none() + .fill(egui::Color32::BLACK) + .rounding(4.) + .outer_margin(4.) + .inner_margin(4.) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label(format!( + "Host: {}", + game.host.as_ref().unwrap().username + )); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + buttons(ui); + + ui.label(format!( + "{}", + chrono_humanize::HumanTime::from(game.created_at) + )); + }); + }); + }); +} + fn enter_game(commands: &mut Commands, global: &mut Global, game_id: &str) { global.cur_game_id = game_id.to_string(); -- 2.44.1