Implements limits on GridSummons
This commit is contained in:
parent
c5f2c9d080
commit
67cc1138e6
2 changed files with 85 additions and 5 deletions
|
|
@ -5,12 +5,26 @@ module Api
|
|||
class GridSummonsController < Api::V1::ApiController
|
||||
before_action :set, only: %w[destroy]
|
||||
|
||||
attr_reader :party, :incoming_summon
|
||||
|
||||
before_action :find_party, only: :create
|
||||
before_action :find_incoming_summon, only: :create
|
||||
|
||||
def create
|
||||
party = Party.find(summon_params[:party_id])
|
||||
canonical_summon = Summon.find(summon_params[:summon_id])
|
||||
# Create the GridSummon with the desired parameters
|
||||
summon = GridSummon.new
|
||||
summon.attributes = summon_params.merge(party_id: party.id, summon_id: incoming_summon.id)
|
||||
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
if summon.validate
|
||||
ap 'Validating'
|
||||
save_summon(summon)
|
||||
else
|
||||
ap 'Handling conflict'
|
||||
handle_conflict(summon)
|
||||
end
|
||||
end
|
||||
|
||||
def save_summon(summon)
|
||||
if (grid_summon = GridSummon.where(
|
||||
party_id: party.id,
|
||||
position: summon_params[:position]
|
||||
|
|
@ -18,8 +32,23 @@ module Api
|
|||
GridSummon.destroy(grid_summon.id)
|
||||
end
|
||||
|
||||
summon = GridSummon.create!(summon_params.merge(party_id: party.id, summon_id: canonical_summon.id))
|
||||
render json: GridSummonBlueprint.render(summon, view: :nested), status: :created if summon.save!
|
||||
return unless summon.save
|
||||
|
||||
output = render_grid_summon_view(summon)
|
||||
render json: output, status: :created
|
||||
end
|
||||
|
||||
def handle_conflict(summon)
|
||||
conflict_summon = summon.conflicts(party)
|
||||
return unless conflict_summon.summon.id == incoming_summon.id
|
||||
|
||||
old_position = conflict_summon.position
|
||||
conflict_summon.position = summon_params[:position]
|
||||
|
||||
return unless conflict_summon.save
|
||||
|
||||
output = render_grid_summon_view(conflict_summon, old_position)
|
||||
render json: output
|
||||
end
|
||||
|
||||
def update_uncap_level
|
||||
|
|
@ -40,6 +69,22 @@ module Api
|
|||
|
||||
private
|
||||
|
||||
def find_incoming_summon
|
||||
@incoming_summon = Summon.find_by(id: summon_params[:summon_id])
|
||||
end
|
||||
|
||||
def find_party
|
||||
# BUG: I can create grid weapons even when I'm not logged in on an authenticated party
|
||||
@party = Party.find(summon_params[:party_id])
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
end
|
||||
|
||||
def render_grid_summon_view(grid_summon, conflict_position = nil)
|
||||
GridSummonBlueprint.render(grid_summon, view: :nested,
|
||||
root: :grid_summon,
|
||||
meta: { replaced: conflict_position })
|
||||
end
|
||||
|
||||
def set
|
||||
@summon = GridSummon.where('id = ?', params[:id]).first
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@
|
|||
class GridSummon < ApplicationRecord
|
||||
belongs_to :party
|
||||
|
||||
validate :compatible_with_position, on: :create
|
||||
validate :no_conflicts, on: :create
|
||||
|
||||
def summon
|
||||
Summon.find(summon_id)
|
||||
end
|
||||
|
|
@ -10,4 +13,36 @@ class GridSummon < ApplicationRecord
|
|||
def blueprint
|
||||
GridSummonBlueprint
|
||||
end
|
||||
|
||||
# Returns conflicting summons if they exist
|
||||
def conflicts(party)
|
||||
return unless summon.limit
|
||||
|
||||
party.summons.find do |party_summon|
|
||||
ap 'Normal summon:'
|
||||
ap summon
|
||||
ap 'Party summon:'
|
||||
ap party_summon
|
||||
|
||||
summon if summon.id == party_summon.summon.id
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Validates whether there is a conflict with the party
|
||||
def no_conflicts
|
||||
ap conflicts(party)
|
||||
|
||||
# Check if the grid weapon conflicts with any of the other grid weapons in the party
|
||||
errors.add(:series, 'must not conflict with existing summons') unless conflicts(party).nil?
|
||||
end
|
||||
|
||||
# Validates whether the weapon can be added to the desired position
|
||||
def compatible_with_position
|
||||
ap [4, 5].include?(position.to_i) && !summon.subaura
|
||||
return unless [4, 5].include?(position.to_i) && !summon.subaura
|
||||
|
||||
errors.add(:position, 'must have subaura for position')
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue