expand game browser

This commit is contained in:
Adam 2024-08-02 02:35:31 -04:00
parent 96bef78efb
commit 98dd4e85c2
6 changed files with 195 additions and 158 deletions

View file

@ -1,4 +1,5 @@
use crate::components::websocket::WebSocketContext; use crate::components::websocket::WebSocketContext;
use leptos::html::Input;
use leptos::*; use leptos::*;
use leptos_use::core::ConnectionReadyState; use leptos_use::core::ConnectionReadyState;
use lib::models::*; use lib::models::*;
@ -12,13 +13,17 @@ pub fn Browser() -> impl IntoView {
let game_update_context = expect_context::<ReadSignal<Option<GamesUpdate>>>(); let game_update_context = expect_context::<ReadSignal<Option<GamesUpdate>>>();
let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]); let (active_games, set_active_games) = create_signal::<Vec<String>>(vec![]);
let fake_new_game_request = NewGameRequest { let new_game_input_ref = create_node_ref::<Input>();
name: String::from("Ligma"),
};
// Game stuff // Game stuff
let new_game_test = move |_| { let new_game = move |_| {
(websocket.send)(&to_string(&fake_new_game_request).unwrap()); (websocket.send)(
&to_string(&NewGameRequest {
name: new_game_input_ref.get().unwrap().value(),
})
.unwrap(),
);
new_game_input_ref.get().unwrap().set_value("");
}; };
create_effect(move |_| { create_effect(move |_| {
@ -42,9 +47,20 @@ pub fn Browser() -> impl IntoView {
<h2 class="text-2xl">Game Browser</h2> <h2 class="text-2xl">Game Browser</h2>
{move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()} {move || active_games().into_iter().map(|n| view! { <li>{n}</li> }).collect_view()}
</ul> </ul>
<button on:click=new_game_test disabled=move || !connected()> <form onsubmit="return false" on:submit=new_game>
"Test New Game" <input
</button> class="w-80 h-11 font-mono rounded-sm bg-slate-900 text-slate-200"
placeholder="Name"
disabled=move || !connected()
node_ref=new_game_input_ref
/>
<input
class="py-2 px-4 pl-4 font-bold text-white rounded border-b-4 bg-slate-600 border-slate-800 hover:bg-slate-700 hover:border-slate-500"
type="submit"
disabled=move || !connected()
value="New Game"
/>
</form>
</div> </div>
} }
} }

View file

@ -69,13 +69,13 @@ pub fn Websocket() -> impl IntoView {
// Contexts for message handler // Contexts for message handler
let (state_summary, set_state_summary) = let (state_summary, set_state_summary) =
create_signal::<Option<ServerStateSummary>>(Option::None); create_signal::<Option<ServerStateSummary>>(Option::None);
let (game_object, set_game_object) = create_signal::<Option<Game>>(Option::None); // let (game_object, set_game_object) = create_signal::<Option<Game>>(Option::None);
let (user_update, set_user_update) = create_signal::<Option<UserUpdate>>(Option::None); let (user_update, set_user_update) = create_signal::<Option<UserUpdate>>(Option::None);
let (chat_update, set_chat_update) = create_signal::<Option<ChatUpdate>>(Option::None); let (chat_update, set_chat_update) = create_signal::<Option<ChatUpdate>>(Option::None);
let (chat_message, set_chat_message) = create_signal::<Option<ChatMessage>>(Option::None); let (chat_message, set_chat_message) = create_signal::<Option<ChatMessage>>(Option::None);
let (active_games, set_active_games) = create_signal::<Option<GamesUpdate>>(Option::None); let (active_games, set_active_games) = create_signal::<Option<GamesUpdate>>(Option::None);
provide_context::<ReadSignal<Option<Game>>>(game_object); // provide_context::<ReadSignal<Option<Game>>>(game_object);
provide_context::<ReadSignal<Option<UserUpdate>>>(user_update); provide_context::<ReadSignal<Option<UserUpdate>>>(user_update);
provide_context::<ReadSignal<Option<ChatUpdate>>>(chat_update); provide_context::<ReadSignal<Option<ChatUpdate>>>(chat_update);
provide_context::<ReadSignal<Option<ChatMessage>>>(chat_message); provide_context::<ReadSignal<Option<ChatMessage>>>(chat_message);
@ -88,9 +88,10 @@ pub fn Websocket() -> impl IntoView {
// Send all messages as strings into chat box // Send all messages as strings into chat box
if let Some(message) = message_raw { if let Some(message) = message_raw {
if let Ok(game) = from_str::<Game>(message) { // if let Ok(game) = from_str::<Game>(message) {
set_game_object(Some(game)); // set_game_object(Some(game));
} else if let Ok(state_summary) = from_str::<ServerStateSummary>(message) { // }
if let Ok(state_summary) = from_str::<ServerStateSummary>(message) {
set_state_summary(Some(state_summary)); set_state_summary(Some(state_summary));
} else if let Ok(chat_message) = from_str::<ChatMessage>(message) { } else if let Ok(chat_message) = from_str::<ChatMessage>(message) {
set_chat_message(Some(chat_message)); set_chat_message(Some(chat_message));

View file

@ -1,115 +1,129 @@
// use anyhow::Result; use anyhow::Result;
// use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
// use rand::thread_rng; use rand::thread_rng;
// use std::sync::{Arc, Mutex};
// use crate::models::*;
// use crate::models::*;
// impl Game {
// /// Build game decks from input data for game start. impl Game {
// /// This should only run once and at startup. /// Build game decks from input data for game start.
// fn _build_decks(&mut self, cards_json: Vec<CardSet>) -> Result<()> { /// This should only run once and at startup.
// for pack in cards_json { fn _build_decks(&mut self, cards_json: Vec<CardSet>) -> Result<()> {
// if let Some(white) = pack.white { for pack in cards_json {
// self.white.extend(white) if let Some(white) = pack.white {
// } self.white.extend(white)
// if let Some(black) = pack.black { }
// self.black.extend(black) if let Some(black) = pack.black {
// } self.black.extend(black)
// } }
// }
// Ok(())
// } Ok(())
// }
// pub fn new(request: NewGameRequest) -> Result<Self> {
// let mut game = Game { pub fn new(request: NewGameManifest) -> Result<Self> {
// ..Default::default() let mut game = Game {
// }; ..Default::default()
// tracing::debug!("Creating game {}", &request.name); };
// game.name = request.name; tracing::debug!(
// "Creating game {} with {} as host",
// // game.build_decks(request.packs)?; &request.name,
// game.create_player(request.host)?; request.host.lock().unwrap().name
// game.deal_black()?; );
// game.name = request.name;
// Ok(game) game.host = request.host.clone();
// }
// // game.build_decks(request.packs)?;
// // pub fn join(request:GameJoinRequest) game.create_player(request.host)?;
// game.deal_black()?;
// /// Log counts of current drawable cards
// /// For testing Ok(game)
// pub fn deck_counts(&self) { }
// tracing::debug!(
// "Deck Counts:\n {} White cards\n {} Black cards", // pub fn join(request:GameJoinRequest)
// self.white.len(),
// self.black.len() /// Log counts of current drawable cards
// ); /// For testing
// } pub fn deck_counts(&self) {
// tracing::debug!(
// /// Draw one white card at random from play deck. "Deck Counts:\n {} White cards\n {} Black cards",
// fn draw_one_white(&mut self) -> Result<CardWhite> { self.white.len(),
// let deck = &mut self.white; self.black.len()
// );
// // this feels sloppy }
// if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
// Ok(deck.swap_remove(index)) /// Draw one white card at random from play deck.
// } else { fn draw_one_white(&mut self) -> Result<CardWhite> {
// Ok(CardWhite { let deck = &mut self.white;
// text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
// pack: 0, // this feels sloppy
// }) if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
// } Ok(deck.swap_remove(index))
// } } else {
// Ok(CardWhite {
// /// Draw one black card at random from play deck. text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
// fn draw_one_black(&mut self) -> Result<CardBlack> { pack: 0,
// let deck = &mut self.black; })
// }
// // this feels sloppy }
// if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
// Ok(deck.swap_remove(index)) /// Draw one black card at random from play deck.
// } else { fn draw_one_black(&mut self) -> Result<CardBlack> {
// Ok(CardBlack { let deck = &mut self.black;
// text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
// pick: 0, // this feels sloppy
// pack: 0, if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
// }) Ok(deck.swap_remove(index))
// } } else {
// } Ok(CardBlack {
// text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
// /// Deal a black card and use it for the current round pick: 0,
// fn deal_black(&mut self) -> Result<()> { pack: 0,
// self.current_black = Some(self.draw_one_black()?); })
// }
// Ok(()) }
// }
// /// Deal a black card and use it for the current round
// /// Create a new player and add them to the game. fn deal_black(&mut self) -> Result<()> {
// pub fn create_player(&mut self, mut player: Player) -> Result<()> { self.current_black = Some(self.draw_one_black()?);
// tracing::debug!("Creating player {} as {:?}", &player.name, &player.role);
// Ok(())
// let mut hand_buf = vec![]; }
// for _ in 0..10 {
// hand_buf.push(self.draw_one_white()?); /// Create a new player and add them to the game.
// } pub fn create_player(&mut self, user: Arc<Mutex<User>>) -> Result<()> {
// tracing::debug!("Dealing hand to {}", &player.name); let mut new_player = Player {
// player.white.extend(hand_buf); user: user.clone(),
// white: vec![],
// self.players.push(player); black: vec![],
// };
// Ok(())
// } let new_player_name = user.lock().unwrap().name.clone();
// tracing::debug!("Creating player for {}", &new_player_name);
// pub fn game_start(&mut self) -> Result<()> {
// self.game_active = true; let mut hand_buf = vec![];
// tracing::debug!("Game Active!"); for _ in 0..10 {
// hand_buf.push(self.draw_one_white()?);
// if let Some(black) = &self.current_black { }
// tracing::debug!("{}", black.text); tracing::debug!("Dealing hand to {}", &new_player_name);
// } else {
// tracing::debug!("YOU DONE FUCKED UP (no current black card)"); new_player.white.extend(hand_buf);
// }
// self.players.push(new_player);
// Ok(())
// } Ok(())
// } }
pub fn game_start(&mut self) -> Result<()> {
self.game_active = true;
tracing::debug!("Game Active!");
if let Some(black) = &self.current_black {
tracing::debug!("{}", black.text);
} else {
tracing::debug!("YOU DONE FUCKED UP (no current black card)");
}
Ok(())
}
}

View file

@ -40,7 +40,7 @@ pub struct ServerStateSummary {
} }
/// User /// User
#[derive(Debug, Eq, PartialEq, Hash)] #[derive(Default, Debug, Eq, PartialEq, Hash)]
pub struct User { pub struct User {
pub name: String, pub name: String,
} }
@ -124,12 +124,10 @@ pub enum PlayerRole {
} }
/// A struct that represents a player /// A struct that represents a player
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug)]
pub struct Player { pub struct Player {
/// Player's username /// Player's username
pub name: String, pub user: Arc<Mutex<User>>,
/// This player's role
pub role: PlayerRole,
/// The player's hand /// The player's hand
pub white: Vec<CardWhite>, pub white: Vec<CardWhite>,
/// The player's wins /// The player's wins
@ -137,10 +135,12 @@ pub struct Player {
} }
/// The game master /// The game master
#[derive(Default, Debug, Serialize, Deserialize)] #[derive(Default, Debug)]
pub struct Game { pub struct Game {
/// The name of the game /// The name of the game
pub name: String, pub name: String,
/// The host user of the game
pub host: Arc<Mutex<User>>,
/// White draw pile /// White draw pile
pub white: Vec<CardWhite>, pub white: Vec<CardWhite>,
/// Black draw pile /// Black draw pile

View file

@ -156,9 +156,11 @@ fn motd() -> String {
/// Generate server summary update - mostly debug stuff /// Generate server summary update - mostly debug stuff
fn server_summary_update(state: &Arc<AppState>) -> String { fn server_summary_update(state: &Arc<AppState>) -> String {
let online_users = state.online_users.lock().unwrap().len();
let active_games = state.games.lock().unwrap().len();
to_string::<ServerStateSummary>(&ServerStateSummary { to_string::<ServerStateSummary>(&ServerStateSummary {
online_users: state.online_users.lock().unwrap().len(), online_users,
active_games: state.games.lock().unwrap().len(), active_games,
}) })
.unwrap() .unwrap()
} }
@ -169,7 +171,11 @@ fn games_update(state: &Arc<AppState>) -> String {
let mut names = vec![]; let mut names = vec![];
for game in state.games.lock().unwrap().iter() { for game in state.games.lock().unwrap().iter() {
names.push(game.name.clone()); names.push(format!(
"Name: {} Host: {}",
game.name,
game.host.lock().unwrap().name
));
} }
to_string::<GamesUpdate>(&GamesUpdate { games: names }).unwrap() to_string::<GamesUpdate>(&GamesUpdate { games: names }).unwrap()
@ -179,7 +185,15 @@ fn games_update(state: &Arc<AppState>) -> String {
fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String { fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
let msg = format!( let msg = format!(
"{} joined.", "{} joined.",
state.online_users.lock().unwrap().get(addr).unwrap().lock().unwrap().name state
.online_users
.lock()
.unwrap()
.get(addr)
.unwrap()
.lock()
.unwrap()
.name
); );
tracing::debug!("{}", &msg); tracing::debug!("{}", &msg);

View file

@ -54,31 +54,23 @@ fn handle_new_game(
tx: &Sender<String>, tx: &Sender<String>,
addr: SocketAddr, addr: SocketAddr,
) -> Result<()> { ) -> Result<()> {
let binding = state.online_users.lock().unwrap();
let user = binding.get(&addr).unwrap();
tracing::debug!("{:#?} from {}", new_game, user.lock().unwrap().name);
let manifest = NewGameManifest { let manifest = NewGameManifest {
name: new_game.name, name: new_game.name,
host: user.clone(), host: state
.online_users
.lock()
.unwrap()
.get(&addr)
.unwrap()
.clone(),
}; };
// create game // create game
// if let Ok(new_game_object) = Game::new(new_game) { if let Ok(new_game_object) = Game::new(manifest) {
// if let Ok(game_json) = to_string(&new_game_object) { state.games.lock().unwrap().push(new_game_object);
// tracing::debug!("Sent new game JSON."); tx.send(games_update(state))?;
// // this is a broadcast tx.send(server_summary_update(state))?;
// // change this }
// tx.send(game_json)?;
// } else {
// // change this
// tracing::error!("Failed to convert Game object to JSON.")
// }
//
// state.games.lock().unwrap().push(new_game_object);
// tx.send(games_update(state))?;
// tx.send(server_summary_update(state))?;
// }
Ok(()) Ok(())
} }