'use strict';

var http = require('http');
var fs = require('fs');

var hiscoresUrl = 'http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=';

var playerName = 'Max Deviant';

http.get(hiscoresUrl + playerName, function (res) {
    var data = [];

    res.on('data', function (chunk) {
        data.push(chunk);
    });

    res.on('end', function () {
        var stats = extractStats(data.join().split('\n'));

        fs.readFile('quests.json', 'utf8', function (err, data) {
            if (err) {
                console.log(err);

                return;
            }

            var questList = JSON.parse(data);

            var tracker = new Quests(questList);

            console.log(tracker.findAvailable(stats));
        });
    });
}).end();

function extractStats(raw) {
    var stats = {};

    var skills = [
        'overall',
        'attack',
        'defence',
        'strength',
        'constitution',
        'ranged',
        'prayer',
        'magic',
        'cooking',
        'woodcutting',
        'fletching',
        'fishing',
        'firemaking',
        'crafting',
        'smithing',
        'mining',
        'herblore',
        'agility',
        'thieving',
        'slayer',
        'farming',
        'runecrafting',
        'hunter',
        'construction'
    ];

    for (var i in skills) {
        var row = raw[i].split(',');

        stats[skills[i]] = {
            rank: row[0],
            level: row[1],
            xp: row[2]
        };
    }

    return stats;
}

function grep (array, key, value) {
    for (var i in array) {
        if (array[i][key] === value) {
            return array[i];
        }
    }
};

var Quests = function (questList) {
    this.list = questList;

    this.findAvailable = function (stats) {
        var available = [];

        for (var i in this.list) {
            var canDo = true;
            var skillReqs = this.list[i].requirements.skills;

            for (var j in skillReqs) {
                if (stats[j].level < skillReqs) {
                    canDo = false;
                }
            }

            if (canDo) {
                available.push(this.list[i].slug);
            }
        }

        return available;
    };
};

@@ -681,16 +681,164 @@
 uestList
+, stats);%0A%0A            tracker.completeQuests(%5B'vampire-slayer', 'pirates-treasure', 'death-plateau'%5D)%0A            tracker.completeQuest('lost-city'
 );%0A%0A    
@@ -879,21 +879,16 @@
 ailable(
-stats
 ));%0A    
@@ -1860,16 +1860,23 @@
 uestList
+, stats
 ) %7B%0A    
@@ -1897,16 +1897,65 @@
 estList;
+%0A    this.stats = stats;%0A    this.completed = %7B%7D;
 %0A%0A    th
@@ -1983,21 +1983,16 @@
 nction (
-stats
 ) %7B%0A    
@@ -2147,52 +2147,677 @@
 ls;%0A
-%0A            for (var j in skillReqs) %7B%0A    
+            var questReqs = this.list%5Bi%5D.requirements.quests;%0A%0A            if (this.completed%5Bthis.list%5Bi%5D.slug%5D) %7B%0A                continue;%0A            %7D%0A%0A            if (this.hasRequirements(this.list%5Bi%5D.slug)) %7B%0A                available.push(this.list%5Bi%5D.slug);%0A            %7D%0A        %7D%0A%0A        return available;%0A    %7D;%0A%0A    this.hasRequirements = function (questName) %7B%0A        var hasStats = true;%0A        var hasQuests = true;%0A%0A        var quest = grep(this.list, 'slug', questName) %7C%7C grep(this.list, 'name', questName);%0A%0A        var skillReqs = quest.requirements.skills;%0A        var questReqs = quest.requirements.quests;%0A%0A        for (var i in skillReqs) %7B%0A
     
@@ -2832,15 +2832,20 @@
 if (
+this.
 stats%5B
-j
+i
 %5D.le
@@ -2851,32 +2851,35 @@
 evel %3C skillReqs
+%5Bi%5D
 ) %7B%0A            
@@ -2886,17 +2886,16 @@
     
-    canDo
+hasStats
  = f
@@ -2906,31 +2906,38 @@
 ;%0A          
+  %7D%0A
       
-%7D
+  %7D%0A
 %0A           
@@ -2937,141 +2937,402 @@
     
-    %7D%0A%0A            if (canDo) %7B%0A                available.push(this.list%5Bi%5D.slug);%0A            %7D%0A        %7D%0A%0A        return available;
+for (var i in questReqs) %7B%0A            if (!this.completed%5Bi%5D) %7B%0A                hasQuests = false;%0A            %7D%0A        %7D%0A%0A        return hasStats && hasQuests;%0A    %7D;%0A%0A    this.completeQuest = function (quest) %7B%0A        this.completeQuests(%5Bquest%5D);%0A    %7D;%0A%0A    this.completeQuests = function (quests) %7B%0A        for (var i in quests) %7B%0A            this.completed%5Bquests%5Bi%5D%5D = true;%0A        %7D
 %0A   

Create separate method for checking if requirements are met