summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/game
diff options
context:
space:
mode:
authorSimponic <elizabeth.hunt@simponic.xyz>2023-01-28 22:56:10 -0700
committerSimponic <elizabeth.hunt@simponic.xyz>2023-01-28 22:56:10 -0700
commit593a631a564926b5b118805b8bea13a753e4757d (patch)
tree3f2a5460eda4ef19142b20386c7dee001f4675b5 /lib/chessh/ssh/client/game
parentbb66cd91a3e76e9b746750de51b5edd34b5d2259 (diff)
downloadchessh-593a631a564926b5b118805b8bea13a753e4757d.tar.gz
chessh-593a631a564926b5b118805b8bea13a753e4757d.zip
Ensure board is flipped when starting a game as dark, move some constants to environment variables, minor frontend changes:
Diffstat (limited to 'lib/chessh/ssh/client/game')
-rw-r--r--lib/chessh/ssh/client/game/game.ex87
-rw-r--r--lib/chessh/ssh/client/game/renderer.ex5
2 files changed, 50 insertions, 42 deletions
diff --git a/lib/chessh/ssh/client/game/game.ex b/lib/chessh/ssh/client/game/game.ex
index 11b00b5..3ecd2e4 100644
--- a/lib/chessh/ssh/client/game/game.ex
+++ b/lib/chessh/ssh/client/game/game.ex
@@ -41,6 +41,7 @@ defmodule Chessh.SSH.Client.Game do
| tail
])
when is_nil(color) do
+ # Joining a game
{is_dark, is_light} = {player_id == dark_player_id, player_id == light_player_id}
new_state =
@@ -58,6 +59,35 @@ defmodule Chessh.SSH.Client.Game do
end
def init([
+ %State{player_session: player_session, color: color, game: nil} = state
+ | tail
+ ]) do
+ # Starting a new game
+ {:ok, %Game{} = game} =
+ Game.changeset(
+ %Game{},
+ Map.merge(
+ if(color == :light,
+ do: %{light_player_id: player_session.player_id},
+ else: %{dark_player_id: player_session.player_id}
+ ),
+ %{
+ fen: @default_fen
+ }
+ )
+ )
+ |> Repo.insert()
+
+ init([
+ %State{
+ state
+ | game: game
+ }
+ | tail
+ ])
+ end
+
+ def init([
%State{
player_session: player_session,
color: color,
@@ -96,10 +126,9 @@ defmodule Chessh.SSH.Client.Game do
end
binbo_pid = initialize_game(game_id, fen)
- new_game = Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])
+ game = Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player])
- player_color =
- if(new_game.light_player_id == player_session.player_id, do: :light, else: :dark)
+ player_color = if(game.light_player_id == player_session.player_id, do: :light, else: :dark)
new_state =
(fn new_state ->
@@ -111,7 +140,7 @@ defmodule Chessh.SSH.Client.Game do
state
| binbo_pid: binbo_pid,
color: player_color,
- game: new_game,
+ game: game,
flipped: player_color == :dark
})
@@ -123,37 +152,6 @@ defmodule Chessh.SSH.Client.Game do
{:ok, new_state}
end
- def init([
- %State{player_session: player_session, color: color, client_pid: client_pid, game: nil} =
- state
- | _
- ]) do
- {:ok, %Game{id: game_id, fen: fen}} =
- Game.changeset(
- %Game{},
- Map.merge(
- if(color == :light,
- do: %{light_player_id: player_session.player_id},
- else: %{dark_player_id: player_session.player_id}
- ),
- %{
- fen: @default_fen
- }
- )
- )
- |> Repo.insert()
-
- binbo_pid = initialize_game(game_id, fen)
- send(client_pid, {:send_to_ssh, Utils.clear_codes()})
-
- {:ok,
- %State{
- state
- | game: Repo.get(Game, game_id) |> Repo.preload([:light_player, :dark_player]),
- binbo_pid: binbo_pid
- }}
- end
-
def handle_info(
{:new_move, move},
%State{
@@ -210,11 +208,20 @@ defmodule Chessh.SSH.Client.Game do
) do
new_cursor =
case action do
- :left -> %{y: cursor_y, x: Utils.wrap_around(cursor_x, -1, Renderer.chess_board_width())}
- :right -> %{y: cursor_y, x: Utils.wrap_around(cursor_x, 1, Renderer.chess_board_width())}
- :down -> %{y: Utils.wrap_around(cursor_y, 1, Renderer.chess_board_height()), x: cursor_x}
- :up -> %{y: Utils.wrap_around(cursor_y, -1, Renderer.chess_board_height()), x: cursor_x}
- _ -> cursor
+ :left ->
+ %{y: cursor_y, x: Utils.wrap_around(cursor_x, -1, Renderer.chess_board_width())}
+
+ :right ->
+ %{y: cursor_y, x: Utils.wrap_around(cursor_x, 1, Renderer.chess_board_width())}
+
+ :down ->
+ %{y: Utils.wrap_around(cursor_y, 1, Renderer.chess_board_height()), x: cursor_x}
+
+ :up ->
+ %{y: Utils.wrap_around(cursor_y, -1, Renderer.chess_board_height()), x: cursor_x}
+
+ _ ->
+ cursor
end
maybe_flipped_cursor_tup =
diff --git a/lib/chessh/ssh/client/game/renderer.ex b/lib/chessh/ssh/client/game/renderer.ex
index 17215ea..6d7d7a7 100644
--- a/lib/chessh/ssh/client/game/renderer.ex
+++ b/lib/chessh/ssh/client/game/renderer.ex
@@ -9,9 +9,10 @@ defmodule Chessh.SSH.Client.Game.Renderer do
@tile_width 7
@tile_height 4
- @previous_move_background ANSI.light_yellow_background()
+ @previous_move_background ANSI.light_magenta_background()
@from_select_background ANSI.light_green_background()
- @to_select_background ANSI.light_green_background()
+ @to_select_background ANSI.light_yellow_background()
+
@dark_piece_color ANSI.red()
@light_piece_color ANSI.light_cyan()