about summary refs log tree commit diff stats
path: root/app/jobs/extract_save_data_job.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/jobs/extract_save_data_job.rb')
-rw-r--r--app/jobs/extract_save_data_job.rb186
1 files changed, 186 insertions, 0 deletions
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