Skip to content

Commit

Permalink
Fixed: identify remained connected to the game. Craziness
Browse files Browse the repository at this point in the history
  • Loading branch information
seanhess committed Nov 11, 2012
1 parent 540a9ef commit ca55cab
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions public/controllers/GameCtrl.ts
Expand Up @@ -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

Expand All @@ -53,7 +53,7 @@ angular.module('controllers')


// AUDIO
//SoundEffects.music()
SoundEffects.music()

$scope.test = function() {
//SoundEffects.rocket()
Expand Down Expand Up @@ -110,4 +110,8 @@ angular.module('controllers')
//});
//if (!collision) {
}

$scope.$on('$destroy', function() {
Players.disconnect(players)
});
})
6 changes: 5 additions & 1 deletion public/controllers/Identify.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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)
});

})


2 changes: 1 addition & 1 deletion public/partials/game.html
Expand Up @@ -49,6 +49,6 @@ <h1 class="winner" ng-show="players.winner">{{players.winner}} Wins!</h1>

<div class="version">v{{version}}</div>

<div class="instructions" style="position: absolute; bottom: 20px; color: white">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. </div>
<div class="instructions" style="position: absolute; bottom: 20px; color: white">Use arrow keys to move. Space to fire. If someone wins, the game restarts in 2 seconds.</div>

</div>
1 change: 1 addition & 0 deletions public/services/FB.ts
Expand Up @@ -16,6 +16,7 @@ module fire {
child(name:string);
val();
on(event:string, cb:IRefCB);
off(event:string, cb:IRefCB);
set(val:any);
removeOnDisconnect();
}
Expand Down
2 changes: 2 additions & 0 deletions public/services/Missiles.ts
Expand Up @@ -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 = []
Expand Down
42 changes: 32 additions & 10 deletions public/services/Players.ts
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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?

Expand All @@ -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')
Expand All @@ -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)
}
Expand All @@ -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();
Expand All @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit ca55cab

Please sign in to comment.