Merge pull request #15 from jedmund/char-dedupe
Conflict resolution → Characters
This commit is contained in:
commit
9708ed39e1
6 changed files with 91 additions and 11 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
class Api::V1::GridCharactersController < Api::V1::ApiController
|
class Api::V1::GridCharactersController < Api::V1::ApiController
|
||||||
def create
|
def create
|
||||||
party = Party.find(character_params[:party_id])
|
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 current_user
|
||||||
if party.user != current_user
|
if party.user != current_user
|
||||||
|
|
@ -9,13 +9,67 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if GridCharacter.where(party_id: party.id, position: character_params[:position]).exists?
|
current_characters = party.characters.map { |c|
|
||||||
@character = GridCharacter.where(party_id: party.id, position: character_params[:position]).limit(1)[0]
|
Character.find(c.character.id).character_id
|
||||||
@character.character_id = canonical_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
|
else
|
||||||
@character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: canonical_character.id))
|
# 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
|
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!
|
render :show, status: :created if @character.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -39,6 +93,10 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
|
||||||
|
|
||||||
# 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)
|
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
|
||||||
end
|
end
|
||||||
|
|
@ -2,6 +2,7 @@ object :character
|
||||||
|
|
||||||
attributes :id,
|
attributes :id,
|
||||||
:granblue_id,
|
:granblue_id,
|
||||||
|
:character_id,
|
||||||
:rarity,
|
:rarity,
|
||||||
:element,
|
:element,
|
||||||
:gender,
|
:gender,
|
||||||
|
|
|
||||||
14
app/views/api/v1/grid_characters/conflict.json.rabl
Normal file
14
app/views/api/v1/grid_characters/conflict.json.rabl
Normal 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
|
||||||
|
|
||||||
|
|
@ -31,6 +31,7 @@ Rails.application.routes.draw do
|
||||||
get 'weapon_keys', to: 'weapon_keys#all'
|
get 'weapon_keys', to: 'weapon_keys#all'
|
||||||
|
|
||||||
post 'characters', to: 'grid_characters#create'
|
post 'characters', to: 'grid_characters#create'
|
||||||
|
post 'characters/resolve', to: 'grid_characters#resolve'
|
||||||
post 'characters/update_uncap', to: 'grid_characters#update_uncap_level'
|
post 'characters/update_uncap', to: 'grid_characters#update_uncap_level'
|
||||||
delete 'characters', to: 'grid_characters#destroy'
|
delete 'characters', to: 'grid_characters#destroy'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "btree_gin"
|
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.boolean "ulb", default: false, null: false
|
||||||
t.integer "max_hp_ulb"
|
t.integer "max_hp_ulb"
|
||||||
t.integer "max_atk_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
|
t.index ["name_en"], name: "index_characters_on_name_en", opclass: :gin_trgm_ops, using: :gin
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue