about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-09-23 10:33:18 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-09-23 10:33:18 -0400
commite21e91ab9eefd9db5e73a67afca5087e4dd293fd (patch)
tree6e13aa2552721323557cb5a517e1f241ec1dc5b9
parentf6cdc2fd2f50224fb7dd31d3ccb374cf4ffe7b6d (diff)
downloadpokeviewer-e21e91ab9eefd9db5e73a67afca5087e4dd293fd.tar.gz
pokeviewer-e21e91ab9eefd9db5e73a67afca5087e4dd293fd.tar.bz2
pokeviewer-e21e91ab9eefd9db5e73a67afca5087e4dd293fd.zip
Started writing extractor
-rw-r--r--Gemfile.lock14
-rw-r--r--app/controllers/pokeviewer/uploader_controller.rb13
-rw-r--r--app/jobs/pokeviewer/extract_save_data_job.rb88
-rw-r--r--app/models/pokeviewer/pokemon.rb2
-rw-r--r--app/models/pokeviewer/revision.rb10
-rw-r--r--config/routes.rb2
-rw-r--r--lib/pokeviewer/engine.rb2
-rw-r--r--pokeviewer.gemspec3
8 files changed, 126 insertions, 8 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 30e3155..4918560 100644 --- a/Gemfile.lock +++ b/Gemfile.lock
@@ -2,9 +2,10 @@ PATH
2 remote: . 2 remote: .
3 specs: 3 specs:
4 pokeviewer (0.1.0) 4 pokeviewer (0.1.0)
5 activerecord-diff
5 enumerize 6 enumerize
6 rails (~> 5.1.4) 7 rails (~> 5.1.4)
7 sequential 8 sequenced
8 9
9GEM 10GEM
10 remote: https://rubygems.org/ 11 remote: https://rubygems.org/
@@ -41,6 +42,7 @@ GEM
41 activemodel (= 5.1.4) 42 activemodel (= 5.1.4)
42 activesupport (= 5.1.4) 43 activesupport (= 5.1.4)
43 arel (~> 8.0) 44 arel (~> 8.0)
45 activerecord-diff (2.0.0)
44 activesupport (5.1.4) 46 activesupport (5.1.4)
45 concurrent-ruby (~> 1.0, >= 1.0.2) 47 concurrent-ruby (~> 1.0, >= 1.0.2)
46 i18n (~> 0.7) 48 i18n (~> 0.7)
@@ -63,11 +65,11 @@ GEM
63 mime-types (3.1) 65 mime-types (3.1)
64 mime-types-data (~> 3.2015) 66 mime-types-data (~> 3.2015)
65 mime-types-data (3.2016.0521) 67 mime-types-data (3.2016.0521)
66 mini_portile2 (2.2.0) 68 mini_portile2 (2.3.0)
67 minitest (5.10.3) 69 minitest (5.10.3)
68 nio4r (2.1.0) 70 nio4r (2.1.0)
69 nokogiri (1.8.0) 71 nokogiri (1.8.1)
70 mini_portile2 (~> 2.2.0) 72 mini_portile2 (~> 2.3.0)
71 rack (2.0.3) 73 rack (2.0.3)
72 rack-test (0.7.0) 74 rack-test (0.7.0)
73 rack (>= 1.0, < 3) 75 rack (>= 1.0, < 3)
@@ -95,7 +97,9 @@ GEM
95 rake (>= 0.8.7) 97 rake (>= 0.8.7)
96 thor (>= 0.18.1, < 2.0) 98 thor (>= 0.18.1, < 2.0)
97 rake (12.1.0) 99 rake (12.1.0)
98 sequential (0.2) 100 sequenced (3.1.1)
101 activerecord (>= 3.0)
102 activesupport (>= 3.0)
99 sprockets (3.7.1) 103 sprockets (3.7.1)
100 concurrent-ruby (~> 1.0) 104 concurrent-ruby (~> 1.0)
101 rack (> 1, < 3) 105 rack (> 1, < 3)
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 @@
1require_dependency "pokeviewer/application_controller"
2
3module 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
13end
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 @@
1module 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
88end
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 @@
1require 'active_record/diff'
2
1module Pokeviewer 3module 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
diff --git a/config/routes.rb b/config/routes.rb index 4801db3..c911744 100644 --- a/config/routes.rb +++ b/config/routes.rb
@@ -1,2 +1,4 @@
1Pokeviewer::Engine.routes.draw do 1Pokeviewer::Engine.routes.draw do
2 post '/', to: 'uploader#submit'
3
2end 4end
diff --git a/lib/pokeviewer/engine.rb b/lib/pokeviewer/engine.rb index d945d6d..4368808 100644 --- a/lib/pokeviewer/engine.rb +++ b/lib/pokeviewer/engine.rb
@@ -1,5 +1,5 @@
1require "enumerize" 1require "enumerize"
2require "sequential" 2require "sequenced"
3 3
4module Pokeviewer 4module Pokeviewer
5 class Engine < ::Rails::Engine 5 class Engine < ::Rails::Engine
diff --git a/pokeviewer.gemspec b/pokeviewer.gemspec index bfc49ce..174d69b 100644 --- a/pokeviewer.gemspec +++ b/pokeviewer.gemspec
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
18 18
19 s.add_dependency "rails", "~> 5.1.4" 19 s.add_dependency "rails", "~> 5.1.4"
20 s.add_dependency "enumerize" 20 s.add_dependency "enumerize"
21 s.add_dependency "sequential" 21 s.add_dependency "sequenced"
22 s.add_dependency "activerecord-diff"
22 23
23 s.add_development_dependency "sqlite3" 24 s.add_development_dependency "sqlite3"
24end 25end