フロントエンドプレイアブル
This commit is contained in:
0
app/assets/builds/.keep
Normal file
0
app/assets/builds/.keep
Normal file
0
app/assets/images/.keep
Normal file
0
app/assets/images/.keep
Normal file
BIN
app/assets/images/background.png
Normal file
BIN
app/assets/images/background.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.4 MiB |
BIN
app/assets/images/background_2.jpg
Normal file
BIN
app/assets/images/background_2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 576 KiB |
BIN
app/assets/images/header-logo.png
Normal file
BIN
app/assets/images/header-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.2 MiB |
10
app/assets/stylesheets/application.css
Normal file
10
app/assets/stylesheets/application.css
Normal file
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* This is a manifest file that'll be compiled into application.css.
|
||||
*
|
||||
* With Propshaft, assets are served efficiently without preprocessing steps. You can still include
|
||||
* application-wide styles in this file, but keep in mind that CSS precedence will follow the standard
|
||||
* cascading order, meaning styles declared later in the document or manifest will override earlier ones,
|
||||
* depending on specificity.
|
||||
*
|
||||
* Consider organizing styles into separate files for maintainability.
|
||||
*/
|
||||
18
app/assets/tailwind/application.css
Normal file
18
app/assets/tailwind/application.css
Normal file
@@ -0,0 +1,18 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
:root {
|
||||
--background-image-diplomacy: url('background.png');
|
||||
}
|
||||
|
||||
body {
|
||||
background-image: var(--background-image-diplomacy);
|
||||
background-attachment: fixed;
|
||||
background-size: 600px;
|
||||
background-repeat: repeat;
|
||||
}
|
||||
|
||||
.game-map svg {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 70vh;
|
||||
}
|
||||
33
app/controllers/application_controller.rb
Normal file
33
app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
|
||||
allow_browser versions: :modern
|
||||
|
||||
# Changes to the importmap will invalidate the etag for HTML responses
|
||||
stale_when_importmap_changes
|
||||
|
||||
helper_method :current_user, :logged_in?
|
||||
|
||||
private
|
||||
|
||||
def current_user
|
||||
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
|
||||
end
|
||||
|
||||
def logged_in?
|
||||
current_user.present?
|
||||
end
|
||||
|
||||
def require_login
|
||||
unless logged_in?
|
||||
flash[:alert] = "ログインが必要です"
|
||||
redirect_to login_path
|
||||
end
|
||||
end
|
||||
|
||||
def require_admin
|
||||
unless logged_in? && current_user.admin?
|
||||
flash[:alert] = "管理者権限が必要です"
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
62
app/controllers/game_participants_controller.rb
Normal file
62
app/controllers/game_participants_controller.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
421
app/controllers/games_controller.rb
Normal file
421
app/controllers/games_controller.rb
Normal file
@@ -0,0 +1,421 @@
|
||||
class GamesController < ApplicationController
|
||||
include GamesHelper
|
||||
before_action :set_game, only: %i[ show edit update destroy join_game start_power_selection start_order_input turn_data vote_draw force_draw ]
|
||||
before_action :require_login, only: %i[ new create join_game ]
|
||||
before_action :require_game_admin, only: %i[ edit update destroy start_power_selection start_order_input ]
|
||||
|
||||
helper_method :get_available_powers_for_select
|
||||
|
||||
# GET /games or /games.json
|
||||
def index
|
||||
@recruiting_games = Game.where(status: "recruiting").includes(:participants)
|
||||
@my_games = current_user ? Game.joins(:participants).where(participants: { user_id: current_user.id }).includes(:participants, :turns) : []
|
||||
@games = Game.all.includes(:participants, :turns)
|
||||
end
|
||||
|
||||
# GET /games/1 or /games/1.json
|
||||
def show
|
||||
@latest_turn = @game.turns.last
|
||||
|
||||
# ゲーム終了判定
|
||||
@game_finished = @game.status == "finished"
|
||||
|
||||
# 表示するターンの決定
|
||||
if @game_finished
|
||||
# 終了済みの場合:
|
||||
# params[:turn_number] があればそのターン
|
||||
# なければ 最初のターン (Turn 1) を表示
|
||||
if params[:turn_number].present?
|
||||
@display_turn = @game.turns.find_by(number: params[:turn_number].to_i)
|
||||
end
|
||||
|
||||
# 指定がない、または見つからない場合は初期ターン(number=1)を表示
|
||||
# もし存在しなければ最新(というかあるやつ)
|
||||
@display_turn ||= @game.turns.find_by(number: 1) || @latest_turn
|
||||
|
||||
# 最終結果の取得 (最後のターン情報から)
|
||||
if @latest_turn.game_state
|
||||
centers = @latest_turn.game_state["centers"] || {}
|
||||
alive_powers = centers.keys
|
||||
|
||||
# ソロ勝利判定
|
||||
solo_winner = @game.solo_victory_power(@latest_turn.game_state)
|
||||
if solo_winner
|
||||
result_type = "Solo Victory"
|
||||
winners = [ solo_winner ]
|
||||
else
|
||||
result_type = "Draw"
|
||||
winners = alive_powers
|
||||
end
|
||||
|
||||
@winner_info = {
|
||||
type: result_type,
|
||||
winners: winners,
|
||||
scores: @game.calculate_scores(@latest_turn.game_state)
|
||||
}
|
||||
end
|
||||
else
|
||||
# 進行中の場合: params[:turn_number] があればそのターン、なければ最新ターンを表示
|
||||
if params[:turn_number].present?
|
||||
@display_turn = @game.turns.find_by(number: params[:turn_number].to_i)
|
||||
end
|
||||
@display_turn ||= @latest_turn
|
||||
end
|
||||
|
||||
if @display_turn
|
||||
@game_state = @display_turn.game_state
|
||||
|
||||
# フェーズ名のパース
|
||||
@current_season_year = parse_phase(@display_turn.phase)
|
||||
|
||||
# 国別情報の集計 (表示対象ターンのデータ)
|
||||
centers = @game_state["centers"] || {}
|
||||
units = @game_state["units"] || {}
|
||||
|
||||
# 全7カ国(固定)
|
||||
powers = %w[AUSTRIA ENGLAND FRANCE GERMANY ITALY RUSSIA TURKEY]
|
||||
|
||||
@country_statuses = powers.map do |power|
|
||||
participant = @game.participants.find_by(power: power)
|
||||
# 終了済みなら全員完了扱い、そうでなければターンごとの提出状況
|
||||
submitted = @game_finished ? true : @display_turn.orders_submitted_for?(power)
|
||||
|
||||
{
|
||||
power: power,
|
||||
sc_count: centers[power]&.size || 0,
|
||||
unit_count: units[power]&.size || 0,
|
||||
submitted: submitted,
|
||||
participant: participant,
|
||||
is_user: current_user && participant&.user_id == current_user.id
|
||||
}
|
||||
end
|
||||
|
||||
# 自国を先頭に移動
|
||||
if current_user
|
||||
user_power_index = @country_statuses.find_index { |s| s[:is_user] }
|
||||
if user_power_index
|
||||
user_status = @country_statuses.delete_at(user_power_index)
|
||||
@country_statuses.unshift(user_status)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# GET /games/new
|
||||
def new
|
||||
@game = Game.new
|
||||
@game.is_solo_mode = current_user&.admin? ? false : false
|
||||
end
|
||||
|
||||
# GET /games/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /games or /games.json
|
||||
def create
|
||||
@game = Game.new(game_params)
|
||||
|
||||
# ソロモードかどうかを判定(管理者のみ選択可能)
|
||||
if current_user&.admin? && params.dig(:game, :game_mode) == "admin_mode"
|
||||
@game.is_solo_mode = true
|
||||
else
|
||||
@game.is_solo_mode = false
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
if @game.save
|
||||
# ゲーム作成者は自動的に参加者として登録(管理者として)
|
||||
Participant.create!(
|
||||
game: @game,
|
||||
user: current_user,
|
||||
is_administrator: true
|
||||
)
|
||||
|
||||
# ソロモードの場合、即座に最初のターンを作成
|
||||
if @game.solo_mode?
|
||||
service = GameSetupService.new(@game)
|
||||
result = service.setup_initial_turn
|
||||
if result[:success]
|
||||
@game.update!(status: "in_progress")
|
||||
else
|
||||
# 失敗したらロールバックしたいところだが・・・
|
||||
# 現状はsave後なので、エラー表示だけにするかdestroyするか。
|
||||
# 今回は簡易的にログに残す
|
||||
logger.error result[:message]
|
||||
end
|
||||
end
|
||||
|
||||
format.html { redirect_to @game, notice: "ゲームが正常に作成されました。" }
|
||||
format.json { render :show, status: :created, location: @game }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @game.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /games/1 or /games/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @game.update(game_params)
|
||||
# スケジュール変更時にデッドラインを再計算
|
||||
if @game.status == "in_progress"
|
||||
@game.update_column(:next_deadline_at, @game.auto_turn? ? @game.calculate_next_deadline : nil)
|
||||
end
|
||||
format.html { redirect_to @game, notice: "ゲームが正常に更新されました。", status: :see_other }
|
||||
format.json { render :show, status: :ok, location: @game }
|
||||
else
|
||||
format.html { render :edit, status: :unprocessable_entity }
|
||||
format.json { render json: @game.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /games/1 or /games/1.json
|
||||
def destroy
|
||||
@game.destroy!
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to games_path, notice: "ゲームが正常に削除されました。", status: :see_other }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
# POST /games/1/join
|
||||
def join_game
|
||||
unless current_user
|
||||
redirect_to login_path, alert: "ゲームに参加するにはログインしてください"
|
||||
return
|
||||
end
|
||||
|
||||
# パスワードチェック
|
||||
if @game.password_protected?
|
||||
password = params.dig(:participant, :password)
|
||||
unless @game.authenticate_password(password)
|
||||
redirect_to @game, alert: "パスワードが正しくありません"
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# 既に参加しているかチェック
|
||||
if @game.participants.exists?(user: current_user)
|
||||
redirect_to @game, alert: "既にこのゲームに参加しています"
|
||||
return
|
||||
end
|
||||
|
||||
# 満員チェック
|
||||
if @game.participants.count >= @game.participants_count
|
||||
redirect_to @game, alert: "このゲームは満員です"
|
||||
return
|
||||
end
|
||||
|
||||
# 参加者を作成
|
||||
Participant.create!(
|
||||
game: @game,
|
||||
user: current_user
|
||||
)
|
||||
|
||||
# 定員到達チェック
|
||||
if @game.participants.count == @game.participants_count
|
||||
@game.update(status: "power_selection")
|
||||
end
|
||||
|
||||
redirect_to @game, notice: "ゲームに正常に参加しました!"
|
||||
end
|
||||
|
||||
# POST /games/1/start_power_selection
|
||||
def start_power_selection
|
||||
unless @game.participants.count >= 2
|
||||
redirect_to @game, alert: "最低2人の参加者が必要です"
|
||||
return
|
||||
end
|
||||
|
||||
@game.update!(status: "power_selection")
|
||||
redirect_to @game, notice: "国選択フェーズを開始しました!"
|
||||
end
|
||||
|
||||
# POST /games/1/start_order_input
|
||||
def start_order_input
|
||||
unless @game.all_powers_assigned?
|
||||
redirect_to @game, alert: "全員が国を選択する必要があります"
|
||||
return
|
||||
end
|
||||
|
||||
# 最初のターンを作成
|
||||
service = GameSetupService.new(@game)
|
||||
result = service.setup_initial_turn
|
||||
|
||||
if result[:success]
|
||||
update_attrs = { status: "in_progress" }
|
||||
update_attrs[:next_deadline_at] = @game.calculate_next_deadline if @game.auto_turn?
|
||||
@game.update!(update_attrs)
|
||||
redirect_to @game, notice: "命令入力フェーズを開始しました!"
|
||||
else
|
||||
redirect_to @game, alert: result[:message]
|
||||
end
|
||||
end
|
||||
|
||||
# GET /games/1/turn_data.json
|
||||
def turn_data
|
||||
if params[:turn_number].present?
|
||||
target_turn = @game.turns.find_by(number: params[:turn_number])
|
||||
end
|
||||
target_turn ||= @game.turns.last
|
||||
|
||||
return head :not_found unless target_turn
|
||||
|
||||
# 国別ステータス情報の再計算(JSON用)
|
||||
# showアクションと同様のロジック
|
||||
game_state = target_turn.game_state
|
||||
centers = game_state["centers"] || {}
|
||||
units = game_state["units"] || {}
|
||||
powers = %w[AUSTRIA ENGLAND FRANCE GERMANY ITALY RUSSIA TURKEY]
|
||||
|
||||
country_statuses = powers.map do |power|
|
||||
# 終了済みなら全員完了扱い
|
||||
submitted = @game.status == "finished" ? true : target_turn.orders_submitted_for?(power)
|
||||
{
|
||||
power: power,
|
||||
sc_count: centers[power]&.size || 0,
|
||||
unit_count: units[power]&.size || 0,
|
||||
submitted: submitted
|
||||
}
|
||||
end
|
||||
|
||||
render json: {
|
||||
turn_number: target_turn.number,
|
||||
phase: target_turn.phase,
|
||||
possible_orders: target_turn.possible_orders,
|
||||
decided_orders: target_turn.orders || {},
|
||||
svg_orders: target_turn.svg_orders || {},
|
||||
# svg_date カラムにデフォルトSVGが入っていると仮定(または svg_orders["NONE"])
|
||||
# ここでは svg_date が単一の画像パスかSVG文字列かによるが、
|
||||
# 既存の show.html.erb 実装を見ると svg_orders["NONE"] を使っている可能性が高い
|
||||
# 既存実装: default_svg: last_turn.svg_date となっていたのでそのまま変更なしでいくが
|
||||
# target_turn.svg_date を返すようにする
|
||||
default_svg: target_turn.svg_date,
|
||||
|
||||
# 完了状況
|
||||
all_orders_submitted: @game.status == "finished" || @game.all_orders_submitted?,
|
||||
missing_orders_powers: @game.status == "finished" ? [] : @game.participants.where(orders_submitted: false).pluck(:power).compact,
|
||||
country_statuses: country_statuses
|
||||
}
|
||||
end
|
||||
|
||||
def vote_draw
|
||||
return unless @game.status == "in_progress"
|
||||
|
||||
turn = @game.turns.last
|
||||
participant = @game.participants.find_by(user: current_user)
|
||||
|
||||
unless participant && participant.power
|
||||
redirect_to @game, alert: "権限がありません。"
|
||||
return
|
||||
end
|
||||
|
||||
power = participant.power
|
||||
if turn.draw_voted?(power)
|
||||
turn.revoke_draw_vote(power)
|
||||
flash[:notice] = "引き分け投票を取り消しました。"
|
||||
else
|
||||
turn.vote_draw(power)
|
||||
flash[:notice] = "引き分けに投票しました。"
|
||||
end
|
||||
|
||||
if turn.unanimous_draw?
|
||||
if execute_draw(turn)
|
||||
flash[:notice] = "全会一致により、ゲームは引き分けとなりました。"
|
||||
else
|
||||
# エラーメッセージは execute_draw 内で flash[:alert] に設定される
|
||||
end
|
||||
end
|
||||
|
||||
redirect_to @game
|
||||
end
|
||||
|
||||
def force_draw
|
||||
return unless current_user.admin?
|
||||
|
||||
turn = @game.turns.last
|
||||
if execute_draw(turn)
|
||||
redirect_to @game, notice: "ゲームを強制的に引き分けにしました。"
|
||||
else
|
||||
redirect_to @game
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def execute_draw(turn)
|
||||
client = GameApiClient.new
|
||||
# 生存国を勝者として扱う(引き分け)
|
||||
winners = turn.powers
|
||||
begin
|
||||
response = client.api_game_draw(turn.game_state, winners: winners)
|
||||
|
||||
if response
|
||||
# 新しいターン(完了状態)を作成
|
||||
Turn.create!(
|
||||
game: @game,
|
||||
number: turn.number + 1,
|
||||
# year, season カラムは存在しないため削除
|
||||
# phase を COMPLETED に設定
|
||||
game_state: response,
|
||||
orders: {},
|
||||
possible_orders: {},
|
||||
phase: "COMPLETED",
|
||||
svg_date: turn.svg_date,
|
||||
svg_orders: turn.svg_orders
|
||||
)
|
||||
@game.update(status: "finished")
|
||||
true
|
||||
else
|
||||
flash[:alert] = "ゲームサーバーからの応答が不正です。"
|
||||
false
|
||||
end
|
||||
rescue Faraday::ConnectionFailed => e
|
||||
flash[:alert] = "ゲームサーバーへの接続に失敗しました。管理者へ連絡してください。"
|
||||
Rails.logger.error "API Connection Failed: #{e.message}"
|
||||
false
|
||||
rescue Faraday::TimeoutError => e
|
||||
flash[:alert] = "ゲームサーバーとの通信がタイムアウトしました。"
|
||||
Rails.logger.error "API Timeout: #{e.message}"
|
||||
false
|
||||
rescue StandardError => e
|
||||
flash[:alert] = "予期せぬエラーが発生しました: #{e.message}"
|
||||
Rails.logger.error "Execute Draw Error: #{e.message}"
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
def set_game
|
||||
@game = Game.find(params.expect(:id))
|
||||
end
|
||||
|
||||
def require_game_admin
|
||||
unless current_user&.admin? || @game.administrator == current_user
|
||||
redirect_to @game, alert: "ゲーム管理者のみこの機能にアクセスできます"
|
||||
end
|
||||
end
|
||||
|
||||
def game_params
|
||||
house_rules = [ :year_limit, :victory_sc_count, :scoring_system, :turn_schedule ]
|
||||
|
||||
if action_name == "update"
|
||||
params.expect(game: [ :title, :memo, :auto_order_mode ] + house_rules)
|
||||
else
|
||||
permitted = [ :title, :memo ] + house_rules
|
||||
|
||||
if current_user&.admin?
|
||||
permitted += [ :participants_count, :password, :auto_order_mode ]
|
||||
else
|
||||
# 一般ユーザーはマルチプレイヤーモードのみ
|
||||
permitted += [ :participants_count, :password, :auto_order_mode ]
|
||||
end
|
||||
|
||||
params.expect(game: permitted)
|
||||
end
|
||||
end
|
||||
end
|
||||
87
app/controllers/participants_controller.rb
Normal file
87
app/controllers/participants_controller.rb
Normal file
@@ -0,0 +1,87 @@
|
||||
class ParticipantsController < ApplicationController
|
||||
before_action :set_game
|
||||
|
||||
# POST /games/:game_id/participants
|
||||
def create
|
||||
# パスワード確認
|
||||
if @game.password_protected?
|
||||
unless @game.authenticate_password(params[:password])
|
||||
redirect_to @game, alert: "パスワードが正しくありません"
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# 参加処理
|
||||
@participant = @game.participants.build(
|
||||
user: current_user,
|
||||
is_administrator: false
|
||||
)
|
||||
|
||||
if @participant.save
|
||||
# 定員到達チェック
|
||||
if @game.participants.count == @game.participants_count
|
||||
@game.update(status: "power_selection")
|
||||
end
|
||||
|
||||
redirect_to @game, notice: "ゲームに参加しました"
|
||||
else
|
||||
redirect_to @game, alert: @participant.errors.full_messages.join(", ")
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH /games/:game_id/participants/:id/select_power
|
||||
def select_power
|
||||
@participant = @game.participants.find(params[:id])
|
||||
|
||||
unless @participant.user == current_user
|
||||
redirect_to @game, alert: "権限がありません"
|
||||
return
|
||||
end
|
||||
|
||||
if @participant.update(power: params[:power])
|
||||
# 全員が国を選択したかチェック
|
||||
if @game.all_powers_assigned?
|
||||
service = GameSetupService.new(@game)
|
||||
result = service.setup_initial_turn
|
||||
|
||||
if result[:success]
|
||||
@game.update(status: "in_progress")
|
||||
flash[:notice] = "国を選択し、ゲームが開始されました!"
|
||||
else
|
||||
flash[:alert] = "ゲーム開始に失敗しました: #{result[:message]}"
|
||||
end
|
||||
else
|
||||
flash[:notice] = "国を選択しました"
|
||||
end
|
||||
|
||||
redirect_to @game
|
||||
else
|
||||
redirect_to @game, alert: @participant.errors.full_messages.join(", ")
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /games/:game_id/participants/:id
|
||||
def destroy
|
||||
@participant = @game.participants.find(params[:id])
|
||||
|
||||
unless @participant.user == current_user || current_user&.admin?
|
||||
redirect_to @game, alert: "権限がありません"
|
||||
return
|
||||
end
|
||||
|
||||
@participant.destroy
|
||||
|
||||
# ゲーム状態を更新
|
||||
if @game.status == "power_selection"
|
||||
@game.update(status: "recruiting")
|
||||
end
|
||||
|
||||
redirect_to games_path, notice: "ゲームから退出しました"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_game
|
||||
@game = Game.find(params[:game_id])
|
||||
end
|
||||
end
|
||||
22
app/controllers/sessions_controller.rb
Normal file
22
app/controllers/sessions_controller.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
class SessionsController < ApplicationController
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find_by(email: params[:email]&.downcase)
|
||||
if user&.authenticate(params[:password])
|
||||
session[:user_id] = user.id
|
||||
flash[:notice] = "ログインしました"
|
||||
redirect_to root_path
|
||||
else
|
||||
flash.now[:alert] = "メールアドレスまたはパスワードが正しくありません"
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
session[:user_id] = nil
|
||||
flash[:notice] = "ログアウトしました"
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
108
app/controllers/turns_controller.rb
Normal file
108
app/controllers/turns_controller.rb
Normal file
@@ -0,0 +1,108 @@
|
||||
class TurnsController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :set_turn, only: %i[ show edit update destroy submit_orders process_turn ]
|
||||
|
||||
# GET /turns or /turns.json
|
||||
def index
|
||||
@turns = Turn.all
|
||||
end
|
||||
|
||||
# GET /turns/1 or /turns/1.json
|
||||
def show
|
||||
end
|
||||
|
||||
# GET /turns/new
|
||||
def new
|
||||
@turn = Turn.new
|
||||
end
|
||||
|
||||
# GET /turns/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /turns or /turns.json
|
||||
def create
|
||||
@turn = Turn.new(turn_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @turn.save
|
||||
format.html { redirect_to @turn, notice: "Turn was successfully created." }
|
||||
format.json { render :show, status: :created, location: @turn }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: @turn.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /turns/1 or /turns/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @turn.update(turn_params)
|
||||
format.html { redirect_to @turn, notice: "Turn was successfully updated.", status: :see_other }
|
||||
format.json { render :show, status: :ok, location: @turn }
|
||||
else
|
||||
format.html { render :edit, status: :unprocessable_entity }
|
||||
format.json { render json: @turn.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /turns/1 or /turns/1.json
|
||||
def destroy
|
||||
@turn.destroy!
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to turns_path, notice: "Turn was successfully destroyed.", status: :see_other }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH /turns/1/submit_orders
|
||||
def submit_orders
|
||||
@turn = Turn.find(params[:id])
|
||||
power = params[:power]
|
||||
orders = params[:orders]&.permit!&.to_h || {}
|
||||
|
||||
service = OrderSubmissionService.new(@turn, current_user)
|
||||
result = service.submit(power: power, orders: orders)
|
||||
|
||||
if result[:success]
|
||||
redirect_to game_path(@turn.game), notice: result[:message]
|
||||
else
|
||||
redirect_to game_path(@turn.game), alert: result[:message]
|
||||
end
|
||||
end
|
||||
|
||||
# POST /turns/1/process_turn
|
||||
def process_turn
|
||||
@turn = Turn.find(params[:id])
|
||||
@game = @turn.game
|
||||
|
||||
# Check admin/turn ending permissions
|
||||
unless @game.solo_mode? || current_user&.admin? || @game.administrator == current_user
|
||||
redirect_to game_path(@game), alert: "ゲーム管理者のみターンを終了できます"
|
||||
return
|
||||
end
|
||||
|
||||
service = TurnProcessingService.new(@turn)
|
||||
result = service.process(force: params[:force])
|
||||
|
||||
if result[:success]
|
||||
redirect_to game_path(@game), notice: result[:message]
|
||||
else
|
||||
redirect_to game_path(@game), alert: result[:message]
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_turn
|
||||
@turn = Turn.find(params.expect(:id))
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def turn_params
|
||||
params.expect(turn: [ :number, :phase, :game_state, :svg_date, :game_id, :possible_orders, :orders ])
|
||||
end
|
||||
end
|
||||
88
app/controllers/users_controller.rb
Normal file
88
app/controllers/users_controller.rb
Normal file
@@ -0,0 +1,88 @@
|
||||
class UsersController < ApplicationController
|
||||
before_action :require_admin, only: [:index, :destroy, :toggle_admin]
|
||||
before_action :set_user, only: [:show, :edit, :update, :destroy, :toggle_admin]
|
||||
before_action :require_admin_or_owner, only: [:show, :edit, :update]
|
||||
|
||||
def index
|
||||
@users = User.all.order(created_at: :desc)
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def new
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
def create
|
||||
@user = User.new(user_params)
|
||||
if @user.save
|
||||
session[:user_id] = @user.id
|
||||
flash[:notice] = "アカウントを作成しました"
|
||||
redirect_to root_path
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
# パスワードが空の場合は更新しない
|
||||
if user_update_params[:password].blank?
|
||||
user_update_params.delete(:password)
|
||||
user_update_params.delete(:password_confirmation)
|
||||
end
|
||||
|
||||
if @user.update(user_update_params)
|
||||
flash[:notice] = "ユーザー情報を更新しました"
|
||||
redirect_to user_path(@user)
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @user == current_user
|
||||
flash[:alert] = "自分自身を削除することはできません"
|
||||
redirect_to users_path
|
||||
else
|
||||
@user.destroy
|
||||
flash[:notice] = "ユーザーを削除しました"
|
||||
redirect_to users_path
|
||||
end
|
||||
end
|
||||
|
||||
def toggle_admin
|
||||
if @user == current_user
|
||||
flash[:alert] = "自分自身の管理者権限は変更できません"
|
||||
else
|
||||
@user.update(admin: !@user.admin)
|
||||
flash[:notice] = "管理者権限を#{@user.admin? ? '付与' : '削除'}しました"
|
||||
end
|
||||
redirect_to users_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
def require_admin_or_owner
|
||||
unless current_user&.admin? || current_user == @user
|
||||
flash[:alert] = "アクセス権限がありません"
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(:username, :email, :password, :password_confirmation)
|
||||
end
|
||||
|
||||
def user_update_params
|
||||
# メールアドレスは変更不可
|
||||
params.require(:user).permit(:username, :password, :password_confirmation)
|
||||
end
|
||||
end
|
||||
2
app/helpers/application_helper.rb
Normal file
2
app/helpers/application_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module ApplicationHelper
|
||||
end
|
||||
51
app/helpers/games_helper.rb
Normal file
51
app/helpers/games_helper.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
module GamesHelper
|
||||
def power_color_class(power)
|
||||
case power
|
||||
when "AUSTRIA"
|
||||
"bg-red-100 text-red-800"
|
||||
when "ENGLAND"
|
||||
"bg-purple-100 text-purple-800"
|
||||
when "FRANCE"
|
||||
"bg-sky-100 text-sky-800"
|
||||
when "GERMANY"
|
||||
"bg-amber-100 text-amber-900"
|
||||
when "ITALY"
|
||||
"bg-green-100 text-green-800"
|
||||
when "RUSSIA"
|
||||
"bg-gray-100 text-gray-800"
|
||||
when "TURKEY"
|
||||
"bg-yellow-100 text-yellow-800"
|
||||
else
|
||||
"bg-gray-100 text-gray-800"
|
||||
end
|
||||
end
|
||||
|
||||
def parse_phase(phase_string)
|
||||
# 例: "S1901M" -> "1901年 春 (移動)"
|
||||
# 例: "F1901R" -> "1901年 秋 (撤退)"
|
||||
# 例: "W1901A" -> "1901年 冬 (調整)"
|
||||
|
||||
return phase_string if phase_string.blank?
|
||||
return phase_string unless phase_string.match?(/^[SFW]\d{4}[MRA]$/)
|
||||
|
||||
season_code = phase_string[0]
|
||||
year = phase_string[1..4]
|
||||
type_code = phase_string[5]
|
||||
|
||||
season = case season_code
|
||||
when "S" then "春"
|
||||
when "F" then "秋"
|
||||
when "W" then "冬"
|
||||
else season_code
|
||||
end
|
||||
|
||||
type = case type_code
|
||||
when "M" then "移動"
|
||||
when "R" then "撤退"
|
||||
when "A" then "調整"
|
||||
else type_code
|
||||
end
|
||||
|
||||
"#{year}年 #{season} (#{type})"
|
||||
end
|
||||
end
|
||||
2
app/helpers/turns_helper.rb
Normal file
2
app/helpers/turns_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module TurnsHelper
|
||||
end
|
||||
3
app/javascript/application.js
Normal file
3
app/javascript/application.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
||||
import "@hotwired/turbo-rails"
|
||||
import "controllers"
|
||||
9
app/javascript/controllers/application.js
Normal file
9
app/javascript/controllers/application.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Application } from "@hotwired/stimulus"
|
||||
|
||||
const application = Application.start()
|
||||
|
||||
// Configure Stimulus development experience
|
||||
application.debug = false
|
||||
window.Stimulus = application
|
||||
|
||||
export { application }
|
||||
7
app/javascript/controllers/hello_controller.js
Normal file
7
app/javascript/controllers/hello_controller.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
this.element.textContent = "Hello World!"
|
||||
}
|
||||
}
|
||||
4
app/javascript/controllers/index.js
Normal file
4
app/javascript/controllers/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
// Import and register all your controllers from the importmap via controllers/**/*_controller
|
||||
import { application } from "controllers/application"
|
||||
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
|
||||
eagerLoadControllersFrom("controllers", application)
|
||||
7
app/jobs/application_job.rb
Normal file
7
app/jobs/application_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class ApplicationJob < ActiveJob::Base
|
||||
# Automatically retry jobs that encountered a deadlock
|
||||
# retry_on ActiveRecord::Deadlocked
|
||||
|
||||
# Most jobs are safe to ignore if the underlying records are no longer available
|
||||
# discard_on ActiveJob::DeserializationError
|
||||
end
|
||||
66
app/jobs/auto_turn_process_job.rb
Normal file
66
app/jobs/auto_turn_process_job.rb
Normal file
@@ -0,0 +1,66 @@
|
||||
class AutoTurnProcessJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform
|
||||
Game.where(status: "in_progress")
|
||||
.where.not(next_deadline_at: nil)
|
||||
.where("next_deadline_at <= ?", Time.current)
|
||||
.find_each do |game|
|
||||
process_game(game)
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "AutoTurnProcessJob: Game #{game.id} failed: #{e.message}"
|
||||
Rails.logger.error e.backtrace.first(5).join("\n")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def process_game(game)
|
||||
latest_turn = game.turns.where.not(phase: "COMPLETED").last
|
||||
return unless latest_turn
|
||||
|
||||
client = GameApiClient.new
|
||||
current_orders = latest_turn.orders || {}
|
||||
|
||||
# 人間が担当していない国のAutoOrderを生成
|
||||
# 人間プレイヤーの未提出分はそのまま(空のまま)処理する
|
||||
all_powers = latest_turn.game_state&.dig("units")&.keys || []
|
||||
human_powers = game.participants.where.not(power: nil).pluck(:power).map(&:upcase)
|
||||
submitted_powers = current_orders.keys.map(&:upcase)
|
||||
|
||||
all_powers.each do |power|
|
||||
next if submitted_powers.include?(power.upcase)
|
||||
|
||||
# 人間プレイヤーが担当している国は命令未提出のまま進行
|
||||
next if human_powers.include?(power.upcase)
|
||||
|
||||
# 人間が担当していない国のみAutoOrderを適用
|
||||
if game.auto_order_mode == "random"
|
||||
auto_orders_response = client.api_calculate_auto_orders(latest_turn.game_state, power)
|
||||
if auto_orders_response && auto_orders_response["orders"]
|
||||
current_orders[power] = auto_orders_response["orders"]
|
||||
end
|
||||
end
|
||||
# auto_order_mode == "hold" の場合はAPIがデフォルトでHOLDを適用するため何もしない
|
||||
end
|
||||
|
||||
# 自動生成した命令を保存
|
||||
latest_turn.update_columns(orders: current_orders) if current_orders.present?
|
||||
|
||||
# ターン処理を実行(force: true で未提出でも強制進行)
|
||||
service = TurnProcessingService.new(latest_turn, client: client)
|
||||
result = service.process(force: "true")
|
||||
|
||||
if result[:success]
|
||||
game.reload
|
||||
if game.status == "in_progress"
|
||||
game.update!(next_deadline_at: game.calculate_next_deadline)
|
||||
else
|
||||
game.update!(next_deadline_at: nil)
|
||||
end
|
||||
Rails.logger.info "AutoTurnProcess: Game #{game.id} processed successfully: #{result[:message]}"
|
||||
else
|
||||
Rails.logger.error "AutoTurnProcess: Game #{game.id} failed: #{result[:message]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
4
app/mailers/application_mailer.rb
Normal file
4
app/mailers/application_mailer.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class ApplicationMailer < ActionMailer::Base
|
||||
default from: "from@example.com"
|
||||
layout "mailer"
|
||||
end
|
||||
3
app/models/application_record.rb
Normal file
3
app/models/application_record.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
primary_abstract_class
|
||||
end
|
||||
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
165
app/models/game.rb
Normal file
165
app/models/game.rb
Normal file
@@ -0,0 +1,165 @@
|
||||
class Game < ApplicationRecord
|
||||
has_many :turns, dependent: :destroy
|
||||
has_many :participants, dependent: :destroy
|
||||
has_many :users, through: :participants
|
||||
|
||||
# パスワード保護
|
||||
has_secure_password :password, validations: false
|
||||
|
||||
# バリデーション
|
||||
validates :status, inclusion: {
|
||||
in: %w[recruiting power_selection in_progress finished cancelled]
|
||||
}
|
||||
validates :auto_order_mode, inclusion: { in: %w[hold random] }
|
||||
validates :participants_count,
|
||||
numericality: { greater_than_or_equal_to: 2, less_than_or_equal_to: 7 },
|
||||
unless: :is_solo_mode?
|
||||
|
||||
# ハウスルールバリデーション
|
||||
validates :year_limit,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 1901, less_than_or_equal_to: 1999 },
|
||||
allow_nil: true
|
||||
validates :victory_sc_count,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 34 }
|
||||
validates :scoring_system, inclusion: { in: %w[none sc_count sc_ratio dss sos] }
|
||||
|
||||
# ターンスケジュールバリデーション
|
||||
validate :validate_turn_schedule
|
||||
|
||||
# ヘルパーメソッド
|
||||
def password_protected?
|
||||
password_digest.present?
|
||||
end
|
||||
|
||||
def solo_mode?
|
||||
is_solo_mode
|
||||
end
|
||||
|
||||
def administrator
|
||||
participants.find_by(is_administrator: true)&.user
|
||||
end
|
||||
|
||||
def available_powers
|
||||
assigned_powers = participants.where.not(power: nil).pluck(:power)
|
||||
%w[AUSTRIA ENGLAND FRANCE GERMANY ITALY RUSSIA TURKEY] - assigned_powers
|
||||
end
|
||||
|
||||
def all_powers_assigned?
|
||||
participants.where(power: nil).empty? &&
|
||||
participants.count == participants_count
|
||||
end
|
||||
|
||||
def all_orders_submitted?
|
||||
participants.where(power: nil).empty? &&
|
||||
participants.all?(&:orders_submitted)
|
||||
end
|
||||
|
||||
def unassigned_powers
|
||||
all_powers = %w[AUSTRIA ENGLAND FRANCE GERMANY ITALY RUSSIA TURKEY]
|
||||
assigned_powers = participants.where.not(power: nil).pluck(:power)
|
||||
all_powers - assigned_powers
|
||||
end
|
||||
|
||||
# ターンスケジュール関連メソッド
|
||||
|
||||
def auto_turn?
|
||||
turn_schedule.present?
|
||||
end
|
||||
|
||||
# 次のデッドラインを計算(日本時間基準)
|
||||
def calculate_next_deadline
|
||||
return nil unless turn_schedule.present?
|
||||
|
||||
hours = turn_schedule.split(",").map(&:strip).map(&:to_i).sort
|
||||
now = Time.current.in_time_zone("Asia/Tokyo")
|
||||
|
||||
# 今日の残りの時間枠を探す(JST基準)
|
||||
next_time = hours.map { |h| now.beginning_of_day + h.hours }
|
||||
.find { |t| t > now }
|
||||
|
||||
# 今日の枠がなければ翌日の最初の枠
|
||||
next_time || (now.beginning_of_day + 1.day + hours.first.hours)
|
||||
end
|
||||
|
||||
# スケジュール表示用
|
||||
def schedule_display
|
||||
return "手動" unless turn_schedule.present?
|
||||
hours = turn_schedule.split(",").map(&:strip)
|
||||
"毎日 " + hours.map { |h| "#{h}時" }.join("・")
|
||||
end
|
||||
|
||||
# ハウスルール関連メソッド
|
||||
|
||||
# 年数制限チェック: フェーズ名から年を抽出し、year_limitを超えているか判定
|
||||
def year_limit_reached?(phase_name)
|
||||
return false unless year_limit.present? && phase_name.present?
|
||||
|
||||
# フェーズ名の例: "S1901M", "F1910R", "W1901A"
|
||||
year_match = phase_name.match(/[SFW](\d{4})[MRA]/)
|
||||
return false unless year_match
|
||||
|
||||
year_match[1].to_i > year_limit
|
||||
end
|
||||
|
||||
# ソロ勝利判定: いずれかの国のSC数がvictory_sc_count以上か
|
||||
def solo_victory?(game_state)
|
||||
centers = game_state&.dig("centers") || {}
|
||||
centers.any? { |_power, scs| scs.size >= victory_sc_count }
|
||||
end
|
||||
|
||||
# ソロ勝利した国を返す
|
||||
def solo_victory_power(game_state)
|
||||
centers = game_state&.dig("centers") || {}
|
||||
centers.find { |_power, scs| scs.size >= victory_sc_count }&.first
|
||||
end
|
||||
|
||||
# スコア計算
|
||||
def calculate_scores(game_state)
|
||||
return {} if scoring_system == "none"
|
||||
|
||||
centers = game_state&.dig("centers") || {}
|
||||
total_scs = centers.values.flatten.size
|
||||
alive_count = centers.count { |_power, scs| scs.any? }
|
||||
|
||||
case scoring_system
|
||||
when "sc_count"
|
||||
# 単純SC数
|
||||
centers.transform_values { |scs| scs.size }
|
||||
when "sc_ratio"
|
||||
# SC比率(%)
|
||||
centers.transform_values { |scs| total_scs > 0 ? (scs.size.to_f / total_scs * 100).round(1) : 0 }
|
||||
when "dss"
|
||||
# Draw Size Scoring: 生存国で均等分割
|
||||
centers.transform_values { |scs| scs.any? ? (100.0 / alive_count).round(1) : 0 }
|
||||
when "sos"
|
||||
# Sum of Squares
|
||||
sum_of_squares = centers.values.sum { |scs| scs.size ** 2 }.to_f
|
||||
centers.transform_values { |scs| sum_of_squares > 0 ? ((scs.size ** 2) / sum_of_squares * 100).round(1) : 0 }
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
# スコアリング方式の日本語名
|
||||
def scoring_system_name
|
||||
case scoring_system
|
||||
when "none" then "なし"
|
||||
when "sc_count" then "SC数"
|
||||
when "sc_ratio" then "SC比率"
|
||||
when "dss" then "DSS(均等分割)"
|
||||
when "sos" then "SoS(二乗和)"
|
||||
else scoring_system
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_turn_schedule
|
||||
return if turn_schedule.blank?
|
||||
|
||||
hours = turn_schedule.split(",").map(&:strip)
|
||||
unless hours.all? { |h| h.match?(/\A\d{1,2}\z/) && h.to_i.between?(0, 23) }
|
||||
errors.add(:turn_schedule, "は0〜23の数値をカンマ区切りで入力してください(例: 0,18)")
|
||||
end
|
||||
end
|
||||
end
|
||||
52
app/models/game_participant.rb
Normal file
52
app/models/game_participant.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
class GameParticipant < ApplicationRecord
|
||||
belongs_to :game
|
||||
belongs_to :user
|
||||
|
||||
validates :status, inclusion: { in: %w[joined ready finished] }
|
||||
validates :user_id, uniqueness: { scope: :game_id }
|
||||
validates :power, uniqueness: { scope: :game_id }, allow_nil: true
|
||||
|
||||
# ステータス管理メソッド
|
||||
def joined?
|
||||
status == 'joined'
|
||||
end
|
||||
|
||||
def ready?
|
||||
status == 'ready'
|
||||
end
|
||||
|
||||
def finished?
|
||||
status == 'finished'
|
||||
end
|
||||
|
||||
# パワー選択関連
|
||||
def has_selected_power?
|
||||
power.present?
|
||||
end
|
||||
|
||||
def select_power!(power_name)
|
||||
update!(power: power_name, status: 'ready')
|
||||
end
|
||||
|
||||
# ゲーム参加メソッド
|
||||
class << self
|
||||
def join_game!(game, user, password = nil)
|
||||
# パスワードチェック(プレイヤーモードの場合)
|
||||
if game.player_mode? && game.password.present?
|
||||
raise "Invalid password" unless game.password == password
|
||||
end
|
||||
|
||||
# 既存参加チェック
|
||||
if game.game_participants.exists?(user_id: user.id)
|
||||
raise "Already joined this game"
|
||||
end
|
||||
|
||||
# 定員チェック
|
||||
if game.player_mode? && game.full?
|
||||
raise "Game is full"
|
||||
end
|
||||
|
||||
create!(game: game, user: user, joined_at: Time.current, status: 'joined')
|
||||
end
|
||||
end
|
||||
end
|
||||
20
app/models/participant.rb
Normal file
20
app/models/participant.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class Participant < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :game
|
||||
|
||||
# バリデーション
|
||||
validates :user_id, uniqueness: {
|
||||
scope: :game_id,
|
||||
message: "既にこのゲームに参加しています"
|
||||
}
|
||||
|
||||
validates :power, uniqueness: {
|
||||
scope: :game_id,
|
||||
message: "この国は既に選択されています"
|
||||
}, allow_nil: true
|
||||
|
||||
validates :power, inclusion: {
|
||||
in: %w[AUSTRIA ENGLAND FRANCE GERMANY ITALY RUSSIA TURKEY],
|
||||
message: "無効な国です"
|
||||
}, allow_nil: true
|
||||
end
|
||||
124
app/models/turn.rb
Normal file
124
app/models/turn.rb
Normal file
@@ -0,0 +1,124 @@
|
||||
class Turn < ApplicationRecord
|
||||
belongs_to :game
|
||||
|
||||
# 特定の国の実行可能な命令を取得
|
||||
# 例: turn.possible_orders_for("FRANCE")
|
||||
def possible_orders_for(power_name)
|
||||
possible_orders&.dig("possible_orders", power_name.to_s.upcase)
|
||||
end
|
||||
|
||||
# 特定の国の決定済み命令を取得
|
||||
# 例: turn.orders_for("FRANCE")
|
||||
def orders_for(power_name)
|
||||
# orders は直接ハッシュを保存する場合を想定(必要に応じてネストを調整)
|
||||
orders&.dig(power_name.to_s.upcase)
|
||||
end
|
||||
|
||||
# 命令が存在するすべての国名リストを取得
|
||||
def powers
|
||||
possible_orders&.dig("possible_orders")&.keys || []
|
||||
end
|
||||
|
||||
# 特定の国の命令が提出済みかチェック
|
||||
def orders_submitted_for?(power_name)
|
||||
orders&.key?(power_name.to_s.upcase)
|
||||
end
|
||||
|
||||
# 未提出の国のリストを取得
|
||||
def pending_powers
|
||||
game.participants.where.not(power: nil).map(&:power).reject do |power|
|
||||
orders_submitted_for?(power)
|
||||
end
|
||||
end
|
||||
|
||||
# 自動命令を生成(参加者がいない国用)
|
||||
def generate_auto_orders_for_unassigned_powers
|
||||
unassigned = game.unassigned_powers
|
||||
return if unassigned.empty?
|
||||
|
||||
client = GameApiClient.new
|
||||
current_orders = orders || {}
|
||||
|
||||
unassigned.each do |power|
|
||||
if game.auto_order_mode == "hold"
|
||||
# HOLD命令を生成
|
||||
current_orders[power] = generate_hold_orders(power)
|
||||
else
|
||||
# ランダム命令を生成
|
||||
auto_orders_response = client.api_calculate_auto_orders(game_state, power)
|
||||
if auto_orders_response
|
||||
# APIレスポンスから命令を抽出
|
||||
# レスポンスが {"orders": [...]} の形式の場合
|
||||
if auto_orders_response.is_a?(Hash) && auto_orders_response["orders"]
|
||||
current_orders[power] = auto_orders_response["orders"]
|
||||
elsif auto_orders_response.is_a?(Array)
|
||||
current_orders[power] = auto_orders_response
|
||||
else
|
||||
# フォールバック: HOLD命令を使用
|
||||
current_orders[power] = generate_hold_orders(power)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
update(orders: current_orders)
|
||||
update(orders: current_orders)
|
||||
end
|
||||
|
||||
# 引き分け投票
|
||||
def vote_draw(power_name)
|
||||
current_votes = draw_votes || []
|
||||
power = power_name.to_s.upcase
|
||||
unless current_votes.include?(power)
|
||||
current_votes << power
|
||||
update(draw_votes: current_votes)
|
||||
end
|
||||
end
|
||||
|
||||
# 引き分け投票取り消し
|
||||
def revoke_draw_vote(power_name)
|
||||
current_votes = draw_votes || []
|
||||
power = power_name.to_s.upcase
|
||||
if current_votes.include?(power)
|
||||
current_votes.delete(power)
|
||||
update(draw_votes: current_votes)
|
||||
end
|
||||
end
|
||||
|
||||
# 投票済みかチェック
|
||||
def draw_voted?(power_name)
|
||||
(draw_votes || []).include?(power_name.to_s.upcase)
|
||||
end
|
||||
|
||||
# 全会一致で引き分けかチェック
|
||||
def unanimous_draw?
|
||||
# 生存している国
|
||||
active_powers = powers
|
||||
return false if active_powers.empty?
|
||||
|
||||
# プレイヤーが割り当てられている国のみを対象とする
|
||||
# (未割り当ての国は投票できないため、除外する)
|
||||
assigned_powers = game.participants.where.not(power: nil).pluck(:power).map(&:upcase)
|
||||
|
||||
# 投票権を持つ国 = 生存している かつ プレイヤーがいる
|
||||
eligible_powers = active_powers.map(&:upcase) & assigned_powers
|
||||
|
||||
return false if eligible_powers.empty?
|
||||
|
||||
current_votes = draw_votes || []
|
||||
eligible_powers.all? { |power| current_votes.include?(power) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_hold_orders(power)
|
||||
possible = possible_orders_for(power)
|
||||
return {} unless possible
|
||||
|
||||
hold_orders = {}
|
||||
possible.each do |unit, moves|
|
||||
hold_orders[unit] = [ "H" ] if moves.is_a?(Array)
|
||||
end
|
||||
hold_orders
|
||||
end
|
||||
end
|
||||
15
app/models/user.rb
Normal file
15
app/models/user.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
|
||||
validates :username, presence: true, length: { minimum: 3, maximum: 50 }
|
||||
validates :email, presence: true,
|
||||
uniqueness: { case_sensitive: false },
|
||||
format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||
validates :password, length: { minimum: 6 }, if: -> { new_record? || !password.nil? }
|
||||
|
||||
before_save { self.email = email.downcase }
|
||||
|
||||
def admin?
|
||||
admin
|
||||
end
|
||||
end
|
||||
126
app/services/game_api_client.rb
Normal file
126
app/services/game_api_client.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
require "faraday"
|
||||
require "json"
|
||||
|
||||
class GameApiClient
|
||||
BASE_URL = "http://0.0.0.0:8000"
|
||||
|
||||
def initialize
|
||||
@connection = Faraday.new(url: BASE_URL) do |f|
|
||||
f.request :json # リクエストをJSON形式にする
|
||||
f.response :json # レスポンスをJSONとしてパースする
|
||||
f.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
||||
|
||||
# GET リクエスト: 初期状態の取得
|
||||
def api_game_initial_state(map_name = "standard")
|
||||
response = @connection.get("/game/initial-state", { map_name: map_name })
|
||||
handle_response(response, "game/initial-state")
|
||||
end
|
||||
|
||||
# POST リクエスト: ゲームのみを強制終了 (Draw) にする
|
||||
def api_game_draw(game_state, winners: nil)
|
||||
response = @connection.post("/game/draw") do |req|
|
||||
body = { game_state: game_state }
|
||||
body[:winners] = winners if winners
|
||||
req.body = body
|
||||
end
|
||||
handle_response(response, "game/draw")
|
||||
end
|
||||
|
||||
# GET リクエスト: 利用可能なマップの取得
|
||||
def api_maps
|
||||
response = @connection.get("/maps")
|
||||
handle_response(response, "maps")
|
||||
end
|
||||
|
||||
# GET /maps/{map_name}
|
||||
def api_maps_data(map_name)
|
||||
response = @connection.get("/maps/#{map_name}")
|
||||
handle_response(response, "maps/#{map_name}")
|
||||
end
|
||||
|
||||
# POST リクエスト: 可能な命令の計算
|
||||
def api_calculate_possible_orders(game_state, power_name: "", by_power: false)
|
||||
response = @connection.post("/calculate/possible-orders") do |req|
|
||||
req.params["power_name"] = power_name
|
||||
req.params["by_power"] = by_power
|
||||
req.body = { game_state: game_state }
|
||||
end
|
||||
handle_response(response, "calculate/possible-orders")
|
||||
end
|
||||
|
||||
# POST リクエスト: 命令を処理して次のフェーズへ進める
|
||||
def api_calculate_process(game_state, orders)
|
||||
normalized_orders = normalize_orders(orders)
|
||||
response = @connection.post("/calculate/process") do |req|
|
||||
req.body = { game_state: game_state, orders: normalized_orders }
|
||||
end
|
||||
handle_response(response, "calculate/process")
|
||||
end
|
||||
|
||||
# POST リクエスト: 命令の妥当性を検証する
|
||||
def api_calculate_validate(game_state, orders)
|
||||
normalized_orders = normalize_orders(orders)
|
||||
response = @connection.post("/calculate/validate") do |req|
|
||||
req.body = { game_state: game_state, orders: normalized_orders }
|
||||
end
|
||||
handle_response(response, "calculate/validate")
|
||||
end
|
||||
|
||||
# POST リクエスト: 特定勢力の命令を自動生成する
|
||||
def api_calculate_auto_orders(game_state, power_name)
|
||||
response = @connection.post("/calculate/auto-orders") do |req|
|
||||
req.params["power_name"] = power_name
|
||||
req.body = { game_state: game_state }
|
||||
end
|
||||
handle_response(response, "calculate/auto-orders")
|
||||
end
|
||||
|
||||
# POST リクエスト: マップをSVGとしてレンダリングする
|
||||
def api_render(game_state, orders: nil, incl_orders: true, incl_abbrev: true)
|
||||
# ordersのキーを文字列に変換し、値を配列に変換して正規化
|
||||
normalized_orders = normalize_orders(orders)
|
||||
|
||||
body = {
|
||||
game_state: game_state,
|
||||
orders: normalized_orders,
|
||||
incl_orders: incl_orders,
|
||||
incl_abbrev: incl_abbrev
|
||||
}
|
||||
Rails.logger.debug "API Render Request Body: #{body.inspect}"
|
||||
|
||||
response = @connection.post("/render") do |req|
|
||||
req.body = body
|
||||
end
|
||||
handle_response(response, "render")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_orders(orders)
|
||||
return nil unless orders
|
||||
|
||||
normalized_orders = {}
|
||||
orders.each do |power, power_orders|
|
||||
# power_ordersがハッシュの場合、値の配列に変換
|
||||
if power_orders.is_a?(Hash)
|
||||
normalized_orders[power.to_s] = power_orders.values
|
||||
elsif power_orders.is_a?(Array)
|
||||
normalized_orders[power.to_s] = power_orders
|
||||
else
|
||||
normalized_orders[power.to_s] = power_orders
|
||||
end
|
||||
end
|
||||
normalized_orders
|
||||
end
|
||||
|
||||
def handle_response(response, endpoint)
|
||||
if response.success?
|
||||
response.body
|
||||
else
|
||||
Rails.logger.error "API Error (#{endpoint}): #{response.status} - #{response.body}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
37
app/services/game_setup_service.rb
Normal file
37
app/services/game_setup_service.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class GameSetupService
|
||||
def initialize(game, client: GameApiClient.new)
|
||||
@game = game
|
||||
@client = client
|
||||
end
|
||||
|
||||
def setup_initial_turn
|
||||
initial_response = @client.api_game_initial_state
|
||||
|
||||
unless initial_response && initial_response["game_state"]
|
||||
return { success: false, message: "初期状態の取得に失敗しました。" }
|
||||
end
|
||||
|
||||
initial_state = initial_response["game_state"]
|
||||
svg_render = @client.api_render(initial_state)
|
||||
possible_orders = @client.api_calculate_possible_orders(initial_state, by_power: true)
|
||||
|
||||
@game.turns.build(
|
||||
number: 1,
|
||||
game_state: initial_state,
|
||||
possible_orders: possible_orders,
|
||||
phase: initial_state&.dig("name"),
|
||||
svg_date: svg_render,
|
||||
# Initialize svg_orders with NONE (and standard rendering as default)
|
||||
svg_orders: { "NONE" => svg_render }
|
||||
)
|
||||
|
||||
if @game.save
|
||||
{ success: true, message: "ゲームが初期化されました。" }
|
||||
else
|
||||
{ success: false, message: "初期ターンの作成に失敗しました: #{@game.errors.full_messages.join(', ')}" }
|
||||
end
|
||||
rescue StandardError => e
|
||||
Rails.logger.error("Game setup failed: #{e.message}")
|
||||
{ success: false, message: "予期せぬエラーが発生しました。" }
|
||||
end
|
||||
end
|
||||
87
app/services/order_submission_service.rb
Normal file
87
app/services/order_submission_service.rb
Normal file
@@ -0,0 +1,87 @@
|
||||
class OrderSubmissionService
|
||||
def initialize(turn, user, client: GameApiClient.new)
|
||||
@turn = turn
|
||||
@game = turn.game
|
||||
@user = user
|
||||
@client = client
|
||||
end
|
||||
|
||||
def submit(power:, orders:)
|
||||
# Check permissions
|
||||
unless valid_permission?(power)
|
||||
return { success: false, message: "権限がありません。" }
|
||||
end
|
||||
|
||||
# Handle multiplayer updates (orders_submitted flag)
|
||||
if !@game.solo_mode?
|
||||
participant = @game.participants.find_by(user: @user)
|
||||
participant.update(orders_submitted: true) if participant
|
||||
end
|
||||
|
||||
# Update orders tentatively for validation
|
||||
current_orders = @turn.orders || {}
|
||||
tentative_orders = current_orders.merge(power => orders)
|
||||
|
||||
# Validate
|
||||
validation_result = @client.api_calculate_validate(@turn.game_state, tentative_orders)
|
||||
|
||||
if validation_result.nil?
|
||||
return { success: false, message: "バリデーションサーバーとの通信に失敗しました。" }
|
||||
end
|
||||
|
||||
# Assuming validation_result is an array of error messages or empty if valid
|
||||
# Adjust this based on actual API response structure if known differently.
|
||||
# Common pattern: ["Order A is invalid", "Order B is impossible"]
|
||||
if validation_result.is_a?(Array) && validation_result.any?
|
||||
return { success: false, message: "命令に誤りがあります: #{validation_result.join(', ')}" }
|
||||
end
|
||||
|
||||
# Check if it returns hash with "errors" key?
|
||||
if validation_result.is_a?(Hash) && validation_result["errors"]&.any?
|
||||
return { success: false, message: "命令に誤りがあります: #{validation_result['errors'].join(', ')}" }
|
||||
end
|
||||
|
||||
# If passed validation, update current_orders
|
||||
current_orders[power] = orders
|
||||
|
||||
# Generate SVGs
|
||||
svg_orders_data = @turn.svg_orders || {}
|
||||
|
||||
# None SVG (first time only)
|
||||
unless svg_orders_data["NONE"]
|
||||
none_svg = @client.api_render(@turn.game_state, orders: nil)
|
||||
svg_orders_data["NONE"] = none_svg if none_svg
|
||||
end
|
||||
|
||||
# Power specific SVG
|
||||
power_orders = { power => orders }
|
||||
power_svg = @client.api_render(@turn.game_state, orders: power_orders)
|
||||
svg_orders_data[power] = power_svg if power_svg
|
||||
|
||||
# All SVG
|
||||
all_svg = @client.api_render(@turn.game_state, orders: current_orders)
|
||||
svg_orders_data["ALL"] = all_svg if all_svg
|
||||
|
||||
# Save
|
||||
if @turn.update(orders: current_orders, svg_orders: svg_orders_data)
|
||||
{ success: true, message: "命令を送信しました" }
|
||||
else
|
||||
{ success: false, message: "命令の送信に失敗しました" }
|
||||
end
|
||||
rescue StandardError => e
|
||||
Rails.logger.error("Order submission failed: #{e.message}")
|
||||
{ success: false, message: "予期せぬエラーが発生しました。" }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_permission?(power)
|
||||
return true if @game.solo_mode?
|
||||
|
||||
participant = @game.participants.find_by(user: @user)
|
||||
return false unless participant
|
||||
|
||||
# Compare case-insensitive
|
||||
participant.power&.upcase == power&.upcase
|
||||
end
|
||||
end
|
||||
135
app/services/turn_processing_service.rb
Normal file
135
app/services/turn_processing_service.rb
Normal file
@@ -0,0 +1,135 @@
|
||||
class TurnProcessingService
|
||||
def initialize(turn, client: GameApiClient.new)
|
||||
@turn = turn
|
||||
@game = turn.game
|
||||
@client = client
|
||||
end
|
||||
|
||||
def process(force: false)
|
||||
# Check for unsubmitted orders in multiplayer
|
||||
if !@game.solo_mode? && !@game.all_orders_submitted?
|
||||
unless force == "true"
|
||||
return { success: false, message: "全プレイヤーの命令入力が完了していません。強制ターン終了ボタンを使用してください。" }
|
||||
end
|
||||
end
|
||||
|
||||
current_orders = @turn.orders || {}
|
||||
|
||||
# 非人間プレイヤーのランダム命令を事前に生成(ターン処理の前に)
|
||||
if @game.auto_order_mode == "random" && !@game.solo_mode?
|
||||
# ゲーム状態から全国のリストを取得(参加者だけでなく全国にランダム命令を生成)
|
||||
all_powers = @turn.game_state&.dig("units")&.keys || []
|
||||
submitted_powers = current_orders.keys.map(&:upcase)
|
||||
|
||||
all_powers.each do |power|
|
||||
# 既に命令が提出済みならスキップ
|
||||
next if submitted_powers.include?(power.upcase)
|
||||
|
||||
auto_orders_response = @client.api_calculate_auto_orders(@turn.game_state, power)
|
||||
|
||||
if auto_orders_response && auto_orders_response["orders"]
|
||||
current_orders[power] = auto_orders_response["orders"]
|
||||
end
|
||||
end
|
||||
|
||||
# 自動生成した命令を現在のターンに保存
|
||||
@turn.update_columns(orders: current_orders) if current_orders.present?
|
||||
end
|
||||
|
||||
# 現在のターンにALL SVG(全プレイヤーの命令を含む画像)を保存
|
||||
# 履歴モードで「このターンでどんな命令が出されたか」を表示するため
|
||||
if current_orders.present?
|
||||
current_svg_orders = @turn.svg_orders || {}
|
||||
all_svg_current = @client.api_render(@turn.game_state, orders: current_orders)
|
||||
if all_svg_current
|
||||
current_svg_orders["ALL"] = all_svg_current
|
||||
@turn.update_columns(svg_orders: current_svg_orders)
|
||||
end
|
||||
end
|
||||
|
||||
# Calculate next state
|
||||
process_response = @client.api_calculate_process(@turn.game_state, current_orders)
|
||||
|
||||
unless process_response
|
||||
return { success: false, message: "ターン処理に失敗しました。" }
|
||||
end
|
||||
|
||||
new_game_state = process_response["game_state"]
|
||||
|
||||
# Transaction to ensure data consistency
|
||||
Game.transaction do
|
||||
# Create next turn foundation
|
||||
possible_orders = @client.api_calculate_possible_orders(new_game_state, by_power: true)
|
||||
svg = @client.api_render(new_game_state, orders: nil)
|
||||
|
||||
new_turn = @game.turns.build(
|
||||
number: @turn.number + 1,
|
||||
game_state: new_game_state,
|
||||
orders: {},
|
||||
phase: new_game_state&.dig("name"),
|
||||
possible_orders: possible_orders,
|
||||
svg_date: svg,
|
||||
svg_orders: { "NONE" => svg }
|
||||
)
|
||||
|
||||
# Save new turn
|
||||
if new_turn.save
|
||||
# Reset orders_submitted flag
|
||||
@game.participants.update_all(orders_submitted: false) unless @game.solo_mode?
|
||||
|
||||
# ハウスルール: 勝利条件判定
|
||||
result = check_victory_conditions(new_turn, new_game_state)
|
||||
return result if result
|
||||
|
||||
{ success: true, message: "ターンを終了し、次のフェーズへ進みました。" }
|
||||
else
|
||||
{ success: false, message: "次のターンの作成に失敗しました: #{new_turn.errors.full_messages.join(', ')}" }
|
||||
end
|
||||
end
|
||||
rescue StandardError => e
|
||||
Rails.logger.error("Turn processing failed: #{e.message}")
|
||||
Rails.logger.error(e.backtrace.join("\n"))
|
||||
{ success: false, message: "予期せぬエラーが発生しました。" }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_victory_conditions(new_turn, new_game_state)
|
||||
centers = new_game_state&.dig("centers") || {}
|
||||
|
||||
# 1. ソロ勝利判定
|
||||
if @game.solo_victory?(new_game_state)
|
||||
winner = @game.solo_victory_power(new_game_state)
|
||||
finish_game(new_turn, "solo", [ winner ])
|
||||
return { success: true, message: "#{winner} が #{@game.victory_sc_count} SC を獲得し、ソロ勝利しました!" }
|
||||
end
|
||||
|
||||
# 2. 年数制限判定
|
||||
if @game.year_limit_reached?(new_turn.phase)
|
||||
# SC数最多の国が勝者、同数なら引き分け
|
||||
max_sc = centers.values.map(&:size).max
|
||||
winners = centers.select { |_power, scs| scs.size == max_sc }.keys
|
||||
finish_game(new_turn, winners.size == 1 ? "year_limit_solo" : "year_limit_draw", winners)
|
||||
result_type = winners.size == 1 ? "#{winners.first} の勝利" : "#{winners.join(', ')} の引き分け"
|
||||
return { success: true, message: "年数制限(#{@game.year_limit}年)に達しました。#{result_type}です。" }
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
def finish_game(last_turn, result_type, winners)
|
||||
# 完了ターンを作成
|
||||
Turn.create!(
|
||||
game: @game,
|
||||
number: last_turn.number + 1,
|
||||
game_state: last_turn.game_state,
|
||||
orders: {},
|
||||
possible_orders: {},
|
||||
phase: "COMPLETED",
|
||||
svg_date: last_turn.svg_date,
|
||||
svg_orders: last_turn.svg_orders
|
||||
)
|
||||
@game.update!(status: "finished")
|
||||
Rails.logger.info("Game #{@game.id} finished: #{result_type}, winners: #{winners.join(', ')}")
|
||||
end
|
||||
end
|
||||
211
app/views/games/_form.html.erb
Normal file
211
app/views/games/_form.html.erb
Normal file
@@ -0,0 +1,211 @@
|
||||
<%= form_with(model: game, class: "space-y-6") do |form| %>
|
||||
<% if game.errors.any? %>
|
||||
<div class="rounded-md bg-red-50 p-4 mb-6">
|
||||
<div class="flex">
|
||||
<div class="flex-shrink-0">
|
||||
<svg class="h-5 w-5 text-red-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
|
||||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</div>
|
||||
<div class="ml-3">
|
||||
<h3 class="text-sm font-medium text-red-800">
|
||||
<%= pluralize(game.errors.count, "error") %> prohibited this game from being saved:
|
||||
</h3>
|
||||
<div class="mt-2 text-sm text-red-700">
|
||||
<ul class="list-disc pl-5 space-y-1">
|
||||
<% game.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.label :title, "ゲームタイトル", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.text_field :title, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md", placeholder: "例: 定例ディプロマシー会 #1" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% unless game.persisted? %>
|
||||
<% if current_user&.admin? %>
|
||||
<div>
|
||||
<%= form.label :game_mode, "ゲームモード", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.select :game_mode,
|
||||
options_for_select([
|
||||
['ソロモード (単独プレイ)', 'admin_mode'],
|
||||
['マルチプレイヤーモード (複数プレイ)', 'player_mode']
|
||||
], game.is_solo_mode ? 'admin_mode' : 'player_mode'),
|
||||
{},
|
||||
{ id: 'game-mode-select', class: 'shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md' } %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div id="participants-count-field" style="<%= game.solo_mode? ? 'display: none;' : '' %>">
|
||||
<%= form.label :participants_count, "参加人数", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.number_field :participants_count, min: 1, max: 7, step: 1, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">1人から7人まで設定可能です。</p>
|
||||
</div>
|
||||
|
||||
<div id="password-field" style="<%= game.solo_mode? ? 'display: none;' : '' %>">
|
||||
<%= form.label :password, "パスワード (任意)", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.password_field :password, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">知人同士でプレイする場合など、アクセス制限をかけたい場合に設定してください。</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div id="auto-fill-mode-field" style="<%= (game.solo_mode? && !game.persisted?) ? 'display: none;' : '' %>">
|
||||
<%= form.label :auto_order_mode, "自動処理モード", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.select :auto_order_mode,
|
||||
options_for_select([
|
||||
['HOLD (待機)', 'hold'],
|
||||
['ランダム動作', 'random']
|
||||
], game.auto_order_mode || 'hold'),
|
||||
{},
|
||||
{ class: 'shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md' } %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">注文未入力の国や、プレイヤー不在の国の動作を設定します。</p>
|
||||
</div>
|
||||
|
||||
<div id="turn-schedule-field" style="<%= (game.solo_mode? && !game.persisted?) ? 'display: none;' : '' %>">
|
||||
<%= form.label :turn_schedule, "ターン進行方式", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<% current_schedule = game.turn_schedule.presence %>
|
||||
<% preset_value = case current_schedule
|
||||
when nil then "manual"
|
||||
when "0" then "daily_0"
|
||||
else "custom"
|
||||
end %>
|
||||
<select id="turn_schedule_preset"
|
||||
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md"
|
||||
onchange="handleSchedulePresetChange(this)">
|
||||
<option value="manual" <%= 'selected' if preset_value == 'manual' %>>手動(管理者が処理)</option>
|
||||
<option value="daily_0" <%= 'selected' if preset_value == 'daily_0' %>>毎日1回(0時)</option>
|
||||
<option value="custom" <%= 'selected' if preset_value == 'custom' %>>カスタム...</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="custom-schedule-field" style="<%= preset_value == 'custom' ? '' : 'display: none;' %>" class="mt-2">
|
||||
<input type="text" id="custom_schedule_input"
|
||||
value="<%= current_schedule if preset_value == 'custom' %>"
|
||||
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md"
|
||||
placeholder="例: 0,12,18(毎日0時・12時・18時)"
|
||||
oninput="document.getElementById('turn_schedule_value').value = this.value">
|
||||
</div>
|
||||
<%= form.hidden_field :turn_schedule, id: "turn_schedule_value", value: current_schedule %>
|
||||
<p class="mt-2 text-sm text-gray-500">
|
||||
自動の場合、締切時刻を過ぎると未入力国はAutoOrderで処理され、ターンが自動進行します。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function handleSchedulePresetChange(select) {
|
||||
const customField = document.getElementById('custom-schedule-field');
|
||||
const hiddenField = document.getElementById('turn_schedule_value');
|
||||
const customInput = document.getElementById('custom_schedule_input');
|
||||
|
||||
switch (select.value) {
|
||||
case 'manual':
|
||||
customField.style.display = 'none';
|
||||
hiddenField.value = '';
|
||||
break;
|
||||
case 'daily_0':
|
||||
customField.style.display = 'none';
|
||||
hiddenField.value = '0';
|
||||
break;
|
||||
case 'custom':
|
||||
customField.style.display = 'block';
|
||||
hiddenField.value = customInput.value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<%= form.label :memo, "メモ", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.textarea :memo, rows: 3, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">ゲームに関するメモや注意事項があれば記載してください。</p>
|
||||
</div>
|
||||
|
||||
<!-- ハウスルール設定 -->
|
||||
<div class="border-t border-gray-200 pt-6 mt-6">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">ハウスルール</h3>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<%= form.label :year_limit, "年数制限", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.number_field :year_limit, min: 1901, max: 1999, step: 1, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md", placeholder: "空欄 = 無制限" %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">指定した年を超えるとゲームが自動終了します(例: 1910)。空欄で無制限。</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :victory_sc_count, "目標SC数", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.number_field :victory_sc_count, min: 1, max: 34, step: 1, value: game.victory_sc_count || 18, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">ソロ勝利に必要なSC数(デフォルト: 18)。</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :scoring_system, "スコアリング方式", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= form.select :scoring_system,
|
||||
options_for_select([
|
||||
['なし', 'none'],
|
||||
['SC数(獲得SC数がスコア)', 'sc_count'],
|
||||
['SC比率(全SC中の割合%)', 'sc_ratio'],
|
||||
['DSS(生存国で均等分割)', 'dss'],
|
||||
['SoS(二乗和比率)', 'sos']
|
||||
], game.scoring_system || 'none'),
|
||||
{},
|
||||
{ class: 'shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md' } %>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">ゲーム終了時のスコア計算方式を選択します。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pt-5">
|
||||
<div class="flex justify-end">
|
||||
<%= form.submit "保存する", class: "ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const gameModeSelect = document.getElementById('game-mode-select');
|
||||
const participantsField = document.getElementById('participants-count-field');
|
||||
const passwordField = document.getElementById('password-field');
|
||||
const autoFillField = document.getElementById('auto-fill-mode-field');
|
||||
|
||||
if (gameModeSelect) {
|
||||
gameModeSelect.addEventListener('change', function() {
|
||||
const isAdminMode = this.value === 'admin_mode';
|
||||
|
||||
if (isAdminMode) {
|
||||
if(participantsField) participantsField.style.display = 'none';
|
||||
if(passwordField) passwordField.style.display = 'none';
|
||||
if(autoFillField) autoFillField.style.display = 'none';
|
||||
} else {
|
||||
if(participantsField) participantsField.style.display = 'block';
|
||||
if(passwordField) passwordField.style.display = 'block';
|
||||
if(autoFillField) autoFillField.style.display = 'block';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<% end %>
|
||||
18
app/views/games/_game.html.erb
Normal file
18
app/views/games/_game.html.erb
Normal file
@@ -0,0 +1,18 @@
|
||||
<div id="<%= dom_id game %>" class="space-y-2">
|
||||
<h3 class="text-lg font-semibold text-gray-900 truncate">
|
||||
<%= game.title %>
|
||||
</h3>
|
||||
|
||||
<div class="mt-2 flex items-center text-sm text-gray-500">
|
||||
<svg class="flex-shrink-0 mr-1.5 h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path d="M9 6a3 3 0 11-6 0 3 3 0 016 0zM17 6a3 3 0 11-6 0 3 3 0 016 0zM12.93 17c.046-.327.07-.66.07-1a6.97 6.97 0 00-1.5-4.33A5 5 0 0119 16v1h-6.07zM6 11a5 5 0 015 5v1H1v-1a5 5 0 015-5z" />
|
||||
</svg>
|
||||
<%= game.participants_count %> Players
|
||||
</div>
|
||||
|
||||
<% if game.memo.present? %>
|
||||
<p class="mt-2 text-sm text-gray-600 line-clamp-2">
|
||||
<%= game.memo %>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
2
app/views/games/_game.json.jbuilder
Normal file
2
app/views/games/_game.json.jbuilder
Normal file
@@ -0,0 +1,2 @@
|
||||
json.extract! game, :id, :title, :participants_count, :memo, :created_at, :updated_at
|
||||
json.url game_url(game, format: :json)
|
||||
25
app/views/games/edit.html.erb
Normal file
25
app/views/games/edit.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<% content_for :title, "ゲーム編集" %>
|
||||
|
||||
<div class="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div class="md:flex md:items-center md:justify-between mb-8">
|
||||
<div class="flex-1 min-w-0">
|
||||
<h2 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
|
||||
ゲーム設定の編集
|
||||
</h2>
|
||||
</div>
|
||||
<div class="mt-4 flex md:mt-0 md:ml-4">
|
||||
<%= link_to @game, class: "inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" do %>
|
||||
<svg class="-ml-1 mr-2 h-5 w-5 text-gray-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M9.707 14.707a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 1.414L7.414 9H15a1 1 0 110 2H7.414l2.293 2.293a1 1 0 010 1.414z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
ゲームに戻る
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
|
||||
<div class="px-4 py-5 sm:p-6">
|
||||
<%= render "form", game: @game %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
117
app/views/games/index.html.erb
Normal file
117
app/views/games/index.html.erb
Normal file
@@ -0,0 +1,117 @@
|
||||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<% content_for :title, "Games" %>
|
||||
|
||||
<% content_for :top_content do %>
|
||||
<div class="flex justify-center mb-8">
|
||||
<%= image_tag "header-logo.png", width: 768, alt: "DipFront Logo" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="flex justify-between items-center mb-8">
|
||||
<h1 class="text-3xl font-bold text-gray-900">Games</h1>
|
||||
<%= link_to "New game", new_game_path, class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
|
||||
<% if current_user && @my_games.any? %>
|
||||
<div class="mb-10">
|
||||
<h2 class="text-2xl font-bold text-gray-900 mb-5 pl-2 border-l-4 border-[#c5a059] font-cinzel"><i class="fa-solid fa-flag mr-2 text-[#c5a059]"></i>参加中のゲーム</h2>
|
||||
<div id="my_games" class="grid gap-8 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<% @my_games.each do |game| %>
|
||||
<div class="diplomacy-card overflow-hidden rounded-lg transition-transform hover:-translate-y-1 duration-300">
|
||||
<div class="bg-green-900 px-4 py-3 border-b border-[#c5a059]">
|
||||
<h3 class="text-lg font-bold text-[#c5a059] font-cinzel truncate"><%= game.title %></h3>
|
||||
</div>
|
||||
<div class="px-4 py-5 sm:p-6 bg-white/90">
|
||||
<div class="text-sm text-gray-700 space-y-2">
|
||||
<% if game.status == 'finished' %>
|
||||
<p><i class="fa-solid fa-hourglass-end w-5 text-center text-gray-400"></i> <span class="font-medium">状態:</span> <span class="bg-gray-100 text-gray-800 text-xs px-2 py-0.5 rounded-full font-bold">履歴モード</span></p>
|
||||
<% else %>
|
||||
<p><i class="fa-solid fa-hourglass-half w-5 text-center text-gray-400"></i> <span class="font-medium">状態:</span> <%= game.status %></p>
|
||||
<% end %>
|
||||
<% if game.turns.present? %>
|
||||
<p><i class="fa-solid fa-calendar-days w-5 text-center text-gray-400"></i> <span class="font-medium">時期:</span> <%= parse_phase(game.turns.sort_by(&:number).last&.phase) %></p>
|
||||
<% end %>
|
||||
<% participant = game.participants.find_by(user: current_user) %>
|
||||
<% if participant %>
|
||||
<p><i class="fa-solid fa-chess-king w-5 text-center text-gray-400"></i> <span class="font-medium">国:</span> <span class="font-bold text-green-800"><%= participant.power || '未選択' %></span></p>
|
||||
<p>
|
||||
<i class="fa-solid fa-pen-fancy w-5 text-center text-gray-400"></i> <span class="font-medium">命令:</span>
|
||||
<span class="<%= participant.orders_submitted ? 'text-green-600 font-bold' : 'text-red-500 font-bold' %>">
|
||||
<%= participant.orders_submitted ? '完了' : '未完了' %>
|
||||
</span>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-gray-50/90 px-4 py-3 sm:px-6 border-t border-gray-200 flex justify-end">
|
||||
<%= link_to game, class: "inline-flex items-center text-sm font-bold text-green-900 hover:text-[#c5a059] transition-colors" do %>
|
||||
プレイする <i class="fa-solid fa-arrow-right ml-2"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if @recruiting_games.any? %>
|
||||
<div class="mb-10">
|
||||
<h2 class="text-2xl font-bold text-gray-900 mb-5 pl-2 border-l-4 border-[#c5a059] font-cinzel"><i class="fa-solid fa-user-plus mr-2 text-[#c5a059]"></i>募集中のゲーム</h2>
|
||||
<div id="recruiting_games" class="grid gap-8 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<% @recruiting_games.each do |game| %>
|
||||
<div class="diplomacy-card overflow-hidden rounded-lg transition-transform hover:-translate-y-1 duration-300">
|
||||
<div class="bg-green-800 px-4 py-3 border-b border-[#c5a059]">
|
||||
<h3 class="text-lg font-bold text-white font-cinzel truncate"><%= game.title %></h3>
|
||||
</div>
|
||||
<div class="px-4 py-5 sm:p-6 bg-white/90">
|
||||
<div class="text-sm text-gray-700 space-y-2">
|
||||
<p><i class="fa-solid fa-users w-5 text-center text-gray-400"></i> <span class="font-medium">参加者:</span> <%= game.participants.count %> / <%= game.participants_count %></p>
|
||||
<% if game.password_protected? %>
|
||||
<p class="text-amber-600"><i class="fa-solid fa-lock w-5 text-center"></i> パスワード保護</p>
|
||||
<% else %>
|
||||
<p class="text-green-600"><i class="fa-solid fa-lock-open w-5 text-center"></i> 公開ゲーム</p>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-gray-50/90 px-4 py-3 sm:px-6 border-t border-gray-200 flex justify-end">
|
||||
<%= link_to game, class: "inline-flex items-center text-sm font-bold text-green-900 hover:text-[#c5a059] transition-colors" do %>
|
||||
詳細を見る <i class="fa-solid fa-arrow-right ml-2"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mb-8">
|
||||
<h2 class="text-2xl font-bold text-gray-900 mb-5 pl-2 border-l-4 border-gray-400 font-cinzel"><i class="fa-solid fa-list mr-2 text-gray-500"></i>すべてのゲーム</h2>
|
||||
<div id="games" class="grid gap-8 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<% @games.each do |game| %>
|
||||
<div class="diplomacy-card overflow-hidden rounded-lg opacity-90 hover:opacity-100 transition-opacity">
|
||||
<div class="bg-gray-800 px-4 py-3 border-b border-gray-600">
|
||||
<h3 class="text-lg font-bold text-gray-300 font-cinzel truncate"><%= game.title %></h3>
|
||||
</div>
|
||||
<div class="px-4 py-5 sm:p-6 bg-white/90">
|
||||
<div class="text-sm text-gray-700">
|
||||
<% if game.status == 'finished' %>
|
||||
<p><i class="fa-solid fa-info-circle w-5 text-center text-gray-400"></i> <span class="bg-gray-100 text-gray-800 text-xs px-2 py-0.5 rounded-full font-bold">履歴モード</span></p>
|
||||
<% else %>
|
||||
<p><i class="fa-solid fa-info-circle w-5 text-center text-gray-400"></i> <%= game.status %></p>
|
||||
<% end %>
|
||||
<% if game.turns.present? %>
|
||||
<p><i class="fa-solid fa-calendar-days w-5 text-center text-gray-400"></i> <span class="font-medium">時期:</span> <%= parse_phase(game.turns.sort_by(&:number).last&.phase) %></p>
|
||||
<% end %>
|
||||
<p><i class="fa-solid fa-users w-5 text-center text-gray-400"></i> <span class="font-medium">参加人数:</span> <%= game.participants.size %> / <%= game.participants_count %></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-gray-50/90 px-4 py-3 sm:px-6 border-t border-gray-200 flex justify-end">
|
||||
<%= link_to game, class: "text-sm font-medium text-gray-600 hover:text-gray-900" do %>
|
||||
Show <i class="fa-solid fa-eye ml-1"></i>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
1
app/views/games/index.json.jbuilder
Normal file
1
app/views/games/index.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.array! @games, partial: "games/game", as: :game
|
||||
25
app/views/games/new.html.erb
Normal file
25
app/views/games/new.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<% content_for :title, "ゲーム作成" %>
|
||||
|
||||
<div class="max-w-2xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div class="md:flex md:items-center md:justify-between mb-8">
|
||||
<div class="flex-1 min-w-0">
|
||||
<h2 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
|
||||
新規ゲーム作成
|
||||
</h2>
|
||||
</div>
|
||||
<div class="mt-4 flex md:mt-0 md:ml-4">
|
||||
<%= link_to games_path, class: "inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" do %>
|
||||
<svg class="-ml-1 mr-2 h-5 w-5 text-gray-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M9.707 14.707a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 1.414L7.414 9H15a1 1 0 110 2H7.414l2.293 2.293a1 1 0 010 1.414z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
一覧に戻る
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
|
||||
<div class="px-4 py-5 sm:p-6">
|
||||
<%= render "form", game: @game %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
1153
app/views/games/show.html.erb
Normal file
1153
app/views/games/show.html.erb
Normal file
File diff suppressed because it is too large
Load Diff
1
app/views/games/show.json.jbuilder
Normal file
1
app/views/games/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! "games/game", game: @game
|
||||
110
app/views/layouts/application.html.erb
Normal file
110
app/views/layouts/application.html.erb
Normal file
@@ -0,0 +1,110 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= content_for(:title) || "Dip Front" %></title>
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="application-name" content="Dip Front">
|
||||
<meta name="mobile-web-app-capable" content="yes">
|
||||
<%= csrf_meta_tags %>
|
||||
<%= csp_meta_tag %>
|
||||
|
||||
<%= yield :head %>
|
||||
|
||||
<link rel="icon" href="/icon.png" type="image/png">
|
||||
<link rel="icon" href="/icon.svg" type="image/svg+xml">
|
||||
<link rel="apple-touch-icon" href="/icon.png">
|
||||
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&family=Lato:wght@400;700&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||
|
||||
<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
|
||||
<%= javascript_importmap_tags %>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Lato', sans-serif;
|
||||
background-image: url('<%= asset_path("background.png") %>');
|
||||
background-repeat: repeat;
|
||||
background-attachment: fixed;
|
||||
background-size: 1920px; /* Adjust as needed */
|
||||
}
|
||||
h1, h2, h3, h4, h5, h6, .font-cinzel {
|
||||
font-family: 'Cinzel', serif;
|
||||
}
|
||||
.diplomacy-card {
|
||||
background-color: rgba(255, 255, 255, 0.9);
|
||||
border: 1px solid #d4c5a9;
|
||||
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
.diplomacy-text-gold {
|
||||
color: #c5a059;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body class="text-gray-900 leading-normal tracking-normal">
|
||||
<nav class="bg-green-900/95 backdrop-blur-sm border-b border-[#c5a059] fixed w-full z-30 top-0 shadow-lg">
|
||||
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||
<div class="flex justify-between h-16">
|
||||
<div class="flex">
|
||||
<div class="flex-shrink-0 flex items-center">
|
||||
</div>
|
||||
<div class="hidden sm:-my-px sm:ml-6 sm:flex sm:space-x-8">
|
||||
<%= link_to root_path, class: "border-transparent text-green-100 hover:text-[#c5a059] hover:border-[#c5a059] inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium transition-colors duration-200" do %>
|
||||
<i class="fa-solid fa-house mr-2"></i> トップ
|
||||
<% end %>
|
||||
<%= link_to new_game_path, class: "border-transparent text-green-100 hover:text-[#c5a059] hover:border-[#c5a059] inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium transition-colors duration-200" do %>
|
||||
<i class="fa-solid fa-plus-circle mr-2"></i> New Game
|
||||
<% end %>
|
||||
<% if logged_in? && current_user.admin? %>
|
||||
<%= link_to users_path, class: "border-transparent text-green-100 hover:text-[#c5a059] hover:border-[#c5a059] inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium transition-colors duration-200" do %>
|
||||
<i class="fa-solid fa-users-cog mr-2"></i> ユーザー管理
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hidden sm:ml-6 sm:flex sm:items-center space-x-4">
|
||||
<% if logged_in? %>
|
||||
<%= link_to user_path(current_user), class: "text-sm text-green-100 hover:text-[#c5a059] transition-colors duration-200 flex items-center" do %>
|
||||
<i class="fa-solid fa-user-shield mr-2"></i>
|
||||
<%= current_user.username %>さん
|
||||
<% if current_user.admin? %>
|
||||
<span class="ml-1 px-2 py-0.5 text-[10px] font-bold text-green-900 bg-[#c5a059] rounded border border-yellow-600">ADMIN</span>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<%= button_to logout_path, method: :delete, class: "inline-flex items-center px-3 py-1.5 border border-[#c5a059] shadow-sm text-sm font-medium rounded text-[#c5a059] bg-green-900 hover:bg-green-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#c5a059] transition-colors duration-200" do %>
|
||||
<i class="fa-solid fa-right-from-bracket mr-2"></i> ログアウト
|
||||
<% end %>
|
||||
<% else %>
|
||||
<%= link_to "ログイン", login_path, class: "inline-flex items-center px-3 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
<%= link_to "新規登録", signup_path, class: "inline-flex items-center px-3 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="pt-24 pb-12 min-h-screen">
|
||||
<%= yield :top_content %>
|
||||
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 bg-white/85 backdrop-blur-sm rounded-xl shadow-xl border border-white/20 py-8">
|
||||
<% if notice %>
|
||||
<div class="bg-green-100/90 border border-green-400 text-green-800 px-4 py-3 rounded relative mb-6" role="alert">
|
||||
<span class="block sm:inline"><%= notice %></span>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if alert %>
|
||||
<div class="bg-red-100/90 border border-red-400 text-red-800 px-4 py-3 rounded relative mb-6" role="alert">
|
||||
<span class="block sm:inline"><%= alert %></span>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<main>
|
||||
<%= yield %>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
13
app/views/layouts/mailer.html.erb
Normal file
13
app/views/layouts/mailer.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<style>
|
||||
/* Email styles need to be inline */
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
1
app/views/layouts/mailer.text.erb
Normal file
1
app/views/layouts/mailer.text.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= yield %>
|
||||
22
app/views/pwa/manifest.json.erb
Normal file
22
app/views/pwa/manifest.json.erb
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "DipFront",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
},
|
||||
{
|
||||
"src": "/icon.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512",
|
||||
"purpose": "maskable"
|
||||
}
|
||||
],
|
||||
"start_url": "/",
|
||||
"display": "standalone",
|
||||
"scope": "/",
|
||||
"description": "DipFront.",
|
||||
"theme_color": "red",
|
||||
"background_color": "red"
|
||||
}
|
||||
26
app/views/pwa/service-worker.js
Normal file
26
app/views/pwa/service-worker.js
Normal file
@@ -0,0 +1,26 @@
|
||||
// Add a service worker for processing Web Push notifications:
|
||||
//
|
||||
// self.addEventListener("push", async (event) => {
|
||||
// const { title, options } = await event.data.json()
|
||||
// event.waitUntil(self.registration.showNotification(title, options))
|
||||
// })
|
||||
//
|
||||
// self.addEventListener("notificationclick", function(event) {
|
||||
// event.notification.close()
|
||||
// event.waitUntil(
|
||||
// clients.matchAll({ type: "window" }).then((clientList) => {
|
||||
// for (let i = 0; i < clientList.length; i++) {
|
||||
// let client = clientList[i]
|
||||
// let clientPath = (new URL(client.url)).pathname
|
||||
//
|
||||
// if (clientPath == event.notification.data.path && "focus" in client) {
|
||||
// return client.focus()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (clients.openWindow) {
|
||||
// return clients.openWindow(event.notification.data.path)
|
||||
// }
|
||||
// })
|
||||
// )
|
||||
// })
|
||||
28
app/views/sessions/new.html.erb
Normal file
28
app/views/sessions/new.html.erb
Normal file
@@ -0,0 +1,28 @@
|
||||
<% content_for :title, "ログイン" %>
|
||||
|
||||
<div class="max-w-md mx-auto">
|
||||
<h1 class="text-3xl font-bold text-gray-900 mb-6">ログイン</h1>
|
||||
|
||||
<%= form_with url: login_path, class: "space-y-6" do |f| %>
|
||||
<div>
|
||||
<%= label_tag :email, "メールアドレス", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= email_field_tag :email, params[:email], class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500", autofocus: true %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= label_tag :password, "パスワード", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= password_field_tag :password, nil, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= submit_tag "ログイン", class: "w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mt-6 text-center">
|
||||
<p class="text-sm text-gray-600">
|
||||
アカウントをお持ちでないですか?
|
||||
<%= link_to "新規登録", signup_path, class: "font-medium text-indigo-600 hover:text-indigo-500" %>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
42
app/views/turns/_form.html.erb
Normal file
42
app/views/turns/_form.html.erb
Normal file
@@ -0,0 +1,42 @@
|
||||
<%= form_with(model: turn) do |form| %>
|
||||
<% if turn.errors.any? %>
|
||||
<div style="color: red">
|
||||
<h2><%= pluralize(turn.errors.count, "error") %> prohibited this turn from being saved:</h2>
|
||||
|
||||
<ul>
|
||||
<% turn.errors.each do |error| %>
|
||||
<li><%= error.full_message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= form.label :number, style: "display: block" %>
|
||||
<%= form.number_field :number %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :phase, style: "display: block" %>
|
||||
<%= form.text_field :phase %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :game_state, style: "display: block" %>
|
||||
<%= form.text_field :game_state %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :svg_date, style: "display: block" %>
|
||||
<%= form.textarea :svg_date %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label :game_id, style: "display: block" %>
|
||||
<%= form.text_field :game_id %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
39
app/views/turns/_turn.html.erb
Normal file
39
app/views/turns/_turn.html.erb
Normal file
@@ -0,0 +1,39 @@
|
||||
<div id="<%= dom_id turn %>">
|
||||
<div>
|
||||
<strong>Number:</strong>
|
||||
<%= turn.number %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Phase:</strong>
|
||||
<%= turn.phase %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Game state:</strong>
|
||||
<pre><%= JSON.pretty_generate(turn.game_state) if turn.game_state %></pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Possible orders:</strong>
|
||||
<pre><%= JSON.pretty_generate(turn.possible_orders) if turn.possible_orders %></pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Orders:</strong>
|
||||
<pre><%= JSON.pretty_generate(turn.orders) if turn.orders %></pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Map View:</strong>
|
||||
<div class="game-map">
|
||||
<%= turn.svg_date.html_safe if turn.svg_date %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<strong>Game ID:</strong>
|
||||
<%= turn.game_id %>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
2
app/views/turns/_turn.json.jbuilder
Normal file
2
app/views/turns/_turn.json.jbuilder
Normal file
@@ -0,0 +1,2 @@
|
||||
json.extract! turn, :id, :number, :phase, :game_state, :possible_orders, :orders, :svg_date, :game_id, :created_at, :updated_at
|
||||
json.url turn_url(turn, format: :json)
|
||||
12
app/views/turns/edit.html.erb
Normal file
12
app/views/turns/edit.html.erb
Normal file
@@ -0,0 +1,12 @@
|
||||
<% content_for :title, "Editing turn" %>
|
||||
|
||||
<h1>Editing turn</h1>
|
||||
|
||||
<%= render "form", turn: @turn %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Show this turn", @turn %> |
|
||||
<%= link_to "Back to turns", turns_path %>
|
||||
</div>
|
||||
19
app/views/turns/index.html.erb
Normal file
19
app/views/turns/index.html.erb
Normal file
@@ -0,0 +1,19 @@
|
||||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<% content_for :title, "Turns" %>
|
||||
|
||||
<h1>Turns</h1>
|
||||
|
||||
<div id="turns">
|
||||
<% @turns.each do |turn| %>
|
||||
<%= turn.id %>
|
||||
<%= turn.game_id %>
|
||||
<%= turn.number %>
|
||||
<%= turn.phase %>
|
||||
<p>
|
||||
<%= link_to "Show this turn", turn %>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= link_to "New turn", new_turn_path %>
|
||||
1
app/views/turns/index.json.jbuilder
Normal file
1
app/views/turns/index.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.array! @turns, partial: "turns/turn", as: :turn
|
||||
11
app/views/turns/new.html.erb
Normal file
11
app/views/turns/new.html.erb
Normal file
@@ -0,0 +1,11 @@
|
||||
<% content_for :title, "New turn" %>
|
||||
|
||||
<h1>New turn</h1>
|
||||
|
||||
<%= render "form", turn: @turn %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Back to turns", turns_path %>
|
||||
</div>
|
||||
42
app/views/turns/show.html.erb
Normal file
42
app/views/turns/show.html.erb
Normal file
@@ -0,0 +1,42 @@
|
||||
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<div class="mb-6 flex justify-between items-start">
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold text-gray-900">Turn <%= @turn.number %> Details</h1>
|
||||
<%= link_to 'Back to Game', game_path(@turn.game), class: "mt-2 inline-block text-indigo-600 hover:text-indigo-900" %>
|
||||
</div>
|
||||
<div class="flex space-x-2">
|
||||
<%= link_to 'Edit', edit_turn_path(@turn), class: "inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
<%= button_to "Destroy this turn", @turn, method: :delete, class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500", data: { turbo_confirm: "Are you sure?" } %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg mb-6">
|
||||
<div class="px-4 py-5 sm:px-6">
|
||||
<h3 class="text-lg leading-6 font-medium text-gray-900">SVG Orders Data</h3>
|
||||
<p class="mt-1 max-w-2xl text-sm text-gray-500">Stored SVG images for each power.</p>
|
||||
</div>
|
||||
<div class="border-t border-gray-200 px-4 py-5 sm:p-0">
|
||||
<dl class="sm:divide-y sm:divide-gray-200">
|
||||
<% if @turn.svg_orders.present? %>
|
||||
<% @turn.svg_orders.each do |key, svg| %>
|
||||
<%= key %>
|
||||
<%= svg.html_safe %>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="py-4 sm:py-5 sm:px-6">
|
||||
<p class="text-sm text-gray-500">No SVG orders data found.</p>
|
||||
</div>
|
||||
<% end %>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
|
||||
<div class="px-4 py-5 sm:px-6">
|
||||
<h3 class="text-lg leading-6 font-medium text-gray-900">Debug Info</h3>
|
||||
</div>
|
||||
<div class="border-t border-gray-200 px-4 py-5 sm:p-6">
|
||||
<pre class="text-xs bg-gray-100 p-2 rounded overflow-auto"><%= JSON.pretty_generate(@turn.attributes.except("svg_orders", "svg_date")) %></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
1
app/views/turns/show.json.jbuilder
Normal file
1
app/views/turns/show.json.jbuilder
Normal file
@@ -0,0 +1 @@
|
||||
json.partial! "turns/turn", turn: @turn
|
||||
55
app/views/users/edit.html.erb
Normal file
55
app/views/users/edit.html.erb
Normal file
@@ -0,0 +1,55 @@
|
||||
<% content_for :title, "ユーザー編集" %>
|
||||
|
||||
<div class="max-w-2xl mx-auto">
|
||||
<div class="mb-6">
|
||||
<h1 class="text-3xl font-bold text-gray-900">ユーザー編集</h1>
|
||||
<p class="mt-2 text-sm text-gray-600"><%= @user.username %> の情報を編集</p>
|
||||
</div>
|
||||
|
||||
<%= form_with model: @user, class: "space-y-6" do |f| %>
|
||||
<% if @user.errors.any? %>
|
||||
<div class="bg-red-100 border border-red-400 text-red-800 px-4 py-3 rounded relative" role="alert">
|
||||
<strong class="font-bold">エラーがあります:</strong>
|
||||
<ul class="mt-2 list-disc list-inside">
|
||||
<% @user.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= f.label :username, "ユーザー名", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.text_field :username, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.label :email, "メールアドレス", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.email_field :email, class: "mt-1 block w-full rounded-md border-gray-300 bg-gray-100 shadow-sm", disabled: true %>
|
||||
<p class="mt-1 text-sm text-gray-500">メールアドレスは変更できません</p>
|
||||
</div>
|
||||
|
||||
<div class="border-t border-gray-200 pt-6">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4">パスワード変更</h3>
|
||||
<p class="text-sm text-gray-600 mb-4">パスワードを変更する場合のみ入力してください。空欄の場合は変更されません。</p>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<%= f.label :password, "新しいパスワード", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.password_field :password, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500", autocomplete: "new-password" %>
|
||||
<p class="mt-1 text-sm text-gray-500">6文字以上</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.label :password_confirmation, "新しいパスワード(確認)", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.password_field :password_confirmation, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500", autocomplete: "new-password" %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center justify-between pt-6 border-t border-gray-200">
|
||||
<%= link_to "キャンセル", user_path(@user), class: "inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50" %>
|
||||
<%= f.submit "更新", class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
82
app/views/users/index.html.erb
Normal file
82
app/views/users/index.html.erb
Normal file
@@ -0,0 +1,82 @@
|
||||
<% content_for :title, "ユーザー管理" %>
|
||||
|
||||
<div class="mb-6">
|
||||
<h1 class="text-3xl font-bold text-gray-900">ユーザー管理</h1>
|
||||
<p class="mt-2 text-sm text-gray-600">登録されているユーザーの一覧と管理</p>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
|
||||
<table class="min-w-full divide-y divide-gray-200">
|
||||
<thead class="bg-gray-50">
|
||||
<tr>
|
||||
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
ユーザー名
|
||||
</th>
|
||||
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
メールアドレス
|
||||
</th>
|
||||
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
権限
|
||||
</th>
|
||||
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
登録日
|
||||
</th>
|
||||
<th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
操作
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="bg-white divide-y divide-gray-200">
|
||||
<% @users.each do |user| %>
|
||||
<tr>
|
||||
<td class="px-6 py-4 whitespace-nowrap">
|
||||
<div class="flex items-center">
|
||||
<div class="text-sm font-medium text-gray-900">
|
||||
<%= user.username %>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap">
|
||||
<div class="text-sm text-gray-900"><%= user.email %></div>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap">
|
||||
<% if user.admin? %>
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">
|
||||
管理者
|
||||
</span>
|
||||
<% else %>
|
||||
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
|
||||
一般ユーザー
|
||||
</span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
<%= user.created_at.strftime("%Y年%m月%d日") %>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium space-x-2">
|
||||
<%= link_to "詳細", user_path(user), class: "text-indigo-600 hover:text-indigo-900" %>
|
||||
<%= link_to "編集", edit_user_path(user), class: "text-blue-600 hover:text-blue-900" %>
|
||||
<% if user != current_user %>
|
||||
<%= button_to "#{user.admin? ? '管理者解除' : '管理者に昇格'}",
|
||||
toggle_admin_user_path(user),
|
||||
method: :patch,
|
||||
class: "inline text-yellow-600 hover:text-yellow-900",
|
||||
data: { confirm: "#{user.admin? ? '管理者権限を削除' : '管理者権限を付与'}しますか?" } %>
|
||||
<%= button_to "削除",
|
||||
user_path(user),
|
||||
method: :delete,
|
||||
class: "inline text-red-600 hover:text-red-900",
|
||||
data: { confirm: "#{user.username}を削除しますか?" } %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="mt-6">
|
||||
<p class="text-sm text-gray-600">
|
||||
合計: <%= @users.count %>ユーザー
|
||||
</p>
|
||||
</div>
|
||||
50
app/views/users/new.html.erb
Normal file
50
app/views/users/new.html.erb
Normal file
@@ -0,0 +1,50 @@
|
||||
<% content_for :title, "新規登録" %>
|
||||
|
||||
<div class="max-w-md mx-auto">
|
||||
<h1 class="text-3xl font-bold text-gray-900 mb-6">新規登録</h1>
|
||||
|
||||
<%= form_with model: @user, url: signup_path, class: "space-y-6" do |f| %>
|
||||
<% if @user.errors.any? %>
|
||||
<div class="bg-red-100 border border-red-400 text-red-800 px-4 py-3 rounded relative" role="alert">
|
||||
<strong class="font-bold">エラーがあります:</strong>
|
||||
<ul class="mt-2 list-disc list-inside">
|
||||
<% @user.errors.full_messages.each do |message| %>
|
||||
<li><%= message %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div>
|
||||
<%= f.label :username, "ユーザー名", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.text_field :username, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500", autofocus: true %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.label :email, "メールアドレス", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.email_field :email, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.label :password, "パスワード", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.password_field :password, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" %>
|
||||
<p class="mt-1 text-sm text-gray-500">6文字以上</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.label :password_confirmation, "パスワード(確認)", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= f.password_field :password_confirmation, class: "mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= f.submit "登録", class: "w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mt-6 text-center">
|
||||
<p class="text-sm text-gray-600">
|
||||
既にアカウントをお持ちですか?
|
||||
<%= link_to "ログイン", login_path, class: "font-medium text-indigo-600 hover:text-indigo-500" %>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
83
app/views/users/show.html.erb
Normal file
83
app/views/users/show.html.erb
Normal file
@@ -0,0 +1,83 @@
|
||||
<% content_for :title, @user.username %>
|
||||
|
||||
<div class="mb-6 flex items-center justify-between">
|
||||
<div>
|
||||
<h1 class="text-3xl font-bold text-gray-900"><%= @user.username %></h1>
|
||||
<% if @user.admin? %>
|
||||
<span class="mt-2 inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-800">
|
||||
管理者
|
||||
</span>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="flex space-x-3">
|
||||
<%= link_to "編集", edit_user_path(@user), class: "inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50" %>
|
||||
<%= link_to "一覧に戻る", users_path, class: "inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
|
||||
<div class="px-4 py-5 sm:px-6">
|
||||
<h3 class="text-lg leading-6 font-medium text-gray-900">
|
||||
ユーザー情報
|
||||
</h3>
|
||||
</div>
|
||||
<div class="border-t border-gray-200">
|
||||
<dl>
|
||||
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||
<dt class="text-sm font-medium text-gray-500">
|
||||
ユーザー名
|
||||
</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||
<%= @user.username %>
|
||||
</dd>
|
||||
</div>
|
||||
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||
<dt class="text-sm font-medium text-gray-500">
|
||||
メールアドレス
|
||||
</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||
<%= @user.email %>
|
||||
</dd>
|
||||
</div>
|
||||
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||
<dt class="text-sm font-medium text-gray-500">
|
||||
権限
|
||||
</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||
<%= @user.admin? ? "管理者" : "一般ユーザー" %>
|
||||
</dd>
|
||||
</div>
|
||||
<div class="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||
<dt class="text-sm font-medium text-gray-500">
|
||||
登録日
|
||||
</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||
<%= @user.created_at.strftime("%Y年%m月%d日 %H:%M") %>
|
||||
</dd>
|
||||
</div>
|
||||
<div class="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||
<dt class="text-sm font-medium text-gray-500">
|
||||
最終更新日
|
||||
</dt>
|
||||
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||
<%= @user.updated_at.strftime("%Y年%m月%d日 %H:%M") %>
|
||||
</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% if @user != current_user %>
|
||||
<div class="mt-6 flex space-x-3">
|
||||
<%= button_to "#{@user.admin? ? '管理者権限を削除' : '管理者権限を付与'}",
|
||||
toggle_admin_user_path(@user),
|
||||
method: :patch,
|
||||
class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-yellow-600 hover:bg-yellow-700",
|
||||
data: { confirm: "#{@user.admin? ? '管理者権限を削除' : '管理者権限を付与'}しますか?" } %>
|
||||
<%= button_to "ユーザーを削除",
|
||||
user_path(@user),
|
||||
method: :delete,
|
||||
class: "inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-red-600 hover:bg-red-700",
|
||||
data: { confirm: "#{@user.username}を削除しますか?" } %>
|
||||
</div>
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user