LoginSignup
4

More than 5 years have passed since last update.

カードゲームを作る その10

Last updated at Posted at 2016-05-02

この記事は

とあるカードゲームが非常に面白く、
そのゲームの見た目クローンを作ろうと一人のプログラマーが奮闘していくブログのような記事です。

その8からの続き
(その9は原因不明のエラーに悩まされた記憶です。
  まだ解決していないので、解決してから載せます。
  よければteratailにあげていますので、
  おヒマがあれば回答くださると両手を上げて喜びます。)

ここでは、前回作ったプログラムのリファクタリングをします。
コントローラーが分厚いので他に移します。

Concernに移す

まずコントローラが薄くなった結果を

app/controllers/battles_controller.rb
hallucigeniaAir:CambrianServer TakuAndo$ cat app/controllers/battles_controller.rb
# encoding: utf-8
#  戦闘コントローラ
#  Author:: Kuroko Sin
#  Date::   2016/2/1

class BattlesController < ApplicationController
  include Battle::BattleTimeline
  before_action :set_battle, only: [:show, :update, :destroy]

  #  戦闘API メインアクション
  #  Param::  BType, UId, BTarget, V
  #  Return:: Result
  def index
    # @battles = Battle.all
    #
    # render json: @battles
    # render json: ["hoge", params[:BType],
    #                       params[:UId], params[:BTarget], params[:V] ]
    _BType = params[:BType]
    _UId   = params[:UId]
    _BTargetId = params[:BTarget]
    _V = params[:V]

    #binding.pry
    # myDeck 取得
    _UDeck = DeckInfo.find_by(UserId: _UId)
    # targetDeck 取得
    _BTarget = DeckInfo.find_by(UserId: _BTargetId)

    # binding.pry
    Rails.root.join("app/models/concern/battle/battle_timeline.rb")
    _result = lets_battle( _UDeck, _BTarget )

    render json:  _result
  end

  # GET /battles/1
  # GET /battles/1.json
  def show
    render json: @battle
  end

  # POST /battles
  # POST /battles.json
  def create
    @battle = Battle.new(battle_params)

    if @battle.save
      render json: @battle, status: :created, location: @battle
    else
      render json: @battle.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /battles/1
  # PATCH/PUT /battles/1.json
  def update
    @battle = Battle.find(params[:id])

    if @battle.update(battle_params)
      head :no_content
    else
      render json: @battle.errors, status: :unprocessable_entity
    end
  end

  # DELETE /battles/1
  # DELETE /battles/1.json
  def destroy
    @battle.destroy

    head :no_content
  end

  private

    def set_battle
      @battle = Battle.find(params[:id])
    end

    def battle_params
      params.require(:battle).permit(:ID, :Btype, :BTarget, :V, :UA)
    end
end

移した先のコード。Concernに移動させました。

app/models/concerns/battle/battle_timeline.rb
module Battle::BattleTimeline extend ActiveSupport::Concern
  def set_field( ur, en )
    return ur
  end

  def set_bonus( deck )
    return deck
  end

  def lets_battle( first_attacker, post_attacker )
    _f = first_attacker
    _p = post_attacker
    _Result = 0
    begin
      case _f.UserId
      when "Kingdom"
        case _p.UserId
        when "Kingdom"
          _BWinner = 0
        when "Spiritual"
          _BWinner = 0
        when "Tribal"
          _BWinner = -1
        when "Pluto"
          _BWinner = 1
        else
          _Result = 9
        end
      when "Spiritual"
        case _p.UserId
        when "Kingdom"
          _BWinner = 0
        when "Spiritual"
          _BWinner = 0
        when "Tribal"
          _BWinner = 1
        when "Pluto"
          _BWinner = -1
        else
          _Result = 9
        end
       when "Tribal"
         case _p.UserId
         when "Kingdom"
           _BWinner = 1
         when "Spiritual"
           _BWinner = -1
         when "Tribal"
           _BWinner = 0
         when "Pluto"
           _BWinner = 0
         else
           _Result = 9
         end
       when "Pluto"
         case _p.UserId
         when "Kingdom"
           _BWinner = -1
         when "Spiritual"
           _BWinner = 1
         when "Tribal"
           _BWinner = 0
         when "Pluto"
           _BWinner = 0
         else
           _Result = 9
         end
       else
         _Result = 9
       end

    rescue => ex
      _Result = 9
      _BProcess = {}
    end

    _ret = {'Result' => _Result, 'BWinner' => _BWinner, 'BProcess' => _BProcess }
    return _ret
  end
end

、、、はい。本当に移しただけです。
で、テストをやってうまく言ってることを確認しました

$ rake test
Run options: --seed 42501

# Running:

......................

Finished in 0.299227s, 73.5227 runs/s, 193.8327 assertions/s.

22 runs, 58 assertions, 0 failures, 0 errors, 0 skips

いくつか言い訳

  • 不定期連載です。
  • 多分途中で挫折します。
  • 筆者のメモも兼ねているため過去の記事も遠慮なく随時編集いたします。
  • プログラミングの開発経験はあってもゲームの開発経験はこれが初めてです。

等色々ありますが、生暖かい目で応援してくださると光栄です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4