Skip to content

Commit

Permalink
Updates to leptos use 0.11, changes to msgpack encoding (#312)
Browse files Browse the repository at this point in the history
* Binary encodes the ws messages with msgpack.

---------

Co-authored-by: PenguinWithATie <[email protected]>
  • Loading branch information
IongIer and PenguinWithATie authored Jul 31, 2024
1 parent 4557e7a commit db621b4
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 161 deletions.
79 changes: 69 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ opt-level = 'z'

[workspace.dependencies]
leptos = { version = "0.6" , features = ["nightly"] }
leptos-use = { version = "0.10" }
leptos-use = { version = "0.11" }
leptos_meta = { version = "0.6" , features = ["nightly"] }
leptos_router = { version = "0.6" , features = ["nightly"] }
leptos_actix = { version = "0.6" }
Expand All @@ -32,6 +32,7 @@ serde_json = { version = "1" }
cfg-if = "1.0.0"
console_error_panic_hook = "0.1.7"
console_log = "1.0.0"
codee = { version = "0.1.2", features = ["msgpack_serde"] }
regex = "1.10"
http = "1.1.0"
log = "0.4.22"
Expand All @@ -56,7 +57,7 @@ cookie = "0.18"
skillratings = "0.27"
chrono = { version = "0.4", features = ["serde"] }
itertools = "0.13.0"
leptix_primitives = { version = "0.2.0" }
leptix_primitives = { version = "0.2" }
tree-ds = {version = "0.1.5", features = ["serde", "compact_serde"] }
bimap = {version = "0.6.3", features = ["serde"] }
# Defines a size-optimized profile for the WASM bundle in release mode
Expand Down
1 change: 1 addition & 0 deletions apis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ actix = { workspace = true, optional = true }
argon2 = { workspace = true, optional = true }
cfg-if = { workspace = true }
chrono = { workspace = true }
codee ={ workspace = true }
console_error_panic_hook = { workspace = true }
cookie = { workspace = true }
db = { path = "../db", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions apis/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub use move_info::MoveInfo;
pub use piece_type::PieceType;
pub use rating_change_info::RatingChangeInfo;
pub use server_result::{
ChallengeUpdate, ExternalServerError, GameActionResponse, GameUpdate, ServerMessage,
ServerResult, TournamentUpdate, UserStatus, UserUpdate,
ChallengeUpdate, CommonMessage, ExternalServerError, GameActionResponse, GameUpdate,
ServerMessage, ServerResult, TournamentUpdate, UserStatus, UserUpdate,
};
pub use svg_pos::SvgPos;
pub use time_signals::TimeSignals;
Expand Down
6 changes: 6 additions & 0 deletions apis/src/common/server_result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::game_reaction::GameReaction;
use super::ClientRequest;
use crate::responses::{
ChallengeResponse, GameResponse, HeartbeatResponse, TournamentResponse, UserResponse,
};
Expand All @@ -15,6 +16,11 @@ pub enum ServerResult {
Err(ExternalServerError),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CommonMessage {
Server(ServerResult),
Client(ClientRequest),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalServerError {
pub user_id: Uuid,
Expand Down
22 changes: 13 additions & 9 deletions apis/src/jobs/tournament_start.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::common::{
GameActionResponse, GameReaction, GameUpdate, ServerMessage, ServerResult, TournamentUpdate,
CommonMessage, GameActionResponse, GameReaction, GameUpdate, ServerMessage, ServerResult,
TournamentUpdate,
};
use crate::responses::{GameResponse, TournamentResponse};
use crate::websockets::internal_server_message::{InternalServerMessage, MessageDestination};
use crate::websockets::messages::ClientActorMessage;
use crate::websockets::ws_server::WsServer;
use actix::Addr;
use actix_web::web::Data;
use codee::binary::MsgpackSerdeCodec;
use codee::Encoder;
use db_lib::{get_conn, models::Tournament, DbPool};
use diesel_async::scoped_futures::ScopedFutureExt;
use diesel_async::AsyncConnection;
Expand Down Expand Up @@ -81,16 +84,17 @@ pub fn run(pool: DbPool, ws_server: Data<Addr<WsServer>>) {
}
}
for message in messages {
let serialized = serde_json::to_string(&ServerResult::Ok(
let serialized = CommonMessage::Server(ServerResult::Ok(
Box::new(message.message),
))
.expect("Failed to serialize a server message");
let cam = ClientActorMessage {
destination: message.destination,
serialized,
from: None,
));
if let Ok(serialized) = MsgpackSerdeCodec::encode(&serialized) {
let cam = ClientActorMessage {
destination: message.destination,
serialized,
from: None,
};
ws_server.do_send(cam);
};
ws_server.do_send(cam);
}
}
Ok(())
Expand Down
45 changes: 15 additions & 30 deletions apis/src/providers/api_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,36 @@ impl ApiRequests {
game_id: game_id.clone(),
action: GameAction::Turn(turn),
};
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
let mut games = expect_context::<GamesSignal>();
// TODO: fix this so that it just removes from next_games games.remove_from_next_games(&game_id);
games.own_games_remove(&game_id);
}

pub fn pong(&self, nonce: u64) {
let msg = ClientRequest::Pong(nonce);
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn game_control(&self, game_id: GameId, gc: GameControl) {
let msg = ClientRequest::Game {
game_id,
action: GameAction::Control(gc),
};
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn tournament_game_start(&self, game_id: GameId) {
let msg = ClientRequest::Game {
game_id,
action: GameAction::Start,
};
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn tournament_abandon(&self, tournament_id: TournamentId) {
let msg = ClientRequest::Tournament(TournamentAction::Abandon(tournament_id));
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn tournament_adjudicate_game_result(
Expand All @@ -74,38 +69,33 @@ impl ApiRequests {
) {
let msg =
ClientRequest::Tournament(TournamentAction::AdjudicateResult(game_id, new_result));
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn chat(&self, message: &ChatMessageContainer) {
let msg = ClientRequest::Chat(message.to_owned());
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn tournament(&self, action: TournamentAction) {
let msg = ClientRequest::Tournament(action.to_owned());
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn game_check_time(&self, game_id: &GameId) {
let msg = ClientRequest::Game {
game_id: game_id.clone(),
action: GameAction::CheckTime,
};
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn join(&self, game_id: GameId) {
let msg = ClientRequest::Game {
game_id,
action: GameAction::Join,
};
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn challenge(&self, challenge_action: ChallengeAction) {
Expand All @@ -130,34 +120,29 @@ impl ApiRequests {
};
if let Some(challenge_action) = challenge_action {
let msg = ClientRequest::Challenge(challenge_action);
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}
}

pub fn challenge_cancel(&self, challenger_id: ChallengeId) {
let msg = ClientRequest::Challenge(ChallengeAction::Delete(challenger_id));
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn challenge_accept(&self, challenger_id: ChallengeId) {
let msg = ClientRequest::Challenge(ChallengeAction::Accept(challenger_id));
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn challenge_get(&self, challenger_id: ChallengeId) {
let msg = ClientRequest::Challenge(ChallengeAction::Get(challenger_id));
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}

pub fn search_user(&self, pattern: String) {
if !pattern.is_empty() {
let msg = ClientRequest::UserSearch(pattern);
self.websocket
.send(&serde_json::to_string(&msg).expect("Serde_json::to_string failed"));
self.websocket.send(&msg);
}
}
}
Loading

0 comments on commit db621b4

Please sign in to comment.