Skip to content

Commit

Permalink
Merge pull request #62 from heroandtn3/NEXT-5
Browse files Browse the repository at this point in the history
NEXT-5 Integrate agario-client to NEXT
  • Loading branch information
kscc25 committed Mar 19, 2016
2 parents 78d45f7 + 146e06f commit d1fec14
Show file tree
Hide file tree
Showing 11 changed files with 1,291 additions and 295 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"expr": true,
"esnext": true,
"globals": {
"console": false
}
}
108 changes: 108 additions & 0 deletions client/js/agario-client/Account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'use strict';

import https from 'https';
import EventEmitter from 'events';

const agarClientId = '677505792353827'; //hardcoded in client

export default class Account {

constructor(name) { //todo doc vars
this.name = name; //debug name
this.token = null; //token after requestFBToken()
this.token_expire = 0; //timestamp after requestFBToken()
this.token_provider = 1; //provider ID after requester
this.c_user = null; //cookie from www.facebook.com
this.datr = null; //cookie from www.facebook.com
this.xs = null; //cookie from www.facebook.com
this.agent = null; //connection agent
this.debug = 1;
this.server = 'wss://web-live-v3-0.agario.miniclippt.com/ws'; //todo doc

this.ws = null;
}

log(text) {
if (this.name) {
console.log('Account(' + this.name + '): ' + text);
} else {
console.log('Account: ' + text);
}
}

requestFBToken(cb) {
var account = this;

if (this.debug >= 1) {
if (!this.c_user) this.log('[warning] You did not specified Account.c_user');
if (!this.datr) this.log('[warning] You did not specified Account.datr');
if (!this.xs) this.log('[warning] You did not specified Account.xs');
}

var ret = {
error: null,
res: null,
data: null,
};

var cUser = this.c_user;
var datr = this.datr;
var xs = this.xs;

//Some users don't decode their cookies, so let's try do it here
if (cUser && cUser.indexOf('%')) cUser = decodeURIComponent(cUser);
if (datr && datr.indexOf('%')) datr = decodeURIComponent(datr);
if (xs && xs.indexOf('%')) xs = decodeURIComponent(xs);

var cookies = 'c_user=' + encodeURIComponent(cUser) + ';' +
'datr=' + encodeURIComponent(datr) + ';' +
'xs=' + encodeURIComponent(xs) + ';';

var options = {
host: 'www.facebook.com',
path: '/dialog/oauth?client_id=' + agarClientId + '&redirect_uri=https://agar.io&scope=public_profile,%20email&response_type=token',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0',
Cookie: cookies,
},
agent: this.agent || null,
};

var req = https.request(options, function(res) {
var data = '';
ret.res = res;

res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
ret.data = data;

if (res && res.headers && res.headers.location) {
res.headers.location.replace(/access_token=([a-zA-Z0-9-_]*)&/, function(_, parsed_token) {
if (parsed_token) {
account.token = parsed_token;
account.token_provider = 1;
}
});
res.headers.location.replace(/expires_in=([0-9]*)/, function(_, expire) {
if (expire) {
account.token_expire = Date.now() + expire * 1000;
}
});
}

if (cb) cb(account.token, ret);
});
});

req.on('error', function(e) {
ret.error = e;
if (cb) cb(null, ret);
});

req.end();
}
}
115 changes: 115 additions & 0 deletions client/js/agario-client/Ball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
'use strict';

export default class Ball {

constructor(client, id) {
if (client.balls[id]) return client.balls[id];

this.id = id;
this.name = null;
this.x = 0;
this.y = 0;
this.size = 0;
this.mass = 0;
this.virus = false;
this.mine = false;

this.client = client;
this.destroyed = false;
this.visible = false;
this.last_update = Date.now();
this.update_tick = 0;

client.balls[id] = this;
return this;
}

destroy(reason) {
this.destroyed = reason;
delete this.client.balls[this.id];
var mine_ball_index = this.client.my_balls.indexOf(this.id);
if (mine_ball_index > -1) {
this.client.my_balls.splice(mine_ball_index, 1);
this.client.emitEvent('mineBallDestroy', this.id, reason);
if (!this.client.my_balls.length) this.client.emitEvent('lostMyBalls');
}

this.emitEvent('destroy', reason);
this.client.emitEvent('ballDestroy', this.id, reason);
}

setCords(new_x, new_y) {
if (this.x == new_x && this.y == new_y) return;
var old_x = this.x;
var old_y = this.y;
this.x = new_x;
this.y = new_y;

if (!old_x && !old_y) return;
this.emitEvent('move', old_x, old_y, new_x, new_y);
this.client.emitEvent('ballMove', this.id, old_x, old_y, new_x, new_y);
}

setSize(new_size) {
if (this.size == new_size) return;
var old_size = this.size;
this.size = new_size;
this.mass = parseInt(Math.pow(new_size / 10, 2));

if (!old_size) return;
this.emitEvent('resize', old_size, new_size);
this.client.emitEvent('ballResize', this.id, old_size, new_size);
if (this.mine) this.client.updateScore();
}

setName(name) {
if (this.name == name) return;
var old_name = this.name;
this.name = name;

this.emitEvent('rename', old_name, name);
this.client.emitEvent('ballRename', this.id, old_name, name);
}

update() {
var old_time = this.last_update;
this.last_update = Date.now();

this.emitEvent('update', old_time, this.last_update);
this.client.emitEvent('ballUpdate', this.id, old_time, this.last_update);
}

appear() {
if (this.visible) return;
this.visible = true;
this.emitEvent('appear');
this.client.emitEvent('ballAppear', this.id);

if (this.mine) this.client.updateScore();
}

disappear() {
if (!this.visible) return;
this.visible = false;
this.emitEvent('disappear');
this.client.emitEvent('ballDisppear', this.id);
}

toString() {
if (this.name) return this.id + '(' + this.name + ')';
return this.id.toString();
}

// Fix https://github.com/pulviscriptor/agario-client/issues/95
emitEvent() {
var args = [];
for (var i = 0; i < arguments.length; i++) args.push(arguments[i]);
try {
this.emit.apply(this, args);
} catch (e) {
process.nextTick(function() {
throw e;
});
}
}
}
Loading

0 comments on commit d1fec14

Please sign in to comment.