Implement detecting and resolving weapon conflicts

This commit is contained in:
Justin Edmund 2022-12-24 23:30:10 -08:00
parent a482c67700
commit ec25230bc5

View file

@ -6,11 +6,41 @@ module Api
before_action :set, except: %w[create update_uncap_level destroy]
def create
# BUG: I can create grid weapons even when I'm not logged in on an authenticated party
party = Party.find(weapon_params[:party_id])
canonical_weapon = Weapon.find(weapon_params[:weapon_id])
render_unauthorized_response if current_user && (party.user != current_user)
incoming_weapon = Weapon.find(weapon_params[:weapon_id])
incoming_weapon.limit
# Set up conflict_position in case it is used
conflict_position = nil
# 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
# Destroy the existing item before adding a new one
if (grid_weapon = GridWeapon.where(
party_id: party.id,
position: weapon_params[:position]
@ -18,14 +48,44 @@ module Api
GridWeapon.destroy(grid_weapon.id)
end
weapon = GridWeapon.create!(weapon_params.merge(party_id: party.id, weapon_id: canonical_weapon.id))
weapon = GridWeapon.new
weapon.attributes = weapon_params.merge(party_id: party.id, weapon_id: incoming_weapon.id)
if weapon.position == -1
party.element = weapon.weapon.element
party.save!
end
render json: GridWeaponBlueprint.render(weapon, view: :full), status: :created if weapon.save!
# Render the new weapon and any weapons changed
return unless weapon.save!
render json: GridWeaponBlueprint.render(weapon, view: :full,
root: :grid_weapon,
meta: {
replaced: conflict_position
}),
status: :created
end
def resolve
incoming = Weapon.find(resolve_params[:incoming])
conflicting = resolve_params[:conflicting].map { |id| GridWeapon.find(id) }
party = conflicting.first.party
# Destroy each conflicting weapon
conflicting.each { |weapon| GridWeapon.destroy(weapon.id) }
# Destroy the weapon at the desired position if it exists
existing_weapon = GridWeapon.where(party: party.id, position: resolve_params[:position]).first
GridWeapon.destroy(existing_weapon.id) if existing_weapon
uncap_level = 3
uncap_level = 4 if incoming.flb
uncap_level = 5 if incoming.ulb
weapon = GridWeapon.create!(party_id: party.id, weapon_id: incoming.id,
position: resolve_params[:position], uncap_level: uncap_level)
render json: GridWeaponBlueprint.render(weapon, view: :nested), status: :created if weapon.save!
end
def update
@ -71,6 +131,10 @@ module Api
:awakening_type, :awakening_level
)
end
def resolve_params
params.require(:resolve).permit(:position, :incoming, conflicting: [])
end
end
end
end