diff --git a/README.md b/README.md index 227f6bf..26bcc38 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ Hit List * Explosion (NEED: explosion and rocket in the same format) * Rocket animation * (bug) you can kill yourself? +* (bug) current player not set when you check winner? BIG ONE: the old Players object is kicking around, and IT gets all set up and good to go, but the new one doesn't +* (bug) disconnect from missiles too (in game) * (clean) use signals/events instead of $rootScope.broadcast * (clean) going back to the main page should clean up the game. diff --git a/public/controllers/GameCtrl.ts b/public/controllers/GameCtrl.ts index d159012..2bb8630 100644 --- a/public/controllers/GameCtrl.ts +++ b/public/controllers/GameCtrl.ts @@ -33,7 +33,7 @@ angular.module('controllers') //return //} - var players = Players.connect($scope.gameId) + var players = Players.connect($scope.gameId, "Game") Players.join(players, CurrentPlayer.player) $scope.players = players @@ -53,7 +53,7 @@ angular.module('controllers') // AUDIO - //SoundEffects.music() + SoundEffects.music() $scope.test = function() { //SoundEffects.rocket() @@ -110,4 +110,8 @@ angular.module('controllers') //}); //if (!collision) { } + + $scope.$on('$destroy', function() { + Players.disconnect(players) + }); }) diff --git a/public/controllers/Identify.ts b/public/controllers/Identify.ts index e59c300..7694fef 100644 --- a/public/controllers/Identify.ts +++ b/public/controllers/Identify.ts @@ -43,7 +43,7 @@ angular.module('controllers').controller('IdentifyCtrl', function($scope: Identi $scope.gameId = $scope.player.gameId || "global" // [ ] detect which game to join ("global") - var players = Players.connect($scope.gameId) + var players = Players.connect($scope.gameId, "Identify") $scope.players = players // available avatars @@ -96,6 +96,10 @@ angular.module('controllers').controller('IdentifyCtrl', function($scope: Identi return ($scope.player && $scope.player.avatar == name) } + $scope.$on('$destroy', function() { + Players.disconnect(players) + }); + }) diff --git a/public/partials/game.html b/public/partials/game.html index cf9d7b9..7d8b7aa 100644 --- a/public/partials/game.html +++ b/public/partials/game.html @@ -49,6 +49,6 @@

{{players.winner}} Wins!

v{{version}}
-
Use arrow keys to move. Space to fire. If you can't move, try clicking the blue board. If someone wins, the game restarts in 3 seconds. If it totally screws up or you are invisible for more than 5 seconds, refresh the page and re-join.
+
Use arrow keys to move. Space to fire. If someone wins, the game restarts in 2 seconds.
diff --git a/public/services/FB.ts b/public/services/FB.ts index cbc6b77..97b0593 100644 --- a/public/services/FB.ts +++ b/public/services/FB.ts @@ -16,6 +16,7 @@ module fire { child(name:string); val(); on(event:string, cb:IRefCB); + off(event:string, cb:IRefCB); set(val:any); removeOnDisconnect(); } diff --git a/public/services/Missiles.ts b/public/services/Missiles.ts index f41fd91..cebd151 100644 --- a/public/services/Missiles.ts +++ b/public/services/Missiles.ts @@ -32,6 +32,8 @@ angular.module('services') } function connect(gameId:string, players:IPlayerState):IMissileState { + + console.log("M.connect", players.id) var missilesRef = FB.game(gameId).child('missiles') var all = [] diff --git a/public/services/Players.ts b/public/services/Players.ts index 4e32545..cd8de29 100644 --- a/public/services/Players.ts +++ b/public/services/Players.ts @@ -31,11 +31,17 @@ interface IPlayerState { winner: string; isPaid: bool; all: IPlayer []; + id?: string; // private stuff. not for binding myname:string; gameRef:fire.IRef; playersRef:fire.IRef; + + boundOnJoin?:fire.IRefCB; + boundOnUpdate?:fire.IRefCB; + boundOnQuit?:fire.IRefCB; + boundOnWinner?:fire.IRefCB; } // only methods @@ -46,7 +52,8 @@ interface IPlayerService { playerByName(players:IPlayer[], name:string):IPlayer; latestVersion(players:IPlayer[]):string; - connect(gameId:string):IPlayerState; + connect(gameId:string, id:string):IPlayerState; + disconnect(state:IPlayerState); join(state:IPlayerState, player:IPlayer); killPlayer(state:IPlayerState, player:IPlayer, killerName:string); move(state:IPlayerState, player:IPlayer); @@ -57,7 +64,7 @@ interface IPlayerService { angular.module('services') -.factory('Players', function($rootScope:ng.IScope, FB:IFirebaseService, Board:IBoard, AppVersion:any):IPlayerService { +.factory('Players', function($rootScope:ng.IScope, FB:IFirebaseService, Board:IBoard, AppVersion:string):IPlayerService { // the big cheese. Does the deed // you can make fancy bindings here, no? @@ -72,9 +79,10 @@ angular.module('services') killPlayer: killPlayer, move: move, resetGame: resetGame, + disconnect: disconnect, } - function connect(gameId:string):IPlayerState { + function connect(gameId:string, id:string):IPlayerState { var gameRef = FB.game(gameId) var playersRef = gameRef.child('players') @@ -87,19 +95,33 @@ angular.module('services') current: null, winner: null, isPaid: isPaid(), - all: [] + all: [], + id: id, } + state.boundOnJoin = FB.apply((p) => onJoin(state,p)) + state.boundOnUpdate = FB.apply((p) => onUpdate(state,p)) + state.boundOnQuit = FB.apply((p) => onQuit(state,p)) + state.boundOnWinner = FB.apply((n) => onWinner(state,n)) + // better way to bind? nope! that's what they are for! - playersRef.on('child_added', FB.apply((p) => onJoin(state,p))) - playersRef.on('child_changed', FB.apply((p) => onUpdate(state,p))) - playersRef.on('child_removed', FB.apply((p) => onQuit(state,p))) + playersRef.on('child_added', state.boundOnJoin) + playersRef.on('child_changed', state.boundOnUpdate) + playersRef.on('child_removed', state.boundOnQuit) - gameRef.child('winner').on('value', FB.apply((n) => onWinner(state,n))) + gameRef.child('winner').on('value', state.boundOnWinner) return state } + function disconnect(state:IPlayerState) { + state.playersRef.off('child_added', state.boundOnJoin) + state.playersRef.off('child_changed', state.boundOnUpdate) + state.playersRef.off('child_removed', state.boundOnQuit) + + state.gameRef.child('winner').off('value', state.boundOnWinner) + } + function isAlive(p:IPlayer):bool { return (p.state == STATE.ALIVE) } @@ -120,7 +142,7 @@ angular.module('services') player.wins = player.wins || 0 player.losses = player.losses || 0 player.taunt = null - player.version = AppVersion.num + player.version = AppVersion var ref = state.playersRef.child(player.name) ref.removeOnDisconnect(); @@ -129,6 +151,7 @@ angular.module('services') // what can change on a person? function onJoin(state:IPlayerState, player:IPlayer) { + // state.current needs to refer to the SAME player you add to the array if (!state.current && player.name == state.myname) { state.current = player } @@ -155,7 +178,6 @@ angular.module('services') if (remotePlayer.killer) player.killer = remotePlayer.killer if (player.state == STATE.DEAD) { - console.log("DEATH", player.name) $rootScope.$broadcast("kill", player) // EVERYONE needs to check the win, because otherwise the game doesn't end! checkWin(state)