about summary refs log tree commit diff stats
path: root/app/jobs/extract_save_data_job.rb
blob: 282bcf19c1a8e18fbc9365164e889690d0321178 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class ExtractSaveDataJob < ApplicationJob
  queue_as :default

  def perform(args)
    game = Trainer.find_or_initialize_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

    game.box_1_name = args["boxes"].shift
    game.box_2_name = args["boxes"].shift
    game.box_3_name = args["boxes"].shift
    game.box_4_name = args["boxes"].shift
    game.box_5_name = args["boxes"].shift
    game.box_6_name = args["boxes"].shift
    game.box_7_name = args["boxes"].shift
    game.box_8_name = args["boxes"].shift
    game.box_9_name = args["boxes"].shift
    game.box_10_name = args["boxes"].shift
    game.box_11_name = args["boxes"].shift
    game.box_12_name = args["boxes"].shift
    game.box_13_name = args["boxes"].shift
    game.box_14_name = args["boxes"].shift

    if args.key? "marineRibbon"
      game.marine_ribbon_id = args["marineRibbon"]
    end

    if args.key? "landRibbon"
      game.land_ribbon_id = args["landRibbon"]
    end

    if args.key? "skyRibbon"
      game.sky_ribbon_id = args["skyRibbon"]
    end

    if args.key? "countryRibbon"
      game.country_ribbon_id = args["countryRibbon"]
    end

    if args.key? "nationalRibbon"
      game.national_ribbon_id = args["nationalRibbon"]
    end

    if args.key? "earthRibbon"
      game.earth_ribbon_id = args["earthRibbon"]
    end

    if args.key? "worldRibbon"
      game.world_ribbon_id = args["worldRibbon"]
    end

    game.save! if game.new_record? or game.changed?

    game.pokemon.clear

    args["pokemon"].each do |param|
      pk = Pokemon.find_or_create_by!(key: param["key"]) do |r|
        r.ot_name = param["otName"]
        r.ot_number = param["otId"]
        r.ot_gender = param["otGender"]

        if param["orre"]
          r.met_type = :orre
        elsif param["metLevel"] == 0
          r.met_type = :hatched
          r.location_id = param["metLocation"]
        elsif param["metLocation"] == 254
          r.met_type = :npc_trade
        elsif param["metLocation"] == 255
          r.met_type = :fateful_encounter
          r.met_level = param["metLevel"]
        else
          r.met_type = :normal
          r.met_level = param["metLevel"]
          r.location_id = param["metLocation"]
        end

        r.shiny = param["shiny"]
        r.nature = Pokemon.nature.values[param["nature"]]
        r.gender = param["gender"]
        r.second_ability = param["secondAbility"]
        r.pokeball = Pokemon.pokeball.values[param["pokeball"] - 1]

        # Handle Unown form
        if param["species"] == 201
          r.unown_letter = Pokemon.unown_letter.values[param["unownLetter"]]
        end
      end

      pk.trainer = game

      if param["storage"] == "party"
        pk.box = nil
      elsif param["storage"] == "box"
        pk.box = param["box"] + 1
      end

      pk.slot = param["slot"]

      pk.save!

      rev = Revision.new(pokemon: pk)
      rev.species_id = param["species"]
      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.item_id = param["heldItem"] if param.key? "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

      rev.cool_ribbons = param["coolRibbons"]
      rev.beauty_ribbons = param["beautyRibbons"]
      rev.cute_ribbons = param["cuteRibbons"]
      rev.smart_ribbons = param["smartRibbons"]
      rev.tough_ribbons = param["toughRibbons"]
      rev.champion_ribbon = param.fetch "championRibbon", false
      rev.winning_ribbon = param.fetch "winningRibbon", false
      rev.victory_ribbon = param.fetch "victoryRibbon", false
      rev.artist_ribbon = param.fetch "artistRibbon", false
      rev.effort_ribbon = param.fetch "effortRibbon", false
      rev.marine_ribbon = param.fetch "marineRibbon", false
      rev.land_ribbon = param.fetch "landRibbon", false
      rev.sky_ribbon = param.fetch "skyRibbon", false
      rev.country_ribbon = param.fetch "countryRibbon", false
      rev.national_ribbon = param.fetch "nationalRibbon", false
      rev.earth_ribbon = param.fetch "earthRibbon", false
      rev.world_ribbon = param.fetch "worldRibbon", false

      if pk.revisions.empty? or rev.diff?(pk.current)
        rev.save!
      end
    end

    game.pokedex_entries.clear

    args["seen"].each do |param|
      game.pokedex_entries.create(species_id: param, caught: false)
    end

    args["caught"].each do |param|
      game.pokedex_entries.create(species_id: param, caught: true)
    end
  end
end