<commit_before>var Map = require('./map');
var Login = require('./login');
var ShipModels = require('./shipModels');
var Ships = require('./ships');
var User = require('./user');

var async = require('async');

function initialize(done) {
    Map.refresh(function() {
        ShipModels.refresh(function() {
            Login.refresh(function() {
                Ships.refresh(function() {
                    User.refresh(function() {
                        console.log("Bot Started");
                        done();
                    });
                });
            });
        });
    });
}

function getRandomCity() {
    if (Map.list.length === 0) return null;
    var c = Map.list[Math.floor(Math.random() * Map.list.length)];
    return c;
}

function getCheapestModel() {
    var res = null;
    var val = null;
    for (var i = 0; i < ShipModels.list.length; i++) {
        var b = ShipModels.list[i];
        if (!val || val > b.price) {
            res = b;
            val = b.price;
        }
    }
    return res;
}


var actions = {
    buildShip: function(done) {
        console.log("--Build Ship");
        Ships.refresh(function() {
            var n = Ships.list.length;
            n++;
            Ships.buildShip(getCheapestModel(), getRandomCity().slug, "Golden Heart MK-" + n, function(err, res) {
                Ships.refresh(done);
            });
        });
    },
    moveShip: function(ship, done) {
        console.log("--Move Ship [" + ship.slug + "]");
        Ships.getShip(ship, function(err, res) {
            if (err) return done(err);
            var s = res;
            if (s.status.value !== "docked") return done();
            var c = getRandomCity();
            while (c === s.city) c = getRandomCity();
            Ships.moveShip(s, c, done);
        });
    },
    tradeProducts: function(ship, done) {
        console.log("--Trade Products [" + ship.slug + "]");
        Ships.getShip(ship, function(err, res) {
            if (err) return done(err);
            var s = res;
            if (s.status.value != 'docked') return done();
            Map.getCityProducts(s.city, function(err, res) {
                if (err) console.log(err);
                var c = res;
                var sellprods = [];
                var buyprods = [];
                for (var i in c)
                    if (c[i].quantity >= 20 && c[i].production > 0) buyprods.push(i);
                for (var j in s.cargo)
                    if (c[j].quantity <= 2 && c[j].production < 0 && s.cargo[j] >= 10) sellprods.push(j);
                    //Sells products
                async.each(sellprods, function(item, cb) {
                    Ships.sellProduct(s.slug, item, 10, function(err, res) {
                        //if (err) console.log(err);
                        cb();
                    });
                }, function() {
                    //Buy products
                    async.each(buyprods, function(item, cb) {
                        Ships.buyProduct(s.slug, item, 10, function(err, res) {
                            //if (err) console.log(err);
                            cb();
                        });
                    }, done);
                });
            });
        });
    }
};

var heuristics = {
    build: function() {
        if (Ships.list.length === 0 && getCheapestModel().price <= User.money) return true;
        else return false;
    }
};

function bindAction(heuristic, action, next) {
    if (heuristic()) return action(next);
    else return next();
}

function mainLoop() {
    console.log("Main Loop");
    bindAction(heuristics.build, actions.buildShip, function(err) {
        if (err) console.log(err);
        async.each(Ships.list, function(item, cb) {
            actions.tradeProducts(item, function(err) {
                if (err) console.log(err);
                actions.moveShip(item, function(err) {
                    if (err) console.log(err);
                    cb();
                });
            });
        }, function(err) {
            if (err) console.log(err);
        });
    });

}

module.exports = function() {
    initialize(function() {
        setInterval(mainLoop, 3000);<commit_msg>added new build rule<commit_after>var Map = require('./map');
var Login = require('./login');
var ShipModels = require('./shipModels');
var Ships = require('./ships');
var User = require('./user');

var async = require('async');

function initialize(done) {
    Map.refresh(function() {
        ShipModels.refresh(function() {
            Login.refresh(function() {
                Ships.refresh(function() {
                    User.refresh(function() {
                        console.log("Bot Started");
                        done();
                    });
                });
            });
        });
    });
}

function getRandomCity() {
    if (Map.list.length === 0) return null;
    var c = Map.list[Math.floor(Math.random() * Map.list.length)];
    return c;
}

function getCheapestModel() {
    var res = null;
    var val = null;
    for (var i = 0; i < ShipModels.list.length; i++) {
        var b = ShipModels.list[i];
        if (!val || val > b.price) {
            res = b;
            val = b.price;
        }
    }
    return res;
}


var actions = {
    buildShip: function(done) {
        console.log("--Build Ship");
        Ships.refresh(function() {
            var n = Ships.list.length;
            n++;
            Ships.buildShip(getCheapestModel(), getRandomCity().slug, "Golden Heart MK-" + n, function(err, res) {
                Ships.refresh(done);
            });
        });
    },
    moveShip: function(ship, done) {
        console.log("--Move Ship [" + ship.slug + "]");
        Ships.getShip(ship, function(err, res) {
            if (err) return done(err);
            var s = res;
            if (s.status.value !== "docked") return done();
            var c = getRandomCity();
            while (c === s.city) c = getRandomCity();
            Ships.moveShip(s, c, done);
        });
    },
    tradeProducts: function(ship, done) {
        console.log("--Trade Products [" + ship.slug + "]");
        Ships.getShip(ship, function(err, res) {
            if (err) return done(err);
            var s = res;
            if (s.status.value != 'docked') return done();
            Map.getCityProducts(s.city, function(err, res) {
                if (err) console.log(err);
                var c = res;
                var sellprods = [];
                var buyprods = [];
                for (var i in c)
                    if (c[i].quantity >= 20 && c[i].production > 0) buyprods.push(i);
                for (var j in s.cargo)
                    if (c[j].quantity <= 2 && c[j].production < 0 && s.cargo[j] >= 10) sellprods.push(j);
                    //Sells products
                async.each(sellprods, function(item, cb) {
                    Ships.sellProduct(s.slug, item, 10, function(err, res) {
                        //if (err) console.log(err);
                        cb();
                    });
                }, function() {
                    //Buy products
                    async.each(buyprods, function(item, cb) {
                        Ships.buyProduct(s.slug, item, 10, function(err, res) {
                            //if (err) console.log(err);
                            cb();
                        });
                    }, done);
                });
            });
        });
    }
};

var heuristics = {
    build: function() {
        if (Ships.list.length === 0 && getCheapestModel().price <= User.money) return true;
        if(User.money>(getCheapestModel().price*3)) return true;
        else return false;
    }
};

function bindAction(heuristic, action, next) {
    if (heuristic()) return action(next);
    else return next();
}

function mainLoop() {
    console.log("Main Loop");
    bindAction(heuristics.build, actions.buildShip, function(err) {
        if (err) console.log(err);
        async.each(Ships.list, function(item, cb) {
            actions.tradeProducts(item, function(err) {
                if (err) console.log(err);
                actions.moveShip(item, function(err) {
                    if (err) console.log(err);
                    cb();
                });
            });
        }, function(err) {
            if (err) console.log(err);
        });
    });

}

module.exports = function() {
    initialize(function() {
        setInterval(mainLoop, 3000);
{'fraction_non_alphanumeric': 0.1507768361581921, 'fraction_numerical': 0.0043549905838041435, 'mean_word_length': 1.4808759124087592, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5763736', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2411, 'n_words': 539}