Skip to content

Commit

Permalink
[server] Rename ChatServer -> GameServer
Browse files Browse the repository at this point in the history
  • Loading branch information
lunacys committed Apr 22, 2024
1 parent 641a7ea commit 3b00470
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod lobbies;
mod ws;

use crate::config::Config;
use crate::ws::server::{ChatServer, ChatServerHandle};
use crate::ws::server::{GameServer, GameServerHandle};
use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use auth::mock::ensure_auth;
use serde::Serialize;
Expand Down Expand Up @@ -40,7 +40,7 @@ async fn not_found() -> impl Responder {
async fn chat_ws(
req: HttpRequest,
stream: web::Payload,
chat_server: web::Data<ChatServerHandle>,
chat_server: web::Data<GameServerHandle>,
) -> Result<HttpResponse, actix_web::error::Error> {
let (res, session, msg_stream) = actix_ws::handle(&req, stream)?;
let ui_res = ensure_auth(req);
Expand All @@ -51,7 +51,7 @@ async fn chat_ws(
}
let (user_info, _token) = ui_res.unwrap();

spawn_local(ws::handler::chat_ws(
spawn_local(ws::handler::game_ws(
(**chat_server).clone(),
session,
msg_stream,
Expand All @@ -70,7 +70,7 @@ async fn main() -> std::io::Result<()> {

log::info!("Using config {:?}", config);

let (chat_server, server_tx) = ChatServer::new();
let (chat_server, server_tx) = GameServer::new();
let chat_server = spawn(chat_server.run());

log::info!("constructing http server");
Expand Down
30 changes: 15 additions & 15 deletions src/server/src/ws/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::{Duration, Instant};

use crate::auth::UserInfo;
use crate::ConnId;
use crate::{ws::server::ChatServerHandle, Msg};
use crate::{ws::server::GameServerHandle, Msg};
use actix_ws::{CloseReason, Message};
use futures_util::StreamExt as _;
use quader_engine::board_command::BoardMoveDir;
Expand All @@ -18,8 +18,8 @@ use tokio::{pin, select, sync::mpsc, time::interval};
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);

pub async fn chat_ws(
chat_server: ChatServerHandle,
pub async fn game_ws(
game_server: GameServerHandle,
mut session: actix_ws::Session,
mut msg_stream: actix_ws::MessageStream,
user_info: UserInfo,
Expand All @@ -32,7 +32,7 @@ pub async fn chat_ws(
let (conn_tx, mut conn_rx) = mpsc::unbounded_channel();

log::debug!("server connect");
let conn_id = chat_server.connect(conn_tx, user_info.clone()).await;
let conn_id = game_server.connect(conn_tx, user_info.clone()).await;
log::debug!("server connected");

let close_reason: Option<CloseReason> = loop {
Expand All @@ -59,7 +59,7 @@ pub async fn chat_ws(
last_heartbeat = Instant::now();
}
Message::Text(text) => {
if let Err(err) = process_text_msg(&chat_server, &mut session, &text, conn_id, &user_info).await {
if let Err(err) = process_text_msg(&game_server, &mut session, &text, conn_id, &user_info).await {
log::error!("Error while processing message: {:?}", err);
let _ = session.text(format!("error while processing message: {:?}", err)).await;
}
Expand All @@ -85,11 +85,11 @@ pub async fn chat_ws(
}
}
msg = msg_rx => {
// chat messages received from other room participants
// messages received from other room participants
if let Some(msg) = &msg {
session.text(msg).await.unwrap();
} else {
unreachable!("all connection message senders were dropped; chat server may have panicked")
unreachable!("all connection message senders were dropped; game server may have panicked")
}
}
// heartbeat internal tick
Expand All @@ -108,7 +108,7 @@ pub async fn chat_ws(
}
};

chat_server.disconnect(conn_id);
game_server.disconnect(conn_id);
log::info!("closed, reason: {:?}", close_reason);

let _ = session.close(close_reason).await;
Expand Down Expand Up @@ -136,11 +136,11 @@ pub enum WsAction {
}

async fn process_text_msg(
chat_server: &ChatServerHandle,
game_server: &GameServerHandle,
session: &mut actix_ws::Session,
text: &str,
conn: ConnId,
user_info: &UserInfo
user_info: &UserInfo,
) -> Result<(), serde_json::Error> {
let msg = text.trim();

Expand All @@ -150,21 +150,21 @@ async fn process_text_msg(
WsAction::Chat(msg) => {
let msg = format!("{}: {msg}", user_info.username);

chat_server.send_message(conn, msg).await;
game_server.send_message(conn, msg).await;
}
WsAction::BoardCommand(cmd) => {
log::info!("conn {conn}: got a board cmd: {:?}", cmd);
let msg = chat_server.on_board_cmd(conn, cmd).await;
let msg = game_server.on_board_cmd(conn, cmd).await;
session.text(msg).await.unwrap();
}
WsAction::StartMatch => {
log::info!("conn {conn}: starting match");
chat_server.start_match(conn).await;
game_server.start_match(conn).await;
}
WsAction::ListLobbies => {
log::info!("conn {conn}: listing lobbies");

let lobbies = chat_server.list_lobbies().await;
let lobbies = game_server.list_lobbies().await;
let lobbies = serde_json::to_string(&lobbies).unwrap();
session.text(lobbies).await.unwrap();
/* for lobby in lobbies {
Expand All @@ -174,7 +174,7 @@ async fn process_text_msg(
WsAction::JoinLobby(lobby) => {
log::info!("conn {conn}: joining lobby {lobby}");

chat_server.join_lobby(conn, &lobby).await;
game_server.join_lobby(conn, &lobby).await;

session.text(format!("joined lobby {lobby}")).await.unwrap();
}
Expand Down
14 changes: 7 additions & 7 deletions src/server/src/ws/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* See the LICENSE file in the repository root for full license text.
*/

use crate::lobbies::LobbyNotification;
use crate::{
auth::UserInfo,
lobbies::{Lobby, LobbyContainer, LobbyListing, LobbySettings},
Expand All @@ -18,7 +19,6 @@ use std::{
},
};
use tokio::sync::{mpsc, oneshot};
use crate::lobbies::LobbyNotification;

use super::handler::WsBoardCommand;

Expand Down Expand Up @@ -90,15 +90,15 @@ enum Command {
/// │Board_1│ │Board_2│ │Board_N│
/// └───────┘ └───────┘ └───────┘
/// ```
pub struct ChatServer {
pub struct GameServer {
sessions: HashMap<ConnId, Session>,
lobby_container: LobbyContainer,
visitor_count: Arc<AtomicUsize>,
cmd_rx: mpsc::UnboundedReceiver<Command>,
}

impl ChatServer {
pub fn new() -> (Self, ChatServerHandle) {
impl GameServer {
pub fn new() -> (Self, GameServerHandle) {
let mut lobby_container = LobbyContainer::new();
seed_lobbies(&mut lobby_container);

Expand All @@ -115,7 +115,7 @@ impl ChatServer {
visitor_count: Arc::new(AtomicUsize::new(0)),
cmd_rx,
},
ChatServerHandle { cmd_tx },
GameServerHandle { cmd_tx },
);

log::debug!("res");
Expand Down Expand Up @@ -318,11 +318,11 @@ impl ChatServer {
}

#[derive(Debug, Clone)]
pub struct ChatServerHandle {
pub struct GameServerHandle {
cmd_tx: mpsc::UnboundedSender<Command>,
}

impl ChatServerHandle {
impl GameServerHandle {
pub async fn connect(
&self,
conn_tx: mpsc::UnboundedSender<String>,
Expand Down

0 comments on commit 3b00470

Please sign in to comment.