exports.name = 'vapor-idler';

exports.plugin = function(VaporAPI) {
    var isIdling = false;

    var log = VaporAPI.getLogger();
    var steamUser = VaporAPI.getHandler('steamUser');
    var utils = VaporAPI.getUtils();
    var Steam = VaporAPI.getSteam();

    var config = VaporAPI.data || {};
    if(!('games' in config)) {
        log.error('Array of game IDs in your config is missing.');
        return;
    }

    // Construct CMsgClientGamesPlayed message
    var clientMessage = {games_played: []};
    for(var i = 0; i < config.games.length; i++) {
        clientMessage.games_played.push({game_id: config.games[i]});
    }

    VaporAPI.registerHandler({
            emitter: 'steamFriends',
            event: 'friendMsg'
        },
        function(user, message, type) {
            if(type === Steam.EChatEntryType.ChatMsg && utils.isAdmin(user)) {
                if(message === '!idle') {
                    if(isIdling) {
                        steamUser.gamesPlayed({games_played:[]});
                        log.info('Stopped idling.');
                    } else {
                        steamUser.gamesPlayed(clientMessage);
                        log.info('Started idling in the following games: %s', config.games.join(', '));
                    }

                    isIdling = !isIdling;
                }
            }
        }
    );

    // Register this handler only if autoStart is set to true
    if(config.autoStart === true) {
        VaporAPI.registerHandler({
                emitter: 'vapor',
                event: 'ready'
            },
            function() {
                steamUser.gamesPlayed(clientMessage);
                log.info('Idling has been started automatically.');
                isIdling = true;
            }
        );
    }

};

@@ -62,18 +62,16 @@
 rAPI) %7B%0A
-  
   var is
@@ -87,18 +87,16 @@
 false;%0A%0A
-  
   var lo
@@ -119,26 +119,24 @@
 Logger();%0A  
- 
- 
 var steamUse
@@ -173,18 +173,16 @@
 User');%0A
-  
   var ut
@@ -208,18 +208,16 @@
 tils();%0A
-  
   var St
@@ -244,18 +244,16 @@
 eam();%0A%0A
-  
   var co
@@ -280,18 +280,16 @@
  %7C%7C %7B%7D;%0A
-  
   if(!('
@@ -309,28 +309,24 @@
 fig)) %7B%0A    
- 
-   
 log.error('A
@@ -380,20 +380,16 @@
     
-  
-  
 return;%0A
     
@@ -384,27 +384,23 @@
 return;%0A
-  
   %7D%0A%0A  
- 
- 
 // Const
@@ -434,18 +434,16 @@
 message%0A
-  
   var cl
@@ -476,18 +476,16 @@
 d: %5B%5D%7D;%0A
-  
   for(va
@@ -521,28 +521,24 @@
 gth; i++) %7B%0A
-    
     clientMe
@@ -590,25 +590,21 @@
 s%5Bi%5D%7D);%0A
-  
   %7D%0A%0A
-  
   VaporA
@@ -620,32 +620,24 @@
 erHandler(%7B%0A
-        
     emitter:
@@ -653,24 +653,16 @@
 iends',%0A
-        
     even
@@ -678,32 +678,18 @@
 dMsg'%0A  
-      %7D,%0A       
+%7D,
  functio
@@ -709,32 +709,24 @@
 ge, type) %7B%0A
-        
     if(type 
@@ -780,34 +780,24 @@
 in(user)) %7B%0A
-          
       if(mes
@@ -812,36 +812,24 @@
  '!idle') %7B%0A
-            
         if(i
@@ -839,30 +839,16 @@
 ling) %7B%0A
-              
         
@@ -897,38 +897,24 @@
 ;%0A          
-       
-       
 log.info('St
@@ -942,43 +942,17 @@
     
-            %7D else %7B%0A              
+%7D else %7B%0A
     
@@ -1005,30 +1005,16 @@
         
-      
-        
 log.info
@@ -1097,35 +1097,11 @@
     
-            %7D%0A%0A            
+%7D%0A%0A
     
@@ -1136,54 +1136,23 @@
     
-          %7D%0A            %7D%0A        
 %7D%0A    
+%7D%0A  %7D
 );%0A%0A
-  
   //
@@ -1207,18 +1207,16 @@
 to true%0A
-  
   if(con
@@ -1237,28 +1237,24 @@
 === true) %7B%0A
-    
     VaporAPI
@@ -1272,26 +1272,16 @@
 ndler(%7B%0A
-          
       em
@@ -1296,26 +1296,16 @@
 vapor',%0A
-          
       ev
@@ -1325,30 +1325,10 @@
     
-        %7D,%0A           
+%7D,
  fun
@@ -1337,26 +1337,16 @@
 ion() %7B%0A
-          
       st
@@ -1381,26 +1381,16 @@
 ssage);%0A
-          
       lo
@@ -1439,26 +1439,16 @@
 lly.');%0A
-          
       is
@@ -1470,31 +1470,12 @@
     
-        %7D%0A        
+%7D
 );%0A
-  
   %7D%0A

fix indentation