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]]
name = "cc"
version = "1.0.95"
version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b"
checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd"
[[package]]
name = "cfg-if"

View file

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

View file

@ -1,4 +1,5 @@
use crate::AppState;
use crate::CAHd_game::*;
use axum::{
extract::{
ws::{Message, WebSocket, WebSocketUpgrade},
@ -16,9 +17,6 @@ pub async fn websocket_handler(
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>) {
// By splitting, we can send and receive at the same time.
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();
// Loop until a text message is found.
while let Some(Ok(message)) = receiver.next().await {
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;
match message {
Message::Text(text) => {
tracing::debug!("Text: {}", text);
// let message_str: &str = message.to_text().unwrap();
tracing::debug!(
"{:#?}",
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 {
tracing::debug!("close sent without close frame")
}
}
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

View file

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

View file

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