summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstale <redkugelblitzin@gmail.com>2022-05-29 23:56:03 -0300
committerstale <redkugelblitzin@gmail.com>2022-05-29 23:56:03 -0300
commitb1fd887402e4577cbb630fc6517cf9cd6e0d7c8f (patch)
tree7018603244d38a361f86aa0b1a6ea11ea39f27c6
parentc896f3a5f9b2e3f7f03ad1871b6566f098c0e295 (diff)
compression! also bumped edition
-rw-r--r--Cargo.lock35
-rw-r--r--Cargo.toml3
-rw-r--r--assets/client.js5
-rw-r--r--assets/room.html1
-rw-r--r--src/main.rs7
5 files changed, 47 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1853a85..d57162e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,12 @@
version = 3
[[package]]
+name = "adler"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
+
+[[package]]
name = "ammonia"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -101,6 +107,15 @@ dependencies = [
]
[[package]]
+name = "crc32fast"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
name = "crypto-common"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -139,6 +154,16 @@ dependencies = [
]
[[package]]
+name = "flate2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6"
+dependencies = [
+ "crc32fast",
+ "miniz_oxide",
+]
+
+[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -501,6 +526,15 @@ dependencies = [
]
[[package]]
+name = "miniz_oxide"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082"
+dependencies = [
+ "adler",
+]
+
+[[package]]
name = "mio"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1375,6 +1409,7 @@ name = "websweeper"
version = "1.0.0"
dependencies = [
"ammonia",
+ "flate2",
"futures-util",
"rand",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 6356860..8997bdd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
name = "websweeper"
version = "1.0.0"
authors = ["stale <stale@masba.net>"]
-edition = "2018"
+edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -10,6 +10,7 @@ edition = "2018"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
+flate2 = "1.0"
warp = { version = "0.3", features = ["tls", "websocket"] }
rand = "0.8"
futures-util = "0.3"
diff --git a/assets/client.js b/assets/client.js
index e50ea9d..7c89495 100644
--- a/assets/client.js
+++ b/assets/client.js
@@ -88,8 +88,9 @@ function connect() {
}
function acceptBoard(data) {
- let vals = new Uint8Array(data);
- board = Array.from(vals).reduce((s,c) => {
+ let dataarr = new Uint8Array(data);
+ let vals = fflate.inflateSync(dataarr);
+ board = vals.reduce((s,c) => {
let v = String.fromCodePoint(c);
if (v == ' ') {
s = s + "&nbsp";
diff --git a/assets/room.html b/assets/room.html
index e3e6ff9..d96bb77 100644
--- a/assets/room.html
+++ b/assets/room.html
@@ -14,5 +14,6 @@
<p id="miscinfo">Loading...</p>
</div>
</body>
+ <script src="https://unpkg.com/fflate"></script>
<script src="/c.js"></script>
</html>
diff --git a/src/main.rs b/src/main.rs
index 3dd7f39..66b2cfd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -176,6 +176,8 @@ async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> {
// If a move is made, broadcast new board, else just send current board
async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, players: PlayerMapData, bconf: minesweeper::BoardConf) {
use minesweeper::*;
+ use flate2::{ Compression, write::DeflateEncoder };
+ use std::io::Write;
let mut game = Game::new(bconf);
let mut latest_player_name = None;
while let Some(req) = move_rx.recv().await {
@@ -192,7 +194,10 @@ async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, p
MetaMove::Reset => { game = Game::new(bconf); },
}
use warp::ws::Message;
- let mut reply = vec![Message::binary(game.board.render())];
+ let mut board_encoder = DeflateEncoder::new(Vec::new(), Compression::default());
+ board_encoder.write_all(&game.board.render()).unwrap();
+ let compressed_board = board_encoder.finish().unwrap();
+ let mut reply = vec![Message::binary(compressed_board)];
let lpname = latest_player_name.as_ref().map(|s| s.as_str()).unwrap_or("unknown player");
match game.phase {
Phase::Win => { reply.push(Message::text(format!("win {lpname}"))); },