Fix initial render when game joined by creating new state in genserver init, also some more color changes
This commit is contained in:
parent
ab5fc4a077
commit
5220ac5823
@ -96,22 +96,22 @@ 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])
|
||||
|
||||
player_color =
|
||||
if(new_game.light_player_id == player_session.player_id, do: :light, else: :dark)
|
||||
|
||||
send(client_pid, {:send_to_ssh, Utils.clear_codes()})
|
||||
|
||||
{:ok,
|
||||
%State{
|
||||
new_state = %State{
|
||||
state
|
||||
| binbo_pid: binbo_pid,
|
||||
color: player_color,
|
||||
game: new_game,
|
||||
flipped: player_color == :dark
|
||||
}}
|
||||
}
|
||||
|
||||
send(client_pid, {:send_to_ssh, [Utils.clear_codes() | render_state(new_state)]})
|
||||
|
||||
{:ok, new_state}
|
||||
end
|
||||
|
||||
def init([
|
||||
|
@ -10,10 +10,10 @@ defmodule Chessh.SSH.Client.Game.Renderer do
|
||||
@tile_height 4
|
||||
|
||||
@previous_move_background ANSI.light_yellow_background()
|
||||
@from_select_background ANSI.light_green_background()
|
||||
@to_select_background ANSI.green_background()
|
||||
@dark_piece_color ANSI.light_red()
|
||||
@light_piece_color ANSI.light_blue()
|
||||
@from_select_background ANSI.light_magenta_background()
|
||||
@to_select_background ANSI.light_magenta_background()
|
||||
@dark_piece_color ANSI.red()
|
||||
@light_piece_color ANSI.light_cyan()
|
||||
|
||||
def chess_board_height(), do: @chess_board_height
|
||||
def chess_board_width(), do: @chess_board_width
|
||||
|
@ -7,7 +7,7 @@ defmodule Chessh.SSH.Client.SelectCurrentGame do
|
||||
use Chessh.SSH.Client.SelectPaginatePoller
|
||||
|
||||
def refresh_options_ms(), do: 4000
|
||||
def max_displayed_options(), do: 10
|
||||
def max_displayed_options(), do: 5
|
||||
def title(), do: ["-- Current Games --"]
|
||||
def dynamic_options(), do: true
|
||||
|
||||
@ -71,6 +71,26 @@ defmodule Chessh.SSH.Client.SelectCurrentGame do
|
||||
|> Enum.map(&format_game_selection_tuple/1)
|
||||
end
|
||||
|
||||
def refresh_options(%State{
|
||||
options: options,
|
||||
player_session: %PlayerSession{player_id: player_id}
|
||||
}) do
|
||||
previous_last_game_id =
|
||||
case List.last(options) do
|
||||
{_label, id} -> id
|
||||
_ -> 0
|
||||
end
|
||||
|
||||
current_screen_games =
|
||||
get_player_sorted_current_games_with_id(player_id, previous_last_game_id - 1, :asc)
|
||||
|
||||
if !is_nil(current_screen_games) && length(current_screen_games),
|
||||
do:
|
||||
current_screen_games
|
||||
|> Enum.map(&format_game_selection_tuple/1),
|
||||
else: options
|
||||
end
|
||||
|
||||
def make_process_tuple(selected_id, %State{
|
||||
player_session: player_session
|
||||
}) do
|
||||
|
@ -7,6 +7,7 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
|
||||
|
||||
def refresh_options_ms(), do: 4000
|
||||
def max_displayed_options(), do: 10
|
||||
def tick_delay_ms(), do: 600
|
||||
def title(), do: ["-- Joinable Games --"]
|
||||
def dynamic_options(), do: true
|
||||
|
||||
@ -16,8 +17,8 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
|
||||
|> where([g], g.status == :continue)
|
||||
|> where(
|
||||
[g],
|
||||
(is_nil(g.dark_player_id) or g.dark_player_id != ^player_id) and
|
||||
(is_nil(g.light_player_id) or g.light_player_id != ^player_id)
|
||||
(is_nil(g.dark_player_id) or is_nil(g.light_player_id)) and
|
||||
(g.dark_player_id != ^player_id or g.light_player_id != ^player_id)
|
||||
)
|
||||
|> limit(^max_displayed_options()),
|
||||
current_id,
|
||||
@ -36,7 +37,7 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
|
||||
{_label, previous_last_game_id} = List.last(options)
|
||||
next_games = get_player_joinable_games_with_id(player_id, previous_last_game_id, :desc)
|
||||
|
||||
if length(next_games) > 0,
|
||||
if !is_nil(next_games) && length(next_games) > 0,
|
||||
do:
|
||||
next_games
|
||||
|> Enum.map(&format_game_selection_tuple/1),
|
||||
@ -51,7 +52,7 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
|
||||
|
||||
previous_games = get_player_joinable_games_with_id(player_id, previous_first_game_id, :asc)
|
||||
|
||||
if length(previous_games) > 0,
|
||||
if !is_nil(previous_games) && length(previous_games) > 0,
|
||||
do:
|
||||
previous_games
|
||||
|> Enum.map(&format_game_selection_tuple/1),
|
||||
@ -63,14 +64,24 @@ defmodule Chessh.SSH.Client.SelectJoinableGame do
|
||||
|> Enum.map(&format_game_selection_tuple/1)
|
||||
end
|
||||
|
||||
def refresh_options(%State{options: options}) do
|
||||
from(g in Game,
|
||||
where: g.id in ^Enum.map(options, fn {_, id} -> id end),
|
||||
order_by: [desc: g.id]
|
||||
)
|
||||
|> Repo.all()
|
||||
|> Repo.preload([:light_player, :dark_player])
|
||||
|> Enum.map(&format_game_selection_tuple/1)
|
||||
def refresh_options(%State{
|
||||
options: options,
|
||||
player_session: %PlayerSession{player_id: player_id}
|
||||
}) do
|
||||
previous_last_game_id =
|
||||
case List.last(options) do
|
||||
{_label, id} -> id
|
||||
_ -> 0
|
||||
end
|
||||
|
||||
current_screen_games =
|
||||
get_player_joinable_games_with_id(player_id, previous_last_game_id - 1, :asc)
|
||||
|
||||
if !is_nil(current_screen_games) && length(current_screen_games),
|
||||
do:
|
||||
current_screen_games
|
||||
|> Enum.map(&format_game_selection_tuple/1),
|
||||
else: options
|
||||
end
|
||||
|
||||
def make_process_tuple(selected_id, %State{
|
||||
|
@ -189,7 +189,7 @@ defmodule Chessh.SSH.Client.SelectPaginatePoller do
|
||||
title() ++
|
||||
[""] ++
|
||||
render_lines(width, height, state) ++
|
||||
if dynamic_options(), do: ["", "<= Previous | Next =>"], else: []
|
||||
if dynamic_options(), do: ["", "<- Previous | Next ->"], else: []
|
||||
|
||||
{y, x} = Utils.center_rect({min(width, max_box_cols()), length(lines)}, {width, height})
|
||||
|
||||
@ -214,12 +214,12 @@ defmodule Chessh.SSH.Client.SelectPaginatePoller do
|
||||
Enum.map(
|
||||
Enum.zip(0..(max_displayed_options() - 1), options),
|
||||
fn {i, {line, _}} ->
|
||||
box_cols = min(max_box_cols(), width)
|
||||
box_cols = min(max_box_cols(), width) - 2
|
||||
linelen = String.length(line)
|
||||
|
||||
line =
|
||||
if linelen > box_cols do
|
||||
delta = max(box_cols - 3 - 1, 0)
|
||||
delta = max(box_cols - 3 - 1 - if(i == selected_option_idx, do: 4, else: 0), 0)
|
||||
overflow = linelen - delta
|
||||
start = if i == selected_option_idx, do: rem(tick, overflow), else: 0
|
||||
"#{String.slice(line, start..(start + delta))}..."
|
||||
@ -238,7 +238,7 @@ defmodule Chessh.SSH.Client.SelectPaginatePoller do
|
||||
end
|
||||
)
|
||||
else
|
||||
["Looks like there's nothing here.", "Use Ctrl+b to go back"]
|
||||
["Looks like there's nothing here.", "Use Ctrl+b to go back."]
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user