138 lines
5.2 KiB
Ruby
138 lines
5.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Api
|
|
module V1
|
|
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
|
|
if conflict_characters&.length.positive?
|
|
# Render a template with the conflicting and incoming characters,
|
|
# as well as the selected position, so the user can be presented with
|
|
# a decision.
|
|
|
|
# Up to 3 characters can be removed at the same time
|
|
conflict_view = render_conflict_view(conflict_characters, incoming_character, character_params[:position])
|
|
render json: conflict_view
|
|
else
|
|
# Replace the grid character in the position if it is already filled
|
|
if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists?
|
|
character = GridCharacter.where(party_id: party.id, position: character_params[:position]).limit(1)[0]
|
|
character.character_id = incoming_character.id
|
|
|
|
# Otherwise, create a new grid character
|
|
else
|
|
character = GridCharacter.create!(character_params.merge(party_id: party.id,
|
|
character_id: incoming_character.id))
|
|
end
|
|
|
|
if character.save!
|
|
grid_character_view = render_grid_character_view(character)
|
|
render json: grid_character_view, status: :created
|
|
end
|
|
end
|
|
end
|
|
|
|
def resolve
|
|
incoming = Character.find(resolve_params[:incoming])
|
|
conflicting = resolve_params[:conflicting].map { |id| GridCharacter.find(id) }
|
|
party = conflicting.first.party
|
|
|
|
# Destroy each conflicting character
|
|
conflicting.each { |character| GridCharacter.destroy(character.id) }
|
|
|
|
# Destroy the character at the desired position if it exists
|
|
existing_character = GridCharacter.where(party: party.id, position: resolve_params[:position]).first
|
|
GridCharacter.destroy(existing_character.id) if existing_character
|
|
|
|
if incoming.special
|
|
uncap_level = 3
|
|
uncap_level = 5 if incoming.ulb
|
|
uncap_level = 4 if incoming.flb
|
|
else
|
|
uncap_level = 4
|
|
uncap_level = 6 if incoming.ulb
|
|
uncap_level = 5 if incoming.flb
|
|
end
|
|
|
|
character = GridCharacter.create!(party_id: party.id, character_id: incoming.id,
|
|
position: resolve_params[:position], uncap_level: uncap_level)
|
|
render json: GridCharacterBlueprint.render(character, view: :nested), status: :created if character.save!
|
|
end
|
|
|
|
def update_uncap_level
|
|
character = GridCharacter.find(character_params[:id])
|
|
|
|
render_unauthorized_response if current_user && (character.party.user != current_user)
|
|
|
|
character.uncap_level = character_params[:uncap_level]
|
|
return unless character.save!
|
|
|
|
render json: GridCharacterBlueprint.render(character, view: :nested, root: :grid_character)
|
|
end
|
|
|
|
# TODO: Implement removing characters
|
|
def destroy; end
|
|
|
|
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.
|
|
def character_params
|
|
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting,
|
|
:incoming)
|
|
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
|
|
params.require(:resolve).permit(:position, :incoming, conflicting: [])
|
|
end
|
|
end
|
|
end
|
|
end
|