about summary refs log tree commit diff stats
path: root/app/jobs
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2024-12-07 11:49:49 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2024-12-07 11:49:49 -0500
commit56f5841d4b9c12296cdfcaeff174b2627d59afc8 (patch)
tree4f7da4ebbe5ee15a1594b26466ed78e2cf10de35 /app/jobs
parentc1b0443ba2aebdbd39291ddab0c189f3f4831320 (diff)
downloadpokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.gz
pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.bz2
pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.zip
Migrate to full rails app
Diffstat (limited to 'app/jobs')
-rw-r--r--app/jobs/application_job.rb7
-rw-r--r--app/jobs/extract_save_data_job.rb186
-rw-r--r--app/jobs/pokeviewer/application_job.rb4
-rw-r--r--app/jobs/pokeviewer/extract_save_data_job.rb188
4 files changed, 193 insertions, 192 deletions
diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 0000000..d394c3d --- /dev/null +++ b/app/jobs/application_job.rb
@@ -0,0 +1,7 @@
1class ApplicationJob < ActiveJob::Base
2 # Automatically retry jobs that encountered a deadlock
3 # retry_on ActiveRecord::Deadlocked
4
5 # Most jobs are safe to ignore if the underlying records are no longer available
6 # discard_on ActiveJob::DeserializationError
7end
diff --git a/app/jobs/extract_save_data_job.rb b/app/jobs/extract_save_data_job.rb new file mode 100644 index 0000000..282bcf1 --- /dev/null +++ b/app/jobs/extract_save_data_job.rb
@@ -0,0 +1,186 @@
1class ExtractSaveDataJob < ApplicationJob
2 queue_as :default
3
4 def perform(args)
5 game = Trainer.find_or_initialize_by(
6 name: args["playerName"],
7 number: args["playerId"]) do |r|
8 case args["gameId"].to_i
9 when 1
10 r.game = :ruby
11 when 2
12 r.game = :sapphire
13 when 3
14 r.game = :firered
15 when 4
16 r.game = :leafgreen
17 when 5
18 r.game = :emerald
19 else
20 # Invalid, so just leave the field nil
21 end
22 end
23
24 game.box_1_name = args["boxes"].shift
25 game.box_2_name = args["boxes"].shift
26 game.box_3_name = args["boxes"].shift
27 game.box_4_name = args["boxes"].shift
28 game.box_5_name = args["boxes"].shift
29 game.box_6_name = args["boxes"].shift
30 game.box_7_name = args["boxes"].shift
31 game.box_8_name = args["boxes"].shift
32 game.box_9_name = args["boxes"].shift
33 game.box_10_name = args["boxes"].shift
34 game.box_11_name = args["boxes"].shift
35 game.box_12_name = args["boxes"].shift
36 game.box_13_name = args["boxes"].shift
37 game.box_14_name = args["boxes"].shift
38
39 if args.key? "marineRibbon"
40 game.marine_ribbon_id = args["marineRibbon"]
41 end
42
43 if args.key? "landRibbon"
44 game.land_ribbon_id = args["landRibbon"]
45 end
46
47 if args.key? "skyRibbon"
48 game.sky_ribbon_id = args["skyRibbon"]
49 end
50
51 if args.key? "countryRibbon"
52 game.country_ribbon_id = args["countryRibbon"]
53 end
54
55 if args.key? "nationalRibbon"
56 game.national_ribbon_id = args["nationalRibbon"]
57 end
58
59 if args.key? "earthRibbon"
60 game.earth_ribbon_id = args["earthRibbon"]
61 end
62
63 if args.key? "worldRibbon"
64 game.world_ribbon_id = args["worldRibbon"]
65 end
66
67 game.save! if game.new_record? or game.changed?
68
69 game.pokemon.clear
70
71 args["pokemon"].each do |param|
72 pk = Pokemon.find_or_create_by!(key: param["key"]) do |r|
73 r.ot_name = param["otName"]
74 r.ot_number = param["otId"]
75 r.ot_gender = param["otGender"]
76
77 if param["orre"]
78 r.met_type = :orre
79 elsif param["metLevel"] == 0
80 r.met_type = :hatched
81 r.location_id = param["metLocation"]
82 elsif param["metLocation"] == 254
83 r.met_type = :npc_trade
84 elsif param["metLocation"] == 255
85 r.met_type = :fateful_encounter
86 r.met_level = param["metLevel"]
87 else
88 r.met_type = :normal
89 r.met_level = param["metLevel"]
90 r.location_id = param["metLocation"]
91 end
92
93 r.shiny = param["shiny"]
94 r.nature = Pokemon.nature.values[param["nature"]]
95 r.gender = param["gender"]
96 r.second_ability = param["secondAbility"]
97 r.pokeball = Pokemon.pokeball.values[param["pokeball"] - 1]
98
99 # Handle Unown form
100 if param["species"] == 201
101 r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]]
102 end
103 end
104
105 pk.trainer = game
106
107 if param["storage"] == "party"
108 pk.box = nil
109 elsif param["storage"] == "box"
110 pk.box = param["box"] + 1
111 end
112
113 pk.slot = param["slot"]
114
115 pk.save!
116
117 rev = Revision.new(pokemon: pk)
118 rev.species_id = param["species"]
119 rev.nickname = param["nickname"]
120 rev.experience = param["experience"]
121 rev.level = param["level"]
122 rev.hp = param["hp"]
123 rev.attack = param["attack"]
124 rev.defense = param["defense"]
125 rev.special_attack = param["spAttack"]
126 rev.special_defense = param["spDefense"]
127 rev.speed = param["speed"]
128 rev.coolness = param["coolness"]
129 rev.beauty = param["beauty"]
130 rev.cuteness = param["cuteness"]
131 rev.smartness = param["smartness"]
132 rev.toughness = param["toughness"]
133 rev.sheen = param["sheen"]
134 rev.item_id = param["heldItem"] if param.key? "heldItem"
135 rev.move_1_id = param["moves"][0]["id"]
136 rev.move_1_pp_bonuses = param["moves"][0]["ppBonuses"]
137
138 if param["moves"].length >= 2
139 rev.move_2_id = param["moves"][1]["id"]
140 rev.move_2_pp_bonuses = param["moves"][1]["ppBonuses"]
141 end
142
143 if param["moves"].length >= 3
144 rev.move_3_id = param["moves"][2]["id"]
145 rev.move_3_pp_bonuses = param["moves"][2]["ppBonuses"]
146 end
147
148 if param["moves"].length == 4
149 rev.move_4_id = param["moves"][3]["id"]
150 rev.move_4_pp_bonuses = param["moves"][3]["ppBonuses"]
151 end
152
153 rev.cool_ribbons = param["coolRibbons"]
154 rev.beauty_ribbons = param["beautyRibbons"]
155 rev.cute_ribbons = param["cuteRibbons"]
156 rev.smart_ribbons = param["smartRibbons"]
157 rev.tough_ribbons = param["toughRibbons"]
158 rev.champion_ribbon = param.fetch "championRibbon", false
159 rev.winning_ribbon = param.fetch "winningRibbon", false
160 rev.victory_ribbon = param.fetch "victoryRibbon", false
161 rev.artist_ribbon = param.fetch "artistRibbon", false
162 rev.effort_ribbon = param.fetch "effortRibbon", false
163 rev.marine_ribbon = param.fetch "marineRibbon", false
164 rev.land_ribbon = param.fetch "landRibbon", false
165 rev.sky_ribbon = param.fetch "skyRibbon", false
166 rev.country_ribbon = param.fetch "countryRibbon", false
167 rev.national_ribbon = param.fetch "nationalRibbon", false
168 rev.earth_ribbon = param.fetch "earthRibbon", false
169 rev.world_ribbon = param.fetch "worldRibbon", false
170
171 if pk.revisions.empty? or rev.diff?(pk.current)
172 rev.save!
173 end
174 end
175
176 game.pokedex_entries.clear
177
178 args["seen"].each do |param|
179 game.pokedex_entries.create(species_id: param, caught: false)
180 end
181
182 args["caught"].each do |param|
183 game.pokedex_entries.create(species_id: param, caught: true)
184 end
185 end
186end
diff --git a/app/jobs/pokeviewer/application_job.rb b/app/jobs/pokeviewer/application_job.rb deleted file mode 100644 index 19ba955..0000000 --- a/app/jobs/pokeviewer/application_job.rb +++ /dev/null
@@ -1,4 +0,0 @@
1module Pokeviewer
2 class ApplicationJob < ActiveJob::Base
3 end
4end
diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb deleted file mode 100644 index bb18d68..0000000 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ /dev/null
@@ -1,188 +0,0 @@
1module Pokeviewer
2 class ExtractSaveDataJob < ApplicationJob
3 queue_as :default
4
5 def perform(args)
6 game = Trainer.find_or_initialize_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 game.box_1_name = args["boxes"].shift
26 game.box_2_name = args["boxes"].shift
27 game.box_3_name = args["boxes"].shift
28 game.box_4_name = args["boxes"].shift
29 game.box_5_name = args["boxes"].shift
30 game.box_6_name = args["boxes"].shift
31 game.box_7_name = args["boxes"].shift
32 game.box_8_name = args["boxes"].shift
33 game.box_9_name = args["boxes"].shift
34 game.box_10_name = args["boxes"].shift
35 game.box_11_name = args["boxes"].shift
36 game.box_12_name = args["boxes"].shift
37 game.box_13_name = args["boxes"].shift
38 game.box_14_name = args["boxes"].shift
39
40 if args.key? "marineRibbon"
41 game.marine_ribbon_id = args["marineRibbon"]
42 end
43
44 if args.key? "landRibbon"
45 game.land_ribbon_id = args["landRibbon"]
46 end
47
48 if args.key? "skyRibbon"
49 game.sky_ribbon_id = args["skyRibbon"]
50 end
51
52 if args.key? "countryRibbon"
53 game.country_ribbon_id = args["countryRibbon"]
54 end
55
56 if args.key? "nationalRibbon"
57 game.national_ribbon_id = args["nationalRibbon"]
58 end
59
60 if args.key? "earthRibbon"
61 game.earth_ribbon_id = args["earthRibbon"]
62 end
63
64 if args.key? "worldRibbon"
65 game.world_ribbon_id = args["worldRibbon"]
66 end
67
68 game.save! if game.new_record? or game.changed?
69
70 game.pokemon.clear
71
72 args["pokemon"].each do |param|
73 pk = Pokemon.find_or_create_by!(key: param["key"]) do |r|
74 r.ot_name = param["otName"]
75 r.ot_number = param["otId"]
76 r.ot_gender = param["otGender"]
77
78 if param["orre"]
79 r.met_type = :orre
80 elsif param["metLevel"] == 0
81 r.met_type = :hatched
82 r.location_id = param["metLocation"]
83 elsif param["metLocation"] == 254
84 r.met_type = :npc_trade
85 elsif param["metLocation"] == 255
86 r.met_type = :fateful_encounter
87 r.met_level = param["metLevel"]
88 else
89 r.met_type = :normal
90 r.met_level = param["metLevel"]
91 r.location_id = param["metLocation"]
92 end
93
94 r.shiny = param["shiny"]
95 r.nature = Pokemon.nature.values[param["nature"]]
96 r.gender = param["gender"]
97 r.second_ability = param["secondAbility"]
98 r.pokeball = Pokemon.pokeball.values[param["pokeball"] - 1]
99
100 # Handle Unown form
101 if param["species"] == 201
102 r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]]
103 end
104 end
105
106 pk.trainer = game
107
108 if param["storage"] == "party"
109 pk.box = nil
110 elsif param["storage"] == "box"
111 pk.box = param["box"] + 1
112 end
113
114 pk.slot = param["slot"]
115
116 pk.save!
117
118 rev = Revision.new(pokemon: pk)
119 rev.species_id = param["species"]
120 rev.nickname = param["nickname"]
121 rev.experience = param["experience"]
122 rev.level = param["level"]
123 rev.hp = param["hp"]
124 rev.attack = param["attack"]
125 rev.defense = param["defense"]
126 rev.special_attack = param["spAttack"]
127 rev.special_defense = param["spDefense"]
128 rev.speed = param["speed"]
129 rev.coolness = param["coolness"]
130 rev.beauty = param["beauty"]
131 rev.cuteness = param["cuteness"]
132 rev.smartness = param["smartness"]
133 rev.toughness = param["toughness"]
134 rev.sheen = param["sheen"]
135 rev.item_id = param["heldItem"] if param.key? "heldItem"
136 rev.move_1_id = param["moves"][0]["id"]
137 rev.move_1_pp_bonuses = param["moves"][0]["ppBonuses"]
138
139 if param["moves"].length >= 2
140 rev.move_2_id = param["moves"][1]["id"]
141 rev.move_2_pp_bonuses = param["moves"][1]["ppBonuses"]
142 end
143
144 if param["moves"].length >= 3
145 rev.move_3_id = param["moves"][2]["id"]
146 rev.move_3_pp_bonuses = param["moves"][2]["ppBonuses"]
147 end
148
149 if param["moves"].length == 4
150 rev.move_4_id = param["moves"][3]["id"]
151 rev.move_4_pp_bonuses = param["moves"][3]["ppBonuses"]
152 end
153
154 rev.cool_ribbons = param["coolRibbons"]
155 rev.beauty_ribbons = param["beautyRibbons"]
156 rev.cute_ribbons = param["cuteRibbons"]
157 rev.smart_ribbons = param["smartRibbons"]
158 rev.tough_ribbons = param["toughRibbons"]
159 rev.champion_ribbon = param.fetch "championRibbon", false
160 rev.winning_ribbon = param.fetch "winningRibbon", false
161 rev.victory_ribbon = param.fetch "victoryRibbon", false
162 rev.artist_ribbon = param.fetch "artistRibbon", false
163 rev.effort_ribbon = param.fetch "effortRibbon", false
164 rev.marine_ribbon = param.fetch "marineRibbon", false
165 rev.land_ribbon = param.fetch "landRibbon", false
166 rev.sky_ribbon = param.fetch "skyRibbon", false
167 rev.country_ribbon = param.fetch "countryRibbon", false
168 rev.national_ribbon = param.fetch "nationalRibbon", false
169 rev.earth_ribbon = param.fetch "earthRibbon", false
170 rev.world_ribbon = param.fetch "worldRibbon", false
171
172 if pk.revisions.empty? or rev.diff?(pk.current)
173 rev.save!
174 end
175 end
176
177 game.pokedex_entries.clear
178
179 args["seen"].each do |param|
180 game.pokedex_entries.create(species_id: param, caught: false)
181 end
182
183 args["caught"].each do |param|
184 game.pokedex_entries.create(species_id: param, caught: true)
185 end
186 end
187 end
188end