Skip to content

Commit cf2fe88

Browse files
committed
feat: ロビーで匿名で好きなコメントを書ける機能を追加
1 parent cda2cb3 commit cf2fe88

16 files changed

Lines changed: 367 additions & 156 deletions

File tree

app/assets/stylesheets/chat.css

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* chat-messages */
2+
.chat-messages {
3+
flex: 1;
4+
flex-grow: 1;
5+
padding: 5px;
6+
padding-left: 20px;
7+
overflow-y: auto;/* 縦スクロールを有効 */
8+
overflow-x: hidden; /* 横スクロールは無効 */
9+
/*background-color: #eef1f5;*/
10+
/*display: flex;
11+
flex-direction: column;
12+
justify-content: flex-end;
13+
align-items: flex-start;
14+
gap: 1px;*/
15+
/* align-items: flex-end; 交差軸(縦方向)の終端に寄せる(右下隅にしたい場合) */
16+
/*max-height: 300px;
17+
height:100px;*/
18+
max-height: calc(100% - 80px); /* フォームの高さ分を差し引く */
19+
}
20+
.chat-message {
21+
margin-bottom: 0; /* gapで間隔を制御するためmarginを削除 */
22+
padding: 10px;
23+
border-radius: 5px;
24+
max-width: 80%; /* メッセージの最大幅を制限 */
25+
word-wrap: break-word; /* 長い文字列を折り返し */
26+
align-self: flex-start; /* 個別に左寄せを確実にする */
27+
flex-shrink: 0; /* メッセージが縮まないようにする */
28+
}
29+
/* chat-form */
30+
.chat-form {
31+
margin: 0;
32+
display: flex;
33+
padding: 10px;
34+
border-top: 1px solid #ddd;
35+
background-color: #ffffff;
36+
}
37+
/* chat-input */
38+
.chat-input {
39+
flex: 1;
40+
flex-grow: 1;
41+
padding: 10px 12px;
42+
border: 1px solid #c9c9c9;
43+
border-radius: 20px;
44+
font-size: 0.9rem;
45+
margin-right: 10px;
46+
outline: none;
47+
}
48+
49+
.chat-input::placeholder {
50+
color: #a0a0a0;
51+
}
52+
53+
/* chat-button */
54+
.chat-button {
55+
padding: 10px 20px;
56+
background-color: #007bff;
57+
color: white;
58+
border: none;
59+
border-radius: 20px;
60+
font-size: 1rem;
61+
cursor: pointer;
62+
transition: background-color 0.2s ease-in-out;
63+
}
64+
.chat-button:hover {
65+
background-color: #0056b3;
66+
}
67+
.chat-button:active {
68+
background-color: #004085;
69+
}
70+
71+
72+
/* 非表示時のチャットコンテナのスタイル */
73+
.chat-container.closed {
74+
transform: translateX(calc(100% - 20px)); /* 右端に隠れるように移動 (ボタンの幅に合わせて調整) */
75+
/*opacity: 0.5; 半透明にする */
76+
pointer-events: none; /* クリックイベントを無効にする */
77+
}
78+
/* 開閉ボタンのスタイル */
79+
.chat-toggle-button {
80+
position: absolute;
81+
left: -15px;
82+
width: 32px;
83+
height: 32px;
84+
color: white;
85+
border: none;
86+
border-radius: 6px;
87+
cursor: pointer;
88+
display: flex;
89+
align-items: center;
90+
justify-content: center;
91+
font-size: 16px;
92+
z-index: 10; /* メッセージより上に表示 */
93+
transition: background-color 0.2s;
94+
/*position: fixed;
95+
bottom: 30px;
96+
right: 20px;
97+
background-color: #007bff;
98+
color: white;
99+
border: none;
100+
border-radius: 50%;
101+
width: 40px;
102+
height: 40px;
103+
font-size: 1.5rem;
104+
display: flex;
105+
align-items: center;
106+
justify-content: center;
107+
cursor: pointer;
108+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
109+
z-index: 1001;*/ /* チャットコンテナ (z-index: 1000) より手前に */
110+
}
111+
/* ボタンがチャットコンテナの隣に位置する場合のスタイル調整 */
112+
.chat-container.closed + .chat-toggle-button {
113+
right: 20px; /* チャットコンテナが閉じている時はボタンが右端に */
114+
}
115+
.chat-container:not(.closed) + .chat-toggle-button {
116+
/* チャットコンテナが開いている時はボタンをチャットコンテナの左端近くに */
117+
/* chat-containerの幅(20vw)からボタンの幅(40px)を引いて、右端からの位置を計算 */
118+
/*right: calc(20vw + 20px);*/
119+
right: calc(0vw + 20px);
120+
}
121+
122+
@keyframes fadeIn {
123+
from { opacity: 0; transform: translateY(20px); }
124+
to { opacity: 1; transform: translateY(0); }
125+
}
126+
.animate-fade-in {
127+
animation: fadeIn 0.8s ease-out forwards;
128+
}

app/assets/stylesheets/game_lobby_matching_board.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,22 @@
1515
100% { transform: rotate(360deg); }
1616
}
1717

18+
.chat-container {
19+
display: flex;
20+
flex-direction: column;
21+
bottom: 0px;
22+
right: 0px;
23+
height: 50vh; /* ビューポートの高さの50% */
24+
25+
border: 1px solid #ccc;
26+
border-radius: 8px;
27+
font-family: Arial, sans-serif;
28+
background-color: #f9f9f9;
29+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
1830

31+
transition: transform 0.3s ease-out, opacity 0.3s ease-out; /* アニメーションを追加 */
32+
position: relative;
33+
}
1934

2035

2136
.btn {

app/assets/stylesheets/shogi.css

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -204,135 +204,3 @@ img#後手 {
204204

205205

206206

207-
/*チャット*/
208-
209-
/* chat-messages */
210-
.chat-messages {
211-
flex: 1;
212-
flex-grow: 1;
213-
padding: 5px;
214-
padding-left: 20px;
215-
overflow-y: auto;/* 縦スクロールを有効 */
216-
overflow-x: hidden; /* 横スクロールは無効 */
217-
/*background-color: #eef1f5;*/
218-
/*display: flex;
219-
flex-direction: column;
220-
justify-content: flex-end;
221-
align-items: flex-start;
222-
gap: 1px;*/
223-
/* align-items: flex-end; 交差軸(縦方向)の終端に寄せる(右下隅にしたい場合) */
224-
/*max-height: 300px;
225-
height:100px;*/
226-
max-height: calc(100% - 80px); /* フォームの高さ分を差し引く */
227-
}
228-
.chat-message {
229-
margin-bottom: 0; /* gapで間隔を制御するためmarginを削除 */
230-
padding: 10px;
231-
border-radius: 5px;
232-
max-width: 80%; /* メッセージの最大幅を制限 */
233-
word-wrap: break-word; /* 長い文字列を折り返し */
234-
align-self: flex-start; /* 個別に左寄せを確実にする */
235-
flex-shrink: 0; /* メッセージが縮まないようにする */
236-
}
237-
/* chat-form */
238-
.chat-form {
239-
margin: 0;
240-
display: flex;
241-
padding: 10px;
242-
border-top: 1px solid #ddd;
243-
background-color: #ffffff;
244-
}
245-
/* chat-input */
246-
.chat-input {
247-
flex: 1;
248-
flex-grow: 1;
249-
padding: 10px 12px;
250-
border: 1px solid #c9c9c9;
251-
border-radius: 20px;
252-
font-size: 0.9rem;
253-
margin-right: 10px;
254-
outline: none;
255-
}
256-
257-
.chat-input::placeholder {
258-
color: #a0a0a0;
259-
}
260-
261-
/* chat-button */
262-
.chat-button {
263-
padding: 10px 20px;
264-
background-color: #007bff;
265-
color: white;
266-
border: none;
267-
border-radius: 20px;
268-
font-size: 1rem;
269-
cursor: pointer;
270-
transition: background-color 0.2s ease-in-out;
271-
}
272-
.chat-button:hover {
273-
background-color: #0056b3;
274-
}
275-
.chat-button:active {
276-
background-color: #004085;
277-
}
278-
279-
280-
/* 非表示時のチャットコンテナのスタイル */
281-
.chat-container.closed {
282-
transform: translateX(calc(100% - 20px)); /* 右端に隠れるように移動 (ボタンの幅に合わせて調整) */
283-
/*opacity: 0.5; 半透明にする */
284-
pointer-events: none; /* クリックイベントを無効にする */
285-
}
286-
/* 開閉ボタンのスタイル */
287-
.chat-toggle-button {
288-
position: absolute;
289-
left: -15px;
290-
width: 32px;
291-
height: 32px;
292-
color: white;
293-
border: none;
294-
border-radius: 6px;
295-
cursor: pointer;
296-
display: flex;
297-
align-items: center;
298-
justify-content: center;
299-
font-size: 16px;
300-
z-index: 10; /* メッセージより上に表示 */
301-
transition: background-color 0.2s;
302-
/*position: fixed;
303-
bottom: 30px;
304-
right: 20px;
305-
background-color: #007bff;
306-
color: white;
307-
border: none;
308-
border-radius: 50%;
309-
width: 40px;
310-
height: 40px;
311-
font-size: 1.5rem;
312-
display: flex;
313-
align-items: center;
314-
justify-content: center;
315-
cursor: pointer;
316-
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
317-
z-index: 1001;*/ /* チャットコンテナ (z-index: 1000) より手前に */
318-
}
319-
/* ボタンがチャットコンテナの隣に位置する場合のスタイル調整 */
320-
.chat-container.closed + .chat-toggle-button {
321-
right: 20px; /* チャットコンテナが閉じている時はボタンが右端に */
322-
}
323-
.chat-container:not(.closed) + .chat-toggle-button {
324-
/* チャットコンテナが開いている時はボタンをチャットコンテナの左端近くに */
325-
/* chat-containerの幅(20vw)からボタンの幅(40px)を引いて、右端からの位置を計算 */
326-
/*right: calc(20vw + 20px);*/
327-
right: calc(0vw + 20px);
328-
}
329-
330-
331-
332-
@keyframes fadeIn {
333-
from { opacity: 0; transform: translateY(20px); }
334-
to { opacity: 1; transform: translateY(0); }
335-
}
336-
.animate-fade-in {
337-
animation: fadeIn 0.8s ease-out forwards;
338-
}

app/channels/matching_channel.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ def unsubscribed
2222
Rails.logger.info "MatchingStatusChannel が解除されました。#{params[:identifier]}"
2323
end
2424

25+
def chat_save_and_broadcast(data)
26+
chat_data = data['chat_data']
27+
@comment = LobbyComment.new( content: chat_data )
28+
if @comment.save
29+
#レコードは10件まで保存でき、データが追加されると古いものから順に消す
30+
comments_record_count = LobbyComment.count # 現在のレコード数を取得
31+
if comments_record_count > 10 # レコード数が10件を超えた場合
32+
#oldest_comments = LobbyComment.order(created_at: :asc).offset(10) # 作成日時が古いものから数えて、余分なレコードを取得・ oldest_commentsは、created_atで昇順に並べたレコードの11件目以降を取得
33+
oldest_comments = LobbyComment.order(created_at: :desc).offset(10) # 作成日時が古いものから数えて、余分なレコードを取得・ oldest_commentsは、created_atで昇順に並べたレコードの11件目以降を取得
34+
puts "oldest_comments: #{oldest_comments}"
35+
oldest_comments.each do |comment| # 取得したレコードをすべて削除
36+
puts "oldest_comment: #{comment}"
37+
comment.destroy
38+
end
39+
end
40+
updated_db_stored_data = LobbyComment.order(created_at: :desc)
41+
ActionCable.server.broadcast( "matching_status",{ data_type: "chat_update", chat_data: updated_db_stored_data.to_json}) # 取得したデータをクライアントにブロードキャスト
42+
else
43+
Rails.logger.info "チャットセーブ失敗"
44+
end
45+
end
46+
2547
#相手に一応マッチ成功の通知
2648
def reNotificationEnemy(data)
2749
game_room_data = data['game_room_data']

app/channels/shogi_game_channel.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ def board_broadcast_and_store(data)
5050

5151
redis_key = "shogi_game:#{@game_id}"
5252
routing_key = "game.#{@room_id}.board_update"
53-
54-
#Rails.logger.info "Redisに値をセット・new_data: #{new_board_data}"
55-
#Rails.logger.info "BoardInfo: #{boardInfo}"
56-
#Rails.logger.info "@room_id: #{@room_id}・@game_id: #{@game_id}・redis_key: #{redis_key}"
57-
5853
$redis.set(redis_key, new_board_data.to_json)#Redisに値をセット
5954
$redis.expire(redis_key, 10) #時間経過後に自動削除
6055

@@ -71,12 +66,6 @@ def chat_broadcast_and_store(data)
7166
redis_chat_key = "shogi_game_chat:#{game_id}"
7267
updated_redis_stored_data=""
7368
if $redis.exists?(redis_chat_key)
74-
#redis_stored_data = $redis.get(redis_chat_key)
75-
#Rails.logger.info "チャット・chat_data: #{redichat_datas_stored_data}"
76-
#parsed_redis_stored_data = JSON.parse(redis_stored_data)#JSON文字列をRubyのハッシュにパース
77-
#parsed_redis_stored_data["chatMessages"] = chat_data# 既存のハッシュにチャットデータを追加/ここでは、chatMessagesという新しいキーでチャットデータを追加します。
78-
#updated_redis_stored_data = parsed_redis_stored_data.to_json#更新されたハッシュを再度JSON文字列に変換(必要に応じて)
79-
#$redis.set(redis_chat_key,updated_redis_stored_data)#Redisに値をセット
8069
$redis.rpush(redis_chat_key, chat_data)
8170
$redis.expire(redis_chat_key, DELETE_TIME)#時間経過後に自動削除
8271
updated_redis_stored_data = $redis.lrange(redis_chat_key, 0, -1) #キーをリスト型としてデータ取得

app/controllers/matching_controller.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def game_lobby_matching_board
1010
zeitwerk_enabled = Rails.autoloaders.zeitwerk_enabled?# Zeitwerkモードが有効化されているかを取得
1111
Rails.logger.info "Zeitwerk enabled: #{zeitwerk_enabled}"
1212

13+
#@comments = LobbyComment.all.to_json
14+
@comments = LobbyComment.order(created_at: :desc)
15+
puts "@comments: #{@comments}"
16+
#puts "@comments: #{@comments.to_json}"
17+
18+
1319
@matching_queue_length = $redis.llen(MATCHING_QUEUE_KEY)#現在のマッチング待ち人数を確認・キューの長さを確認
1420
@matching_queue_data = $redis.lrange(MATCHING_QUEUE_KEY, 0, -1)# 既存のキューから全てのデータを取得
1521

@@ -302,9 +308,24 @@ def contacts
302308
end
303309
end
304310

311+
=begin
312+
def lobby_comment_create
313+
@comment = LobbyComment.new(lobby_comment_params)
314+
if @comment.save
315+
render json: @comment, status: :created
316+
else
317+
render json: @comment.errors, status: :unprocessable_entity
318+
end
319+
end
320+
=end
305321
private
306322

307323
def contact_params
308324
params.require(:contact).permit(:message)
309325
end
326+
=begin
327+
def lobby_comment_params
328+
params.require(:lobby_comment).permit(:content)
329+
end
330+
=end
310331
end

app/javascript/components/shogi/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ class Room extends React.Component {
616616
alert("チャットサーバーに接続されていません。");
617617
}
618618
}
619-
620619
//チャットの開閉の表示を切り替えるメソッド
621620
toggleChat() {
622621
this.setState(prevState => ({

0 commit comments

Comments
 (0)