Merge pull request #30 from jedmund/revert-refactor
Revert GridWeaponsController refactor
This commit is contained in:
commit
bac92ffdcd
1 changed files with 44 additions and 69 deletions
|
|
@ -3,24 +3,44 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class GridWeaponsController < Api::V1::ApiController
|
class GridWeaponsController < Api::V1::ApiController
|
||||||
attr_reader :party, :incoming_weapon
|
|
||||||
|
|
||||||
before_action :find_party, only: :create
|
|
||||||
before_action :find_incoming_weapon, only: :create
|
|
||||||
before_action :check_weapon_compatibility, only: :create
|
|
||||||
before_action :set, except: %w[create update_uncap_level destroy]
|
before_action :set, except: %w[create update_uncap_level destroy]
|
||||||
|
|
||||||
def create
|
def create
|
||||||
if conflict_weapon
|
# BUG: I can create grid weapons even when I'm not logged in on an authenticated party
|
||||||
if conflict_weapon.weapon.id != incoming_weapon.id
|
party = Party.find(weapon_params[:party_id])
|
||||||
# Render the conflict view as a string and assign it to a variable
|
render_unauthorized_response if current_user && (party.user != current_user)
|
||||||
conflict_view = render_conflict_view(conflict_weapon, incoming_weapon, weapon_params[:position])
|
|
||||||
return render json: conflict_view
|
incoming_weapon = Weapon.find(weapon_params[:weapon_id])
|
||||||
else
|
incoming_weapon.limit
|
||||||
# Destroy the original grid weapon
|
|
||||||
# TODO: Use conflict_position to alert the client that that position has changed
|
# Set up conflict_position in case it is used
|
||||||
conflict_position = conflict_weapon.position
|
conflict_position = nil
|
||||||
GridWeapon.destroy(conflict_weapon.id)
|
|
||||||
|
if [9, 10, 11].include?(weapon_params[:position].to_i) && ![11, 16, 17, 28, 29].include?(incoming_weapon.series)
|
||||||
|
raise Api::V1::IncompatibleWeaponForPositionError.new(weapon: incoming_weapon)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 1. If the weapon has a limit
|
||||||
|
# 2. If the weapon does not match a weapon already in grid
|
||||||
|
# 3. If the incoming weapon has a limit and other weapons of the same series are in grid
|
||||||
|
if incoming_weapon.limit && incoming_weapon.limit.positive?
|
||||||
|
conflict_weapon = party.weapons.find do |weapon|
|
||||||
|
weapon if incoming_weapon.series == weapon.weapon.series ||
|
||||||
|
([2, 3].include?(incoming_weapon.series) && [2, 3].include?(weapon.weapon.series))
|
||||||
|
end
|
||||||
|
|
||||||
|
if conflict_weapon
|
||||||
|
if conflict_weapon.weapon.id != incoming_weapon.id
|
||||||
|
return render json: ConflictBlueprint.render(nil, view: :weapons,
|
||||||
|
conflict_weapons: [conflict_weapon],
|
||||||
|
incoming_weapon: incoming_weapon,
|
||||||
|
incoming_position: weapon_params[:position])
|
||||||
|
else
|
||||||
|
# Destroy the original grid weapon
|
||||||
|
# TODO: Use conflict_position to alert the client that that position has changed
|
||||||
|
conflict_position = conflict_weapon.position
|
||||||
|
GridWeapon.destroy(conflict_weapon.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -40,11 +60,16 @@ module Api
|
||||||
party.save!
|
party.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render the grid weapon view as a string and assign it to a variable
|
# Render the new weapon and any weapons changed
|
||||||
return unless weapon.save!
|
if weapon.save!
|
||||||
|
|
||||||
grid_weapon_view = render_grid_weapon_view(weapon, conflict_position)
|
render json: GridWeaponBlueprint.render(weapon, view: :full,
|
||||||
render json: grid_weapon_view, status: :created
|
root: :grid_weapon,
|
||||||
|
meta: {
|
||||||
|
replaced: conflict_position
|
||||||
|
}),
|
||||||
|
status: :created
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolve
|
def resolve
|
||||||
|
|
@ -97,56 +122,6 @@ module Api
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_weapon_compatibility
|
|
||||||
return if compatible_with_position?(incoming_weapon, weapon_params[:position])
|
|
||||||
|
|
||||||
raise Api::V1::IncompatibleWeaponForPositionError.new(weapon: incoming_weapon)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check if the incoming weapon is compatible with the specified position
|
|
||||||
def compatible_with_position?(incoming_weapon, position)
|
|
||||||
false if [9, 10, 11].include?(position.to_i) && ![11, 16, 17, 28, 29].include?(incoming_weapon.series)
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def conflict_weapon
|
|
||||||
@conflict_weapon ||= find_conflict_weapon(party, incoming_weapon)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Find a conflict weapon if one exists
|
|
||||||
def find_conflict_weapon(party, incoming_weapon)
|
|
||||||
return unless incoming_weapon.limit
|
|
||||||
|
|
||||||
party.weapons.find do |weapon|
|
|
||||||
weapon if incoming_weapon.series == weapon.weapon.series || [2, 3].include?(weapon.weapon.series)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_incoming_weapon
|
|
||||||
@incoming_weapon = Weapon.find(weapon_params[:weapon_id])
|
|
||||||
@incoming_weapon.limit
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_party
|
|
||||||
# BUG: I can create grid weapons even when I'm not logged in on an authenticated party
|
|
||||||
@party = Party.find(weapon_params[:party_id])
|
|
||||||
render_unauthorized_response if current_user && (party.user != current_user)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Render the conflict view as a string
|
|
||||||
def render_conflict_view(conflict_weapon, incoming_weapon, incoming_position)
|
|
||||||
ConflictBlueprint.render(nil, view: :weapons,
|
|
||||||
conflict_weapons: [conflict_weapon],
|
|
||||||
incoming_weapon: incoming_weapon,
|
|
||||||
incoming_position: incoming_position)
|
|
||||||
end
|
|
||||||
|
|
||||||
def render_grid_weapon_view(grid_weapon, conflict_position)
|
|
||||||
GridWeaponBlueprint.render(grid_weapon, view: :full,
|
|
||||||
root: :grid_weapon,
|
|
||||||
meta: { replaced: conflict_position })
|
|
||||||
end
|
|
||||||
|
|
||||||
def set
|
def set
|
||||||
@weapon = GridWeapon.where('id = ?', params[:id]).first
|
@weapon = GridWeapon.where('id = ?', params[:id]).first
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue