From a2af394bb98da0b712b01e1f397f5748c9fce84e Mon Sep 17 00:00:00 2001 From: Adam Doyle Date: Mon, 2 Dec 2024 22:50:49 -0500 Subject: [PATCH] add outgoing message handler --- Cargo.lock | 330 +++++++++++---------- clients/leptos/Cargo.toml | 1 + clients/leptos/src/components/websocket.rs | 66 +++-- lib/src/lib.rs | 64 +++- server/src/game_handler.rs | 68 +++-- server/src/lib.rs | 14 +- server/src/main.rs | 23 +- server/src/outgoing_message_handler.rs | 46 +++ server/src/user_handler.rs | 268 ++++++++++------- 9 files changed, 538 insertions(+), 342 deletions(-) create mode 100644 server/src/outgoing_message_handler.rs diff --git a/Cargo.lock b/Cargo.lock index 144e4c8..9fdee3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,12 +107,12 @@ dependencies = [ [[package]] name = "any_spawner" -version = "0.1.1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9747eb01aed7603aba23f7c869d5d7e5d37aab9c3501aced42d8fdb786f1f6e3" +checksum = "41058deaa38c9d9dd933d6d238d825227cffa668e2839b52879f6619c63eee3b" dependencies = [ "futures", - "thiserror 1.0.69", + "thiserror 2.0.3", "wasm-bindgen-futures", ] @@ -139,9 +139,9 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] name = "async-compression" -version = "0.4.17" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cb8f1d480b0ea3783ab015936d2a55c87e219676f0c0b7dec61494043f21857" +checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" dependencies = [ "brotli", "flate2", @@ -205,9 +205,9 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "attribute-derive" -version = "0.9.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f1ee502851995027b06f99f5ffbeffa1406b38d0b318a1ebfa469332c6cbafd" +checksum = "0053e96dd3bec5b4879c23a138d6ef26f2cb936c9cdc96274ac2b9ed44b5bb54" dependencies = [ "attribute-derive-macro", "derive-where", @@ -219,14 +219,14 @@ dependencies = [ [[package]] name = "attribute-derive-macro" -version = "0.9.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3601467f634cfe36c4780ca9c75dea9a5b34529c1f2810676a337e7e0997f954" +checksum = "463b53ad0fd5b460af4b1915fe045ff4d946d025fb6c4dc3337752eaa980f71b" dependencies = [ "collection_literals", "interpolator", "manyhow", - "proc-macro-utils 0.8.0", + "proc-macro-utils", "proc-macro2", "quote", "quote-use", @@ -410,9 +410,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "camino" @@ -422,9 +422,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" [[package]] name = "cc" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" dependencies = [ "jobserver", "libc", @@ -658,9 +658,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "cpufeatures" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" dependencies = [ "libc", ] @@ -860,9 +860,9 @@ dependencies = [ [[package]] name = "event-listener-strategy" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" +checksum = "3c3e4e0dd3673c1139bf041f3008816d9cf2946bbfac2945c09e523b8d7b05b2" dependencies = [ "event-listener", "pin-project-lite", @@ -949,6 +949,7 @@ dependencies = [ "futures-core", "futures-task", "futures-util", + "num_cpus", ] [[package]] @@ -1123,7 +1124,7 @@ dependencies = [ "futures-core", "futures-sink", "http", - "indexmap 2.6.0", + "indexmap 2.7.0", "slab", "tokio", "tokio-util", @@ -1144,9 +1145,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "hdrhistogram" @@ -1212,9 +1213,9 @@ dependencies = [ [[package]] name = "http-range-header" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a" +checksum = "9171a2ea8a68358193d15dd5d70c1c10a2afc3e7e4c5bc92bc9f025cebd7359c" [[package]] name = "httparse" @@ -1236,9 +1237,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hydration_context" -version = "0.2.0-rc1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "571a2756b518de368146ba56d4feeba265b0b555d176d49b8debd9ce9b69a6fa" +checksum = "ef13071fe13b55c85fe2b70246d2e3b49d2c6a764fd3e0edaf262cc385ff1854" dependencies = [ "futures", "once_cell", @@ -1686,12 +1687,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", - "hashbrown 0.15.1", + "hashbrown 0.15.2", ] [[package]] @@ -1717,9 +1718,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "jobserver" @@ -1732,10 +1733,11 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -1747,9 +1749,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "leptos" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87a99b3ca2611376257a8b29466dc5ebf22c6bbed8715f86847808c269844785" +checksum = "ba5046c590aea121f6ad5e71fcb75453a933425d39527b9a3b1b295235afc8df" dependencies = [ "any_spawner", "cfg-if", @@ -1772,7 +1774,7 @@ dependencies = [ "server_fn", "slotmap", "tachys", - "thiserror 1.0.69", + "thiserror 2.0.3", "throw_error", "typed-builder", "typed-builder-macro", @@ -1782,9 +1784,9 @@ dependencies = [ [[package]] name = "leptos-use" -version = "0.14.0-rc3" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc40bd3ae7bf4dd84b5a67f7fc0e91d0335341e07430900001242dd81ac7aec2" +checksum = "5b50a99041c6685fdbca516bf6f90e75013e00a093d231c5f6a73b7b9dfff633" dependencies = [ "cfg-if", "codee", @@ -1806,22 +1808,22 @@ dependencies = [ [[package]] name = "leptos_config" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04c89503f59940cef245827075362107d4d53f9a710db31bfc361151866ba39e" +checksum = "5e2d64c43e2554108c26da3127f8384d92ca76c6f0b7288d1c09c8cc68152064" dependencies = [ "config", "regex", "serde", - "thiserror 1.0.69", + "thiserror 2.0.3", "typed-builder", ] [[package]] name = "leptos_dom" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b108dc3960fac70b98d2302e568cd51c4d09255f1925bb797eba95575acc69b8" +checksum = "6c15aca81dc2edd040b51c46734f65c6f36e6ba8a31347c1354c94b958044ae0" dependencies = [ "js-sys", "or_poisoned", @@ -1834,13 +1836,13 @@ dependencies = [ [[package]] name = "leptos_hot_reload" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10230c3b99bffd7490f192c54e02f0b35411d08089aba7f662d78be2adfbb736" +checksum = "0445f3a62696d2d66bef288911af34405718880b4b8dd6c5cfb7751fd8ffcc6b" dependencies = [ "anyhow", "camino", - "indexmap 2.6.0", + "indexmap 2.7.0", "parking_lot", "proc-macro2", "quote", @@ -1852,9 +1854,9 @@ dependencies = [ [[package]] name = "leptos_macro" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e948a21600d13a6e91d9a0fb56de9c58692f79890caeaec7dabb1cd63e8ec92" +checksum = "92f690c955274f1722ee6c66463ace79301d53a8c2bf7f6e4e61b978ca239e20" dependencies = [ "attribute-derive", "cfg-if", @@ -1874,12 +1876,12 @@ dependencies = [ [[package]] name = "leptos_meta" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15ad116cab8a1073bb6556d65c153d4c951ff06fa84387f723289ee16bb6a7b4" +checksum = "7c651d788bbbf1c57ee95dd3835f9d433b85a409b6256f338c3c7146eb8b7f53" dependencies = [ "futures", - "indexmap 2.6.0", + "indexmap 2.7.0", "leptos", "once_cell", "or_poisoned", @@ -1890,9 +1892,9 @@ dependencies = [ [[package]] name = "leptos_router" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9924f84cfc4dbe4440c2e58bf54694471e96b21557cb1e693e5bd73f3acae355" +checksum = "32a4f1784486ebf36805dac22faee21e3e610aa64b6662a7386f065eeec27ae8" dependencies = [ "any_spawner", "either_of", @@ -1906,7 +1908,7 @@ dependencies = [ "reactive_graph", "send_wrapper", "tachys", - "thiserror 1.0.69", + "thiserror 2.0.3", "url", "wasm-bindgen", "web-sys", @@ -1914,9 +1916,9 @@ dependencies = [ [[package]] name = "leptos_router_macro" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf8fc6fbb1d1f8f3656b8e33265a2e92319fb877b38014407767d85362f528cb" +checksum = "eee7ecef3f1c69b51864190c564e4873d84f200e44efb37934208f9525f02a5f" dependencies = [ "proc-macro-error2", "proc-macro2", @@ -1925,9 +1927,9 @@ dependencies = [ [[package]] name = "leptos_server" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd1abdb945660f19f7510bdf208069d3bc391aa9bb7183aa58e4af5429e6115" +checksum = "93450589df3b3e398c7f5ea64d8f1c8369b1ba9b90e1f70f6cb996b8d443ca3e" dependencies = [ "any_spawner", "base64 0.22.1", @@ -1956,9 +1958,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.164" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "linear-map" @@ -1968,9 +1970,9 @@ checksum = "bfae20f6b19ad527b550c223fddc3077a547fc70cda94b9b566575423fd303ee" [[package]] name = "litemap" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" +checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" [[package]] name = "lock_api" @@ -1990,9 +1992,9 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "manyhow" -version = "0.10.4" +version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91ea592d76c0b6471965708ccff7e6a5d277f676b90ab31f4d3f3fc77fade64" +checksum = "b33efb3ca6d3b07393750d4030418d594ab1139cee518f0dc88db70fec873587" dependencies = [ "manyhow-macros", "proc-macro2", @@ -2002,11 +2004,11 @@ dependencies = [ [[package]] name = "manyhow-macros" -version = "0.10.4" +version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c64621e2c08f2576e4194ea8be11daf24ac01249a4f53cd8befcbb7077120ead" +checksum = "46fce34d199b78b6e6073abf984c9cf5fd3e9330145a93ee0738a7443e371495" dependencies = [ - "proc-macro-utils 0.8.0", + "proc-macro-utils", "proc-macro2", "quote", ] @@ -2075,11 +2077,10 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ - "hermit-abi", "libc", "wasi", "windows-sys 0.52.0", @@ -2087,9 +2088,9 @@ dependencies = [ [[package]] name = "next_tuple" -version = "0.1.0-rc1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3615947b64839fe27e9ee557f282b04ad13ead564a543a6548b0cfd17457ff4" +checksum = "60993920e071b0c9b66f14e2b32740a4e27ffc82854dcd72035887f336a09a28" [[package]] name = "no-std-compat" @@ -2144,6 +2145,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.36.5" @@ -2242,9 +2253,9 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pathdiff" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d61c5ce1153ab5b689d0c074c4e7fc613e942dfb7dd9eea5ab202d2ad91fe361" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" [[package]] name = "percent-encoding" @@ -2334,9 +2345,9 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "portable-atomic" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" +checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" [[package]] name = "powerfmt" @@ -2408,17 +2419,6 @@ dependencies = [ "syn", ] -[[package]] -name = "proc-macro-utils" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f59e109e2f795a5070e69578c4dc101068139f74616778025ae1011d4cd41a8" -dependencies = [ - "proc-macro2", - "quote", - "smallvec", -] - [[package]] name = "proc-macro-utils" version = "0.10.0" @@ -2432,9 +2432,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.91" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -2524,7 +2524,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82ebfb7faafadc06a7ab141a6f67bcfb24cb8beb158c6fe933f2f035afa99f35" dependencies = [ - "proc-macro-utils 0.10.0", + "proc-macro-utils", "proc-macro2", "quote", "syn", @@ -2571,9 +2571,9 @@ dependencies = [ [[package]] name = "reactive_graph" -version = "0.1.0-rc1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cdfe96bdf3d95c9bdd15af1cfc82252ef85c026ea960f83df53feca9fec61a" +checksum = "c27f54685c1416af1f323a0c40e71cbdae281a1ebc623591790d367222d0ac65" dependencies = [ "any_spawner", "async-lock", @@ -2586,15 +2586,15 @@ dependencies = [ "send_wrapper", "serde", "slotmap", - "thiserror 1.0.69", + "thiserror 2.0.3", "web-sys", ] [[package]] name = "reactive_stores" -version = "0.1.0-rc1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fa994eaec6638c22faeedd4524cbd5a3653e7f32a67e130a60a0c8c9e1ee81" +checksum = "efe3f866edc7647e19a68a229a2e5cc9730549836d722eeaa073116f2b07966e" dependencies = [ "guardian", "itertools", @@ -2607,9 +2607,9 @@ dependencies = [ [[package]] name = "reactive_stores_macro" -version = "0.1.0-rc1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9d7cfe1c6d9f1005605ac7c928aef7e037d6808d6eda0429e73f0ca90cb9cb5" +checksum = "4d86e4f08f361b05d11422398cef4bc4cf356f2fdd2f06a96646b0e9cd902226" dependencies = [ "convert_case", "proc-macro-error2", @@ -2694,9 +2694,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" [[package]] name = "rustversion" @@ -2839,9 +2839,9 @@ dependencies = [ [[package]] name = "server_fn" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "166689e862d227e838f359cce92681cf338e60a8cbcbd029bae03548d5a6d785" +checksum = "033cb8014aa86a7ce0c6ee58d23dce1a078b2e320dc6c53bb439663993199b1f" dependencies = [ "bytes", "const_format", @@ -2857,7 +2857,7 @@ dependencies = [ "serde_json", "serde_qs", "server_fn_macro_default", - "thiserror 1.0.69", + "thiserror 2.0.3", "throw_error", "url", "wasm-bindgen", @@ -2869,9 +2869,9 @@ dependencies = [ [[package]] name = "server_fn_macro" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4230cd52992681cc370885a613a5aad9fb56d61995b789de5c42dc55ff628e98" +checksum = "0249e8a55ca464a1e69f02a95d562f2c65e92e301093a02ebf15d21f68f2a99e" dependencies = [ "const_format", "convert_case", @@ -2883,9 +2883,9 @@ dependencies = [ [[package]] name = "server_fn_macro_default" -version = "0.7.0-rc1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8d4b8ee78096ff2b09c64afacb83f023f4edfdace60c00590b2aea215fea8a4" +checksum = "91c54a6d43cd0f3d2bdf0c85b6119f378b6b89d528159af9cde77f229faeecbc" dependencies = [ "server_fn_macro", "syn", @@ -2958,9 +2958,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" dependencies = [ "libc", "windows-sys 0.52.0", @@ -3000,9 +3000,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.89" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -3046,9 +3046,9 @@ dependencies = [ [[package]] name = "tachys" -version = "0.1.0-rc1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b24784a27f391dedcc8160ea4b2ba7a6ad0f5484ca89ff88a10494f960aee270" +checksum = "8be68dfd4abf192e11a1bdd484239daa84ffa6fcd27c25cf4f011b2b0fb27ddb" dependencies = [ "any_spawner", "const_str_slice_concat", @@ -3056,7 +3056,7 @@ dependencies = [ "either_of", "futures", "html-escape", - "indexmap 2.6.0", + "indexmap 2.7.0", "itertools", "js-sys", "linear-map", @@ -3078,8 +3078,8 @@ dependencies = [ [[package]] name = "thaw" -version = "0.4.0-beta4" -source = "git+https://github.com/thaw-ui/thaw.git#52f725daded4c8581d73bca8c6cb851cc6194727" +version = "0.4.0-beta5" +source = "git+https://github.com/thaw-ui/thaw.git#152cfc8dfedefe29be3554788901dc9a2e19b01b" dependencies = [ "cfg-if", "chrono", @@ -3100,8 +3100,8 @@ dependencies = [ [[package]] name = "thaw_components" -version = "0.2.0-beta4" -source = "git+https://github.com/thaw-ui/thaw.git#52f725daded4c8581d73bca8c6cb851cc6194727" +version = "0.2.0-beta5" +source = "git+https://github.com/thaw-ui/thaw.git#152cfc8dfedefe29be3554788901dc9a2e19b01b" dependencies = [ "cfg-if", "leptos", @@ -3113,8 +3113,8 @@ dependencies = [ [[package]] name = "thaw_macro" -version = "0.1.0-beta4" -source = "git+https://github.com/thaw-ui/thaw.git#52f725daded4c8581d73bca8c6cb851cc6194727" +version = "0.1.0-beta5" +source = "git+https://github.com/thaw-ui/thaw.git#152cfc8dfedefe29be3554788901dc9a2e19b01b" dependencies = [ "proc-macro2", "quote", @@ -3123,12 +3123,13 @@ dependencies = [ [[package]] name = "thaw_utils" -version = "0.1.0-beta4" -source = "git+https://github.com/thaw-ui/thaw.git#52f725daded4c8581d73bca8c6cb851cc6194727" +version = "0.1.0-beta5" +source = "git+https://github.com/thaw-ui/thaw.git#152cfc8dfedefe29be3554788901dc9a2e19b01b" dependencies = [ "cfg-if", "chrono", "leptos", + "reactive_stores", "send_wrapper", "wasm-bindgen", "web-sys", @@ -3186,9 +3187,9 @@ dependencies = [ [[package]] name = "throw_error" -version = "0.2.0-rc1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84bf87b0433891f1029e5868d4de3a11e794a206899082ebb4e82fca139b0d70" +checksum = "e4ef8bf264c6ae02a065a4a16553283f0656bd6266fc1fcb09fd2e6b5e91427b" dependencies = [ "pin-project-lite", ] @@ -3327,7 +3328,7 @@ version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ - "indexmap 2.6.0", + "indexmap 2.7.0", "serde", "serde_spanned", "toml_datetime", @@ -3457,9 +3458,9 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.40" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", @@ -3469,9 +3470,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", @@ -3480,9 +3481,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", "valuable", @@ -3501,9 +3502,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.18" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" dependencies = [ "matchers", "nu-ansi-term", @@ -3543,18 +3544,18 @@ dependencies = [ [[package]] name = "typed-builder" -version = "0.19.1" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a06fbd5b8de54c5f7c91f6fe4cebb949be2125d7758e630bb58b1d831dbce600" +checksum = "7e14ed59dc8b7b26cacb2a92bad2e8b1f098806063898ab42a3bd121d7d45e75" dependencies = [ "typed-builder-macro", ] [[package]] name = "typed-builder-macro" -version = "0.19.1" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9534daa9fd3ed0bd911d462a37f172228077e7abf18c18a5f67199d959205f8" +checksum = "560b82d656506509d43abe30e0ba64c56b1953ab3d4fe7ba5902747a7a3cedd5" dependencies = [ "proc-macro2", "quote", @@ -3611,9 +3612,9 @@ checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] name = "url" -version = "2.5.3" +version = "2.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" dependencies = [ "form_urlencoded", "idna", @@ -3701,9 +3702,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" dependencies = [ "cfg-if", "once_cell", @@ -3712,9 +3713,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" dependencies = [ "bumpalo", "log", @@ -3727,21 +3728,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.45" +version = "0.4.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" +checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" dependencies = [ "cfg-if", "js-sys", + "once_cell", "wasm-bindgen", "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3749,9 +3751,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" dependencies = [ "proc-macro2", "quote", @@ -3762,19 +3764,19 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" [[package]] name = "wasm-bindgen-test" -version = "0.3.45" +version = "0.3.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d381749acb0943d357dcbd8f0b100640679883fcdeeef04def49daf8d33a5426" +checksum = "3d919bb60ebcecb9160afee6c71b43a58a4f0517a2de0054cd050d02cec08201" dependencies = [ - "console_error_panic_hook", "js-sys", "minicov", + "once_cell", "scoped-tls", "wasm-bindgen", "wasm-bindgen-futures", @@ -3783,9 +3785,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.45" +version = "0.3.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c97b2ef2c8d627381e51c071c2ab328eac606d3f69dd82bcbca20a9e389d95f0" +checksum = "222ebde6ea87fbfa6bdd2e9f1fd8a91d60aee5db68792632176c4e16a74fc7d8" dependencies = [ "proc-macro2", "quote", @@ -3807,9 +3809,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.72" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" dependencies = [ "js-sys", "wasm-bindgen", @@ -3972,9 +3974,9 @@ checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "yoke" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" dependencies = [ "serde", "stable_deref_trait", @@ -3984,9 +3986,9 @@ dependencies = [ [[package]] name = "yoke-derive" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", @@ -4017,18 +4019,18 @@ dependencies = [ [[package]] name = "zerofrom" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", diff --git a/clients/leptos/Cargo.toml b/clients/leptos/Cargo.toml index 5d7261a..6845dc5 100644 --- a/clients/leptos/Cargo.toml +++ b/clients/leptos/Cargo.toml @@ -26,6 +26,7 @@ log = "0" # Leptos-use leptos-use = { version = "0.14.0-rc3", features = ["use_media_query"] } codee = "0" +# codee = { path = "../../../codee" } # UI stuff # thaw = { version = "0.4.0-beta", features = ["csr", "nightly"] } diff --git a/clients/leptos/src/components/websocket.rs b/clients/leptos/src/components/websocket.rs index 8421d75..a0e8cc8 100644 --- a/clients/leptos/src/components/websocket.rs +++ b/clients/leptos/src/components/websocket.rs @@ -1,3 +1,4 @@ +// use bitcode::decode; use codee::string::FromToStringCodec; use leptos::prelude::*; use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebSocketReturn}; @@ -106,32 +107,53 @@ pub fn Websocket() -> impl IntoView { provide_context::>>(users_count); provide_context::>>(games_count); - // Message handler + // Incoming Message handler Effect::new(move |_| { message.with(move |message_raw| { if let Some(message) = message_raw { - if let Ok(users_count) = from_str::(message) { - set_users_count(Some(users_count)); - } else if let Ok(games_count) = from_str::(message) { - set_games_count(Some(games_count)); - } else if let Ok(chat_message) = from_str::(message) { - set_chat_message(Some(chat_message)); - } else if let Ok(user_update) = from_str::(message) { - set_user_update(Some(user_update)); - } else if let Ok(chat_update) = from_str::(message) { - set_chat_update(Some(chat_update)); - } else if let Ok(games_update) = from_str::(message) { - set_active_games(games_update.games); - } else if let Ok(game_meta) = from_str::(message) { - set_game_meta(Some(game_meta)); - } else if let Ok(game_update) = from_str::(message) { - set_game_state(Some(game_update)); - } else if let Ok(packs_meta_update) = from_str::(message) { - set_card_packs_meta(packs_meta_update); - } else if let Ok(judge_update) = from_str::(message) { - set_judge_round(Some(judge_update)); + if let Ok(message) = from_str::(message) { + match message { + ServerToClientMessage::CardPacksMeta(card_packs_meta) => { + set_card_packs_meta(card_packs_meta); + } + + ServerToClientMessage::ChatMessage(chat_message) => { + set_chat_message(Some(chat_message)); + } + ServerToClientMessage::ChatUpdate(chat_update) => { + set_chat_update(Some(chat_update)); + } + + ServerToClientMessage::GameMeta(game_meta) => { + set_game_meta(Some(game_meta)); + } + + ServerToClientMessage::GameStateMeta(game_state_meta) => { + set_game_state(Some(game_state_meta)); + } + + ServerToClientMessage::GamesUpdate(games_update) => { + set_active_games(games_update.games); + } + + ServerToClientMessage::JudgeRound(judge_round) => { + set_judge_round(Some(judge_round)); + } + + ServerToClientMessage::ServerActiveGames(server_active_games) => { + set_games_count(Some(server_active_games)); + } + + ServerToClientMessage::ServerOnlineUsers(server_online_users) => { + set_users_count(Some(server_online_users)); + } + + ServerToClientMessage::UserUpdate(user_update) => { + set_user_update(Some(user_update)); + } + } } else { - println!("Unhandled message: {:#?}", message); + leptos::logging::log!("Unhandled message: {:#?}", message); } } }); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 57d924a..f11b260 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,6 +1,38 @@ use bitcode::{Decode, Encode}; use serde::{Deserialize, Serialize}; +pub enum ClientToServerMessage {} + +#[derive(Serialize, Deserialize, Encode, Decode, Debug)] +pub enum ServerToClientMessage { + CardPacksMeta(CardPacksMeta), + ChatMessage(ChatMessage), + ChatUpdate(ChatUpdate), + GameMeta(GameMeta), + GameStateMeta(GameStateMeta), + GamesUpdate(GamesUpdate), + JudgeRound(JudgeRound), + ServerActiveGames(ServerActiveGames), + ServerOnlineUsers(ServerOnlineUsers), + UserUpdate(UserUpdate), +} + +#[derive(Serialize, Deserialize, Encode, Decode)] +pub enum CardsMessage { + BlackCardMeta(BlackCardMeta), + CardPackMeta(CardPackMeta), + GameBrowserMeta(GameBrowserMeta), + GameDeleteRequest(GameDeleteRequest), + GameJoinRequest(GameJoinRequest), + GamePlayerMeta(GamePlayerMeta), + JudgeDecisionRequest(JudgeDecisionRequest), + JudgeEntry(JudgeEntry), + NewGameRequest(NewGameRequest), + PlayerMoveRequest(PlayerMoveRequest), + UserLogInRequest(UserLogInRequest), + WhiteCardMeta(WhiteCardMeta), +} + /// Judge decision #[derive(Clone, Serialize, Deserialize, Encode, Decode)] pub struct JudgeDecisionRequest { @@ -9,20 +41,20 @@ pub struct JudgeDecisionRequest { } /// White Card Meta -#[derive(Clone, Encode, Decode, Hash, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, Encode, Decode, Hash, Eq, PartialEq, Serialize, Deserialize)] pub struct WhiteCardMeta { pub uuid: String, pub text: String, } /// Judge entry -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)] pub struct JudgeEntry { pub cards_to_judge: Vec, } /// Judge round -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)] pub struct JudgeRound { pub entries: Vec, } @@ -47,7 +79,7 @@ pub struct PlayerMoveRequest { } /// Game Player Meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct GamePlayerMeta { pub name: String, pub score: u32, @@ -55,7 +87,7 @@ pub struct GamePlayerMeta { } /// Game meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct GameMeta { pub uuid: String, pub name: String, @@ -69,21 +101,21 @@ pub struct GameMeta { } /// Black card meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct BlackCardMeta { pub text: String, pub pick: u32, } /// Game state meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct GameStateMeta { pub black: Option, // this has to be an option or prost gets cranky pub white: Vec, } /// Game browser meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct GameBrowserMeta { pub uuid: String, pub name: String, @@ -93,7 +125,7 @@ pub struct GameBrowserMeta { } /// Card Pack Meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct CardPackMeta { pub name: String, pub pack: String, @@ -102,20 +134,20 @@ pub struct CardPackMeta { } /// Card Packs Meta -#[derive(Clone, Encode, Decode, Serialize, Deserialize)] +#[derive(Clone, Encode, Decode, Serialize, Deserialize, Debug)] pub struct CardPacksMeta { pub official_meta: Vec, pub unofficial_meta: Vec, } /// Games update -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Encode, Decode, Serialize, Deserialize, Debug)] pub struct GamesUpdate { pub games: Vec, } /// Chat update -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Encode, Decode, Serialize, Deserialize, Debug)] pub struct ChatUpdate { pub room: String, pub users: Vec, @@ -128,25 +160,25 @@ pub struct UserLogInRequest { } /// Response to user name change containing new name -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Debug, Encode, Decode, Serialize, Deserialize)] pub struct UserUpdate { pub username: String, } /// Chat message -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Debug, Encode, Decode, Serialize, Deserialize)] pub struct ChatMessage { pub text: String, } /// Server user count -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Encode, Decode, Serialize, Deserialize, Debug)] pub struct ServerOnlineUsers { pub online_users: u32, } /// Server games count -#[derive(Encode, Decode, Serialize, Deserialize)] +#[derive(Encode, Decode, Serialize, Deserialize, Debug)] pub struct ServerActiveGames { pub active_games: u32, } diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 10ba7cb..ba111f3 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -3,11 +3,11 @@ use crate::game::*; use crate::AppState; use crate::DmUserMethod::*; use crate::GameHandlerMessage::*; +use crate::OutgoingMessageHandlerMessage::*; use crate::SendUserMessage::*; use crate::UserHandlerMessage::*; use axum::extract::ws::Message; use lib::*; -use serde_json::to_string; use std::{collections::HashMap, net::SocketAddr, sync::Arc}; use tokio::sync::mpsc::Sender; @@ -51,7 +51,7 @@ impl GameHandler { match message { NewGame(request, addr) => self.create_new_game(request, addr).await, JoinGame(request, addr) => self.join_game(request, addr).await, - MoveRequest(request, addr) => self.handle_player_move(request, addr), + MoveRequest(request, addr) => self.handle_player_move(request, addr).await, JudgeDecision(request, addr) => self.handle_judging(request, addr).await, DeleteGame(request) => self.delete_game(request).await, SendGameStateUpdate(game_ids) => self.send_game_state_update_all(game_ids), @@ -96,7 +96,7 @@ impl GameHandler { } /// Process player move request - fn handle_player_move(&mut self, request: PlayerMoveRequest, addr: SocketAddr) { + async fn handle_player_move(&mut self, request: PlayerMoveRequest, addr: SocketAddr) { if let Some(this_game) = self.games.get_mut(&request.game_id) { if let Some(player_user) = self.state.online_users.read().unwrap().get(&addr) { let player_user_id = player_user.read().unwrap().uuid.to_string(); @@ -235,14 +235,16 @@ impl GameHandler { white_discard_count: this_game.white_discard.len().try_into().unwrap(), }; - // Send user's update - let msg = Message::Text(to_string(&meta).unwrap()); + let tx = self.state.tx_outgoing_message_handler.clone(); let user_tx = player.user.read().unwrap().tx.clone(); tokio::spawn(async move { // channel can still close after this point but it'll catch most of it // TODO: handle this later? make this situation impossible to begin with? if !user_tx.is_closed() { - if let Err(e) = user_tx.send(msg).await { + if let Err(e) = tx + .send(Unicast((user_tx, ServerToClientMessage::GameMeta(meta)))) + .await + { tracing::error!("Error sending user update: {}", e) } } @@ -276,9 +278,15 @@ impl GameHandler { }; // Send user's update - let msg = Message::Text(to_string(&meta).unwrap()); + let tx = self.state.tx_outgoing_message_handler.clone(); let user_tx = player.user.read().unwrap().tx.clone(); - tokio::spawn(async move { user_tx.send(msg).await }); + tokio::spawn(async move { + tx.send(Unicast(( + user_tx, + ServerToClientMessage::GameStateMeta(meta), + ))) + .await + }); } } else { tracing::error!("Attempted to create game state update for nonexistent game!"); @@ -307,9 +315,15 @@ impl GameHandler { }; // Send user's update - let msg = Message::Text(to_string(&meta).unwrap()); + let tx = self.state.tx_outgoing_message_handler.clone(); let user_tx = player.user.read().unwrap().tx.clone(); - tokio::spawn(async move { user_tx.send(msg).await }); + tokio::spawn(async move { + tx.send(Unicast(( + user_tx, + ServerToClientMessage::GameStateMeta(meta), + ))) + .await + }); } else { tracing::error!("Attempted to create game state update for nonexistent player!"); } @@ -389,10 +403,16 @@ impl GameHandler { }) .collect::>(); - let msg = to_string::(&GamesUpdate { games }).unwrap(); - let tx = self.state.tx_broadcast.clone(); + let tx = self.state.tx_outgoing_message_handler.clone(); + let btx = self.state.tx_broadcast.clone(); tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(msg)) { + if let Err(e) = tx + .send(Broadcast(( + btx, + ServerToClientMessage::GamesUpdate(GamesUpdate { games }), + ))) + .await + { tracing::error!("Error broadcasting games update: {}", e); } }); @@ -400,11 +420,18 @@ impl GameHandler { /// Broadcast updated game count fn broadcast_game_count(&self) { - let tx = self.state.tx_broadcast.clone(); + let tx = self.state.tx_outgoing_message_handler.clone(); + let btx = self.state.tx_broadcast.clone(); let active_games: u32 = self.games.len().try_into().unwrap(); - let msg = to_string(&ServerActiveGames { active_games }).unwrap(); + let msg = ServerActiveGames { active_games }; tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(msg)) { + if let Err(e) = tx + .send(Broadcast(( + btx, + ServerToClientMessage::ServerActiveGames(msg), + ))) + .await + { tracing::error!("Error broadcasting game count: {}", e); } }); @@ -412,8 +439,13 @@ impl GameHandler { /// Send available card packs to a user async fn send_card_packs(&self, tx: Sender) { - let msg = Message::Text(to_string::(&self.packs_meta).unwrap()); - if let Err(e) = tx.send(msg).await { + let msg = ServerToClientMessage::CardPacksMeta(self.packs_meta.clone()); + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast((tx, msg))) + .await + { tracing::error!("Error sending card packs: {}", e) } } diff --git a/server/src/lib.rs b/server/src/lib.rs index b9eb5c0..92d8206 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,13 +1,14 @@ #![feature(if_let_guard)] - use crate::game_handler::*; +use crate::outgoing_message_handler::*; use axum::extract::ws::Message; +use lib::*; +use std::fmt::Debug; use std::{ collections::HashMap, net::SocketAddr, sync::{Arc, RwLock}, }; -use tokio::sync::mpsc::Sender; use tokio::sync::{broadcast, mpsc}; use user_handler::*; use uuid::Uuid; @@ -16,6 +17,7 @@ pub mod game; pub mod game_handler; pub mod incoming_message_handler; pub mod name_generator; +pub mod outgoing_message_handler; pub mod user_handler; pub mod websocket; @@ -24,12 +26,12 @@ pub mod websocket; pub struct User { pub uuid: String, pub name: String, - pub tx: Sender, + pub tx: mpsc::Sender, } impl User { /// Create a new user object from incoming data - pub fn new(name: String, tx: Sender) -> User { + pub fn new(name: String, tx: mpsc::Sender) -> User { User { name, tx, @@ -42,7 +44,7 @@ impl User { } } -// Our shared state +/// Shared state pub struct AppState { pub games_by_user: RwLock>>, pub offline_users: RwLock>>>, @@ -50,5 +52,7 @@ pub struct AppState { pub tx_broadcast: broadcast::Sender, pub tx_game_handler: mpsc::Sender, pub tx_incoming_message_handler: mpsc::Sender<(SocketAddr, Message)>, + pub tx_outgoing_message_handler: + mpsc::Sender>, pub tx_user_handler: mpsc::Sender, } diff --git a/server/src/main.rs b/server/src/main.rs index 52435d6..8775f20 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,5 +1,6 @@ use crate::game_handler::*; use crate::incoming_message_handler::*; +use crate::outgoing_message_handler::*; use crate::websocket::*; use anyhow::{Context, Result}; use axum::{routing::get, Router}; @@ -71,6 +72,7 @@ async fn main() -> Result<()> { let (tx_broadcast, _rx_broadcast) = broadcast::channel(32); let (tx_game_handler, mut rx_game_handler) = mpsc::channel(32); let (tx_incoming_message_handler, mut rx_incoming_message_handler) = mpsc::channel(32); + let (tx_outgoing_message_handler, mut rx_outgoing_message_handler) = mpsc::channel(32); let (tx_user_handler, mut rx_user_handler) = mpsc::channel(32); let games_by_user = RwLock::new(HashMap::>::new()); let offline_users = RwLock::new(HashMap::>>::new()); @@ -83,22 +85,31 @@ async fn main() -> Result<()> { tx_broadcast, tx_game_handler, tx_incoming_message_handler, + tx_outgoing_message_handler, tx_user_handler, }); - // Spawn task to handle incoming messages, also handles outging messages - let message_handler = IncomingMessageHandler::new(app_state.clone()); + // Spawn task to handle incoming messages + let incoming_message_handler = IncomingMessageHandler::new(app_state.clone()); tokio::task::Builder::new() - .name("Message Handler") + .name("Incoming Message Handler") .spawn(async move { while let Some((addr, message)) = rx_incoming_message_handler.recv().await { - message_handler.handle(addr, message).await + incoming_message_handler.handle(addr, message).await } }) .unwrap(); - // TODO: Restart handler threads if they crash - // TODO: Make an outgoing message handler? + // Spawn task to handle outgoing messages + let outgoing_message_handler = OutgoingMessageHandler::new(); + tokio::task::Builder::new() + .name("Outgoing Message Handler") + .spawn(async move { + while let Some(message) = rx_outgoing_message_handler.recv().await { + outgoing_message_handler.handle(message).await + } + }) + .unwrap(); // Spawn task to handle User things let mut user_handler = UserHandler::new(app_state.clone()); diff --git a/server/src/outgoing_message_handler.rs b/server/src/outgoing_message_handler.rs new file mode 100644 index 0000000..f773787 --- /dev/null +++ b/server/src/outgoing_message_handler.rs @@ -0,0 +1,46 @@ +use crate::OutgoingMessageHandlerMessage::*; +use axum::extract::ws::Message; +use lib::*; +use serde::Serialize; +use serde_json::to_string; + +/// For interacting with the outgoing message handler +pub enum OutgoingMessageHandlerMessage { + Unicast((tokio::sync::mpsc::Sender, T)), + Broadcast((tokio::sync::broadcast::Sender, T)), +} + +pub struct OutgoingMessageHandler {} + +impl OutgoingMessageHandler { + pub fn new() -> Self { + OutgoingMessageHandler {} + } + + pub async fn handle(&self, message: OutgoingMessageHandlerMessage) { + match message { + Unicast(message) => { + if let Err(e) = message.0.send(self.serialize(message.1)).await { + tracing::error!("Error sending message: {}", e) + } + } + Broadcast(message) => { + if let Err(e) = message.0.send(self.serialize(message.1)) { + tracing::error!("Error sending message: {}", e) + } + } + } + } + + fn serialize(&self, message: T) -> Message { + let msg = Message::Text(to_string::(&message).unwrap()); + return msg; + } + pub fn to_user(_channel: tokio::sync::mpsc::Sender, _message: T) { + todo!() + } + + pub fn to_all(_channel: tokio::sync::broadcast::Sender, _message: T) { + todo!() + } +} diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 91b498d..7ae24ee 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -2,12 +2,12 @@ use crate::name_generator::*; use crate::AppState; use crate::DmUserMethod::*; use crate::GameHandlerMessage; +use crate::OutgoingMessageHandlerMessage::*; use crate::SendUserMessage::*; use crate::User; use crate::UserHandlerMessage::*; use axum::extract::ws::Message; use lib::*; -use serde_json::to_string; use std::{ collections::{HashMap, HashSet}, net::SocketAddr, @@ -61,13 +61,10 @@ impl UserHandler { /// Handles incoming messages pub async fn handle(&mut self, message: UserHandlerMessage) { match message { - NewUser(dm_tx, addr) => { - // TODO: make this not async - self.set_up_new_user(dm_tx, addr).await - } + NewUser(dm_tx, addr) => self.set_up_new_user(dm_tx, addr).await, UserLogIn(request, addr) => self.login(request, addr).await, DmUser(message, method) => self.dm_user(message, method).await, - Cleanup(addr) => self.user_cleanup(addr), + Cleanup(addr) => self.user_cleanup(addr).await, } } @@ -98,31 +95,36 @@ impl UserHandler { } // Serialize and send message - // TODO: Send failures are suppressed match message { SendUserUpdate(message) => { - let msg = to_string::(&message).unwrap(); - tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(msg)).await { - tracing::error!("Error sending user update: {}", e) - } - }); + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast((tx, ServerToClientMessage::UserUpdate(message)))) + .await + { + tracing::error!("Error sending user update: {}", e) + } } SendChatMessage(message) => { - let msg = to_string::(&message).unwrap(); - tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(msg)).await { - tracing::error!("Error sending chat message: {}", e) - } - }); + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast((tx, ServerToClientMessage::ChatMessage(message)))) + .await + { + tracing::error!("Error sending chat message: {}", e) + } } SendJudgeRound(message) => { - let msg = to_string::(&message).unwrap(); - tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(msg)).await { - tracing::error!("Error sending judge round: {}", e) - } - }); + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast((tx, ServerToClientMessage::JudgeRound(message)))) + .await + { + tracing::error!("Error sending judge round: {}", e) + } } } } @@ -141,51 +143,72 @@ impl UserHandler { self.set_user_online(addr, new_user.clone()); // Hydrate client - if !dm_tx.is_closed() { - // Notify client of username - if let Err(e) = dm_tx.send(user_client_self_update(&new_user)).await { - tracing::error!("Error sending client update: {}", e) - } + // Notify client of username + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast(( + dm_tx.clone(), + ServerToClientMessage::UserUpdate(user_client_self_update(&new_user)), + ))) + .await + { + tracing::error!("Error sending client update: {}", e) + } - // Send game update - // TODO: send single update instad of broadcasting to all users - if let Err(e) = self - .state - .tx_game_handler - .send(GameHandlerMessage::BroadcastGamesUpdate()) - .await - { - tracing::error!("Error contacing game handler {}", e) - } + // Send game update + // TODO: send single update instad of broadcasting to all users + if let Err(e) = self + .state + .tx_game_handler + .send(GameHandlerMessage::BroadcastGamesUpdate()) + .await + { + tracing::error!("Error contacing game handler {}", e) + } - // Send card packs for new game form - if let Err(e) = self - .state - .tx_game_handler - .send(GameHandlerMessage::SendCardPacks(dm_tx.clone())) - .await - { - tracing::error!("Error contacing game handler {}", e) - } - } else { - self.user_cleanup(addr); + // Send card packs for new game form + if let Err(e) = self + .state + .tx_game_handler + .send(GameHandlerMessage::SendCardPacks(dm_tx.clone())) + .await + { + tracing::error!("Error contacing game handler {}", e) } // Broadcast new user's existence // TODO: this should probably be combined and sent as one - let tx = self.state.tx_broadcast.clone(); - let msg = meta_announce_user_join(&self.state, &addr); - tokio::spawn(async move { tx.send(msg) }); + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Broadcast(( + self.state.tx_broadcast.clone(), + ServerToClientMessage::ChatMessage(meta_announce_user_join(&self.state, &addr)), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } self.broadcast_user_count(); - let tx = self.state.tx_broadcast.clone(); - let msg = meta_chat_update(&self.state); - tokio::spawn(async move { tx.send(msg) }); + self.broadcast_chat_update().await; + // Send motd // TODO: this races the broadcasts but if it's done last it'll probably show up last... - let tx = dm_tx.clone(); - tokio::spawn(async move { tx.send(meta_motd()).await }); + + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Unicast(( + dm_tx.clone(), + ServerToClientMessage::ChatMessage(meta_motd()), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } } /// Handle user login @@ -243,9 +266,18 @@ impl UserHandler { old_name, new_name }; - let tx = broadcast_tx.clone(); - let msg = to_string(&ChatMessage { text: msg }).unwrap(); - tokio::spawn(async move { tx.send(Message::Text(msg)) }); + + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Broadcast(( + self.state.tx_broadcast.clone(), + ServerToClientMessage::ChatMessage(ChatMessage { text: msg }), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } } // Check if name is taken by an online user else if self.reserved_names.contains(&new_name) { @@ -281,9 +313,18 @@ impl UserHandler { old_name, new_name }; - let chat_message = to_string::(&ChatMessage { text: msg }).unwrap(); - let tx = broadcast_tx.clone(); - tokio::spawn(async move { tx.send(Message::Text(chat_message)) }); + + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Broadcast(( + broadcast_tx.clone(), + ServerToClientMessage::ChatMessage(ChatMessage { text: msg }), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } } // Send the user their new name @@ -325,14 +366,14 @@ impl UserHandler { let tx = self.state.tx_game_handler.clone(); let msg = GameHandlerMessage::BroadcastGamesUpdate(); tokio::spawn(async move { tx.send(msg).await }); - let tx = broadcast_tx.clone(); - let msg = meta_chat_update(&self.state); - tokio::spawn(async move { tx.send(msg) }); + + self.broadcast_chat_update().await; } /// Broadcast updated user count fn broadcast_user_count(&self) { - let tx = self.state.tx_broadcast.clone(); + let tx = self.state.tx_outgoing_message_handler.clone(); + let btx = self.state.tx_broadcast.clone(); let online_users: u32 = self .state .online_users @@ -341,12 +382,12 @@ impl UserHandler { .len() .try_into() .unwrap(); - let msg = to_string(&ServerOnlineUsers { online_users }).unwrap(); - tokio::spawn(async move { tx.send(Message::Text(msg)) }); + let msg = ServerToClientMessage::ServerOnlineUsers(ServerOnlineUsers { online_users }); + tokio::spawn(async move { tx.send(Broadcast((btx, msg))).await }); } /// Clean up after a user when they disconnect - fn user_cleanup(&self, addr: SocketAddr) { + async fn user_cleanup(&self, addr: SocketAddr) { let user_name = self .state .online_users @@ -363,26 +404,25 @@ impl UserHandler { let msg = ChatMessage { text: format!("{0} left.", &user_name), }; - let chat_message = to_string::(&msg).unwrap(); - let tx = self.state.tx_broadcast.clone(); - tokio::spawn(async move { - if let Err(e) = tx.send(Message::Text(chat_message)) { - tracing::error!("Error broadcasting user leave message: {}", e) - } - }); + + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Broadcast(( + self.state.tx_broadcast.clone(), + ServerToClientMessage::ChatMessage(msg), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } // Move user to offline self.set_user_offline(user_name, &addr); self.broadcast_user_count(); - let msg = meta_chat_update(&self.state); - let tx = self.state.tx_broadcast.clone(); - tokio::spawn(async move { - if let Err(e) = tx.send(msg) { - tracing::error!("Error broadcasting chat update: {}", e) - } - }); + self.broadcast_chat_update().await; } /// Set user status to online @@ -402,20 +442,32 @@ impl UserHandler { .unwrap(), ); } + + /// Update chat stuff like users list + async fn broadcast_chat_update(&self) { + if let Err(e) = self + .state + .tx_outgoing_message_handler + .send(Broadcast(( + self.state.tx_broadcast.clone(), + ServerToClientMessage::ChatUpdate(generate_chat_update(&self.state)), + ))) + .await + { + tracing::error!("Error contacting outgoing message handler: {}", e); + } + } } /// Generate message to notify client of user changes -pub fn user_client_self_update(new_user: &Arc>) -> Message { - Message::Text( - to_string::(&UserUpdate { - username: new_user.read().unwrap().name.clone(), - }) - .unwrap(), - ) +pub fn user_client_self_update(new_user: &Arc>) -> UserUpdate { + UserUpdate { + username: new_user.read().unwrap().name.clone(), + } } /// Generate chatroom metadata update -pub fn meta_chat_update(state: &Arc) -> Message { +pub fn generate_chat_update(state: &Arc) -> ChatUpdate { // TODO: this may get expensive if there are many users let mut names = vec![]; @@ -423,34 +475,28 @@ pub fn meta_chat_update(state: &Arc) -> Message { names.push(user.1.read().unwrap().name.clone()); } - Message::Text( - to_string::(&ChatUpdate { - room: "Lobby".to_string(), - users: names, - }) - .unwrap(), - ) + ChatUpdate { + room: "Lobby".to_string(), + users: names, + } } /// Generate chatroom join announcement -pub fn meta_announce_user_join(state: &Arc, addr: &SocketAddr) -> Message { +pub fn meta_announce_user_join(state: &Arc, addr: &SocketAddr) -> ChatMessage { let msg = format!("{} joined.", { if let Some(user) = state.online_users.read().unwrap().get(addr) { user.read().unwrap().name.clone() } else { - return Message::Text("Error".to_string()); + "Error!".to_string() } }); - Message::Text(to_string::(&ChatMessage { text: msg }).unwrap()) + ChatMessage { text: msg } } /// Generate message-of-the-day server greeting -pub fn meta_motd() -> Message { - Message::Text( - to_string::(&ChatMessage { - text: "Greetings from the game server!".to_string(), - }) - .unwrap(), - ) +pub fn meta_motd() -> ChatMessage { + ChatMessage { + text: "Greetings from the game server!".to_string(), + } }