Merge pull request #15 from jedmund/char-dedupe

Conflict resolution → Characters
This commit is contained in:
Justin Edmund 2022-11-19 17:05:12 -08:00 committed by GitHub
commit 9708ed39e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 11 deletions

View file

@ -1,7 +1,7 @@
class Api::V1::GridCharactersController < Api::V1::ApiController
def create
party = Party.find(character_params[:party_id])
canonical_character = Character.find(character_params[:character_id])
incoming_character = Character.find(character_params[:character_id])
if current_user
if party.user != current_user
@ -9,13 +9,67 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
end
end
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 = canonical_character.id
else
@character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: canonical_character.id))
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
@ -39,6 +93,10 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
# Specify whitelisted properties that can be modified.
def character_params
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level)
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting, :incoming)
end
end
def resolve_params
params.require(:resolve).permit(:position, :incoming, :conflicting => [])
end
end

View file

@ -2,6 +2,7 @@ object :character
attributes :id,
:granblue_id,
:character_id,
:rarity,
:element,
:gender,
@ -64,4 +65,4 @@ node :ougi_ratio do |w|
:ougi_ratio => w.ougi_ratio,
:ougi_ratio_flb => w.ougi_ratio_flb
}
end
end

View file

@ -0,0 +1,14 @@
object false
node :conflicts do
partial('grid_characters/base', :object => @conflict_characters)
end
node :incoming do
partial('characters/base', :object => @incoming_character)
end
node :position do
@incoming_position
end

View file

@ -31,6 +31,7 @@ Rails.application.routes.draw do
get 'weapon_keys', to: 'weapon_keys#all'
post 'characters', to: 'grid_characters#create'
post 'characters/resolve', to: 'grid_characters#resolve'
post 'characters/update_uncap', to: 'grid_characters#update_uncap_level'
delete 'characters', to: 'grid_characters#destroy'

View file

@ -0,0 +1,5 @@
class AddCharacterIdToCharacters < ActiveRecord::Migration[6.1]
def change
add_column :characters, :character_id, :integer, array: true, null: false, default: []
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_04_10_190152) do
ActiveRecord::Schema.define(version: 2022_11_17_070255) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gin"
@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 2022_04_10_190152) do
t.boolean "ulb", default: false, null: false
t.integer "max_hp_ulb"
t.integer "max_atk_ulb"
t.integer "character_id", default: [], null: false, array: true
t.index ["name_en"], name: "index_characters_on_name_en", opclass: :gin_trgm_ops, using: :gin
end