some game stuff

This commit is contained in:
Adam 2024-08-09 01:21:04 -04:00
parent 426cbaf6b2
commit 22b2b58079
8 changed files with 112 additions and 27 deletions

View file

@ -17,14 +17,18 @@ pub fn Browser() -> impl IntoView {
let new_game_name_ref = create_node_ref::<Input>();
let (selected_packs, set_selected_packs) = create_signal::<BTreeSet<u8>>(BTreeSet::new());
// create_effect(move |_| {
// logging::log!("{:#?}", selected_packs().iter().collect::<Vec<_>>());
// });
create_effect(move |_| {
logging::log!("{:#?}", selected_packs().iter().collect::<Vec<_>>());
});
// Game stuff
let new_game = move |_| {
if let Some(input) = new_game_name_ref.get() {
if input.value() != String::from("") {
if input.value() == String::from("") {
logging::log!("New game name is empty!");
} else if selected_packs().is_empty() {
logging::log!("New game selected packs is empty!");
} else {
(websocket.send)(
&to_string(&NewGameRequest {
name: input.value(),

View file

@ -0,0 +1,16 @@
use leptos::*;
#[component]
pub fn Game() -> impl IntoView {
view! {
<div class="p-1">
<h2 class="text-2xl">Game</h2>
<p>Name:</p>
<p>Host:</p>
<p>Players:</p>
<p>Czar:</p>
<p>Black Card:</p>
<p>Your Cards:</p>
</div>
}
}

View file

@ -2,4 +2,5 @@ pub mod auth;
pub mod browser;
pub mod chat;
pub mod debug;
pub mod game;
pub mod websocket;

View file

@ -2,6 +2,7 @@ use crate::components::auth::*;
use crate::components::browser::*;
use crate::components::chat::*;
use crate::components::debug::*;
use crate::components::game::*;
use crate::components::websocket::*;
use leptos::*;
@ -29,7 +30,7 @@ pub fn Home() -> impl IntoView {
}>
<div class="container m-auto">
<h1 class="inter-med text-6xl text-neutral-200">"Cards For Humanity"</h1>
<h1 class="text-6xl inter-med text-neutral-200">"Cards For Humanity"</h1>
<div class="bg-neutral-950">
<Websocket/>
<hr/>
@ -37,6 +38,8 @@ pub fn Home() -> impl IntoView {
<hr/>
<Browser/>
<hr/>
<Game/>
<hr/>
<Chat/>
<hr/>
<Debug/>

View file

@ -3,11 +3,22 @@ use crate::AppState;
use crate::Game;
use crate::NewGameManifest;
use crate::NewGameRequest;
use crate::User;
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
/// Handle incoming messages over the WebSocket
pub enum GameHandlerMessage {
NewGame {
addr: SocketAddr,
new_game: NewGameRequest,
},
JoinGame {
user: Arc<User>,
game_name: String,
},
}
pub struct GameHandler {
state: Arc<AppState>,
}
@ -17,27 +28,43 @@ impl GameHandler {
GameHandler { state }
}
pub async fn handle(&self, message: (SocketAddr, NewGameRequest)) {
pub async fn handle(&self, message: GameHandlerMessage) {
match message {
GameHandlerMessage::NewGame { addr, new_game } => self.new_game(addr, new_game).await,
_ => {
tracing::debug!("Unhandled at game handler");
}
}
}
async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
if new_game.packs.is_empty() {
tracing::debug!("Cards are empty");
return;
} else if new_game.name.is_empty() {
tracing::debug!("Name are empty");
return;
}
let manifest = NewGameManifest {
name: message.1.name,
name: new_game.name,
host: self
.state
.online_users
.read()
.unwrap()
.get(&message.0)
.get(&addr)
.unwrap()
.clone(),
};
tracing::debug!("Game Packs {:?}", message.1.packs);
tracing::debug!("Game Packs {:?}", new_game.packs);
// create game
if let Ok(new_game_object) = Game::new(manifest) {
self.state
.games
.write()
.unwrap()
.insert(new_game_object.name.clone(), RwLock::new(new_game_object));
self.state.games.write().unwrap().insert(
new_game_object.name.clone(),
Arc::new(RwLock::new(new_game_object)),
);
self.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))

View file

@ -2,6 +2,7 @@
use anyhow::{Context, Result};
use axum::extract::ws::Message;
use game_handler::GameHandlerMessage;
use lib::*;
use rand::prelude::IteratorRandom;
use rand::thread_rng;
@ -364,7 +365,7 @@ pub struct AppState {
pub broadcast_tx: broadcast::Sender<String>,
pub users_tx: mpsc::Sender<UserHandlerMessage>,
pub messages_tx: mpsc::Sender<(SocketAddr, Message)>,
pub games_tx: mpsc::Sender<(SocketAddr, NewGameRequest)>,
pub games_tx: mpsc::Sender<GameHandlerMessage>,
pub first_names: Vec<String>,
pub last_names: Vec<String>,
pub reserved_names: RwLock<HashSet<String>>,
@ -372,5 +373,5 @@ pub struct AppState {
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
pub packs: CardPacks,
pub packs_meta: CardPacksMeta,
pub games: RwLock<HashMap<String, RwLock<Game>>>,
pub games: RwLock<HashMap<String, Arc<RwLock<Game>>>>,
}

View file

@ -1,5 +1,6 @@
use crate::user_handler::*;
use crate::AppState;
use crate::GameHandlerMessage;
use axum::extract::ws::{CloseFrame, Message};
use lib::*;
use serde_json::{from_str, to_string};
@ -41,7 +42,11 @@ impl MessageHandler {
tracing::debug!("passed login to user handler");
}
_new_game if let Ok(new_game) = from_str::<NewGameRequest>(&text) => {
self.state.games_tx.send((addr, new_game)).await.unwrap();
self.state
.games_tx
.send(GameHandlerMessage::NewGame { addr, new_game })
.await
.unwrap();
}
_ => tracing::debug!("Unhandled text from {}", addr),

View file

@ -5,6 +5,8 @@ use serde_json::to_string;
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
// This file is a mess, don't read it
pub enum UserHandlerMessage {
NewUser { user: User, addr: SocketAddr },
UserLogIn { username: String, addr: SocketAddr },
@ -50,6 +52,7 @@ impl UserHandler {
tx.send(meta_games_browser_update(&self.state))
.await
.unwrap();
tx.send(meta_new_game_card_packs(&self.state))
.await
.unwrap();
@ -134,6 +137,14 @@ impl UserHandler {
old_name,
new_name
};
dm.send(
serde_json::to_string(&ChatMessage {
text: "Welcome back!".to_string(),
})
.unwrap(),
)
.await
.unwrap();
tracing::debug!("{msg}");
} else if self
@ -144,6 +155,23 @@ impl UserHandler {
.contains(&new_name)
{
tracing::debug!("name is taken");
dm.send(
serde_json::to_string(&ChatMessage {
text: "Name is taken".to_string(),
})
.unwrap(),
)
.await
.unwrap();
tracing::debug!("{}", old_name.clone());
dm.send(
to_string::<UserUpdate>(&UserUpdate {
username: old_name.clone(),
})
.unwrap(),
)
.await
.unwrap();
} else {
self.state
.online_users
@ -173,6 +201,16 @@ impl UserHandler {
.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap())
.unwrap();
tracing::debug!("Name {} reserved.", &new_name);
// send the user their new name
dm.send(
to_string::<UserUpdate>(&UserUpdate {
username: new_name.clone(),
})
.unwrap(),
)
.await
.unwrap();
}
tracing::debug!(
@ -186,16 +224,6 @@ impl UserHandler {
.unwrap();
broadcast.send(meta_chat_update(&self.state)).unwrap();
// send the user their new name
dm.send(
to_string::<UserUpdate>(&UserUpdate {
username: new_name.clone(),
})
.unwrap(),
)
.await
.unwrap();
tracing::debug!(" HI! login received {} {} {}", addr, new_name, old_name)
}
}