From e21e91ab9eefd9db5e73a67afca5087e4dd293fd Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 23 Sep 2017 10:33:18 -0400 Subject: Started writing extractor --- app/controllers/pokeviewer/uploader_controller.rb | 13 ++++ app/jobs/pokeviewer/extract_save_data_job.rb | 88 +++++++++++++++++++++++ app/models/pokeviewer/pokemon.rb | 2 +- app/models/pokeviewer/revision.rb | 10 +++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 app/controllers/pokeviewer/uploader_controller.rb create mode 100644 app/jobs/pokeviewer/extract_save_data_job.rb (limited to 'app') diff --git a/app/controllers/pokeviewer/uploader_controller.rb b/app/controllers/pokeviewer/uploader_controller.rb new file mode 100644 index 0000000..dbef241 --- /dev/null +++ b/app/controllers/pokeviewer/uploader_controller.rb @@ -0,0 +1,13 @@ +require_dependency "pokeviewer/application_controller" + +module Pokeviewer + class UploaderController < ApplicationController + skip_before_action :verify_authenticity_token + + def submit + ExtractSaveDataJob.perform_later params[:game].as_json + + render json: { message: "Data submitted for processing." } + end + end +end diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb new file mode 100644 index 0000000..193bd73 --- /dev/null +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -0,0 +1,88 @@ +module Pokeviewer + class ExtractSaveDataJob < ApplicationJob + queue_as :default + + def perform(args) + game = Trainer.find_or_create_by!( + name: args["playerName"], + number: args["playerId"]) do |r| + case args["gameId"].to_i + when 1 + r.game = :ruby + when 2 + r.game = :sapphire + when 3 + r.game = :firered + when 4 + r.game = :leafgreen + when 5 + r.game = :emerald + else + # Invalid, so just leave the field nil + end + end + + pokemons = args["pokemon"].map do |param| + pk = Pokemon.find_or_create_by!(key: param["key"]) do |r| + r.species_id = param["species"] + r.ot_name = param["otName"] + r.ot_number = param["otId"] + r.met_type = :normal + r.met_level = param["metLevel"] + r.shiny = param["shiny"] + r.nature = Pokemon.nature.values[param["nature"]] + r.gender = param["gender"] + r.second_ability = param["secondAbility"] + + # Handle Unown form + if r.species_id == 201 + r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]] + end + end + + rev = Revision.new(pokemon: pk) + rev.nickname = param["nickname"] + rev.experience = param["experience"] + rev.level = param["level"] + rev.hp = param["hp"] + rev.attack = param["attack"] + rev.defense = param["defense"] + rev.special_attack = param["spAttack"] + rev.special_defense = param["spDefense"] + rev.speed = param["speed"] + rev.coolness = param["coolness"] + rev.beauty = param["beauty"] + rev.cuteness = param["cuteness"] + rev.smartness = param["smartness"] + rev.toughness = param["toughness"] + rev.sheen = param["sheen"] + rev.hold_item = param["heldItem"] + rev.move_1_id = param["moves"][0]["id"] + rev.move_1_pp_bonuses = param["moves"][0]["ppBonuses"] + + if param["moves"].length >= 2 + rev.move_2_id = param["moves"][1]["id"] + rev.move_2_pp_bonuses = param["moves"][1]["ppBonuses"] + end + + if param["moves"].length >= 3 + rev.move_3_id = param["moves"][2]["id"] + rev.move_3_pp_bonuses = param["moves"][2]["ppBonuses"] + end + + if param["moves"].length == 4 + rev.move_4_id = param["moves"][3]["id"] + rev.move_4_pp_bonuses = param["moves"][3]["ppBonuses"] + end + + if pk.revisions.empty? or rev.diff?(pk.revisions.last) + rev.save! + end + + pk + end + + game.pokemon = pokemons + end + end +end diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 114e9dd..3b750ac 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb @@ -4,7 +4,7 @@ module Pokeviewer belongs_to :species belongs_to :trainer, optional: true - has_many :revisions, dependent: :destroy + has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy validate :uuid_is_constant, on: :update before_create :set_uuid diff --git a/app/models/pokeviewer/revision.rb b/app/models/pokeviewer/revision.rb index 9d1ddf4..4cafd07 100644 --- a/app/models/pokeviewer/revision.rb +++ b/app/models/pokeviewer/revision.rb @@ -1,5 +1,15 @@ +require 'active_record/diff' + module Pokeviewer class Revision < ApplicationRecord + include ActiveRecord::Diff + + diff :nickname, :experience, :level, :hp, :attack, :defense, + :special_attack, :special_defense, :speed, :coolness, :beauty, :cuteness, + :smartness, :toughness, :sheen, :hold_item, :move_1_id, :move_2_id, + :move_3_id, :move_4_id, :move_1_pp_bonuses, :move_2_pp_bonuses, + :move_3_pp_bonuses, :move_4_pp_bonuses + belongs_to :pokemon acts_as_sequenced scope: :pokemon_id -- cgit 1.4.1