hensei-api/app/controllers/api/v1/grid_characters_controller.rb
Justin Edmund 559507977b Added logic for conflict resolution
* Conflict is detected when attempting to save a new GridCharacter
* New `resolve` method executes the replacement
* Template to render a decision on the frontend
2022-11-19 06:03:39 -08:00

102 lines
3.8 KiB
Ruby

class Api::V1::GridCharactersController < Api::V1::ApiController
def create
party = Party.find(character_params[:party_id])
incoming_character = Character.find(character_params[:character_id])
if current_user
if party.user != current_user
render_unauthorized_response
end
end
current_characters = party.characters.map { |c|
Character.find(c.character.id).character_id
}.flatten
# Check all character ids on incoming character against current characters
conflict_ids = (current_characters & incoming_character.character_id)
if conflict_ids.length > 0
# Find conflicting character ids in party characters
conflict_characters = party.characters.filter { |c|
c if (conflict_ids & c.character.character_id).length > 0
}.flatten
# 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_characters = conflict_characters
@incoming_character = incoming_character
@incoming_position = character_params[:position]
render :conflict, status: :ok
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
render :show, status: :created if @character.save!
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) unless !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 :show, status: :created if @character.save!
end
def update_uncap_level
@character = GridCharacter.find(character_params[:id])
if current_user
if @character.party.user != current_user
render_unauthorized_response
end
end
@character.uncap_level = character_params[:uncap_level]
render :show, status: :ok if @character.save!
end
def destroy
end
private
# 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 resolve_params
params.require(:resolve).permit(:position, :incoming, :conflicting => [])
end
end