Permalink
Browse files

sweet service class style going ON

  • Loading branch information...
1 parent 83015af commit 94c27dd613d2c3bf17bf4d1754a2541233d10d55 @seanhess committed Nov 9, 2012
Showing with 360 additions and 352 deletions.
  1. +2 −3 public/controllers/Identify.js
  2. +4 −7 public/controllers/Identify.ts
  3. +3 −0 public/services/FB.ts
  4. +157 −145 public/services/Players.js
  5. +194 −197 public/services/Players.ts
@@ -11,7 +11,7 @@ define(["require", "exports"], function(require, exports) {
$scope.version = AppVersion;
$scope.player = CurrentPlayer.loadPreferences();
$scope.gameId = $scope.player.gameId || "global";
- var players = Players($scope.gameId);
+ var players = Players.connect($scope.gameId);
$scope.players = players;
$scope.avatars = [
'player2',
@@ -39,7 +39,7 @@ define(["require", "exports"], function(require, exports) {
$scope.error = "Please select a valid name and an avatar";
return;
}
- if(players.playerByName($scope.player.name)) {
+ if(Players.playerByName(players.all, $scope.player.name)) {
$scope.error = '"' + $scope.player.name + '" is already taken';
return;
}
@@ -59,7 +59,6 @@ define(["require", "exports"], function(require, exports) {
$scope.isPlayerAvatar = function (name) {
return ($scope.player && $scope.player.avatar == name);
};
- players.listen();
}
return Controller;
})();
@@ -11,7 +11,7 @@ export interface Scope extends ng.IScope {
version:any;
player:any;
gameId:string;
- players:ps.IPlayers;
+ players:ps.IPlayerState;
avatars:string [];
freeAvatars:string [];
@@ -25,7 +25,7 @@ export interface Scope extends ng.IScope {
}
export class Controller {
- constructor ($scope: Scope, $location: any, Players: ps.IPlayerFactory, CurrentPlayer: any, AppVersion: any) {
+ constructor ($scope: Scope, $location: any, Players:ps.PlayerService, CurrentPlayer: any, AppVersion: any) {
// HACKY way to do the transition
$scope.intro = "intro"
@@ -43,7 +43,7 @@ export class Controller {
$scope.gameId = $scope.player.gameId || "global"
// [ ] detect which game to join ("global")
- var players = Players($scope.gameId)
+ var players = Players.connect($scope.gameId)
$scope.players = players
// available avatars
@@ -72,7 +72,7 @@ export class Controller {
return
}
- if (players.playerByName($scope.player.name)) {
+ if (Players.playerByName(players.all, $scope.player.name)) {
$scope.error = '"' + $scope.player.name + '" is already taken'
return
}
@@ -94,9 +94,6 @@ export class Controller {
$scope.isPlayerAvatar = function(name) {
return ($scope.player && $scope.player.avatar == name)
}
-
- players.listen()
-
}
}
View
@@ -26,6 +26,9 @@ export interface IFB {
// Functional Style Class: it's the same as the module that you were looking for!
+// wait, this sucks. To call other functions in the module, you have to use this
+// oh, it's not that bad
+
export class FB {
constructor(
View
@@ -3,6 +3,163 @@ define(["require", "exports", "../app"], function(require, exports, __app__) {
+ var PlayerService = (function () {
+ function PlayerService($rootScope, FB, Board, AppVersion) {
+ this.$rootScope = $rootScope;
+ this.FB = FB;
+ this.Board = Board;
+ this.AppVersion = AppVersion;
+ }
+ PlayerService.prototype.connect = function (gameId) {
+ var _this = this;
+ var gameRef = this.FB.game(gameId);
+ var playersRef = gameRef.child('players');
+ var state = {
+ myname: null,
+ gameRef: gameRef,
+ playersRef: playersRef,
+ current: null,
+ winner: null,
+ taunt: null,
+ isPaid: this.isPaid(),
+ all: []
+ };
+ playersRef.on('child_added', this.FB.apply(function (p) {
+ return _this.onJoin(state, p);
+ }));
+ playersRef.on('child_changed', this.FB.apply(function (p) {
+ return _this.onUpdate(state, p);
+ }));
+ playersRef.on('child_removed', this.FB.apply(function (p) {
+ return _this.onQuit(state, p);
+ }));
+ return state;
+ };
+ PlayerService.prototype.isAlive = function (p) {
+ return (p.state == STATE.ALIVE);
+ };
+ PlayerService.prototype.alivePlayers = function (players) {
+ return players.filter(this.isAlive);
+ };
+ PlayerService.prototype.join = function (state, player) {
+ state.myname = player.name;
+ player.x = this.Board.randomX();
+ player.y = this.Board.randomY();
+ player.sprite = '1';
+ player.facing = "down";
+ player.state = STATE.ALIVE;
+ player.wins = player.wins || 0;
+ player.losses = player.losses || 0;
+ player.message = null;
+ player.version = this.AppVersion.num;
+ var ref = state.playersRef.child(player.name);
+ ref.removeOnDisconnect();
+ this.FB.update(ref, player);
+ };
+ PlayerService.prototype.onJoin = function (state, player) {
+ if(!state.current && player.name == state.myname) {
+ state.current = player;
+ }
+ state.all.push(player);
+ };
+ PlayerService.prototype.onUpdate = function (state, remotePlayer) {
+ var player = this.playerByName(state.all, remotePlayer.name);
+ if(!player) {
+ return console.log("Error, player not found: " + remotePlayer.name);
+ }
+ player.x = remotePlayer.x;
+ player.y = remotePlayer.y;
+ player.facing = remotePlayer.facing;
+ player.state = remotePlayer.state;
+ player.wins = remotePlayer.wins;
+ player.losses = remotePlayer.losses;
+ if(remotePlayer.killer) {
+ player.killer = remotePlayer.killer;
+ }
+ if(player.state == STATE.DEAD) {
+ this.$rootScope.$broadcast("kill", player);
+ this.checkWin(state);
+ }
+ };
+ PlayerService.prototype.onQuit = function (state, player) {
+ state.all = state.all.filter(function (p) {
+ return p.name != player.name;
+ });
+ };
+ PlayerService.prototype.checkWin = function (state) {
+ var alive = this.alivePlayers(state.all);
+ if(alive.length > 1) {
+ return;
+ }
+ var winner = alive[0];
+ if(state.current == null || winner != state.current) {
+ return;
+ }
+ winner.wins += 1;
+ state.playersRef.child(winner.name).child("wins").set(winner.wins);
+ state.gameRef.child("winner").removeOnDisconnect();
+ this.FB.update(state.gameRef.child("winner"), winner);
+ };
+ PlayerService.prototype.onWinner = function (state, player) {
+ var _this = this;
+ if(!player) {
+ state.winner = null;
+ state.taunt = null;
+ return;
+ }
+ if(state.winner && state.winner.name == player.name) {
+ return;
+ }
+ state.winner = player;
+ state.taunt = TAUNT_LIST[Math.floor(Math.random() * TAUNT_LIST.length)];
+ if(state.current && state.current.name == player.name) {
+ setTimeout(function () {
+ return _this.resetGame(state);
+ }, 3000);
+ }
+ };
+ PlayerService.prototype.resetGame = function (state) {
+ var _this = this;
+ console.log("Initialize Game");
+ state.gameRef.child('winner').remove();
+ state.all.forEach(function (player) {
+ player.x = _this.Board.randomX();
+ player.y = _this.Board.randomY();
+ player.sprite = '1';
+ player.facing = "down";
+ player.state = STATE.ALIVE;
+ _this.FB.update(state.playersRef.child(player.name), player);
+ });
+ };
+ PlayerService.prototype.killPlayer = function (state, player, killerName) {
+ player.state = STATE.DEAD;
+ player.losses += 1;
+ player.killer = killerName;
+ this.FB.update(state.playersRef.child(player.name), player);
+ };
+ PlayerService.prototype.move = function (state, player) {
+ var playerRef = state.playersRef.child(player.name);
+ this.FB.update(playerRef, player);
+ };
+ PlayerService.prototype.playerByName = function (players, name) {
+ return players.filter(function (p) {
+ return (p.name == name);
+ })[0];
+ };
+ PlayerService.prototype.latestVersion = function (players) {
+ return _.max(players, function (player) {
+ return player.version;
+ });
+ };
+ PlayerService.prototype.isPaid = function () {
+ return (localStorage.getItem("payment_status") == "paid");
+ };
+ return PlayerService;
+ })();
+ exports.PlayerService = PlayerService;
+ app.main.factory('Players', function ($rootScope, FB, Board, AppVersion) {
+ return new PlayerService($rootScope, FB, Board, AppVersion);
+ });
var TAUNT_LIST = [
"Oooh yeah!",
"I fart in your general direction.",
@@ -16,150 +173,5 @@ define(["require", "exports", "../app"], function(require, exports, __app__) {
DEAD: "dead",
ALIVE: "alive"
};
- app.main.factory('Players', function ($rootScope, FB, Board, AppVersion) {
- return function (gameId) {
- var gameRef = FB.game(gameId);
- var playersRef = gameRef.child('players');
- var myname;
- var players = {
- current: null,
- winner: null,
- taunt: null,
- isPaid: isPaid(),
- all: [],
- alivePlayers: alivePlayers,
- join: join,
- listen: listen,
- move: move,
- killPlayer: killPlayer,
- playerByName: playerByName,
- latestVersion: latestVersion
- };
- function join(player) {
- myname = player.name;
- player.x = Board.randomX();
- player.y = Board.randomY();
- player.sprite = '1';
- player.facing = "down";
- player.state = STATE.ALIVE;
- player.wins = player.wins || 0;
- player.losses = player.losses || 0;
- player.message = null;
- player.version = AppVersion.num;
- var ref = playersRef.child(player.name);
- ref.removeOnDisconnect();
- FB.update(ref, player);
- }
- function listen() {
- playersRef.on('child_added', FB.apply(onJoin));
- playersRef.on('child_changed', FB.apply(onUpdate));
- playersRef.on('child_removed', FB.apply(onQuit));
- }
- function onJoin(player) {
- if(!players.current && player.name == myname) {
- players.current = player;
- }
- players.all.push(player);
- }
- function onUpdate(remotePlayer) {
- var player = playerByName(remotePlayer.name);
- if(!player) {
- return console.log("Error, player not found: " + remotePlayer.name);
- }
- player.x = remotePlayer.x;
- player.y = remotePlayer.y;
- player.facing = remotePlayer.facing;
- player.state = remotePlayer.state;
- player.wins = remotePlayer.wins;
- player.losses = remotePlayer.losses;
- if(remotePlayer.killer) {
- player.killer = remotePlayer.killer;
- }
- if(player.state == STATE.DEAD) {
- $rootScope.$broadcast("kill", player);
- checkWin();
- }
- }
- function onQuit(player) {
- players.all = players.all.filter(function (p) {
- return p.name != player.name;
- });
- }
- function checkWin() {
- var alive = alivePlayers();
- if(alive.length > 1) {
- return;
- }
- var winner = alive[0];
- if(players.current == null || winner != players.current) {
- return;
- }
- winner.wins += 1;
- playersRef.child(winner.name).child("wins").set(winner.wins);
- gameRef.child("winner").removeOnDisconnect();
- FB.update(gameRef.child("winner"), winner);
- }
- function onWinner(player) {
- if(!player) {
- players.winner = null;
- players.taunt = null;
- return;
- }
- if(players.winner && players.winner.name == player.name) {
- return;
- }
- players.winner = player;
- players.taunt = TAUNT_LIST[Math.floor(Math.random() * TAUNT_LIST.length)];
- if(players.current && players.current.name == player.name) {
- setTimeout(resetGame, 3000);
- }
- }
- function resetGame() {
- console.log("Initialize Game");
- gameRef.child('winner').remove();
- players.all.forEach(function (player) {
- player.x = Board.randomX();
- player.y = Board.randomY();
- player.sprite = '1';
- player.facing = "down";
- player.state = STATE.ALIVE;
- FB.update(playersRef.child(player.name), player);
- });
- }
- function killPlayer(player, killerName) {
- player.state = STATE.DEAD;
- player.losses += 1;
- player.killer = killerName;
- FB.update(playersRef.child(player.name), player);
- }
- function move(player) {
- var playerRef = getPlayerRef(player.name);
- FB.update(playerRef, player);
- }
- function getPlayerRef(name) {
- return playersRef.child(name);
- }
- function playerByName(name) {
- return players.all.filter(function (p) {
- return (p.name == name);
- })[0];
- }
- function isAlive(p) {
- return (p.state == STATE.ALIVE);
- }
- function alivePlayers() {
- return players.all.filter(isAlive);
- }
- function isPaid() {
- return (localStorage.getItem("payment_status") == "paid");
- }
- function latestVersion() {
- return _.max(players.all, function (player) {
- return player.version;
- });
- }
- return players;
- }
- });
})
Oops, something went wrong.

0 comments on commit 94c27dd

Please sign in to comment.