From c5f2c9d0805286272e1c93501a10866180b2903b Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 21 Jan 2023 15:41:16 -0800 Subject: [PATCH 1/4] Implement deleting summons --- app/blueprints/api/v1/grid_summon_blueprint.rb | 4 ++++ app/controllers/api/v1/grid_summons_controller.rb | 12 ++++++++++-- app/controllers/api/v1/grid_weapons_controller.rb | 1 - config/routes.rb | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/blueprints/api/v1/grid_summon_blueprint.rb b/app/blueprints/api/v1/grid_summon_blueprint.rb index af2cec6..1a27594 100644 --- a/app/blueprints/api/v1/grid_summon_blueprint.rb +++ b/app/blueprints/api/v1/grid_summon_blueprint.rb @@ -17,6 +17,10 @@ module Api include_view :nested association :party, blueprint: PartyBlueprint, view: :minimal end + + view :destroyed do + fields :main, :friend, :position, :created_at, :updated_at + end end end end diff --git a/app/controllers/api/v1/grid_summons_controller.rb b/app/controllers/api/v1/grid_summons_controller.rb index cf9ea7d..6fbc3f1 100644 --- a/app/controllers/api/v1/grid_summons_controller.rb +++ b/app/controllers/api/v1/grid_summons_controller.rb @@ -3,6 +3,8 @@ module Api module V1 class GridSummonsController < Api::V1::ApiController + before_action :set, only: %w[destroy] + def create party = Party.find(summon_params[:party_id]) canonical_summon = Summon.find(summon_params[:summon_id]) @@ -31,11 +33,17 @@ module Api render json: GridSummonBlueprint.render(summon, view: :nested, root: :grid_summon) end - # TODO: Implement removing summons - def destroy; end + def destroy + render_unauthorized_response if @summon.party.user != current_user + return render json: GridSummonBlueprint.render(@summon, view: :destroyed) if @summon.destroy + end private + def set + @summon = GridSummon.where('id = ?', params[:id]).first + end + # Specify whitelisted properties that can be modified. def summon_params params.require(:summon).permit(:id, :party_id, :summon_id, :position, :main, :friend, :uncap_level) diff --git a/app/controllers/api/v1/grid_weapons_controller.rb b/app/controllers/api/v1/grid_weapons_controller.rb index 7282161..652fdbc 100644 --- a/app/controllers/api/v1/grid_weapons_controller.rb +++ b/app/controllers/api/v1/grid_weapons_controller.rb @@ -55,7 +55,6 @@ module Api render json: GridWeaponBlueprint.render(@weapon, view: :nested) if @weapon.update(weapon_params) end - # TODO: Implement removing characters def destroy render_unauthorized_response if @weapon.party.user != current_user return render json: GridCharacterBlueprint.render(@weapon, view: :destroyed) if @weapon.destroy diff --git a/config/routes.rb b/config/routes.rb index 38775d6..e9a6b9f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,7 @@ Rails.application.routes.draw do resources :users, only: %i[create update show] resources :grid_weapons, only: %i[update destroy] resources :grid_characters, only: %i[update destroy] + resources :grid_summons, only: %i[destroy] resources :favorites, only: [:create] get 'users/info/:id', to: 'users#info' From 67cc1138e6e2d96ad0b93be50f866c8ca5156ae7 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sat, 21 Jan 2023 18:44:25 -0800 Subject: [PATCH 2/4] 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 From 65d80244762212112e614a2da8fc99cd2b769180 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 22 Jan 2023 19:57:49 -0800 Subject: [PATCH 3/4] Add XLB stat fields --- db/migrate/20230123035602_add_max_hpatkxlb_to_summon.rb | 6 ++++++ db/schema.rb | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230123035602_add_max_hpatkxlb_to_summon.rb diff --git a/db/migrate/20230123035602_add_max_hpatkxlb_to_summon.rb b/db/migrate/20230123035602_add_max_hpatkxlb_to_summon.rb new file mode 100644 index 0000000..a5b5651 --- /dev/null +++ b/db/migrate/20230123035602_add_max_hpatkxlb_to_summon.rb @@ -0,0 +1,6 @@ +class AddMaxHpatkxlbToSummon < ActiveRecord::Migration[7.0] + def change + add_column :summons, :max_atk_xlb, :integer + add_column :summons, :max_hp_xlb, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 4cea1cb..28c80a7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_07_153724) do +ActiveRecord::Schema[7.0].define(version: 2023_01_23_035602) do # These are extensions that must be enabled in order to support this database enable_extension "btree_gin" enable_extension "pg_trgm" @@ -202,11 +202,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_07_153724) do t.integer "button_count" t.integer "chain_count" t.integer "turn_count" + t.uuid "source_party_id" t.index ["job_id"], name: "index_parties_on_job_id" t.index ["skill0_id"], name: "index_parties_on_skill0_id" t.index ["skill1_id"], name: "index_parties_on_skill1_id" t.index ["skill2_id"], name: "index_parties_on_skill2_id" t.index ["skill3_id"], name: "index_parties_on_skill3_id" + t.index ["source_party_id"], name: "index_parties_on_source_party_id" t.index ["user_id"], name: "index_parties_on_user_id" end @@ -240,6 +242,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_07_153724) do t.boolean "subaura", default: false, null: false t.boolean "limit", default: false, null: false t.boolean "xlb", default: false, null: false + t.integer "max_atk_xlb" + t.integer "max_hp_xlb" t.index ["name_en"], name: "index_summons_on_name_en", opclass: :gin_trgm_ops, using: :gin end @@ -313,6 +317,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_07_153724) do add_foreign_key "parties", "job_skills", column: "skill2_id" add_foreign_key "parties", "job_skills", column: "skill3_id" add_foreign_key "parties", "jobs" + add_foreign_key "parties", "parties", column: "source_party_id" add_foreign_key "parties", "raids" add_foreign_key "parties", "users" end From cf270f02434e65d25b1089cac195d044659d153b Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 22 Jan 2023 19:57:57 -0800 Subject: [PATCH 4/4] Add XLB and stats to JSON output --- app/blueprints/api/v1/summon_blueprint.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/blueprints/api/v1/summon_blueprint.rb b/app/blueprints/api/v1/summon_blueprint.rb index 3db846d..7c9b84a 100644 --- a/app/blueprints/api/v1/summon_blueprint.rb +++ b/app/blueprints/api/v1/summon_blueprint.rb @@ -15,7 +15,8 @@ module Api field :uncap do |w| { flb: w.flb, - ulb: w.ulb + ulb: w.ulb, + xlb: w.xlb } end @@ -24,7 +25,8 @@ module Api min_hp: w.min_hp, max_hp: w.max_hp, max_hp_flb: w.max_hp_flb, - max_hp_ulb: w.max_hp_ulb + max_hp_ulb: w.max_hp_ulb, + max_hp_xlb: w.max_hp_xlb } end @@ -33,7 +35,8 @@ module Api min_atk: w.min_atk, max_atk: w.max_atk, max_atk_flb: w.max_atk_flb, - max_atk_ulb: w.max_atk_ulb + max_atk_ulb: w.max_atk_ulb, + max_atk_xlb: w.max_atk_xlb } end end