From 67cc1138e6e2d96ad0b93be50f866c8ca5156ae7 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 21 Jan 2023 18:44:25 -0800 Subject: [PATCH] Implements limits on GridSummons --- .../api/v1/grid_summons_controller.rb | 55 +++++++++++++++++-- app/models/grid_summon.rb | 35 ++++++++++++ 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/grid_summons_controller.rb b/app/controllers/api/v1/grid_summons_controller.rb index 6fbc3f1..2a40033 100644 --- a/app/controllers/api/v1/grid_summons_controller.rb +++ b/app/controllers/api/v1/grid_summons_controller.rb @@ -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 diff --git a/app/models/grid_summon.rb b/app/models/grid_summon.rb index 9857a9b..aa65f3c 100644 --- a/app/models/grid_summon.rb +++ b/app/models/grid_summon.rb @@ -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