Skip to content

Commit ac1ff6b

Browse files
committed
feat: AI対戦機能、棋譜解析、成りダイアログ、自殺手エラー、など
1 parent cd737bc commit ac1ff6b

15 files changed

Lines changed: 4429 additions & 240 deletions

File tree

app/assets/stylesheets/shogi.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ header {
123123

124124

125125
#未選択.square {
126-
background: rgba(0, 0, 0, .5);
126+
/*background: rgba(0, 0, 0, .5);*/
127127
}
128128

129129
#選択状態.square {
130130
background: red;
131131
}
132132

133133
#配置可能.square {
134-
background: yellow;
134+
background: #7bd710;
135135
}
136136

137137
#持駒.square {
138-
background: darkgoldenrod;
138+
/*background: darkgoldenrod;*/
139139
}
140140

141141
img#後手 {

app/channels/matching_channel.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class MatchingChannel < ApplicationCable::Channel
22
MATCHING_QUEUE_KEY = 'shogi:matching_queue'
33
#DELETE_TIME=20
4-
DELETE_TIME = 30 * 60 #30分を秒単位で定義・30分 * 60秒 = 1800秒
4+
#DELETE_TIME = 30 * 60 #30分を秒単位で定義・30分 * 60秒 = 1800秒
5+
DELETE_TIME = 60 * 500 #8時間20分・500分・60秒 x 500秒 = 30000秒
56

67
def subscribed
78
identifier = params[:identifier]

app/channels/shogi_game_channel.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class ShogiGameChannel < ApplicationCable::Channel
22
#DELETE_TIME=10
3-
DELETE_TIME = 30 * 60 #30分を秒単位で定義・30分 * 60秒 = 1800秒
3+
#DELETE_TIME = 30 * 60 #30分を秒単位で定義・30秒 x 60秒 = 1800秒
4+
DELETE_TIME = 60 * 500 #8時間20分・500分・60秒 x 500秒 = 30000秒
45

56
# 購読(subscribe)時に呼び出される
67
def subscribed
78
# 部屋番号をパラメータから取得 (/?room_id=123)
89
@room_id = params[:room_id]
9-
@game_id = params[:room_id]
10+
#@game_id = params[:room_id]
1011
reject unless @room_id.present? # 部屋番号がない場合は購読を拒否
1112

1213
# この接続を特定のストリーム(部屋)に紐付ける・Action Cableの概念で、特定のブロードキャストに対してリスナーになる
@@ -20,7 +21,8 @@ def subscribed
2021
transmit({ type: 'initial_timer_state', data: initial_timer_state })
2122

2223
#最初のセットアップ
23-
init_state(@room_id,@game_id)
24+
#init_state(@room_id,@game_id)
25+
init_state(@room_id)
2426

2527
# 購読者カウントを増やす
2628
#increment_subscriber_count(@room_id)
@@ -230,7 +232,8 @@ def cleanup_room_data(room_id)
230232
end
231233

232234
#初期設定
233-
def init_state(room_id,game_id)
235+
#def init_state(room_id,game_id)
236+
def init_state(room_id)
234237
#Rails.logger.info "WebSocket初期読み込みrequest_initial_board_state: room_id:#{room_id}・game_id:#{game_id}"
235238

236239
redis_key = "shogi_game:#{room_id}"

app/controllers/matching_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def game_lobby_matching_board
1515
puts "@comments: #{@comments}"
1616
#puts "@comments: #{@comments.to_json}"
1717

18+
#game_rooms_key = "game_room:ai"
19+
#$redis.del(game_rooms_key)
1820

1921
@matching_queue_length = $redis.llen(MATCHING_QUEUE_KEY)#現在のマッチング待ち人数を確認・キューの長さを確認
2022
@matching_queue_data = $redis.lrange(MATCHING_QUEUE_KEY, 0, -1)# 既存のキューから全てのデータを取得
@@ -28,6 +30,8 @@ def game_lobby_matching_board
2830
end
2931
end
3032
@game_room_datas = @game_room_datas.to_json
33+
34+
@rails_env = Rails.env
3135
end
3236

3337
#このstartメソッドは、将棋のオンライン対戦における「マッチング機能」を実装・Redisのキューを使って2つのプレイヤーを待機させ、2人揃ったら対戦部屋を作成する仕組み

app/controllers/shogi_controller.rb

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,76 @@ class ShogiController < ApplicationController
22
MATCHING_QUEUE_KEY = 'shogi:matching_queue' # Redisのリストキー
33
GAME_ROOMS_HASH_KEY = 'shogi:game_rooms' # Redisのハッシュキー
44

5+
#DELETE_TIME = 30 * 60 #30分を秒単位で定義・30秒 x 60秒 = 1800秒
6+
DELETE_TIME = 60 * 500 #8時間20分・500分・60秒 x 500秒 = 30000秒
7+
58
def index
6-
@game_id = params[:id]
9+
@rails_env = Rails.env
10+
#@game_id = params[:id]
711
@room_id = params[:id]
812
game_rooms_key = "game_room:#{@room_id}"
913

1014
if $redis.get(game_rooms_key).nil? # nil の場合、ゲームが終了している場合の処理
11-
redirect_to root_path
15+
#Aiモードの時
16+
#if @room_id == "ai"
17+
if @room_id.include?("ai_")#含まれてたら
18+
ai_state="ai"
19+
player_identifier=session.id.to_s
20+
player_user_agent=request.user_agent
21+
@your_user_name="あなた"
22+
if [true, false].sample # ランダムに振り分け
23+
#この場合はplayer1が先手でplayer2が後手
24+
sente_identifier = player_identifier; sente_user_agent = player_user_agent; sente_user_name = @your_user_name;
25+
gote_identifier = ai_state; gote_user_agent = ai_state; gote_user_name = ai_state;
26+
@your_role = "先手"
27+
@enemy_role = "後手"
28+
else
29+
#この場合はplayer2が先手でplayer1が後手
30+
sente_identifier = ai_state; sente_user_agent = ai_state; sente_user_name = ai_state;
31+
gote_identifier = player_identifier; gote_user_agent = player_user_agent; gote_user_name = @your_user_name;
32+
@your_role = "後手"
33+
@enemy_role = "先手"
34+
end
35+
# 一意な room_id を生成
36+
#room_id = SecureRandom.uuid
37+
# ゲーム部屋の情報をRedisのHashにまとめる
38+
@game_room_data_json = {
39+
sente_identifier: sente_identifier,
40+
gote_identifier: gote_identifier,
41+
sente_user_agent: sente_user_agent,
42+
gote_user_agent: gote_user_agent,
43+
sente_user_name: sente_user_name,
44+
gote_user_name: gote_user_name,
45+
status: 'active',
46+
battleType: '10min',
47+
created_at: Time.current.to_i,
48+
}
49+
#game_rooms_key = "game_room:#{room_id}"
50+
game_rooms_key = "game_room:#{@room_id}"
51+
#Redisのハッシュ(GAME_ROOMS_HASH_KEY)に対し、キーroom_idでroom_dataをJSON文字列として保存する
52+
#$redis.setex(game_rooms_key, DELETE_TIME, room_data.to_json)
53+
#$redis.setex(game_rooms_key, DELETE_TIME, @game_room_data_json)
54+
$redis.setex(game_rooms_key, DELETE_TIME, @game_room_data_json.to_json)
55+
@ai_mode=true
56+
@audienceUser=false
57+
#AIモードの時
58+
else
59+
redirect_to root_path #リダイレクト
60+
end
1261
else# データが存在する場合の処理
1362
# 特定のroom_idのデータを取得
1463
@game_room_data_json = $redis.get(game_rooms_key)
1564
Rails.logger.info "redisからデータ取得: #{@game_room_data_json}"#redisからデータ取得: {"sente_identifier":"9a407895123bef7a65202dfb165a9aff","gote_identifier":"9f63ac8f86022e04a98aa62b8be8a737","status":"active","created_at":1750569827,"player1_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36","player2_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"}
16-
1765
@game_room_data_json=JSON.parse(@game_room_data_json)
18-
1966
current_session_id = session.id.to_s
2067
@your_role=""
2168
now_player=""
2269
your_user_agent=""
2370
your_session_id=""
24-
2571
battleType = @game_room_data_json["battleType"]
2672
#Rails.logger.info "バトルタイプ: #{battleType}"
2773
@your_user_name = @game_room_data_json["sente_user_name"]
74+
@ai_mode=false
2875

2976
# セッションIDがsenteかgoteかで分岐
3077
if current_session_id == @game_room_data_json["sente_identifier"]
@@ -48,7 +95,7 @@ def index
4895
# どちらにも該当しない場合(不正アクセスなど)
4996
#render json: { error: "不正なセッションです。" }, status: :unauthorized and return
5097
end
51-
Rails.logger.info "あなたの出番: #{@your_role}"
98+
#Rails.logger.info "あなたの出番: #{@your_role}"
5299
#Rails.logger.info "session[:opponent_identifier]: #{session[:opponent_identifier]}"
53100

54101
# マッチング成立後に不要になったセッション情報をクリア
@@ -57,11 +104,12 @@ def index
57104
#session.delete(:player_role)
58105
#session.delete(:opponent_identifier)
59106

60-
redis_board_key = "shogi_game:#{@room_id}"
61-
redis_chat_key = "shogi_game_chat:#{@room_id}"
107+
#redis_board_key = "shogi_game:#{@room_id}"
108+
#redis_chat_key = "shogi_game_chat:#{@room_id}"
62109
end
63110
end
64111

112+
=begin
65113
def update #移動を処理して状態を更新します
66114
game_id = params[:game_id]
67115
move = params[:move]
@@ -72,4 +120,6 @@ def update #移動を処理して状態を更新します
72120
game_state = RedisService.get_game_state(@game_id)
73121
Rails.logger.error "ShogiControllerのRedisServiceからの戻り値のgame_state: #{game_state}"
74122
end
123+
=end
124+
75125
end

0 commit comments

Comments
 (0)