Skip to content

Commit

Permalink
Use self instead of bind/that
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewda committed Aug 10, 2016
1 parent b6d0022 commit 03146f3
Showing 1 changed file with 81 additions and 81 deletions.
162 changes: 81 additions & 81 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ var inherits = require('util').inherits;
var CONNECTION = 'http://scorebot2.hltv.org';
var PORT = 10022;

var that;
var self;

function Livescore(options) {
this.connected = false;
self = this;

this.matchid = options.matchid;
this.listid = options.listid;
this.url = options.url || CONNECTION;
this.port = options.port || PORT;
self.connected = false;

this.socket = io(this.url + ':' + this.port);
this.reconnect = false;
self.matchid = options.matchid;
self.listid = options.listid;
self.url = options.url || CONNECTION;
self.port = options.port || PORT;

this.time = 0;
this.map;
this.interval;
self.socket = io(self.url + ':' + self.port);
self.reconnect = false;

this.scoreboard;
self.time = 0;
self.map;
self.interval;

this.players = {};
this.teams = {};
self.scoreboard;

this.kills = 0;
this.knifeKills = 0;
self.players = {};
self.teams = {};

this.options = {};
self.kills = 0;
self.knifeKills = 0;

this.options[Livescore.Enums.EOption['ROUND_TIME']] = options.roundTime || 115; // 105 before update
this.options[Livescore.Enums.EOption['BOMB_TIME']] = options.bombTime || 40; // 35 before update
this.options[Livescore.Enums.EOption['FREEZE_TIME']] = options.freezeTime || 15;
self.options = {};

this.socket.on('connect', this._onConnect.bind(this));
self.options[Livescore.Enums.EOption['ROUND_TIME']] = options.roundTime || 115; // 105 before update
self.options[Livescore.Enums.EOption['BOMB_TIME']] = options.bombTime || 40; // 35 before update
self.options[Livescore.Enums.EOption['FREEZE_TIME']] = options.freezeTime || 15;

that = this;
self.socket.on('connect', self._onConnect);
}

inherits(Livescore, EE);
Expand All @@ -47,53 +47,53 @@ Livescore.Enums = require('../Livescore.js').Enums;
Livescore.Classes = require('../Livescore.js').Classes;

Livescore.prototype.disconnect = function() {
this.connected = false;
this.socket.disconnect();
self.connected = false;
self.socket.disconnect();
};

Livescore.prototype.getPlayers = function(callback) {
callback(this.players);
callback(self.players);
};

Livescore.prototype.getTeams = function(callback) {
callback(this.teams);
callback(self.teams);
};

Livescore.prototype.setTime = function(time) {
clearInterval(this.interval);
clearInterval(self.interval);

this.time = time;
this.interval = setInterval(function() {
this.time = this.time - 1;
this.emit('time', this.time);
}.bind(this), 1000);
self.time = time;
self.interval = setInterval(function() {
self.time = self.time - 1;
self.emit('time', self.time);
}, 1000);
};

Livescore.prototype.getTime = function(callback) {
callback(this.time);
callback(self.time);
};

Livescore.prototype._onConnect = function() {
if (!this.reconnect) {
this.socket.on('log', this._onLog.bind(this));
this.socket.on('scoreboard', this._onScoreboard.bind(this));
if (!self.reconnect) {
self.socket.on('log', self._onLog);
self.socket.on('scoreboard', self._onScoreboard);
}

this.socket.emit('readyForMatch', this.listid);
self.socket.emit('readyForMatch', self.listid);
};

Livescore.prototype._onReconnect = function() {
this.reconnect = true;
this.socket.emit('readyForMatch', this.listid);
self.reconnect = true;
self.socket.emit('readyForMatch', self.listid);
};

Livescore.prototype._onLog = function(logs) {
this.getPlayers(function(players) {
self.getPlayers(function(players) {
if (Object.keys(players).length) {
logs = JSON.parse(logs).log.reverse();
logs.forEach(function(log) {
for (event in log) {
that.emit('debug', 'received event: ' + event);
self.emit('debug', 'received event: ' + event);

switch (event) {
case 'Kill':
Expand All @@ -108,22 +108,22 @@ Livescore.prototype._onLog = function(logs) {
case 'MatchStarted':
case 'Restart':
case 'Suicide':
that['_on' + event](log[event]);
self['_on' + event](log[event]);
break;
default:
that.emit('debug', 'unrecognized event: ' + event);
self.emit('debug', 'unrecognized event: ' + event);
break;
}
}
}.bind(this));
});
}
});
};

Livescore.prototype._onScoreboard = function(event) {
if (!this.connected) {
this.connected = true;
this.emit('connected');
if (!self.connected) {
self.connected = true;
self.emit('connected');
}

updateGame(event);
Expand All @@ -135,67 +135,67 @@ Livescore.prototype._onScoreboard = function(event) {
currentRound: event.currentRound
};

this.getTeams(function(teams) {
self.getTeams(function(teams) {
scoreboard.teams[Livescore.Enums.ESide['TERRORIST']] = teams[Livescore.Enums.ESide['TERRORIST']];
scoreboard.teams[Livescore.Enums.ESide['COUNTERTERRORIST']] = teams[Livescore.Enums.ESide['COUNTERTERRORIST']];

that.emit('scoreboard', scoreboard);
self.emit('scoreboard', scoreboard);
});
};

Livescore.prototype._onKill = function(event) {
this.getPlayers(function(players) {
that.emit('kill', {
self.getPlayers(function(players) {
self.emit('kill', {
killer: players[event.killerName],
victim: players[event.victimName],
weapon: event.weapon,
headshot: event.headShot
});
});

this.kills++;
self.kills++;
if (event.weapon.indexOf('knife') > -1) {
this.knifeKills++;
self.knifeKills++;
}
};

Livescore.prototype._onSuicide = function(event) {
this.emit('suicide', {
self.emit('suicide', {
playerName: event.playerName,
playerSide: event.side
});
};

Livescore.prototype._onBombPlanted = function(event) {
this.setTime(this.options[Livescore.Enums.EOption['BOMB_TIME']]);
self.setTime(self.options[Livescore.Enums.EOption['BOMB_TIME']]);

this.getPlayers(function(players) {
that.emit('bombPlanted', {
self.getPlayers(function(players) {
self.emit('bombPlanted', {
player: players[event.playerName]
});
});
};

Livescore.prototype._onBombDefused = function(event) {
this.getPlayers(function(players) {
that.emit('bombDefused', {
self.getPlayers(function(players) {
self.emit('bombDefused', {
player: players[event.playerName]
});
});
};

Livescore.prototype._onMatchStarted = function(event) {
this.emit('matchStart', event);
self.emit('matchStart', event);
};

Livescore.prototype._onRoundStart = function() {
this.setTime(this.options[Livescore.Enums.EOption["ROUND_TIME"]]);
this.emit('roundStart', {
round: this.scoreboard.currentRound
self.setTime(self.options[Livescore.Enums.EOption["ROUND_TIME"]]);
self.emit('roundStart', {
round: self.scoreboard.currentRound
});

this.kills = 0;
this.knifeKills = 0;
self.kills = 0;
self.knifeKills = 0;
};

Livescore.prototype._onRoundEnd = function(event) {
Expand All @@ -208,9 +208,9 @@ Livescore.prototype._onRoundEnd = function(event) {
winner = Livescore.Enums.ESide['COUNTERTERRORIST'];
}

this.setTime(this.options[Livescore.Enums.EOption["FREEZE_TIME"]]);
self.setTime(self.options[Livescore.Enums.EOption["FREEZE_TIME"]]);

this.getTeams(function(teams) {
self.getTeams(function(teams) {
var t = teams[Livescore.Enums.ESide['TERRORIST']];
var ct = teams[Livescore.Enums.ESide['COUNTERTERRORIST']];

Expand All @@ -223,45 +223,45 @@ Livescore.prototype._onRoundEnd = function(event) {

// If at least 80% of the kills are knife kills, count it as a knife
// round. Sometimes players will have pistols on knife rounds and
// kill teammates after the round is over, so this takes that into
// kill teammates after the round is over, so self takes that into
// account.
that.emit('roundEnd', {
self.emit('roundEnd', {
teams: teams,
winner: teams[winner],
roundType: Livescore.Enums.ERoundType[event.winType],
knifeRound: (this.knifeKills/this.kills) >= 0.8
knifeRound: (self.knifeKills/self.kills) >= 0.8
});
}
});
};

Livescore.prototype._onPlayerJoin = function(event) {
this.emit('playerJoin', {
self.emit('playerJoin', {
playerName: event.playerName
});
};

Livescore.prototype._onPlayerQuit = function(event) {
this.getPlayers(function(players) {
that.emit('playerQuit', {
self.getPlayers(function(players) {
self.emit('playerQuit', {
player: players[event.playerName]
});
});
};

Livescore.prototype._onRestart = function() {
this.emit('restart');
self.emit('restart');
};

Livescore.prototype._onMapChange = function(event) {
this.emit('mapChange', event);
self.emit('mapChange', event);
};

function updateGame(scoreboard) {
var tPlayers = [];
var ctPlayers = [];

that.teams[Livescore.Enums.ESide['TERRORIST']] = new Livescore.Classes.Team({
self.teams[Livescore.Enums.ESide['TERRORIST']] = new Livescore.Classes.Team({
name: scoreboard.terroristTeamName,
id: scoreboard.tTeamId,
score: scoreboard.terroristScore,
Expand All @@ -270,7 +270,7 @@ function updateGame(scoreboard) {
history: scoreboard.terroristMatchHistory
});

that.teams[Livescore.Enums.ESide['COUNTERTERRORIST']] = new Livescore.Classes.Team({
self.teams[Livescore.Enums.ESide['COUNTERTERRORIST']] = new Livescore.Classes.Team({
name: scoreboard.ctTeamName,
id: scoreboard.ctTeamId,
score: scoreboard.counterTerroristScore,
Expand All @@ -279,25 +279,25 @@ function updateGame(scoreboard) {
history: scoreboard.ctMatchHistory
});

that.getTeams(function(teams) {
self.getTeams(function(teams) {
scoreboard.TERRORIST.forEach(function(player) {
var player = new Livescore.Classes.Player(player);
player.team = teams[Livescore.Enums.ESide['TERRORIST']];

that.players[player.name] = player;
self.players[player.name] = player;
tPlayers.push(player);
});

scoreboard.CT.forEach(function(player) {
var player = new Livescore.Classes.Player(player);
player.team = teams[Livescore.Enums.ESide['COUNTERTERRORIST']];

that.players[player.name] = player;
self.players[player.name] = player;
ctPlayers.push(player);
});
});

that.scoreboard = scoreboard;
self.scoreboard = scoreboard;
}

module.exports = Livescore;

0 comments on commit 03146f3

Please sign in to comment.