63 lines
1.8 KiB
Ruby
63 lines
1.8 KiB
Ruby
class GameParticipantsController < ApplicationController
|
|
before_action :set_game
|
|
before_action :require_login
|
|
before_action :require_participant, only: [:select_power]
|
|
|
|
# POST /games/1/game_participants/select_power
|
|
def select_power
|
|
power_name = params[:power_name]
|
|
|
|
unless power_name.present?
|
|
redirect_to @game, alert: "国を選択してください"
|
|
return
|
|
end
|
|
|
|
# 利用可能な国のリストを取得
|
|
available_powers = get_available_powers
|
|
|
|
unless available_powers.include?(power_name)
|
|
redirect_to @game, alert: "無効な国です"
|
|
return
|
|
end
|
|
|
|
# 既に選択されているかチェック
|
|
if @game.game_participants.exists?(power: power_name)
|
|
redirect_to @game, alert: "その国は既に選択されています"
|
|
return
|
|
end
|
|
|
|
# 国を選択
|
|
@current_participant.select_power!(power_name)
|
|
|
|
# 全員が選択したかチェック
|
|
if @game.can_start_order_input?
|
|
redirect_to @game, notice: "国を選択しました。全員の選択が完了しました!"
|
|
else
|
|
redirect_to @game, notice: "国を選択しました。他のプレイヤーの選択を待っています..."
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_game
|
|
@game = Game.find(params[:game_id])
|
|
@current_participant = current_user && @game.game_participants.find_by(user: current_user)
|
|
end
|
|
|
|
def require_participant
|
|
unless @current_participant
|
|
redirect_to @game, alert: "このゲームに参加していません"
|
|
end
|
|
end
|
|
|
|
def get_available_powers
|
|
# ディプロマシーの標準的な国
|
|
standard_powers = %w[Austria England France Germany Italy Russia Turkey]
|
|
|
|
# 既に選択されている国を除外
|
|
selected_powers = @game.game_participants.where.not(power: nil).pluck(:power)
|
|
|
|
standard_powers - selected_powers
|
|
end
|
|
end
|