summaryrefslogtreecommitdiff
path: root/app/controllers/characters_controller.rb
blob: 77e1a94ab74b344201b03d765362a38954ee59db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class CharactersController < ApplicationController
  skip_before_action :redirect_if_no_active_character, only: [:new, :create]

  def show
    @character = Character.find(params[:id])
  end

  def new
    @character = Character.new
  end

  def create
    @character = Character.new(character_params)
    @character.user = current_user
    if @character.save
      current_user.update(active_character: @character)
      redirect_to @character
    else
      flash[:alert] = "Error creating character: #{@character.errors.full_messages.join(", ")}"
      redirect_to action: :new
    end
  end

  def set_combat_styles
    @character = Character.find(params[:character_id])
    unless @character == current_char
      flash[:alert] = "You can't set the combat styles of another character."
      redirect_to character_path(@character) and return
    end
    if @character.update(offensive_style: params[:offensive_style],
                         defensive_style: params[:defensive_style])
      flash[:notice] = "Changed combat styles to #{@character.offensive_style} and #{@character.defensive_style}."
    else
      flash[:alert] = "Failed to set combat styles."
    end
    redirect_to character_path(@character)
  end

  private
    def character_params
      params.require(:character).permit(:name)
    end
end