Class: ItchRewards::CLI::Commands::Rewards::Automate

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
AuthOptions, Helper
Defined in:
lib/itch_rewards/cli.rb

Instance Method Summary collapse

Methods included from AuthOptions

included

Methods included from Helper

#authenticated_client, #authenticated_client!, #cli, #color, #objects_to_table, #render_table, #show_rewards

Instance Method Details

#call(**options) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/itch_rewards/cli.rb', line 252

def call(**options)
  client = authenticated_client!(options)

  if !File.exist? options[:config]
    cli.error("Config file #{options[:config]} does not exist")
    exit 1
  end

  config = load_config(options[:config])
  unless config["games"].is_a? Hash
    cli.error("No games configured for rewards updates in config file")
    exit 1
  end
  
  unless options[:save]
    cli.warn "Dry run, results will not be saved"
  end

  purchases_by_game = client.purchases.history.each.group_by {|row| row['object_name'] }.to_h

  config["games"].each do |name, data|
    name = name.chomp

    next unless data["reward_by_tip"] > 0 || data["reward_by_purchase"] > 0 || data["minimum_available"] > 0
    game = client.game(data["id"])
    rewards = game.rewards.list
  
    reward = rewards.find {|r| r.id == data["reward_id"]}
    unless reward
      cli.warn "Could not find reward #{data["reward_id"]} for game #{name}, skipping..."
      next
    end

    tip_modifier = data["reward_by_tip"].to_f
    purchase_modifier = data["reward_by_purchase"].to_f
    minimum = data["minimum_available"].to_i
    template = data["reward_description_template"]

    new_description = reward.description              
    new_amount = Array(purchases_by_game[name]).inject(data["reward_offset"]) do |sum, purchase|
      price = purchase["product_price"].to_i
      tip = (purchase["tip"].to_f * 100).to_i

      next sum unless price > 0
      
      sum += (tip.fdiv(price)) * tip_modifier
      sum += purchase_modifier

      sum
    end
    new_amount = [new_amount, reward.claimed + minimum].max if minimum > 0

    if template && !template.empty?
      new_description = template.gsub(/{ *(quantity|remaining_percent|remaining_percent_integer) *}/, "{\\1}")
        .gsub(/{ *(quantity|remaining_percent|remaining_percent_integer) *}/,
          "{quantity}" => new_amount.floor,
          "{remaining_percent}" => ((new_amount % 1) * 100).floor(1),
          "{remaining_percent_integer}" => ((new_amount % 1) * 100).floor
        )
    end
    
    if new_amount.to_i != reward.amount
      cli.say "Changing #{name} reward #{reward.id} quantity from #{color.green.bold(reward.amount.to_s)} to #{color.yellow.bold(new_amount.to_i)}"
    end
                
    if new_description != reward.description
      cli.say "Changing #{name} reward #{reward.id} description to:\n#{color.yellow.bold(new_description)}"
    end

    if options[:save]
      reward.description = new_description
      reward.amount = new_amount.to_i

      game.rewards.save rewards
    end
  end
end

#load_config(path) ⇒ Object



240
241
242
243
244
245
# File 'lib/itch_rewards/cli.rb', line 240

def load_config(path)
  YAML.load_file(path)
rescue YAML::ParseError => e
  cli.error("Config file (#{path}) is not valid yaml")
  exit 1
end