Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanRJohnston committed Feb 7, 2024
1 parent 1a1f6ec commit 109947d
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 154 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
".direnv": true,
"node_modules": true,
".wrangler": true
}
// "rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]
},
"rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]
}
4 changes: 2 additions & 2 deletions end2end/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ test("can setup and join lobby", async ({ browser }) => {
joinGame(bobPage, code, "Bob"),
];

outer: while (true) {
loop: while (true) {
for (const player of players) {
if ((await player.next()).done) {
break outer;
break loop;
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions server/src/models/commands/place_bets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Command for PlaceBets {
return Err("cannot place a bet with a total value greater than your balance".into());
}

Ok(input
let mut new_events = input
.bets
.iter()
.map(|bet| {
Expand All @@ -64,7 +64,18 @@ impl Command for PlaceBets {
amount: bet.amount,
})
})
.collect())
.collect::<Vector<_>>();

if projections::all_players_have_bet(&{
let mut events = events.clone();
events.append(new_events.clone());

events
}) {
new_events.push_back(Event::RaceStarted);
}

Ok(new_events.into_iter().collect())
}
}

19 changes: 16 additions & 3 deletions server/src/models/projections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ pub fn minimum_bet(events: Vector<Event>) -> i32 {
starting_bet
}

pub fn placed_bets(events: &Vector<Event>) -> Vector<PlacedBet> {
let mut bets = Vector::new();
pub fn placed_bets(events: &Vector<Event>) -> HashMap<Uuid, Vector<PlacedBet>> {
let mut bets = HashMap::<Uuid, Vector<PlacedBet>>::new();

for event in events {
match event {
Event::PlacedBet(bet) => bets.push_back(bet.clone()),
Event::PlacedBet(bet) => bets.entry(bet.session_id).or_default().push_back(*bet),
Event::RaceFinished { .. } => bets.clear(),
_ => {}
}
Expand All @@ -109,6 +109,19 @@ pub fn placed_bets(events: &Vector<Event>) -> Vector<PlacedBet> {
bets
}

pub fn all_players_have_bet(events: &Vector<Event>) -> bool {
let players = players(events);
let bets = placed_bets(events);

for player in players.keys().into_iter() {
if !bets.contains_key(player) {
return false;
}
}

true
}

pub fn account_balance(events: &Vector<Event>) -> HashMap<Uuid, i32> {
let mut accounts = HashMap::<Uuid, i32>::new();
let mut bets = Vector::new();
Expand Down
54 changes: 54 additions & 0 deletions server/src/screens/host/lobby.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use leptos::{component, view, CollectView, IntoView, SignalGet};

use crate::{
models::projections,
utils::{use_events, use_game_id},
};

#[component]
pub fn lobby() -> impl IntoView {
let events = use_events();
let game_id = use_game_id();

let players = move || projections::players(&events.get());
let player_count = move || players().len();
let ready_count = move || {
players()
.into_iter()
.filter_map(|(_, info)| info.ready.then_some(true))
.count()
};

view! {
<div class="host-lobby-container">
<div class="top-row">
<div>"Lobby: " <span data-testid="game_code">{game_id}</span></div>
<div>"Ready: " {ready_count} "/" {player_count}</div>
</div>
<div class="avatar-previews">

{move || {
players()
.values()
.map(|info| {
view! {
<div class="avatar-container">
<img
class="profile-picture"
src="https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png"
/>
<div class="name">{info.name.clone()}</div>
<div class="status">
"Ready: " {if info.ready { "✅" } else { "❌" }}
</div>
</div>
}
})
.collect_view()
}}

</div>
</div>
}
}

9 changes: 9 additions & 0 deletions server/src/screens/host/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod lobby;
pub use lobby::*;

mod pre_game;
pub use pre_game::*;

mod race;
pub use race::*;

7 changes: 7 additions & 0 deletions server/src/screens/host/pre_game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use leptos::*;

#[component]
pub fn pre_game() -> impl IntoView {
view! { <h1>"Host pregame screen!"</h1> }
}

7 changes: 7 additions & 0 deletions server/src/screens/host/race.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use leptos::*;

#[component]
pub fn race() -> impl IntoView {
view! { <h1>"Host race screen!"</h1> }
}

43 changes: 0 additions & 43 deletions server/src/screens/host_lobby.rs

This file was deleted.

3 changes: 2 additions & 1 deletion server/src/screens/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn main_menu() -> impl IntoView {
view! {
<Form action="/api/create_game" method="POST" class="main-menu">
<h1 class="title">"Deep Space Derby"</h1>
<input class="host" type="submit" value="Host" />
<input class="host" type="submit" value="Host"/>
<div class="play">
<a href="/play">
<h2>"Play"</h2>
Expand All @@ -18,3 +18,4 @@ pub fn main_menu() -> impl IntoView {
</Form>
}
}

104 changes: 9 additions & 95 deletions server/src/screens/mod.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,24 @@
mod player_lobby;
use leptos::{component, view, IntoView};
use leptos_meta::{provide_meta_context, Stylesheet};
use leptos_reactive::create_memo;
pub use player_lobby::*;

mod host_lobby;
pub use host_lobby::*;

mod game_wrapper;
pub use game_wrapper::*;
use crate::{models::events::Event, utils::use_events};

mod join_screen;
pub use join_screen::*;
use router::Router;

mod game_wrapper;
mod host;
mod main_menu;
pub use main_menu::*;

mod player_pregame;
pub use player_pregame::*;

use leptos::{component, view, IntoView};
use leptos_meta::{provide_meta_context, Stylesheet};
use leptos_router::{Route, Router, Routes};

use crate::{models::events::Event, utils::use_events};
mod player;
mod router;

#[component]
pub fn app() -> impl leptos::IntoView {
provide_meta_context();

view! {
<Stylesheet id="leptos" href="/pkg/style.css"/>

<Router>
<Routes>
<Route path="/" view=MainMenu/>
<Route
path="/host/:game_id"
view=|| {
view! {
<GameConnectionWrapper>
<GameStateRouter lobby=HostLobby pre_game=PlayerPreGame/>
</GameConnectionWrapper>
}
}
/>

<Route path="/play" view=JoinScreen/>
<Route
path="/play/:game_id"
view=|| {
view! {
<GameConnectionWrapper>
<GameStateRouter lobby=PlayerLobby pre_game=PlayerPreGame/>
</GameConnectionWrapper>
}
}
/>

</Routes>
</Router>
}
}

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
enum GameState {
#[default]
Lobby,
PreGame,
Race,
Summary,
FinalScreen,
}

#[component]
pub fn game_state_router<Lobby, LobbyIV, PreGame, PreGameIV>(
lobby: Lobby,
pre_game: PreGame,
) -> impl IntoView
where
Lobby: Fn() -> LobbyIV + 'static,
LobbyIV: IntoView + 'static,
PreGame: Fn() -> PreGameIV + 'static,
PreGameIV: IntoView + 'static,
{
let events = use_events();

let state = create_memo(move |_| {
events()
.iter()
.rev()
.find_map(|event| match event {
Event::GameCreated { .. } => Some(GameState::Lobby),
Event::GameStarted => Some(GameState::PreGame),
Event::RaceStarted => Some(GameState::Race),
Event::RaceFinished { .. } => Some(GameState::Summary),
Event::GameFinished => Some(GameState::FinalScreen),
_ => None,
})
.unwrap_or_default()
});

move || match state() {
GameState::Lobby => lobby().into_view(),
GameState::PreGame => pre_game().into_view(),
GameState::Race => todo!(),
GameState::Summary => todo!(),
GameState::FinalScreen => todo!(),
<Router/>
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use leptos::*;
use leptos_router::*;

#[component]
pub fn join_screen() -> impl IntoView {
pub fn join() -> impl IntoView {
view! {
<Form action="/api/join_game" method="POST" class="join-screen">
<h1>"Join Game"</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

#[component]
pub fn player_lobby() -> impl IntoView {
pub fn lobby() -> impl IntoView {
let events = use_events();
let game_id = use_game_id();
let session_id = use_session_id();
Expand Down
11 changes: 11 additions & 0 deletions server/src/screens/player/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod join;
pub use join::*;

mod lobby;
pub use lobby::*;

mod pregame;
pub use pregame::*;

mod race;
pub use race::*;
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct Bet {
}

#[component]
pub fn player_pre_game() -> impl IntoView {
pub fn pre_game() -> impl IntoView {
let game_id = use_game_id();
let player_id = use_session_id();
let events = use_events();
Expand Down Expand Up @@ -163,9 +163,9 @@ pub fn player_pre_game() -> impl IntoView {

let placed_bets = create_memo(move |_| {
projections::placed_bets(&events())
.into_iter()
.filter(|it| it.session_id == player_id)
.collect::<Vector<_>>()
.get(&player_id)
.cloned()
.unwrap_or_default()
});

create_effect({
Expand Down
Loading

0 comments on commit 109947d

Please sign in to comment.