Skip to content

Commit

Permalink
feat: create gameroom state and use in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyabrain committed Nov 22, 2024
1 parent 90ee4ba commit 84ece4a
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 31 deletions.
6 changes: 6 additions & 0 deletions lib/viral_spiral/canon/card.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule ViralSpiral.Canon.Card do
alias ViralSpiral.Canon.Card.Bias
alias ViralSpiral.Affinity

@type t :: Affinity.t() | Bias.t()
end
5 changes: 5 additions & 0 deletions lib/viral_spiral/canon/card/affinity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ defmodule ViralSpiral.Canon.Card.Affinity do
headline: nil,
image: nil,
article_id: nil

@type t :: %__MODULE__{
id: String.t(),
tgb: integer()
}
end

# defmodule ViralSpiral.Canon.Card.Affinity.Cat do
Expand Down
5 changes: 5 additions & 0 deletions lib/viral_spiral/canon/card/bias.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ defmodule ViralSpiral.Canon.Card.Bias do
headline: nil,
image: nil,
article_id: nil

@type t :: %__MODULE__{
id: String.t(),
tgb: integer()
}
end
34 changes: 34 additions & 0 deletions lib/viral_spiral/room/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ViralSpiral.Gameplay.Factory do
@moduledoc """
Create entities for a Game Room
"""
alias ViralSpiralWeb.GameRoomState
alias ViralSpiral.Entity.Deck
alias ViralSpiral.Canon.Deck, as: CanonDeck
alias ViralSpiral.Canon.DrawTypeRequirements
Expand Down Expand Up @@ -70,4 +71,37 @@ defmodule ViralSpiral.Gameplay.Factory do
store: CanonDeck.create_store(cards)
}
end

def make_gameroom(%State{} = state) do
%GameRoomState{
room: %{
name: state.room.name,
chaos: state.room.chaos
},
players:
for(
{_id, player} <- state.players,
do: %{
name: player.name,
clout: player.clout,
affinities: player.affinities,
biases: player.biases,
is_active: state.turn.current == player.id,
cards:
player.active_cards
|> Enum.map(&state.deck.store[{&1, true}])
|> Enum.map(
&%{
id: &1.id,
type: &1.type,
veracity: &1.veracity,
headline: &1.headline,
image: &1.image,
article_id: &1.article_id
}
)
}
)
}
end
end
2 changes: 1 addition & 1 deletion lib/viral_spiral/room/reducer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule ViralSpiral.Room.Reducer do
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Room.Action

@spec reduce(State.t(), Action.t()) :: State.t()
# @spec reduce(State.t(), Action.t()) :: State.t()
def reduce(%State{} = state, %{type: :draw_card} = action) do
draw_type = action.payload.draw_type

Expand Down
13 changes: 13 additions & 0 deletions lib/viral_spiral_web/components/atoms.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ViralSpiralWeb.Atoms do
use ViralSpiralWeb, :html

attr :card, :map, required: true

def card(assigns) do
~H"""
<div class="p-2 border-2 rounded-lg bg-slate-400">
<p><%= @card.headline %></p>
</div>
"""
end
end
16 changes: 5 additions & 11 deletions lib/viral_spiral_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
<div class="flex items-center gap-4">
<a href="/">
<img src={~p"/images/logo.svg"} width="36" />
<img src={~p"/images/logo.png"} width="36" />
</a>
<p class="bg-brand/5 text-brand rounded-full px-2 font-medium leading-6">
v<%= Application.spec(:phoenix, :vsn) %>
<%= Application.spec(:viral_spiral, :vsn) %>
</p>
</div>
<div class="flex items-center gap-4 font-semibold leading-6 text-zinc-900">
<a href="https://twitter.com/elixirphoenix" class="hover:text-zinc-700">
@elixirphoenix
<a href="https://tattle.co.in/products/viral-spiral/" class="hover:text-zinc-700">
Website
</a>
<a href="https://github.com/phoenixframework/phoenix" class="hover:text-zinc-700">
<a href="https://github.com/tattle-made/viral-spiral" class="hover:text-zinc-700">
GitHub
</a>
<a
href="https://hexdocs.pm/phoenix/overview.html"
class="rounded-lg bg-zinc-100 px-2 py-1 hover:bg-zinc-200/80"
>
Get Started <span aria-hidden="true">&rarr;</span>
</a>
</div>
</div>
</header>
Expand Down
39 changes: 32 additions & 7 deletions lib/viral_spiral_web/live/game_room.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
defmodule ViralSpiralWeb.GameRoom do
import ViralSpiralWeb.Atoms
alias ViralSpiral.Canon.Card
alias ViralSpiral.Entity.Player
alias ViralSpiral.Canon.Deck
alias ViralSpiral.Gameplay.Factory
alias ViralSpiral.Room.Actions
alias ViralSpiral.Room.Reducer
alias ViralSpiral.Room.State
alias ViralSpiral.Entity.Room
use ViralSpiralWeb, :live_view

def mount(params, session, socket) do
room =
Room.new()
|> Room.start(4)
room = Room.new() |> Room.start(4)
state = State.new(room, ["adhiraj", "krys", "aman", "farah"])
requirements = Factory.draw_type(state)
draw_type = Deck.draw_type(requirements)
state = Reducer.reduce(state, Actions.draw_card(draw_type))

root = State.new(room, ["adhiraj", "krys", "aman", "farah"])
IO.inspect(root)
IO.puts("hi")
gameroom_state = Factory.make_gameroom(state)

{:ok, assign(socket, :root, root)}
{:ok, assign(socket, :state, gameroom_state)}
end

def handle_event("start_game", _params, socket) do
{:noreply, socket}
end

def handle_event("pass_to", params, socket) do
# card = socket.assigns.card

# state = Reducers.reduce(state, Actions.pass_card())

{:noreply, socket}
end

def player_options(state, player) do
pass_to_ids = state.turn.pass_to

pass_to_names =
pass_to_ids
|> Enum.map(&state.players[&1].name)

pass_to_names
end
end
65 changes: 53 additions & 12 deletions lib/viral_spiral_web/live/game_room.html.heex
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
<div class="p-4 border-red-200 border-2 rounded-2xl flex flex-row justify-between ">
<span><%= @root.room.name %></span>
<span><%= @root.room.id %></span>
<span><%= @root.room.state %></span>
<span><%= @root.room.chaos_counter %></span>
<span><%= @state.room.name %></span>
<%!-- <span><%= @state.room.id %></span>
<span><%= @state.room.state %></span> --%>
<span><%= @state.room.chaos %></span>
</div>

<div class="p-4 mt-2 border-red-200 border-2 rounded-2xl flex flex-row justify-between">
<%= for player <- Enum.map(@root.players, fn {_id, player} -> player end) do %>
<div class={[
"p-2 border-2 w-full rounded-xl",
if(@root.turn.current == player.id,
<div class="mt-2 flex flex-row justify-between">
<div
:for={player <- @state.players}
class={[
"p-2 border-2 w-full rounded-xl m-2",
if(player.is_active,
do: "border-red-400",
else: "border-red-100"
)
]}>
<%= player.name %>
]}
>
<span><%= player.name %></span>

<%!-- <span class="ml-6"><%= player.identity %></span> --%>
<div class="h-2"></div>
<div>
<h2 class="font-semibold">Clout</h2>
<span><%= player.clout %></span>
</div>
<% end %>
<div class="h-2"></div>
<div>
<h2 class="font-semibold">Affinities</h2>
<div :for={affinity <- player.affinities}>
<span><%= elem(affinity, 0) %></span>
<span><%= elem(affinity, 1) %></span>
</div>

<div class="h-2"></div>
<h2 class="font-semibold">Bias</h2>
<div :for={bias <- player.biases}>
<span><%= elem(bias, 0) %></span>
<span><%= elem(bias, 1) %></span>
</div>

<div class="h-4"></div>

<div :for={card <- player.cards} :if={player.is_active}>
<.card card={card}></.card>
</div>
</div>

<%!-- <div class="h-4"></div>
<%= if current_player?(@state, player) do %>
<.card>
<p><%= card(@state, player).headline %></p>
</.card>
<h3>Pass to</h3>
<%= for pass_to <- player_options(@state, player) do %>
<span class="mr-4"><%= pass_to %></span>
<% end %>
<% end %> --%>
</div>
</div>
15 changes: 15 additions & 0 deletions lib/viral_spiral_web/live/game_room_state.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule ViralSpiralWeb.GameRoomState do
defstruct room: %{}, players: []

@type t :: %__MODULE__{
room: %{
name: String.t(),
chaos: integer()
},
players:
list(%{
name: String.t(),
affinities: %{atom() => integer()}
})
}
end
Binary file added priv/static/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 84ece4a

Please sign in to comment.