summaryrefslogtreecommitdiff
path: root/lib/chessh/ssh/client/trongle_chat.ex
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-04 18:56:35 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-04 18:56:35 -0600
commit882e2c321ff88acac9894b488f1de109758f8f7d (patch)
treebc779ed8a9b23dc54cf1edd4a40e3396d8781c45 /lib/chessh/ssh/client/trongle_chat.ex
parent48dd80dbf745bd0bb0bd0186ad126d74f66b872b (diff)
downloadchessh-882e2c321ff88acac9894b488f1de109758f8f7d.tar.gz
chessh-882e2c321ff88acac9894b488f1de109758f8f7d.zip
initial prompt
Diffstat (limited to 'lib/chessh/ssh/client/trongle_chat.ex')
-rw-r--r--lib/chessh/ssh/client/trongle_chat.ex75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/chessh/ssh/client/trongle_chat.ex b/lib/chessh/ssh/client/trongle_chat.ex
new file mode 100644
index 0000000..30c1ec2
--- /dev/null
+++ b/lib/chessh/ssh/client/trongle_chat.ex
@@ -0,0 +1,75 @@
+defmodule Chessh.SSH.Client.TrongleChat do
+ require Logger
+ alias Chessh.{Player, Chat, Utils, Repo, PlayerSession}
+ import Ecto.Query
+
+ defmodule State do
+ defstruct client_pid: nil,
+ message: "",
+ player_session: nil,
+ chats: []
+ end
+
+ use Chessh.SSH.Client.Screen
+
+ defp get_initial_chats() do
+ from(c in Chat,
+ order_by: [desc: c.id],
+ limit: 100
+ )
+ |> Repo.all()
+ |> Repo.preload([:chatter])
+ end
+
+ def get_player(%PlayerSession{player_id: player_id} = player_session) do
+ Repo.get!(Player, player_id)
+ end
+
+ def init([%State{client_pid: client_pid, player_session: player_session} = state]) do
+ :syn.add_node_to_scopes([:chat])
+ :ok = :syn.join(:chat, {:tronglechat}, self())
+
+ send(client_pid, {:send_to_ssh, Utils.clear_codes()})
+
+ chats = get_initial_chats()
+
+ {:ok,
+ %State{
+ state
+ | chats: chats,
+ player_session: %PlayerSession{player_session | player: get_player(player_session)}
+ }}
+ end
+
+ def render(
+ width,
+ height,
+ %State{
+ client_pid: client_pid,
+ chats: chats,
+ message: message,
+ player_session: %PlayerSession{player: %Player{username: username}} = player_session
+ } = state
+ ) do
+ send(client_pid, {:send_to_ssh, [Utils.clear_codes(), username <> "> " <> message]})
+
+ state
+ end
+
+ def input(width, height, action, data, %State{message: message} = state) do
+ appended_message =
+ case action do
+ :backspace ->
+ %State{state | message: String.slice(message, 0..-2)}
+
+ _ ->
+ if String.match?(data, ~r/[a-zA-Z \.-]/) do
+ %State{state | message: message <> data}
+ else
+ state
+ end
+ end
+
+ render(width, height, appended_message)
+ end
+end