initial prompt
This commit is contained in:
parent
48dd80dbf7
commit
882e2c321f
19
lib/chessh/schema/chat.ex
Normal file
19
lib/chessh/schema/chat.ex
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
defmodule Chessh.Chat do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
alias Chessh.Player
|
||||||
|
|
||||||
|
schema "chats" do
|
||||||
|
field(:message, :string)
|
||||||
|
belongs_to(:chatter, Player, foreign_key: :chatter_id)
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(chat, attrs) do
|
||||||
|
chat
|
||||||
|
|> cast(attrs, [
|
||||||
|
:message,
|
||||||
|
:chatter_id
|
||||||
|
])
|
||||||
|
end
|
||||||
|
end
|
@ -111,7 +111,7 @@ defmodule Chessh.SSH.Client do
|
|||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
|
|
||||||
action ->
|
action ->
|
||||||
send(screen_pid, {:input, width, height, action})
|
send(screen_pid, {:input, width, height, action, data})
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -172,6 +172,7 @@ defmodule Chessh.SSH.Client do
|
|||||||
"\eOD" -> :left
|
"\eOD" -> :left
|
||||||
"\eOC" -> :right
|
"\eOC" -> :right
|
||||||
"\r" -> :return
|
"\r" -> :return
|
||||||
|
"\d" -> :backspace
|
||||||
x -> x
|
x -> x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -221,6 +221,7 @@ defmodule Chessh.SSH.Client.Game do
|
|||||||
_width,
|
_width,
|
||||||
_height,
|
_height,
|
||||||
action,
|
action,
|
||||||
|
_data,
|
||||||
%State{
|
%State{
|
||||||
move_from: move_from,
|
move_from: move_from,
|
||||||
cursor: %{x: cursor_x, y: cursor_y} = cursor,
|
cursor: %{x: cursor_x, y: cursor_y} = cursor,
|
||||||
|
@ -57,6 +57,7 @@ defmodule Chessh.SSH.Client.PreviousGame do
|
|||||||
_width,
|
_width,
|
||||||
_height,
|
_height,
|
||||||
action,
|
action,
|
||||||
|
_data,
|
||||||
%State{
|
%State{
|
||||||
move_idx: move_idx,
|
move_idx: move_idx,
|
||||||
flipped: flipped,
|
flipped: flipped,
|
||||||
|
@ -47,11 +47,12 @@ defmodule Chessh.SSH.Client.Game.PromotionScreen do
|
|||||||
def input(
|
def input(
|
||||||
_,
|
_,
|
||||||
_,
|
_,
|
||||||
action,
|
_,
|
||||||
|
data,
|
||||||
%State{client_pid: client_pid, game_pid: game_pid, game_state: %Game.State{} = game_state} =
|
%State{client_pid: client_pid, game_pid: game_pid, game_state: %Game.State{} = game_state} =
|
||||||
state
|
state
|
||||||
) do
|
) do
|
||||||
promotion = if Enum.member?(["q", "b", "n", "r"], action), do: action, else: nil
|
promotion = if Enum.member?(["q", "b", "n", "r"], data), do: data, else: nil
|
||||||
|
|
||||||
if promotion do
|
if promotion do
|
||||||
send(client_pid, {:go_back_one_screen, game_state})
|
send(client_pid, {:go_back_one_screen, game_state})
|
||||||
|
@ -34,7 +34,10 @@ defmodule Chessh.SSH.Client.MainMenu do
|
|||||||
%Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
|
%Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
|
||||||
{"Previous Games",
|
{"Previous Games",
|
||||||
{Chessh.SSH.Client.SelectPreviousGame,
|
{Chessh.SSH.Client.SelectPreviousGame,
|
||||||
%Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}}
|
%Chessh.SSH.Client.SelectPaginatePoller.State{player_session: player_session}}},
|
||||||
|
{"TrongleChat",
|
||||||
|
{Chessh.SSH.Client.TrongleChat,
|
||||||
|
%Chessh.SSH.Client.TrongleChat.State{player_session: player_session}}}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -119,6 +119,7 @@ defmodule Chessh.SSH.Client.SelectPaginatePoller do
|
|||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
action,
|
action,
|
||||||
|
_data,
|
||||||
%State{
|
%State{
|
||||||
client_pid: client_pid,
|
client_pid: client_pid,
|
||||||
options: options,
|
options: options,
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
defmodule Chessh.SSH.Client.Screen do
|
defmodule Chessh.SSH.Client.Screen do
|
||||||
@callback render(width :: integer(), height :: integer(), state :: any()) :: any()
|
@callback render(width :: integer(), height :: integer(), state :: any()) :: any()
|
||||||
@callback input(width :: integer(), height :: integer(), action :: any(), state :: any()) ::
|
@callback input(
|
||||||
|
width :: integer(),
|
||||||
|
height :: integer(),
|
||||||
|
action :: any(),
|
||||||
|
data :: String.t(),
|
||||||
|
state :: any()
|
||||||
|
) ::
|
||||||
any()
|
any()
|
||||||
|
|
||||||
defmacro __using__(_) do
|
defmacro __using__(_) do
|
||||||
@ -11,8 +17,8 @@ defmodule Chessh.SSH.Client.Screen do
|
|||||||
def handle_info({:render, width, height}, state),
|
def handle_info({:render, width, height}, state),
|
||||||
do: {:noreply, render(width, height, state)}
|
do: {:noreply, render(width, height, state)}
|
||||||
|
|
||||||
def handle_info({:input, width, height, action}, state),
|
def handle_info({:input, width, height, action, data}, state),
|
||||||
do: {:noreply, input(width, height, action, state)}
|
do: {:noreply, input(width, height, action, data, state)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
75
lib/chessh/ssh/client/trongle_chat.ex
Normal file
75
lib/chessh/ssh/client/trongle_chat.ex
Normal file
@ -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
|
12
priv/repo/migrations/20231004233032_add_trongle_chat.exs
Normal file
12
priv/repo/migrations/20231004233032_add_trongle_chat.exs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
defmodule Chessh.Repo.Migrations.AddTrongleChat do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table(:chats) do
|
||||||
|
add(:message, :string, null: false)
|
||||||
|
add(:chatter_id, references(:players), null: false)
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user