mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Consolidate opponent and player functions
This commit is contained in:
parent
430a34e906
commit
e700efc288
@ -3,11 +3,12 @@ defmodule ChessWeb.GameChannel do
|
||||
|
||||
use ChessWeb, :channel
|
||||
|
||||
import ChessWeb.GameView, only: [player: 2, opponent: 2]
|
||||
|
||||
alias Chess.Board
|
||||
alias Chess.MoveList
|
||||
alias Chess.Moves
|
||||
alias Chess.Repo.Queries
|
||||
alias Chess.Store.Game
|
||||
|
||||
def join("game:" <> game_id, _params, socket) do
|
||||
send(self(), {:after_join, game_id})
|
||||
@ -21,8 +22,8 @@ defmodule ChessWeb.GameChannel do
|
||||
|> Queries.game_for_info(game_id)
|
||||
|
||||
payload = %{
|
||||
player: player(socket, game),
|
||||
opponent: opponent(socket, game),
|
||||
player: player(game, socket.assigns.current_user_id),
|
||||
opponent: opponent(game, socket.assigns.current_user_id).name,
|
||||
board: Board.transform(game.board),
|
||||
turn: game.turn,
|
||||
state: game.state,
|
||||
@ -95,20 +96,4 @@ defmodule ChessWeb.GameChannel do
|
||||
|
||||
ChessWeb.Endpoint.broadcast("game:#{game.id}", "game:update", payload)
|
||||
end
|
||||
|
||||
defp player(socket, game) do
|
||||
if game.user_id == socket.assigns.current_user_id do
|
||||
"white"
|
||||
else
|
||||
"black"
|
||||
end
|
||||
end
|
||||
|
||||
defp opponent(socket, game) do
|
||||
if game.user_id == socket.assigns.current_user_id do
|
||||
game.opponent.name
|
||||
else
|
||||
game.user.name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
</span>
|
||||
<%= link gettext(
|
||||
"Game with %{name}",
|
||||
name: opponent(@conn, game).name
|
||||
name: opponent(game, current_user(@conn).id).name
|
||||
),
|
||||
to: game_path(@conn, :show, game),
|
||||
class: "btn btn-default btn-xs" %>
|
||||
|
||||
@ -41,8 +41,16 @@ defmodule ChessWeb.GameView do
|
||||
current_user(conn).id == game.user_id && "white" || "black"
|
||||
end
|
||||
|
||||
def opponent(conn, game) do
|
||||
if current_user(conn).id == game.user_id do
|
||||
def player(game, user_id) do
|
||||
if game.user_id == user_id do
|
||||
"white"
|
||||
else
|
||||
"black"
|
||||
end
|
||||
end
|
||||
|
||||
def opponent(game, user_id) do
|
||||
if game.user_id == user_id do
|
||||
game.opponent
|
||||
else
|
||||
game.user
|
||||
|
||||
Loading…
Reference in New Issue
Block a user