Skip to content

Commit

Permalink
opponents have play button
Browse files Browse the repository at this point in the history
  • Loading branch information
randallard committed Nov 30, 2024
1 parent ca36b0f commit 0b6474a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
28 changes: 28 additions & 0 deletions src/components/game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Serialize, Deserialize};
use crate::components::board::SavedBoard;
use crate::components::opponent::Opponent;

#[derive(Clone, Serialize, Deserialize)]
pub struct GameState {
pub player1: String, // Current player's name
pub player2: Option<Opponent>, // Selected opponent
pub current_round: usize,
pub player1_score: i32,
pub player2_score: i32,
pub player1_board: Option<SavedBoard>,
pub player2_board: Option<SavedBoard>,
}

impl GameState {
pub fn new(player_name: String, opponent: Opponent) -> Self {
GameState {
player1: player_name,
player2: Some(opponent),
current_round: 1,
player1_score: 0,
player2_score: 0,
player1_board: None,
player2_board: None,
}
}
}
1 change: 1 addition & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod opponent;
pub mod board;
pub mod game;
pub mod saved_boards;
pub mod utils;
42 changes: 29 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use leptos::*;
use leptos::prelude::*;
use leptos::logging::log;
use web_sys::{MouseEvent, Storage, window};
use serde::{Serialize, Deserialize};

Expand Down Expand Up @@ -124,20 +125,35 @@ fn App() -> impl IntoView {
children=move |opponent: Opponent| {
view! {
<div class="flex items-center justify-between p-2 bg-slate-800 rounded">
<div class="flex items-center gap-2 text-gray-300">
<span class="w-4 h-4 rounded-full bg-blue-600 flex items-center justify-center text-xs">
{if matches!(opponent.opponent_type, OpponentType::Computer) { "C" } else { "H" }}
</span>
{opponent.name.clone()}
</div>
<button
class="text-red-400 hover:text-red-300 opacity-50 hover:opacity-100 transition-opacity"
on:click=move |_| opponent_to_delete.set(Some(opponent.clone()))
>
"Remove"
</button>
<div class="flex items-center gap-2 text-gray-300">
<span class="w-4 h-4 rounded-full bg-blue-600 flex items-center justify-center text-xs">
{if matches!(opponent.opponent_type, OpponentType::Computer) { "C" } else { "H" }}
</span>
{opponent.name.clone()}
</div>
}
<div class="flex gap-2">
{
let play_opponent = opponent.clone();
let delete_opponent = opponent.clone();
view! {
<button
class="px-3 py-1 bg-green-600 hover:bg-green-700 rounded text-sm"
on:click=move |_| {
log!("Starting game with {}", play_opponent.name);
}
>
"Play"
</button>
<button
class="text-red-400 hover:text-red-300 opacity-50 hover:opacity-100 transition-opacity"
on:click=move |_| opponent_to_delete.set(Some(delete_opponent.clone()))
>
"Remove"
</button>
}
}
</div>
</div> }
}
/>
</div>
Expand Down

0 comments on commit 0b6474a

Please sign in to comment.