Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/trophy_scraper/controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Controller

Returns a new instance of Controller.



2
3
4
5
6
# File 'lib/trophy_scraper/controller.rb', line 2

def initialize(options)
  @options = options.clone
  $logger = Logger.new(@options['log_dest'])
  $logger.level = @options['log_level']
end

Instance Method Details

#processObject



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
# File 'lib/trophy_scraper/controller.rb', line 8

def process
  #Initialize connection to db and do any version migration needed.
  data_store = DataStore.new(@options['db'], @options['db_log_level'], @options['db_log_dest'])
  
  users_to_check = get_users_to_check
  trophy_levels = TrophyLevel.all
  
  users_to_check.each do |user|
    $logger.info("Preparing to update #{user.username}.")
    scraper = Scraper.new(user.username)
    
    user_summary_stats = scraper.get_user_summary_stats
    if user_summary_stats.total_trophies > user.unlocks.size
      $logger.info("Website reports more trophies for #{user.username} than data store.")
      user.user_stats << user_summary_stats
      
      games_summary_stats = scraper.get_game_summary_stats()
      games_summary_stats.each_pair do |found_game, trophies_earned|
        game = Game.first(:title => found_game.title)
        
        if game == nil
          game = found_game
          
          game.save
          $logger.debug("Saved new game: #{game.title}")
        end
        
        trophy_info = scraper.get_game_detail_stats(found_game.uri, trophy_levels)
        trophy_info.each_pair do |found_trophy, earned_date|
          trophy = game.trophies.first(:name => found_trophy.name, :game_id => game.id, :trophy_level_id => found_trophy.trophy_level_id)
          if trophy == nil
            trophy = found_trophy
            
            game.trophies << trophy
            trophy.save
            $logger.debug("Saved new trophy: #{trophy.name}")
          end
          
          if earned_date != nil
            unlock = Unlock.first(:user_username => user.username, :trophy_id => trophy.id)
            if unlock == nil
              unlock = Unlock.new(:earned_on => earned_date)
              trophy.unlocks << unlock
              user.unlocks << unlock
              unlock.save
              $logger.info("#{user.username} has unlocked '#{trophy.name}' in '#{game.title}'.")
            end
          end
        end
      end
    elsif user_summary_stats.total_trophies == user.unlocks.size
      $logger.info("Same number of trophies on website and in data store. Nothing to update for #{user.username}")
    else
      $logger.warn("Website reports less trophies than data store for #{user.username}. Something is wrong.")
    end
    
    user.last_updated = Time.now
    user.save
  end
  
  $logger.close
end