Refactor GridCharactersController
This is a similar refactor to GridWeaponsController
This commit is contained in:
parent
ebbbdc174b
commit
9387bd321f
1 changed files with 57 additions and 23 deletions
|
|
@ -3,34 +3,21 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class GridCharactersController < Api::V1::ApiController
|
class GridCharactersController < Api::V1::ApiController
|
||||||
|
attr_reader :party, :incoming_character, :current_characters
|
||||||
|
|
||||||
|
before_action :find_party, only: :create
|
||||||
|
before_action :find_incoming_character, only: :create
|
||||||
|
before_action :find_current_characters, only: :create
|
||||||
|
|
||||||
def create
|
def create
|
||||||
party = Party.find(character_params[:party_id])
|
if conflict_characters&.length.positive?
|
||||||
incoming_character = Character.find(character_params[:character_id])
|
|
||||||
|
|
||||||
render_unauthorized_response if current_user && (party.user != current_user)
|
|
||||||
|
|
||||||
current_characters = party.characters.map do |c|
|
|
||||||
Character.find(c.character.id).character_id
|
|
||||||
end.flatten
|
|
||||||
|
|
||||||
# Check all character ids on incoming character against current characters
|
|
||||||
conflict_ids = (current_characters & incoming_character.character_id)
|
|
||||||
|
|
||||||
if conflict_ids.length.positive?
|
|
||||||
# Find conflicting character ids in party characters
|
|
||||||
conflict_characters = party.characters.filter do |c|
|
|
||||||
c if (conflict_ids & c.character.character_id).length.positive?
|
|
||||||
end.flatten
|
|
||||||
|
|
||||||
# Render a template with the conflicting and incoming characters,
|
# Render a template with the conflicting and incoming characters,
|
||||||
# as well as the selected position, so the user can be presented with
|
# as well as the selected position, so the user can be presented with
|
||||||
# a decision.
|
# a decision.
|
||||||
|
|
||||||
# Up to 3 characters can be removed at the same time
|
# Up to 3 characters can be removed at the same time
|
||||||
render json: ConflictBlueprint.render(nil, view: :characters,
|
conflict_view = render_conflict_view(conflict_characters, incoming_character, character_params[:position])
|
||||||
conflict_characters: conflict_characters,
|
render json: conflict_view
|
||||||
incoming_character: incoming_character,
|
|
||||||
incoming_position: character_params[:position])
|
|
||||||
else
|
else
|
||||||
# Replace the grid character in the position if it is already filled
|
# Replace the grid character in the position if it is already filled
|
||||||
if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists?
|
if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists?
|
||||||
|
|
@ -43,7 +30,10 @@ module Api
|
||||||
character_id: incoming_character.id))
|
character_id: incoming_character.id))
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: GridCharacterBlueprint.render(character, view: :nested), status: :created if character.save!
|
if character.save!
|
||||||
|
grid_character_view = render_grid_character_view(character)
|
||||||
|
render json: grid_character_view, status: :created
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -90,12 +80,56 @@ module Api
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def conflict_characters
|
||||||
|
@conflict_characters ||= find_conflict_characters(incoming_character)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_conflict_characters(incoming_character)
|
||||||
|
# Check all character ids on incoming character against current characters
|
||||||
|
conflict_ids = (current_characters & incoming_character.character_id)
|
||||||
|
|
||||||
|
return unless conflict_ids.length.positive?
|
||||||
|
|
||||||
|
# Find conflicting character ids in party characters
|
||||||
|
party.characters.filter do |c|
|
||||||
|
c if (conflict_ids & c.character.character_id).length.positive?
|
||||||
|
end.flatten
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_current_characters
|
||||||
|
# Make a list of all character IDs
|
||||||
|
@current_characters = party.characters.map do |c|
|
||||||
|
Character.find(c.character.id).character_id
|
||||||
|
end.flatten
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_incoming_character
|
||||||
|
@incoming_character = Character.find(character_params[:character_id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_party
|
||||||
|
@party = Party.find(character_params[:party_id])
|
||||||
|
render_unauthorized_response if current_user && (party.user != current_user)
|
||||||
|
end
|
||||||
|
|
||||||
# Specify whitelisted properties that can be modified.
|
# Specify whitelisted properties that can be modified.
|
||||||
def character_params
|
def character_params
|
||||||
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting,
|
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting,
|
||||||
:incoming)
|
:incoming)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_conflict_view(conflict_characters, incoming_character, incoming_position)
|
||||||
|
ConflictBlueprint.render(nil,
|
||||||
|
view: :characters,
|
||||||
|
conflict_characters: conflict_characters,
|
||||||
|
incoming_character: incoming_character,
|
||||||
|
incoming_position: incoming_position)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_grid_character_view(grid_character)
|
||||||
|
GridCharacterBlueprint.render(grid_character, view: :nested)
|
||||||
|
end
|
||||||
|
|
||||||
def resolve_params
|
def resolve_params
|
||||||
params.require(:resolve).permit(:position, :incoming, conflicting: [])
|
params.require(:resolve).permit(:position, :incoming, conflicting: [])
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue