Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created March 24, 2011 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timruffles/884966 to your computer and use it in GitHub Desktop.
Save timruffles/884966 to your computer and use it in GitHub Desktop.
discussion of why pub/sub vs 'event' methods on objects is better at encapsulation
realGameSub: function(el, body) {
var oldPlayer = this.players[body.playerOff.id];
// TODO refactor this - why is the application making players for RealTeam?
var newPlayer = new Player({
'id': body.player.id,
'pos': oldPlayer.position,
'no': body.player.number,
'name': body.player.name,
'st': 'playing'
});
this.players[newPlayer.id] = newPlayer;
var t = this.realgame.homeTeam;
if (this.realgame.awayTeam.id === body.playerOff.teamID) {
t = this.realgame.awayTeam;
}
for (var i = 0, len = t.players.length; i < len; i += 1) {
if (t.players[i].id === oldPlayer.id) {
t.players[i] = newPlayer;
break;
}
}
this.eventFeed.add({
'time': body.periodTime,
'action': 'action_sub'
});
this.realgame.init(this);
if (this.game.isState('inGame')) {
$('.player .sub').html('Sub');
}
if (this.game.userteam.isSubbingOut(oldPlayer)) {
// Cancel sub
// TODO this should be moved (eventually) into the realGameSub method of userteam - subCancelled happens for a reason
// and that is the event that userteam should know about.
// - good example of why publishing events gives a cleaner break between objects
// even if a similar code length: if each object just publishes events to anonymous objects,
// it can't tell them how to react. If you have event methods on the object, you may end up
// coordinating its reaction.
this.game.userteam.subCancelled();
effects.game.updateSideboard(this.game);
effects.players(this);
this.subCancelledModal.open(oldPlayer.name + ' has been subbed off, your substitution has been cancelled.');
this.game.userteam.subsLeft += 1;
common.subsLeft.removeClass('pick3players').html($('#tmpl_subs_left').tmpl({ left: this.game.userteam.subsLeft }));
}
if (this.game.userteam.hasPlayer(oldPlayer)) {
this.game.userteam.realGameSub(oldPlayer,newPlayer);
effects.game.updateSideboard(this.game);
}
effects.players(this);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment