hensei-api/app/controllers/api/v1/grid_weapons_controller.rb
Justin Edmund 7faa4883f0
June 2023 Update (#102)
* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add tables used for Siero's revival (#94)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Deploy unauthenticated profile fix (#93)

* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add guidebooks migration

* Implement business logic for reading Guidebooks

* Change to individual guidebook columns

* Properly output guidebook description

* Move to 1-index guidebooks

* Update party-related files for 1-index guidebooks

* Add tables for Siero

* Update download and export scripts

* Support for Siero, raids rework and edit party rework (#96)

* Add guidebooks migration

* Implement business logic for reading Guidebooks

* Change to individual guidebook columns

* Properly output guidebook description

* Move to 1-index guidebooks

* Update party-related files for 1-index guidebooks

* Add tables for Siero

* Add raid groups table

* Update raid model

To belong to the RaidGroup class

* Update job class

To have many job skills

* Add endpoint for raid groups

* Update Raid blueprint with views

* Added down for creating table

* Add guidebooks flag and auto summon flag

* Guidebooks → RaidGroup
* Auto summon → Party

* Add views to Raid blueprint

* Add views and guidebook flag to RaidGroup blueprint

* Add auto summon and Raid view to Party blueprint

* Add quick summons (#97)

* Adds quick summon migration
* Add route to update quick summon
* Add logic to update quick summon

* Remove ap call

* Add logic to updating summon uncap

You couldn't actually update summon transcendence on staging, maybe due to a bug?

Now you can. And it won't let you update outside of the bounds of what is defined on the canonical object.

* Fix summon logic

Updated logic for updating summon uncap and transcendence levels and quick summon status.

* Fix searching in Japanese (#99)

This was broken because we were using the browser-provided locale as a prefix to our method, but that is 'ja' and our methods were prefixed with 'jp'.

* Fix display of base skills (#100)

Base skills were not displaying due to a missing case in our search code. This is now fixed.

* Update awakenings (#101)

* Deploy unauthenticated profile fix (#93)

* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add tables used for Siero's revival (#94)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Deploy unauthenticated profile fix (#93)

* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add guidebooks migration

* Implement business logic for reading Guidebooks

* Change to individual guidebook columns

* Properly output guidebook description

* Move to 1-index guidebooks

* Update party-related files for 1-index guidebooks

* Add tables for Siero

* Adds guidebooks and gacha tables (#95)

* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add tables used for Siero's revival (#94)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Deploy unauthenticated profile fix (#93)

* Implements advanced filters (#90)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix a bug where unauthenticated users couldn't view profiles (#92)

* Implements advanced filters (#90) (#91)

* Add advanced filters

Adds new filters to search:

* Full auto
* Charge attack
* Auto guard
* Number of weapons (user-selectable now)
* Number of summons
* Number of characters
* Maximum number of turns
* Maximum number of buttons
* Maximum clear time
* User quality (No anonymous users)
* Name quality (No untitled teams)
* Remixes (Only show original teams)

* Update advanced filter params

* Add default to party counter cache

* Fix being unable to see profiles when unauth

* Add guidebooks migration

* Implement business logic for reading Guidebooks

* Change to individual guidebook columns

* Properly output guidebook description

* Move to 1-index guidebooks

* Update party-related files for 1-index guidebooks

* Add tables for Siero

* Update download and export scripts

* Support for Siero, raids rework and edit party rework (#96)

* Add guidebooks migration

* Implement business logic for reading Guidebooks

* Change to individual guidebook columns

* Properly output guidebook description

* Move to 1-index guidebooks

* Update party-related files for 1-index guidebooks

* Add tables for Siero

* Add raid groups table

* Update raid model

To belong to the RaidGroup class

* Update job class

To have many job skills

* Add endpoint for raid groups

* Update Raid blueprint with views

* Added down for creating table

* Add guidebooks flag and auto summon flag

* Guidebooks → RaidGroup
* Auto summon → Party

* Add views to Raid blueprint

* Add views and guidebook flag to RaidGroup blueprint

* Add auto summon and Raid view to Party blueprint

* Remove ap call

* Add quick summons (#97)

* Adds quick summon migration
* Add route to update quick summon
* Add logic to update quick summon

* Add logic to updating summon uncap

You couldn't actually update summon transcendence on staging, maybe due to a bug?

Now you can. And it won't let you update outside of the bounds of what is defined on the canonical object.

* Fix summon logic

Updated logic for updating summon uncap and transcendence levels and quick summon status.

* Fix searching in Japanese (#99)

This was broken because we were using the browser-provided locale as a prefix to our method, but that is 'ja' and our methods were prefixed with 'jp'.

* Fix display of base skills (#100)

Base skills were not displaying due to a missing case in our search code. This is now fixed.

* Squash migrations into one file

* Add migrations

This adds migrations and data migrations for the awakening update.

* A new `Awakenings` table now exists that stores all possible awakenings, with a column for object type.
* GridCharacter and GridWeapon now have `awakening_id` and `awakening_level` fields. Their `awakening` and `awakening_type` fields were removed.
* Weapons have a mapping table, `WeaponAwakenings`, as not all weapons can be awakened.
* Data migrations are included to migrate existing user data. They should be run automatically when the migration is run with `rails db:migrate:with_data`

* Add and update models

This updates models for the awakening update.

* Awakening and WeaponAwakening models were added
* Weapon, GridWeapon and GridCharacter models get relationships to the new models defined
* GridCharacter had a validation on `awakening_level` that needed to be fixed

* Update controllers

This updates the GridCharacters and GridWeapons controllers to accept the new fields `awakening_id` and `awakening_level` from clients

* Update blueprints

This updates the blueprints to match the new schema. The new AwakeningBlueprint was accidentally added already, oops.

* Remove re-added migrations

* Implement removing job skills (#103)

This lets users remove job skills, for if they accidentally set them.

* Fix migration
2023-06-19 03:53:48 -07:00

210 lines
7.4 KiB
Ruby

# frozen_string_literal: true
module Api
module V1
class GridWeaponsController < Api::V1::ApiController
attr_reader :party, :incoming_weapon
before_action :set, except: %w[create update_uncap_level]
before_action :find_party, only: :create
before_action :find_incoming_weapon, only: :create
before_action :authorize, only: %i[create update destroy]
def create
# Create the GridWeapon with the desired parameters
weapon = GridWeapon.new
weapon.attributes = weapon_params.merge(party_id: party.id, weapon_id: incoming_weapon.id)
if weapon.validate
save_weapon(weapon)
else
handle_conflict(weapon)
end
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)
if weapon.save
view = render_grid_weapon_view(weapon, resolve_params[:position])
render json: view, status: :created
end
end
def update
render_unauthorized_response if current_user && (@weapon.party.user != current_user)
# TODO: Server-side validation of weapon mods
# We don't want someone modifying the JSON and adding
# keys to weapons that cannot have them
# Maybe we make methods on the model to validate for us somehow
render json: GridWeaponBlueprint.render(@weapon, view: :nested) if @weapon.update(weapon_params)
end
def destroy
render_unauthorized_response if @weapon.party.user != current_user
return render json: GridCharacterBlueprint.render(@weapon, view: :destroyed) if @weapon.destroy
end
def update_uncap_level
weapon = GridWeapon.find(weapon_params[:id])
render_unauthorized_response if current_user && (weapon.party.user != current_user)
weapon.uncap_level = weapon_params[:uncap_level]
return unless weapon.save!
render json: GridWeaponBlueprint.render(weapon, view: :nested, root: :grid_weapon),
status: :created
end
private
def check_weapon_compatibility
return if compatible_with_position?(incoming_weapon, weapon_params[:position])
raise Api::V1::IncompatibleWeaponForPositionError.new(weapon: incoming_weapon)
end
# Check if the incoming weapon is compatible with the specified position
def compatible_with_position?(incoming_weapon, position)
false if [9, 10, 11].include?(position.to_i) && ![11, 16, 17, 28, 29].include?(incoming_weapon.series)
true
end
def conflict_weapon
@conflict_weapon ||= find_conflict_weapon(party, incoming_weapon)
end
# Find a conflict weapon if one exists
def find_conflict_weapon(party, incoming_weapon)
return unless incoming_weapon.limit
party.weapons.find do |weapon|
series_match = incoming_weapon.series == weapon.weapon.series
weapon if series_match || opus_or_draconic?(weapon.weapon) && opus_or_draconic?(incoming_weapon)
end
end
def find_incoming_weapon
@incoming_weapon = Weapon.find_by(id: weapon_params[:weapon_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(weapon_params[:party_id])
render_unauthorized_response if current_user && (party.user != current_user)
end
def opus_or_draconic?(weapon)
[2, 3].include?(weapon.series)
end
# Render the conflict view as a string
def render_conflict_view(conflict_weapon, incoming_weapon, incoming_position)
ConflictBlueprint.render(nil, view: :weapons,
conflict_weapon: conflict_weapon,
incoming_weapon: incoming_weapon,
incoming_position: incoming_position)
end
def render_grid_weapon_view(grid_weapon, conflict_position)
GridWeaponBlueprint.render(grid_weapon, view: :full,
root: :grid_weapon,
meta: { replaced: conflict_position })
end
def save_weapon(weapon)
# Check weapon validation and delete existing grid weapon
# if one already exists at position
if (grid_weapon = GridWeapon.where(
party_id: party.id,
position: weapon_params[:position]
).first)
GridWeapon.destroy(grid_weapon.id)
end
# Set the party's element if the grid weapon is being set as mainhand
if weapon.position == -1
party.element = weapon.weapon.element
party.save!
elsif [9, 10, 11].include?(weapon.position)
party.extra = true
party.save!
end
# Render the weapon if it can be saved
return unless weapon.save
output = GridWeaponBlueprint.render(weapon, view: :full, root: :grid_weapon)
render json: output, status: :created
end
def handle_conflict(weapon)
conflict_weapon = weapon.conflicts(party)
if conflict_weapon.weapon.id != incoming_weapon.id
# Render conflict view if the underlying canonical weapons
# are not identical
output = render_conflict_view([conflict_weapon], incoming_weapon, weapon_params[:position])
render json: output
else
# Move the original grid weapon to the new position
# to preserve keys and other modifications
old_position = conflict_weapon.position
conflict_weapon.position = weapon_params[:position]
if conflict_weapon.save
output = render_grid_weapon_view(conflict_weapon, old_position)
render json: output
end
end
end
def set
@weapon = GridWeapon.where('id = ?', params[:id]).first
end
def authorize
# Create
unauthorized_create = @party && (@party.user != current_user || @party.edit_key != edit_key)
unauthorized_update = @weapon && @weapon.party && (@weapon.party.user != current_user || @weapon.party.edit_key != edit_key)
render_unauthorized_response if unauthorized_create || unauthorized_update
end
# Specify whitelisted properties that can be modified.
def weapon_params
params.require(:weapon).permit(
:id, :party_id, :weapon_id,
:position, :mainhand, :uncap_level, :element,
:weapon_key1_id, :weapon_key2_id, :weapon_key3_id,
:ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2,
:awakening_id, :awakening_level
)
end
def resolve_params
params.require(:resolve).permit(:position, :incoming, conflicting: [])
end
end
end
end