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/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 diff --git a/app/controllers/api/v1/grid_summons_controller.rb b/app/controllers/api/v1/grid_summons_controller.rb index cf9ea7d..2a40033 100644 --- a/app/controllers/api/v1/grid_summons_controller.rb +++ b/app/controllers/api/v1/grid_summons_controller.rb @@ -3,12 +3,28 @@ module Api module V1 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] @@ -16,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 @@ -31,11 +62,33 @@ 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 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 + # 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/app/models/grid_summon.rb b/app/models/grid_summon.rb index 565a708..7da4930 100644 --- a/app/models/grid_summon.rb +++ b/app/models/grid_summon.rb @@ -6,6 +6,9 @@ class GridSummon < ApplicationRecord inverse_of: :summons validates_presence_of :party + validate :compatible_with_position, on: :create + validate :no_conflicts, on: :create + def summon Summon.find(summon_id) end @@ -13,4 +16,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 diff --git a/config/routes.rb b/config/routes.rb index 8b3b570..3b54234 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' 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 7cfa3d0..65680cb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -178,38 +178,38 @@ ActiveRecord::Schema[7.0].define(version: 20_230_108_150_956) do t.index ['uid'], name: 'index_oauth_applications_on_uid', unique: true end - create_table 'parties', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.uuid 'user_id' - t.string 'shortcode' - t.datetime 'created_at', null: false - t.datetime 'updated_at', null: false - t.boolean 'extra', default: false, null: false - t.string 'name' - t.text 'description' - t.uuid 'raid_id' - t.integer 'element' - t.integer 'weapons_count' - t.uuid 'job_id' - t.integer 'ml' - t.uuid 'skill1_id' - t.uuid 'skill2_id' - t.uuid 'skill3_id' - t.uuid 'skill0_id' - t.boolean 'full_auto', default: false, null: false - t.boolean 'auto_guard', default: false, null: false - t.boolean 'charge_attack', default: true, null: false - t.integer 'clear_time', default: 0, null: false - 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' + create_table "parties", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.uuid "user_id" + t.string "shortcode" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "extra", default: false, null: false + t.string "name" + t.text "description" + t.uuid "raid_id" + t.integer "element" + t.integer "weapons_count" + t.uuid "job_id" + t.integer "ml" + t.uuid "skill1_id" + t.uuid "skill2_id" + t.uuid "skill3_id" + t.uuid "skill0_id" + t.boolean "full_auto", default: false, null: false + t.boolean "auto_guard", default: false, null: false + t.boolean "charge_attack", default: true, null: false + t.integer "clear_time", default: 0, null: false + 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 create_table 'raids', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| @@ -221,28 +221,30 @@ ActiveRecord::Schema[7.0].define(version: 20_230_108_150_956) do t.string 'slug' end - create_table 'summons', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| - t.string 'name_en' - t.string 'name_jp' - t.string 'granblue_id' - t.integer 'rarity' - t.integer 'element' - t.string 'series' - t.boolean 'flb', default: false, null: false - t.boolean 'ulb', default: false, null: false - t.integer 'max_level', default: 100, null: false - t.integer 'min_hp' - t.integer 'max_hp' - t.integer 'max_hp_flb' - t.integer 'max_hp_ulb' - t.integer 'min_atk' - t.integer 'max_atk' - t.integer 'max_atk_flb' - t.integer 'max_atk_ulb' - t.boolean 'subaura', default: false, null: false - t.boolean 'limit', default: false, null: false - t.boolean 'xlb', default: false, null: false - t.index ['name_en'], name: 'index_summons_on_name_en', opclass: :gin_trgm_ops, using: :gin + create_table "summons", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.string "name_en" + t.string "name_jp" + t.string "granblue_id" + t.integer "rarity" + t.integer "element" + t.string "series" + t.boolean "flb", default: false, null: false + t.boolean "ulb", default: false, null: false + t.integer "max_level", default: 100, null: false + t.integer "min_hp" + t.integer "max_hp" + t.integer "max_hp_flb" + t.integer "max_hp_ulb" + t.integer "min_atk" + t.integer "max_atk" + t.integer "max_atk_flb" + t.integer "max_atk_ulb" + 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 create_table 'users', id: :uuid, default: -> { 'gen_random_uuid()' }, force: :cascade do |t| @@ -298,24 +300,24 @@ ActiveRecord::Schema[7.0].define(version: 20_230_108_150_956) do t.index ['name_en'], name: 'index_weapons_on_name_en', opclass: :gin_trgm_ops, using: :gin end - add_foreign_key 'favorites', 'parties' - add_foreign_key 'favorites', 'users' - add_foreign_key 'grid_characters', 'characters' - add_foreign_key 'grid_characters', 'parties' - add_foreign_key 'grid_summons', 'parties' - add_foreign_key 'grid_summons', 'summons' - add_foreign_key 'grid_weapons', 'parties' - add_foreign_key 'grid_weapons', 'weapon_keys', column: 'weapon_key3_id' - add_foreign_key 'grid_weapons', 'weapons' - add_foreign_key 'jobs', 'jobs', column: 'base_job_id' - add_foreign_key 'oauth_access_grants', 'oauth_applications', column: 'application_id' - add_foreign_key 'oauth_access_tokens', 'oauth_applications', column: 'application_id' - add_foreign_key 'parties', 'job_skills', column: 'skill0_id' - add_foreign_key 'parties', 'job_skills', column: 'skill1_id' - 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' + add_foreign_key "favorites", "parties" + add_foreign_key "favorites", "users" + add_foreign_key "grid_characters", "characters" + add_foreign_key "grid_characters", "parties" + add_foreign_key "grid_summons", "parties" + add_foreign_key "grid_summons", "summons" + add_foreign_key "grid_weapons", "parties" + add_foreign_key "grid_weapons", "weapon_keys", column: "weapon_key3_id" + add_foreign_key "grid_weapons", "weapons" + add_foreign_key "jobs", "jobs", column: "base_job_id" + add_foreign_key "oauth_access_grants", "oauth_applications", column: "application_id" + add_foreign_key "oauth_access_tokens", "oauth_applications", column: "application_id" + add_foreign_key "parties", "job_skills", column: "skill0_id" + add_foreign_key "parties", "job_skills", column: "skill1_id" + 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