diff options
Diffstat (limited to 'lib/chessh/ssh/client/menus')
-rw-r--r-- | lib/chessh/ssh/client/menus/select_current_game.ex | 22 | ||||
-rw-r--r-- | lib/chessh/ssh/client/menus/select_joinable_game.ex | 35 | ||||
-rw-r--r-- | lib/chessh/ssh/client/menus/select_paginate_poller.ex | 8 |
3 files changed, 48 insertions, 17 deletions
diff --git a/lib/chessh/ssh/client/menus/select_current_game.ex b/lib/chessh/ssh/client/menus/select_current_game.ex index ff1eb30..3c47b15 100644 --- a/lib/chessh/ssh/client/menus/select_current_game.ex +++ b/lib/chessh/ssh/client/menus/select_current_game.ex @@ -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 diff --git a/lib/chessh/ssh/client/menus/select_joinable_game.ex b/lib/chessh/ssh/client/menus/select_joinable_game.ex index 7c477c0..f2b7b1f 100644 --- a/lib/chessh/ssh/client/menus/select_joinable_game.ex +++ b/lib/chessh/ssh/client/menus/select_joinable_game.ex @@ -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{ diff --git a/lib/chessh/ssh/client/menus/select_paginate_poller.ex b/lib/chessh/ssh/client/menus/select_paginate_poller.ex index c6f9e1d..adca697 100644 --- a/lib/chessh/ssh/client/menus/select_paginate_poller.ex +++ b/lib/chessh/ssh/client/menus/select_paginate_poller.ex @@ -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 |