Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Clients #22

Closed
henopied opened this issue Oct 10, 2015 · 7 comments
Closed

Multiple Clients #22

henopied opened this issue Oct 10, 2015 · 7 comments

Comments

@henopied
Copy link
Contributor

What is the best way of going about spawning multiple clients off of one script?

@clouedoc
Copy link

Write script like mine...

var region = 'TK-Turkey'; //server region to request

var http = require('http');
var AgarioClient = require('./agario-client.js'); //in your code you should do require('agario-client')

var connection = false;
var margeX = 0;
var margeY = 0;
var posXX = 0;
var posYY = 0;
var lastX = 0;
var lastY = 0;
var serveur = "ws://70.35.206.89:443";

var client = new AgarioClient('worker');
client.connect(serveur,"");
client.on('connected', function() {client.log('spawning');client.spawn('Cameraman');client.moveTo(0, 0);});

client.on('lostMyBalls', function() { //when i lost all my balls
client.log('lost all my balls, respawning');
client.spawn('Cameraman'); //spawning new ball with nickname "agario-client"
client.moveTo(lastX, lastY);
});

AgarioClient.prototype.defineActor = function(ball_id) { //adding client.addFriend(ball_id) function
var ball = client.balls[ball_id];
ball.is_actor = true; //set ball.is_friend to true
ball.on('destroy', function() { //when this friend will be destroyed
client.emit('actorLost', ball); //emit friendEaten event
});
client.log(ball.name + ' is defined as actor ');
};

AgarioClient.Ball.prototype.isActor = function() { //adding ball.isMyFriend() funtion
return this.is_friend == true; //if ball is_friend is true, then true will be returned
};

client.on('ballAppear', function(ball_id) { //when we somebody
var ball = client.balls[ball_id];
if(ball.mine) return; //this is mine ball
if(ball.isActor()) return; //this ball is already a friend
if(ball.name == 'Acteur.') { //if ball have name 'agario-client'
client.defineActor(ball_id); //add it to friends
client.log('first ball id: '+client.my_balls);
client.log('x: '+ client.balls[client.my_balls].x + ' y: '+ client.balls[client.my_balls].y);
client.log('actorX: '+ ball.x + ' actorY: '+ ball.y);
client.moveTo(client.balls[client.my_balls].x, client.balls[client.my_balls].y);
launchFeeders();
ball.on('move', function(old_x, old_y, new_x, new_y) {
client.log("ActorX: "+ new_x + "actorY: "+ new_y);
client.log(ball.size);
client.moveTo(new_x + ball.size_2, new_y + ball.size_2);
updatePosition(new_x, new_y);
lastX = new_x;
lastY = new_y;
})
}

});

var client3 = new AgarioClient('worker3');
var client1 = new AgarioClient('worker1');
var client2 = new AgarioClient('worker2');
var client4 = new AgarioClient('worker4');
var client5 = new AgarioClient('worker5');
var client13 = new AgarioClient('worker13');
var client11 = new AgarioClient('worker11');
var client12 = new AgarioClient('worker12');
var client14 = new AgarioClient('worker14');
var client15 = new AgarioClient('worker15');
var client23 = new AgarioClient('worker23');
var client21 = new AgarioClient('worker21');
var client22 = new AgarioClient('worker22');
var client24 = new AgarioClient('worker24');
var client25 = new AgarioClient('worker25');

function launchFeeders() {

client1.connect(serveur,"");
client1.on('connected', function() {client1.log('spawning');client1.spawn('Hyper Balle de la mort qui tue a ne pas manger');client1.moveTo(posXX, posYY);});


client2.connect(serveur,"");
client2.on('connected', function() {client2.log('spawning');client2.spawn('Hyper Balle de la mort qui tue a ne pas manger');client2.moveTo(0, 0);});


client3.connect(serveur,"");
client3.on('connected', function() {client3.log('spawning');client3.spawn('Hyper Balle de la mort qui tue a ne pas manger');client3.moveTo(0, 0);});


client4.connect(serveur,"");
client4.on('connected', function() {client4.log('spawning');client4.spawn('Hyper Balle de la mort qui tue a ne pas manger');client4.moveTo(0, 0);});


client5.connect(serveur,"");
client5.on('connected', function() {client5.log('spawning');client5.spawn('Hyper Balle de la mort qui tue a ne pas manger');client5.moveTo(0, 0);});

client11.connect(serveur,"");
client11.on('connected', function() {client11.log('spawning');client11.spawn('Hyper Balle de la mort qui tue a ne pas manger');client11.moveTo(posXX, posYY);});


client12.connect(serveur,"");
client12.on('connected', function() {client12.log('spawning');client12.spawn('Hyper Balle de la mort qui tue a ne pas manger');client12.moveTo(0, 0);});


client13.connect(serveur,"");
client13.on('connected', function() {client13.log('spawning');client13.spawn('Hyper Balle de la mort qui tue a ne pas manger');client13.moveTo(0, 0);});


client14.connect(serveur,"");
client14.on('connected', function() {client14.log('spawning');client14.spawn('Hyper Balle de la mort qui tue a ne pas manger');client14.moveTo(0, 0);});


client15.connect(serveur,"");
client15.on('connected', function() {client15.log('spawning');client15.spawn('Hyper Balle de la mort qui tue a ne pas manger');client15.moveTo(0, 0);});

client21.connect(serveur,"");
client21.on('connected', function() {client21.log('spawning');client21.spawn('Hyper Balle de la mort qui tue a ne pas manger');client21.moveTo(posXX, posYY);});


client22.connect(serveur,"");
client22.on('connected', function() {client22.log('spawning');client22.spawn('Hyper Balle de la mort qui tue a ne pas manger');client22.moveTo(0, 0);});


client23.connect(serveur,"");
client23.on('connected', function() {client23.log('spawning');client23.spawn('Hyper Balle de la mort qui tue a ne pas manger');client23.moveTo(0, 0);});


client24.connect(serveur,"");
client24.on('connected', function() {client24.log('spawning');client24.spawn('Hyper Balle de la mort qui tue a ne pas manger');client24.moveTo(0, 0);});


client25.connect(serveur,"");
client25.on('connected', function() {client25.log('spawning');client25.spawn('Hyper Balle de la mort qui tue a ne pas manger');client25.moveTo(0, 0);});


client.on('lostMyBalls', function() { //when i lost all my balls
    client.log('lost all my balls, respawning');
    client.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client"
    client.moveTo(0, 0);
});

client2.on('lostMyBalls', function() { //when i lost all my balls
    client2.log('lost all my balls, respawning');
    client2.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client2"
    client2.moveTo(0, 0);
});

client3.on('lostMyBalls', function() { //when i lost all my balls
    client3.log('lost all my balls, respawning');
    client3.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client3"
    client3.moveTo(0, 0);
});

client4.on('lostMyBalls', function() { //when i lost all my balls
    client4.log('lost all my balls, respawning');
    client4.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client4"
    client4.moveTo(0, 0);
});

client5.on('lostMyBalls', function() { //when i lost all my balls
    client5.log('lost all my balls, respawning');
    client5.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client5"
    client5.moveTo(0, 0);
});

client1.on('lostMyBalls', function() { //when i lost all my balls
    client1.log('lost all my balls, respawning');
    client1.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client1"
    client1.moveTo(0, 0);
});

client12.on('lostMyBalls', function() { //when i lost all my balls
    client12.log('lost all my balls, respawning');
    client12.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client12"
    client12.moveTo(0, 0);
});

client13.on('lostMyBalls', function() { //when i lost all my balls
    client13.log('lost all my balls, respawning');
    client13.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client13"
    client13.moveTo(0, 0);
});

client14.on('lostMyBalls', function() { //when i lost all my balls
    client14.log('lost all my balls, respawning');
    client14.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client14"
    client14.moveTo(0, 0);
});

client15.on('lostMyBalls', function() { //when i lost all my balls
    client15.log('lost all my balls, respawning');
    client15.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client15"
    client15.moveTo(0, 0);
});

client11.on('lostMyBalls', function() { //when i lost all my balls
    client11.log('lost all my balls, respawning');
    client11.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client11"
    client11.moveTo(0, 0);
});



client22.on('lostMyBalls', function() { //when i lost all my balls
    client22.log('lost all my balls, respawning');
    client22.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client22"
    client22.moveTo(0, 0);
});

client23.on('lostMyBalls', function() { //when i lost all my balls
    client23.log('lost all my balls, respawning');
    client23.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client23"
    client23.moveTo(0, 0);
});

client24.on('lostMyBalls', function() { //when i lost all my balls
    client24.log('lost all my balls, respawning');
    client24.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client24"
    client24.moveTo(0, 0);
});

client25.on('lostMyBalls', function() { //when i lost all my balls
    client25.log('lost all my balls, respawning');
    client25.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client25"
    client25.moveTo(0, 0);
});

client21.on('lostMyBalls', function() { //when i lost all my balls
    client21.log('lost all my balls, respawning');
    client21.spawn('Hyper Balle de la mort qui tue a ne pas manger'); //spawning new ball with nickname "agario-client21"
    client21.moveTo(0, 0);
});

}

function updatePosition(posX, posY) {
posXX = posX;
posYY = posY;
client1.moveTo(posX, posY);
client2.moveTo(posX, posY);
client3.moveTo(posX, posY);
client4.moveTo(posX, posY);
client5.moveTo(posX, posY);
client11.moveTo(posX, posY);
client12.moveTo(posX, posY);
client13.moveTo(posX, posY);
client14.moveTo(posX, posY);
client15.moveTo(posX, posY);
client21.moveTo(posX, posY);
client22.moveTo(posX, posY);
client23.moveTo(posX, posY);
client24.moveTo(posX, posY);
client25.moveTo(posX, posY);
};

Néanmoins j'ai un problème : le terminal m'indique cette erreur après quelques minutes (2-3) :

[warning] moveTo() was called before connection established, packet will be dropped

@clouedoc
Copy link

Escuse me, I am french and I have a very bad english.

@pulviscriptor
Copy link
Owner

Don't know about really "best", but the best way i can think of is:
Example Bot (Ebot):

ebot.js:

var AgarioClient = require('agario-client');

function Ebot(bot_id) {
    this.bot_id   = bot_id;      //ID of bot for logging
    this.region   = 'EU-London'; //default region
    this.nickname = 'Ebot';      //default nickname
    this.verbose  = true;        //default logging enabled

    this.server     = '';   //server address will be stored here
    this.server_key = '';   //server key will be stored here
    this.client     = null; //AgarioClient will be stored here
}

Ebot.prototype = {
    log: function(text) {
        if(this.verbose) {
            console.log('Bot #' + this.bot_id + ' says: ' + text);
        }
    },

    engage: function() {
        this.client       = new AgarioClient('worker #' + this.bot_id); //create new client
        this.client.debug = 0; //not interested in debug
        this.findServer();
    },

    findServer: function() {
        var ebot = this;
        AgarioClient.servers.getFFAServer({region: this.region}, function(srv) {
            if(!srv.server) return this.log('Failed to request server (error=' + srv.error + ', error_source=' + srv.error_source + ')');
            ebot.connect('ws://' + srv.server, srv.key);
        });
    },

    connect: function(server, key) {
        this.log('Connecting to ' + server + ' with key ' + key);
        this.server = server;
        this.server_key = key;
        this.client.connect(server, key);
        this.attachEvents();
    },

    attachEvents: function() {
        var ebot = this;

        ebot.client.on('connected', function() {
            ebot.log('connected, spawning');
            ebot.client.spawn(ebot.nickname);
        });

        ebot.client.on('connectionError', function(e) {
            ebot.log('Connection failed with reason: ' + e);
            ebot.log('Server address set to: ' + ebot.server + ' key ' + ebot.server_key);
        });

        ebot.client.on('myNewBall', function(ball_id) {
            ebot.log('my new ball ' + ball_id);
        });

        ebot.client.once('leaderBoardUpdate', function(old, leaders) {
            var name_array = leaders.map(function(ball_id) {
                return ebot.client.balls[ball_id].name || 'unnamed'
            });

            ebot.log('leaders on server: ' + name_array.join(', '));
        });

        ebot.client.on('lostMyBalls', function() {
            ebot.log('Lost all my balls, example is over, disconnecting. Bye.');
            ebot.client.disconnect();
        });
    }
};

module.exports = Ebot;

spawn3bots.js:

var Ebot = require('./ebot.js');

//object of bots
var ebots = {
    'Alpha'  : null,
    'Bravo'  : null,
    'Charlie': null
};

//creating bots
for(var bot_id in ebots) {
    ebots[bot_id] = new Ebot(bot_id);
    ebots[bot_id].nickname = 'MyEbot'; //change default nickname
    ebots[bot_id].engage();
}

Should look like this:
example

To Ebot.prototype you can add targets calculate and other routine you need for your bot.

@clouedoc
Copy link

How you can control all bots without his ID ?

@pulviscriptor
Copy link
Owner

camilleeyries,
Inside bots is this.client.moveTo() and outside is ebots['Charlie'].client.moveTo()
Highly recommend controlling only from inside, using this

@clouedoc
Copy link

Hum... Ok thank you :-) I work on a suicidal bot project 👍

@henopied
Copy link
Contributor Author

@pulviscriptor I was thinking about doing it that way thank you very much :D should you add something about this to the readme?

@pulviscriptor pulviscriptor mentioned this issue Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants