hensei-api/app/controllers/api/v1/jobs_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

195 lines
6.3 KiB
Ruby

# frozen_string_literal: true
module Api
module V1
class JobsController < Api::V1::ApiController
before_action :set, only: %w[update_job update_job_skills destroy_job_skill]
before_action :authorize, only: %w[update_job update_job_skills destroy_job_skill]
def all
render json: JobBlueprint.render(Job.all)
end
def update_job
if job_params[:job_id] != -1
# Extract job and find its main skills
old_job = @party.job
job = Job.find(job_params[:job_id])
main_skills = JobSkill.where(job: job.id, main: true)
# Update the party
@party.job = job
main_skills.each_with_index do |skill, index|
@party["skill#{index}_id"] = skill.id
end
# Check for incompatible Base and EMP skills
%w[skill1_id skill2_id skill3_id].each do |key|
@party[key] = nil if @party[key] && mismatched_skill(@party.job, JobSkill.find(@party[key]))
end
# Remove extra subskills if necessary
if old_job &&
%w[1 2 3].include?(old_job.row) &&
%w[4 5 ex2].include?(job.row) &&
@party.skill1 && @party.skill2 && @party.skill3 &&
@party.skill1.sub && @party.skill2.sub && @party.skill3.sub
@party['skill3_id'] = nil
end
else
@party.job = nil
%w[skill0_id skill1_id skill2_id skill3_id].each do |key|
@party[key] = nil
end
end
render json: PartyBlueprint.render(@party, view: :jobs) if @party.save!
end
def update_job_skills
throw NoJobSkillProvidedError unless job_params[:skill1_id] || job_params[:skill2_id] || job_params[:skill3_id]
# Determine which incoming keys contain new skills
skill_keys = %w[skill1_id skill2_id skill3_id]
new_skill_keys = job_params.keys.select { |key| skill_keys.include?(key) }
# If there are new skills, merge them with the existing skills
unless new_skill_keys.empty?
existing_skills = {
1 => @party.skill1,
2 => @party.skill2,
3 => @party.skill3
}
new_skill_ids = new_skill_keys.map { |key| job_params[key] }
new_skill_ids.map do |id|
skill = JobSkill.find(id)
raise Api::V1::IncompatibleSkillError.new(job: @party.job, skill: skill) if mismatched_skill(@party.job, skill)
end
positions = extract_positions_from_keys(new_skill_keys)
new_skills = merge_skills_with_existing_skills(existing_skills, new_skill_ids, positions)
new_skill_ids = new_skills.each_with_object({}) do |(index, skill), memo|
memo["skill#{index}_id"] = skill.id if skill
end
@party.attributes = new_skill_ids
end
render json: PartyBlueprint.render(@party, view: :jobs) if @party.save!
end
def destroy_job_skill
position = job_params[:skill_position].to_i
@party["skill#{position}_id"] = nil
render json: PartyBlueprint.render(@party, view: :jobs) if @party.save
end
private
def merge_skills_with_existing_skills(
existing_skills,
new_skill_ids,
positions
)
new_skills = new_skill_ids.map { |id| JobSkill.find(id) }
new_skills.each_with_index do |skill, index|
existing_skills = place_skill_in_existing_skills(existing_skills, skill, positions[index])
end
existing_skills
end
def place_skill_in_existing_skills(existing_skills, skill, position)
# Test if skill will exceed allowances of skill types
skill_type = skill.sub ? 'sub' : 'emp'
unless can_add_skill_of_type(existing_skills, position, skill_type)
raise Api::V1::TooManySkillsOfTypeError.new(skill_type: skill_type)
end
if !existing_skills[position]
existing_skills[position] = skill
else
value = existing_skills.compact.detect { |_, value| value && value.id == skill.id }
old_position = existing_skills.key(value[1]) if value
if old_position
existing_skills = swap_skills_at_position(existing_skills, skill, position, old_position)
else
existing_skills[position] = skill
end
end
existing_skills
end
def swap_skills_at_position(skills, new_skill, position1, position2)
# Check desired position for a skill
displaced_skill = skills[position1] if skills[position1].present?
# Put skill in new position
skills[position1] = new_skill
skills[position2] = displaced_skill
skills
end
def extract_positions_from_keys(keys)
# Subtract by 1 because we won't operate on the 0th skill, so we don't pass it
keys.map { |key| key['skill'.length].to_i }
end
def can_add_skill_of_type(skills, position, type)
if %w[4 5 ex2].include?(@party.job.row) && skills.values.compact.length.positive?
max_skill_of_type = 2
skills_to_check = skills.compact.reject { |key, _| key == position }
sum = skills_to_check.values.count { |value| value.send(type) }
return sum + 1 <= max_skill_of_type
end
true
end
def mismatched_skill(job, skill)
mismatched_main = (skill.job.id != job.id) && skill.main && !skill.sub
mismatched_emp = (skill.job.id != job.id && skill.job.base_job.id != job.base_job.id) && skill.emp
mismatched_base = skill.job.base_job && (job.row != 'ex2' || skill.job.base_job.id != job.base_job.id) && skill.base
if %w[4 5 ex2].include?(job.row)
if skill.base && !mismatched_base
false
else
true if mismatched_emp || mismatched_main
end
elsif mismatched_emp || mismatched_main
true
else
false
end
end
def authorize
render_unauthorized_response if @party.user != current_user || @party.edit_key != edit_key
end
def set
@party = Party.where('id = ?', params[:id]).first
end
def job_params
params.require(:party).permit(
:job_id,
:skill0_id,
:skill1_id,
:skill2_id,
:skill3_id,
:skill_position
)
end
end
end
end