diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/pokeviewer/uploader_controller.rb | 13 | ||||
-rw-r--r-- | app/jobs/pokeviewer/extract_save_data_job.rb | 88 | ||||
-rw-r--r-- | app/models/pokeviewer/pokemon.rb | 2 | ||||
-rw-r--r-- | app/models/pokeviewer/revision.rb | 10 |
4 files changed, 112 insertions, 1 deletions
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 @@ | |||
1 | require_dependency "pokeviewer/application_controller" | ||
2 | |||
3 | module Pokeviewer | ||
4 | class UploaderController < ApplicationController | ||
5 | skip_before_action :verify_authenticity_token | ||
6 | |||
7 | def submit | ||
8 | ExtractSaveDataJob.perform_later params[:game].as_json | ||
9 | |||
10 | render json: { message: "Data submitted for processing." } | ||
11 | end | ||
12 | end | ||
13 | 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 @@ | |||
1 | module Pokeviewer | ||
2 | class ExtractSaveDataJob < ApplicationJob | ||
3 | queue_as :default | ||
4 | |||
5 | def perform(args) | ||
6 | game = Trainer.find_or_create_by!( | ||
7 | name: args["playerName"], | ||
8 | number: args["playerId"]) do |r| | ||
9 | case args["gameId"].to_i | ||
10 | when 1 | ||
11 | r.game = :ruby | ||
12 | when 2 | ||
13 | r.game = :sapphire | ||
14 | when 3 | ||
15 | r.game = :firered | ||
16 | when 4 | ||
17 | r.game = :leafgreen | ||
18 | when 5 | ||
19 | r.game = :emerald | ||
20 | else | ||
21 | # Invalid, so just leave the field nil | ||
22 | end | ||
23 | end | ||
24 | |||
25 | pokemons = args["pokemon"].map do |param| | ||
26 | pk = Pokemon.find_or_create_by!(key: param["key"]) do |r| | ||
27 | r.species_id = param["species"] | ||
28 | r.ot_name = param["otName"] | ||
29 | r.ot_number = param["otId"] | ||
30 | r.met_type = :normal | ||
31 | r.met_level = param["metLevel"] | ||
32 | r.shiny = param["shiny"] | ||
33 | r.nature = Pokemon.nature.values[param["nature"]] | ||
34 | r.gender = param["gender"] | ||
35 | r.second_ability = param["secondAbility"] | ||
36 | |||
37 | # Handle Unown form | ||
38 | if r.species_id == 201 | ||
39 | r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]] | ||
40 | end | ||
41 | end | ||
42 | |||
43 | rev = Revision.new(pokemon: pk) | ||
44 | rev.nickname = param["nickname"] | ||
45 | rev.experience = param["experience"] | ||
46 | rev.level = param["level"] | ||
47 | rev.hp = param["hp"] | ||
48 | rev.attack = param["attack"] | ||
49 | rev.defense = param["defense"] | ||
50 | rev.special_attack = param["spAttack"] | ||
51 | rev.special_defense = param["spDefense"] | ||
52 | rev.speed = param["speed"] | ||
53 | rev.coolness = param["coolness"] | ||
54 | rev.beauty = param["beauty"] | ||
55 | rev.cuteness = param["cuteness"] | ||
56 | rev.smartness = param["smartness"] | ||
57 | rev.toughness = param["toughness"] | ||
58 | rev.sheen = param["sheen"] | ||
59 | rev.hold_item = param["heldItem"] | ||
60 | rev.move_1_id = param["moves"][0]["id"] | ||
61 | rev.move_1_pp_bonuses = param["moves"][0]["ppBonuses"] | ||
62 | |||
63 | if param["moves"].length >= 2 | ||
64 | rev.move_2_id = param["moves"][1]["id"] | ||
65 | rev.move_2_pp_bonuses = param["moves"][1]["ppBonuses"] | ||
66 | end | ||
67 | |||
68 | if param["moves"].length >= 3 | ||
69 | rev.move_3_id = param["moves"][2]["id"] | ||
70 | rev.move_3_pp_bonuses = param["moves"][2]["ppBonuses"] | ||
71 | end | ||
72 | |||
73 | if param["moves"].length == 4 | ||
74 | rev.move_4_id = param["moves"][3]["id"] | ||
75 | rev.move_4_pp_bonuses = param["moves"][3]["ppBonuses"] | ||
76 | end | ||
77 | |||
78 | if pk.revisions.empty? or rev.diff?(pk.revisions.last) | ||
79 | rev.save! | ||
80 | end | ||
81 | |||
82 | pk | ||
83 | end | ||
84 | |||
85 | game.pokemon = pokemons | ||
86 | end | ||
87 | end | ||
88 | 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 | |||
4 | 4 | ||
5 | belongs_to :species | 5 | belongs_to :species |
6 | belongs_to :trainer, optional: true | 6 | belongs_to :trainer, optional: true |
7 | has_many :revisions, dependent: :destroy | 7 | has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy |
8 | 8 | ||
9 | validate :uuid_is_constant, on: :update | 9 | validate :uuid_is_constant, on: :update |
10 | before_create :set_uuid | 10 | 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 @@ | |||
1 | require 'active_record/diff' | ||
2 | |||
1 | module Pokeviewer | 3 | module Pokeviewer |
2 | class Revision < ApplicationRecord | 4 | class Revision < ApplicationRecord |
5 | include ActiveRecord::Diff | ||
6 | |||
7 | diff :nickname, :experience, :level, :hp, :attack, :defense, | ||
8 | :special_attack, :special_defense, :speed, :coolness, :beauty, :cuteness, | ||
9 | :smartness, :toughness, :sheen, :hold_item, :move_1_id, :move_2_id, | ||
10 | :move_3_id, :move_4_id, :move_1_pp_bonuses, :move_2_pp_bonuses, | ||
11 | :move_3_pp_bonuses, :move_4_pp_bonuses | ||
12 | |||
3 | belongs_to :pokemon | 13 | belongs_to :pokemon |
4 | acts_as_sequenced scope: :pokemon_id | 14 | acts_as_sequenced scope: :pokemon_id |
5 | 15 | ||