avoid repeating websocket creation

This commit is contained in:
Thilo Behnke
2022-06-26 15:48:23 +02:00
parent fe64d62c5c
commit 6c36860908
4 changed files with 33 additions and 31 deletions

View File

@@ -86,15 +86,9 @@ async fn handle_session_create(
.body(Body::from(e))
.unwrap());
}
let session = session_create_res.unwrap();
error!("session created by player {:?}: {:?}", player, session);
let reason = format!("player {:?} created session", player);
let session_created = SessionEvent::Created(SessionEventPayload {
session,
actor: player,
reason
});
let serialized = json!(session_created);
let session_event = session_create_res.unwrap();
error!("session created: {:?}", session_event);
let serialized = json!(session_event);
return build_success_res(&serialized.to_string());
}
@@ -116,15 +110,10 @@ async fn handle_session_join(
.body(Body::from(e))
.unwrap());
}
let session = session_join_res.unwrap();
info!("player {:?} successfully joined session: {:?}", player, session);
let session_event = session_join_res.unwrap();
info!("player {:?} successfully joined session: {:?}", player, session_event);
let reason = format!("player {:?} joined session", player);
let session_joined = SessionEvent::Joined(SessionEventPayload {
actor: player,
session,
reason
});
let serialized = json!(session_joined);
let serialized = json!(session_event);
return build_success_res(&serialized.to_string());
}

View File

@@ -10,6 +10,7 @@ use crate::kafka::{
KafkaTopicManager,
};
use crate::actor::Player;
use crate::event::{SessionEvent, SessionEventPayload};
use crate::session::{Session, SessionState};
pub struct SessionManager {
@@ -37,7 +38,7 @@ impl SessionManager {
.map_or_else(|| None, |s| Some(s.clone()))
}
pub async fn create_session(&mut self, player: Player) -> Result<Session, String> {
pub async fn create_session(&mut self, player: Player) -> Result<SessionEvent, String> {
info!("called to create new session by player {:?}", player);
let add_partition_res = self.topic_manager.add_partition().await;
if let Err(e) = add_partition_res {
@@ -62,14 +63,19 @@ impl SessionManager {
);
}
info!("successfully persisted create session event.");
Ok(session)
let session_created = SessionEvent::Created(SessionEventPayload {
session,
actor: player,
reason: format!("session created")
});
Ok(session_created)
}
pub async fn join_session(
&mut self,
session_id: String,
player: Player,
) -> Result<Session, String> {
) -> Result<SessionEvent, String> {
let updated_session = {
let session = self.sessions.iter_mut().find(|s| s.session_id == session_id);
if let None = session {
@@ -107,12 +113,17 @@ impl SessionManager {
}
};
println!("sessions = {:?}", self.sessions);
Ok(updated_session.clone())
let session_joined_event = SessionEvent::Joined(SessionEventPayload {
session: updated_session,
reason: "session joined".to_owned(),
actor: player
});
Ok(session_joined_event)
}
fn write_to_producer<T>(&mut self, session_event: T) -> Result<(), String>
where
T: SessionEvent,
T: SessionAwareEvent,
{
let session_id = session_event.session_id();
let session_producer = match self.session_producers.get_mut(session_id) {
@@ -233,7 +244,7 @@ impl SessionReader {
}
}
pub trait SessionEvent : Serialize {
pub trait SessionAwareEvent: Serialize {
fn session_id(&self) -> &str;
}
@@ -254,7 +265,7 @@ impl SessionCreatedEvent {
}
}
impl SessionEvent for SessionCreatedEvent {
impl SessionAwareEvent for SessionCreatedEvent {
fn session_id(&self) -> &str {
&self.session.session_id
}
@@ -277,7 +288,7 @@ impl SessionJoinedEvent {
}
}
impl SessionEvent for SessionJoinedEvent {
impl SessionAwareEvent for SessionJoinedEvent {
fn session_id(&self) -> &str {
&self.session.session_id
}