From 593a631a564926b5b118805b8bea13a753e4757d Mon Sep 17 00:00:00 2001 From: Simponic Date: Sat, 28 Jan 2023 22:56:10 -0700 Subject: Ensure board is flipped when starting a game as dark, move some constants to environment variables, minor frontend changes: --- lib/chessh/ssh/client/game/game.ex | 87 ++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'lib/chessh/ssh/client/game/game.ex') 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 = @@ -57,6 +58,35 @@ defmodule Chessh.SSH.Client.Game do init([new_state | tail]) 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, @@ -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 = -- cgit v1.2.3-70-g09d2