summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/menus
diff options
context:
space:
mode:
authorLizzy Hunt <elizabeth.hunt@simponic.xyz>2023-03-13 14:13:11 -0600
committerGitHub <noreply@github.com>2023-03-13 14:13:11 -0600
commita1f01d4a2b1ce2a167b7fec1506f3970bf13ea79 (patch)
tree3eb1a991569da428a77155b6e6bb601d00c3eb98 /lib/chessh/ssh/client/menus
parent3cb88323c22e72f36d82ae5ada384311123eae67 (diff)
downloadchessh-a1f01d4a2b1ce2a167b7fec1506f3970bf13ea79.tar.gz
chessh-a1f01d4a2b1ce2a167b7fec1506f3970bf13ea79.zip
Move history (#18)
* store move history * Add previous game viewer
Diffstat (limited to 'lib/chessh/ssh/client/menus')
-rw-r--r--lib/chessh/ssh/client/menus/main_menu.ex33
-rw-r--r--lib/chessh/ssh/client/menus/select_previous_game.ex102
2 files changed, 120 insertions, 15 deletions
diff --git a/lib/chessh/ssh/client/menus/main_menu.ex b/lib/chessh/ssh/client/menus/main_menu.ex
index 09aea14..ee4b976 100644
--- a/lib/chessh/ssh/client/menus/main_menu.ex
+++ b/lib/chessh/ssh/client/menus/main_menu.ex
@@ -4,13 +4,13 @@ defmodule Chessh.SSH.Client.MainMenu do
require Logger
- @logo " Simponic's
- dP MP\"\"\"\"\"\"`MM MP\"\"\"\"\"\"`MM M\"\"MMMMM\"\"MM
- 88 M mmmmm..M M mmmmm..M M MMMMM MM
-.d8888b. 88d888b. .d8888b. M. `YM M. `YM M `M
-88' `\"\" 88' `88 88ooood8 MMMMMMM. M MMMMMMM. M M MMMMM MM
-88. ... 88 88 88. ... M. .MMM' M M. .MMM' M M MMMMM MM
-`88888P' dP dP `88888P' Mb. .dM Mb. .dM M MMMMM MM
+ @logo " Simponic's
+ dP MP\"\"\"\"\"\"`MM MP\"\"\"\"\"\"`MM M\"\"MMMMM\"\"MM
+ 88 M mmmmm..M M mmmmm..M M MMMMM MM
+.d8888b. 88d888b. .d8888b. M. `YM M. `YM M `M
+88' `\"\" 88' `88 88ooood8 MMMMMMM. M MMMMMMM. M M MMMMM MM
+88. ... 88 88 88. ... M. .MMM' M M. .MMM' M M MMMMM MM
+`88888P' dP dP `88888P' Mb. .dM Mb. .dM M MMMMM MM
MMMMMMMMMMM MMMMMMMMMMM MMMMMMMMMMMM" |> String.split("\n")
@logo_cols @logo |> Enum.map(&String.length/1) |> Enum.max()
@@ -18,24 +18,27 @@ defmodule Chessh.SSH.Client.MainMenu do
def dynamic_options(), do: false
def tick_delay_ms(), do: 1000
- def max_displayed_options(), do: 4
+ def max_displayed_options(), do: 5
def max_box_cols(), do: @logo_cols
def title(), do: @logo ++ ["- Connected on: #{System.get_env("NODE_ID")}"]
def initial_options(%State{player_session: %PlayerSession{} = player_session}) do
[
+ {"My Current Games",
+ {Chessh.SSH.Client.SelectCurrentGame,
+ %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
+ {"Joinable Games (lobby)",
+ {Chessh.SSH.Client.SelectJoinableGame,
+ %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
+ {"Previous Games",
+ {Chessh.SSH.Client.SelectPreviousGame,
+ %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
{"Start A Game (Light)",
{Chessh.SSH.Client.Game,
%Chessh.SSH.Client.Game.State{player_session: player_session, color: :light}}},
{"Start A Game (Dark)",
{Chessh.SSH.Client.Game,
- %Chessh.SSH.Client.Game.State{player_session: player_session, color: :dark}}},
- {"Current Games",
- {Chessh.SSH.Client.SelectCurrentGame,
- %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
- {"Joinable Games (lobby)",
- {Chessh.SSH.Client.SelectJoinableGame,
- %Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}}
+ %Chessh.SSH.Client.Game.State{player_session: player_session, color: :dark}}}
]
end
diff --git a/lib/chessh/ssh/client/menus/select_previous_game.ex b/lib/chessh/ssh/client/menus/select_previous_game.ex
new file mode 100644
index 0000000..5f55c3d
--- /dev/null
+++ b/lib/chessh/ssh/client/menus/select_previous_game.ex
@@ -0,0 +1,102 @@
+defmodule Chessh.SSH.Client.SelectPreviousGame do
+ alias Chessh.{Utils, Repo, Game, PlayerSession}
+ alias Chessh.SSH.Client.GameSelector
+ import Ecto.Query
+ require Logger
+
+ use Chessh.SSH.Client.SelectPaginatePoller
+
+ def refresh_options_ms(), do: 4000
+ def max_displayed_options(), do: 5
+ def title(), do: ["-- Previous Games --"]
+ def dynamic_options(), do: true
+
+ def get_player_sorted_current_games_with_id(player_id, current_id \\ nil, direction \\ :desc) do
+ GameSelector.paginate_ish_query(
+ Game
+ |> where([g], g.status != :continue)
+ |> where([g], g.light_player_id == ^player_id or g.dark_player_id == ^player_id)
+ |> limit(^max_displayed_options()),
+ current_id,
+ direction
+ )
+ end
+
+ def format_game_selection_tuple(%Game{id: game_id} = game) do
+ {Chessh.SSH.Client.Game.Renderer.make_status_line(game, false), game_id}
+ end
+
+ def next_page_options(%State{
+ options: options,
+ player_session: %PlayerSession{player_id: player_id}
+ }) do
+ {_label, previous_last_game_id} = List.last(options)
+ next_games = get_player_sorted_current_games_with_id(player_id, previous_last_game_id, :desc)
+
+ if length(next_games) > 0,
+ do:
+ next_games
+ |> Enum.map(&format_game_selection_tuple/1),
+ else: options
+ end
+
+ def previous_page_options(%State{
+ options: options,
+ player_session: %PlayerSession{player_id: player_id}
+ }) do
+ {_label, previous_first_game_id} = List.first(options)
+
+ previous_games =
+ get_player_sorted_current_games_with_id(player_id, previous_first_game_id, :asc)
+
+ if length(previous_games) > 0,
+ do:
+ previous_games
+ |> Enum.map(&format_game_selection_tuple/1),
+ else: options
+ end
+
+ def initial_options(%State{player_session: %PlayerSession{player_id: player_id}}) do
+ get_player_sorted_current_games_with_id(player_id)
+ |> 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)
+ 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) do
+ game = Repo.get(Game, selected_id)
+
+ {Chessh.SSH.Client.PreviousGame,
+ %Chessh.SSH.Client.PreviousGame.State{
+ game: game
+ }}
+ end
+end