This commit is contained in:
Adam 2024-05-01 04:56:58 -04:00
parent 40419d5e9f
commit 4adec7dc2c
5 changed files with 70 additions and 34 deletions

4
Cargo.lock generated
View file

@ -166,9 +166,9 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.95" version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"

View file

@ -24,7 +24,7 @@ pub struct CAHCardBlack {
} }
/// A CAH pack /// A CAH pack
#[derive(Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct CAHCardSet { pub struct CAHCardSet {
/// Name of the pack /// Name of the pack
name: String, name: String,
@ -39,7 +39,7 @@ pub struct CAHCardSet {
} }
/// Player roles /// Player roles
#[derive(Debug)] #[derive(Debug, Deserialize)]
pub enum PlayerRole { pub enum PlayerRole {
/// Player is host /// Player is host
Host, Host,
@ -50,7 +50,7 @@ pub enum PlayerRole {
} }
/// A struct that represents a player /// A struct that represents a player
#[derive(Debug)] #[derive(Debug, Deserialize)]
pub struct CAHPlayer { pub struct CAHPlayer {
/// Player's username /// Player's username
pub name: String, pub name: String,
@ -86,13 +86,14 @@ pub struct CAHGame {
} }
/// New game request structure /// New game request structure
#[derive(Debug, Deserialize)]
pub struct NewGameRequest { pub struct NewGameRequest {
/// Game name /// Game name
pub name: String, pub name: String,
/// Game host /// Game host
pub host: CAHPlayer, pub host: CAHPlayer,
/// Chosen packs /// Chosen packs
pub packs: Vec<CAHCardSet>, pub packs: Vec<u8>,
} }
/// Game join request structure /// Game join request structure
@ -128,7 +129,7 @@ impl CAHGame {
println!("Creating game {}", &request.name); println!("Creating game {}", &request.name);
game.name = request.name; game.name = request.name;
game.build_decks(request.packs)?; // game.build_decks(request.packs)?;
game.create_player(request.host)?; game.create_player(request.host)?;
game.deal_black()?; game.deal_black()?;

View file

@ -1,4 +1,5 @@
use crate::AppState; use crate::AppState;
use crate::CAHd_game::*;
use axum::{ use axum::{
extract::{ extract::{
ws::{Message, WebSocket, WebSocketUpgrade}, ws::{Message, WebSocket, WebSocketUpgrade},
@ -16,9 +17,6 @@ pub async fn websocket_handler(
ws.on_upgrade(|socket| websocket(socket, state)) ws.on_upgrade(|socket| websocket(socket, state))
} }
// This function deals with a single websocket connection, i.e., a single
// connected client / user, for which we will spawn two independent tasks (for
// receiving / sending chat messages).
pub async fn websocket(stream: WebSocket, state: Arc<AppState>) { pub async fn websocket(stream: WebSocket, state: Arc<AppState>) {
// By splitting, we can send and receive at the same time. // By splitting, we can send and receive at the same time.
let (mut sender, mut receiver) = stream.split(); let (mut sender, mut receiver) = stream.split();
@ -27,22 +25,55 @@ pub async fn websocket(stream: WebSocket, state: Arc<AppState>) {
let mut newplayer = String::new(); let mut newplayer = String::new();
// Loop until a text message is found. // Loop until a text message is found.
while let Some(Ok(message)) = receiver.next().await { while let Some(Ok(message)) = receiver.next().await {
if let Message::Text(name) = message { match message {
// If newplayer that is sent by client is not taken, fill newplayer string. Message::Text(text) => {
check_username(&state, &mut newplayer, &name); tracing::debug!("Text: {}", text);
// let message_str: &str = message.to_text().unwrap();
// If not empty we want to quit the loop else we want to quit function. tracing::debug!(
if !newplayer.is_empty() { "{:#?}",
break; serde_json::from_str::<NewGameRequest>(&text)
.unwrap()
.host
.name
);
}
Message::Binary(data) => {
tracing::debug!("Binary: {:?}", data)
}
Message::Close(c) => {
if let Some(cf) = c {
tracing::debug!(
"Close received with code: {} and reason: {}",
cf.code,
cf.reason
)
} else { } else {
// Only send our client that newplayer is taken. tracing::debug!("close sent without close frame")
let _ = sender
.send(Message::Text(String::from("Username already taken.")))
.await;
return;
} }
} }
Message::Pong(ping) => {
tracing::debug!("Pong sent with {:?}", ping);
}
Message::Ping(pong) => {
tracing::debug!("Pong sent with {:?}", pong);
}
}
// if let Message::Text(name) = message {
// // If newplayer that is sent by client is not taken, fill newplayer string.
// check_username(&state, &mut newplayer, &name);
//
// // If not empty we want to quit the loop else we want to quit function.
// if !newplayer.is_empty() {
// break;
// } else {
// // Only send our client that newplayer is taken.
// let _ = sender
// .send(Message::Text(String::from("Username already taken.")))
// .await;
//
// return;
// }
// }
} }
// We subscribe *before* sending the "joined" message, so that we will also // We subscribe *before* sending the "joined" message, so that we will also

View file

@ -54,7 +54,8 @@ fn test() -> Result<(), Box<dyn Error>> {
let test_game0 = NewGameRequest { let test_game0 = NewGameRequest {
name: "Test0".to_string(), name: "Test0".to_string(),
host: test_player0, host: test_player0,
packs: chosen_packs, // packs: chosen_packs,
packs: vec![0],
}; };
games.push(CAHGame::new(test_game0)?); games.push(CAHGame::new(test_game0)?);
@ -117,6 +118,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
&app_state.all_cards.lock().unwrap().len() &app_state.all_cards.lock().unwrap().len()
); );
tracing::debug!("{} active games", &app_state.games.lock().unwrap().len());
let app = Router::new() let app = Router::new()
.route("/", get(index)) .route("/", get(index))
.route("/test", get(spawnclients)) .route("/test", get(spawnclients))

View file

@ -43,14 +43,15 @@
</form> </form>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
websocket = new WebSocket("ws://localhost:3030/websocket"); socket = new WebSocket("ws://localhost:3030/websocket");
socket.binaryType = "ArrayBuffer";
function createGame() { function createGame() {
document.getElementById("create-game").disabled = true; document.getElementById("create-game").disabled = true;
let CAHPlayer = { let CAHPlayer = {
name: username.value, name: username.value,
role: 'h', role: 'Host',
white: [], white: [],
black: [], black: [],
}; };
@ -61,21 +62,21 @@
packs: [0], packs: [0],
}; };
websocket.send(JSON.stringify(NewGameRequest)); socket.send(JSON.stringify(NewGameRequest));
}; };
websocket.onopen = function() { socket.onopen = function() {
console.log("connection opened",websocket.extensions, websocket.protocol, websocket.readyState); console.log("connection opened",socket.extensions, socket.protocol, socket.readyState);
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>'; document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
} }
websocket.onclose = function() { socket.onclose = function() {
console.log("connection closed"); console.log("connection closed");
// document.getElementById("join-chat").disabled = false; // document.getElementById("join-chat").disabled = false;
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>'; document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
} }
websocket.onmessage = function(e) { socket.onmessage = function(e) {
const history = document.getElementById("chat-history") const history = document.getElementById("chat-history")
console.log("received message: "+e.data); console.log("received message: "+e.data);
history.value += e.data+"\r"; history.value += e.data+"\r";
@ -84,11 +85,11 @@
function chatSubmit() { function chatSubmit() {
let input = document.getElementById("chat-input"); let input = document.getElementById("chat-input");
websocket.send(input.value); socket.send(input.value);
input.value = ""; input.value = "";
}; };
websocket.addEventListener("error", (event) => { socket.addEventListener("error", (event) => {
console.log("WebSocket error: ", event); console.log("WebSocket error: ", event);
}); });
</script> </script>