From 99580b767d0cf17f67bc1c3d03f8119e3d32d69a Mon Sep 17 00:00:00 2001 From: Denny Date: Wed, 18 Sep 2024 16:01:13 +0530 Subject: [PATCH] feat: init setup of liveviews for home, waiting room and game room --- assets/js/app.js | 29 ++++++++------- lib/viral_spiral/room/room_genserver.ex | 36 +++++++++++++++++++ .../components/layouts/app.html.heex | 2 +- lib/viral_spiral_web/live/game_room.ex | 21 +++++++++++ lib/viral_spiral_web/live/game_room.html.heex | 20 +++++++++++ lib/viral_spiral_web/live/home.ex | 14 ++++++++ lib/viral_spiral_web/live/home.html.heex | 3 ++ lib/viral_spiral_web/live/waiting_room.ex | 16 +++++++++ .../live/waiting_room.html.heex | 1 + lib/viral_spiral_web/router.ex | 5 ++- 10 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 lib/viral_spiral/room/room_genserver.ex create mode 100644 lib/viral_spiral_web/live/game_room.ex create mode 100644 lib/viral_spiral_web/live/game_room.html.heex create mode 100644 lib/viral_spiral_web/live/home.ex create mode 100644 lib/viral_spiral_web/live/home.html.heex create mode 100644 lib/viral_spiral_web/live/waiting_room.ex create mode 100644 lib/viral_spiral_web/live/waiting_room.html.heex diff --git a/assets/js/app.js b/assets/js/app.js index df0cdd9..9d2f160 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -16,26 +16,29 @@ // // Include phoenix_html to handle method=PUT/DELETE in forms and buttons. -import "phoenix_html" +import "phoenix_html"; // Establish Phoenix Socket and LiveView configuration. -import {Socket} from "phoenix" -import {LiveSocket} from "phoenix_live_view" -import topbar from "../vendor/topbar" +import { Socket } from "phoenix"; +import { LiveSocket } from "phoenix_live_view"; +import topbar from "../vendor/topbar"; -let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") -let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) +let csrfToken = document + .querySelector("meta[name='csrf-token']") + .getAttribute("content"); +let liveSocket = new LiveSocket("/live", Socket, { + params: { _csrf_token: csrfToken }, +}); // Show progress bar on live navigation and form submits -topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) -window.addEventListener("phx:page-loading-start", _info => topbar.show(300)) -window.addEventListener("phx:page-loading-stop", _info => topbar.hide()) +topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" }); +window.addEventListener("phx:page-loading-start", (_info) => topbar.show(300)); +window.addEventListener("phx:page-loading-stop", (_info) => topbar.hide()); // connect if there are any LiveViews on the page -liveSocket.connect() +liveSocket.connect(); // expose liveSocket on window for web console debug logs and latency simulation: -// >> liveSocket.enableDebug() +liveSocket.enableDebug(); // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session // >> liveSocket.disableLatencySim() -window.liveSocket = liveSocket - +window.liveSocket = liveSocket; diff --git a/lib/viral_spiral/room/room_genserver.ex b/lib/viral_spiral/room/room_genserver.ex new file mode 100644 index 0000000..9200052 --- /dev/null +++ b/lib/viral_spiral/room/room_genserver.ex @@ -0,0 +1,36 @@ +defmodule ViralSpiral.Room.RoomGenserver do + use GenServer + + @impl true + def init(init_arg) do + {:ok, root} + end + + @impl true + def handle_cast({:join, player_name}, state) do + end + + @impl true + def handle_cast({:pass, from, to}, state) do + end + + @impl true + def handle_cast({:keep, from}, state) do + end + + @impl true + def handle_cast({:discard, from}, state) do + end + + @impl true + def handle_cast({:check_source, from}, state) do + end + + @impl true + def handle_cast({:cancel, from, target}, state) do + end + + @impl true + def handle_cast({:viral_spiral, from, targets}, state) do + end +end diff --git a/lib/viral_spiral_web/components/layouts/app.html.heex b/lib/viral_spiral_web/components/layouts/app.html.heex index e23bfc8..74698cb 100644 --- a/lib/viral_spiral_web/components/layouts/app.html.heex +++ b/lib/viral_spiral_web/components/layouts/app.html.heex @@ -25,7 +25,7 @@
-
+
<.flash_group flash={@flash} /> <%= @inner_content %>
diff --git a/lib/viral_spiral_web/live/game_room.ex b/lib/viral_spiral_web/live/game_room.ex new file mode 100644 index 0000000..a30bc0d --- /dev/null +++ b/lib/viral_spiral_web/live/game_room.ex @@ -0,0 +1,21 @@ +defmodule ViralSpiralWeb.GameRoom do + alias ViralSpiral.Room.State.Room + alias ViralSpiral.Room.State.Root + use ViralSpiralWeb, :live_view + + def mount(params, session, socket) do + room = + Room.new() + |> Room.start(4) + + root = Root.new(room, ["adhiraj", "krys", "aman", "farah"]) + IO.inspect(root) + IO.puts("hi") + + {:ok, assign(socket, :root, root)} + end + + def handle_event("start_game", _params, socket) do + {:noreply, socket} + end +end diff --git a/lib/viral_spiral_web/live/game_room.html.heex b/lib/viral_spiral_web/live/game_room.html.heex new file mode 100644 index 0000000..9c82ea1 --- /dev/null +++ b/lib/viral_spiral_web/live/game_room.html.heex @@ -0,0 +1,20 @@ +
+ <%= @root.room.name %> + <%= @root.room.id %> + <%= @root.room.state %> + <%= @root.room.chaos_counter %> +
+ +
+ <%= for player <- Enum.map(@root.players, fn {_id, player} -> player end) do %> +
+ <%= player.name %> +
+ <% end %> +
diff --git a/lib/viral_spiral_web/live/home.ex b/lib/viral_spiral_web/live/home.ex new file mode 100644 index 0000000..3316d4b --- /dev/null +++ b/lib/viral_spiral_web/live/home.ex @@ -0,0 +1,14 @@ +defmodule ViralSpiralWeb.Home do + alias ViralSpiral.Room.State.Room + use ViralSpiralWeb, :live_view + + def mount(params, session, socket) do + {:ok, socket} + end + + def handle_event("create_room", _params, socket) do + name = Room.name() + # {:noreply, push_navigate(socket, to: "/waiting-room/#{name}")} + {:noreply, push_navigate(socket, to: "/room/#{name}")} + end +end diff --git a/lib/viral_spiral_web/live/home.html.heex b/lib/viral_spiral_web/live/home.html.heex new file mode 100644 index 0000000..9beafec --- /dev/null +++ b/lib/viral_spiral_web/live/home.html.heex @@ -0,0 +1,3 @@ +

Home

+ +<.button phx-click="create_room" class="ml-2">Create a new Room! diff --git a/lib/viral_spiral_web/live/waiting_room.ex b/lib/viral_spiral_web/live/waiting_room.ex new file mode 100644 index 0000000..b44babf --- /dev/null +++ b/lib/viral_spiral_web/live/waiting_room.ex @@ -0,0 +1,16 @@ +defmodule ViralSpiralWeb.WaitingRoom do + @moduledoc """ + A space for people to wait while other players join. + + We enforce that atleast 3 players are present before someone can start the game. + """ + use ViralSpiralWeb, :live_view + + def mount(params, session, socket) do + {:ok, socket} + end + + def handle_event("start_game", _params, socket) do + {:noreply, socket} + end +end diff --git a/lib/viral_spiral_web/live/waiting_room.html.heex b/lib/viral_spiral_web/live/waiting_room.html.heex new file mode 100644 index 0000000..f5bcbf6 --- /dev/null +++ b/lib/viral_spiral_web/live/waiting_room.html.heex @@ -0,0 +1 @@ +

Waiting for everyone to join

diff --git a/lib/viral_spiral_web/router.ex b/lib/viral_spiral_web/router.ex index eee14ab..bf4b6d9 100644 --- a/lib/viral_spiral_web/router.ex +++ b/lib/viral_spiral_web/router.ex @@ -17,7 +17,10 @@ defmodule ViralSpiralWeb.Router do scope "/", ViralSpiralWeb do pipe_through :browser - get "/", PageController, :home + # get "/", PageController, :home + live "/", Home + live "/waiting-room/:room", WaitingRoom + live "/room/:room", GameRoom end # Other scopes may use custom stacks.