Permalink
Browse files

really happy with class-based service

  • Loading branch information...
1 parent f8d0e90 commit 83015af13b8e97c663711e6e1fdf8239dd24ed8f @seanhess committed Nov 9, 2012
Showing with 132 additions and 74 deletions.
  1. +2 −0 public/app.ts
  2. +38 −36 public/services/FB.js
  3. +62 −0 public/services/FB.ts
  4. +1 −1 public/services/Missiles.js
  5. +5 −4 public/services/Players.js
  6. +24 −33 public/services/Players.ts
View
@@ -1,4 +1,6 @@
+///<reference path="def/jquery.d.ts"/>
///<reference path="def/angular.d.ts"/>
+///<reference path="def/underscore.d.ts"/>
import Identify = module('controllers/Identify')
View
@@ -1,36 +1,38 @@
-define(function(require) {
- var app = require('app')
-
- app.main.factory('FB', function($rootScope) {
- var FB = function(gameId) {
- var ref = new Firebase("https://seanhess.firebaseio.com/uarbg2/" + gameId)
- return ref
- }
-
- // helps you bind to firebase events
- FB.apply = function(f) {
- return function(ref) {
- if ($rootScope.$$phase)
- return f(ref.val())
- $rootScope.$apply(function() {
- f(ref.val())
- })
- }
- }
-
- // just updates everything, ignore angular $$hashKey
- FB.update = function(ref, obj) {
- //console.log("UPDATE", obj)
-
- for (var key in obj) {
- if (obj[key] === undefined)
- delete obj[key]
- }
-
- ref.set(_.omit(obj, "$$hashKey"))
- }
-
- return FB
-})
-
-})
+define(["require", "exports", '../app'], function(require, exports, __app__) {
+ var app = __app__;
+
+ var FB = (function () {
+ function FB($rootScope) {
+ this.$rootScope = $rootScope;
+ }
+ FB.prototype.game = function (gameId) {
+ var ref = new Firebase("https://seanhess.firebaseio.com/uarbg2/" + gameId);
+ return ref;
+ };
+ FB.prototype.apply = function (f) {
+ var _this = this;
+ return function (ref) {
+ if((_this.$rootScope).$$phase) {
+ return f(ref.val());
+ }
+ _this.$rootScope.$apply(function () {
+ f(ref.val());
+ });
+ }
+ };
+ FB.prototype.update = function (ref, obj) {
+ for(var key in obj) {
+ if(obj[key] === undefined) {
+ delete obj[key];
+ }
+ }
+ ref.set(_.omit(obj, "$$hashKey"));
+ };
+ return FB;
+ })();
+ exports.FB = FB;
+ app.main.factory('FB', function ($rootScope) {
+ return new FB($rootScope);
+ });
+})
+
View
@@ -0,0 +1,62 @@
+import app = module('../app')
+
+declare var Firebase;
+
+export interface IRef {
+ child(name:string);
+ val();
+ on(event:string, cb:IRefCB);
+ set(val:any);
+ removeOnDisconnect();
+}
+
+export interface IRefCB {
+ (ref:IRef);
+}
+
+export interface IValueCB {
+ (val:any);
+}
+
+export interface IFB {
+ (gameId:string):IRef;
+ apply(cb:IValueCB):IRefCB;
+ update(ref:IRef, obj:any);
+}
+
+
+// Functional Style Class: it's the same as the module that you were looking for!
+export class FB {
+
+ constructor(
+ private $rootScope:ng.IRootScopeService
+ ) { }
+
+ game(gameId:string):IRef {
+ var ref = new Firebase("https://seanhess.firebaseio.com/uarbg2/" + gameId)
+ return ref
+ }
+
+ apply(f:IValueCB):IRefCB {
+ return (ref:IRef) => {
+ if ((<any>this.$rootScope).$$phase)
+ return f(ref.val())
+ this.$rootScope.$apply(function() {
+ f(ref.val())
+ })
+ }
+ }
+
+ update(ref:IRef, obj:any) {
+ for (var key in obj) {
+ if (obj[key] === undefined)
+ delete obj[key]
+ }
+
+ ref.set(_.omit(obj, "$$hashKey"))
+ }
+}
+
+app.main.factory('FB', function($rootScope) {
+ return new FB($rootScope)
+})
@@ -7,7 +7,7 @@ define(function(require) {
.factory('Missiles', function($rootScope, FB, Board) {
return function(gameId, Players) {
- var gameRef = new FB(gameId)
+ var gameRef = FB.game(gameId)
var missilesRef = gameRef.child('missiles')
var gameStatusRef = gameRef.child('gameStatus')
@@ -2,21 +2,23 @@ define(["require", "exports", "../app"], function(require, exports, __app__) {
var app = __app__;
+
var TAUNT_LIST = [
"Oooh yeah!",
"I fart in your general direction.",
"Your mother was a hamster and your father smelt of elderberries.",
"All your base are belong to us!",
"OK, next round, try it WITH your glasses on.",
- "If your daddy's aim is as bad as yours, I'm surprised you're here at all!"
+ "If your daddy's aim is as bad as yours, I'm surprised you're here at all!",
+ "Boom!"
];
var STATE = {
DEAD: "dead",
ALIVE: "alive"
};
app.main.factory('Players', function ($rootScope, FB, Board, AppVersion) {
return function (gameId) {
- var gameRef = new FB(gameId);
+ var gameRef = FB.game(gameId);
var playersRef = gameRef.child('players');
var myname;
var players = {
@@ -43,7 +45,7 @@ define(["require", "exports", "../app"], function(require, exports, __app__) {
player.wins = player.wins || 0;
player.losses = player.losses || 0;
player.message = null;
- player.version = AppVersion;
+ player.version = AppVersion.num;
var ref = playersRef.child(player.name);
ref.removeOnDisconnect();
FB.update(ref, player);
@@ -128,7 +130,6 @@ define(["require", "exports", "../app"], function(require, exports, __app__) {
player.state = STATE.DEAD;
player.losses += 1;
player.killer = killerName;
- console.log("KILL PLAYER", player);
FB.update(playersRef.child(player.name), player);
}
function move(player) {
View
@@ -1,14 +1,6 @@
-///<reference path="../def/jquery.d.ts"/>
-///<reference path="../def/angular.d.ts"/>
-///<reference path="../def/underscore.d.ts"/>
-
-// This service keeps track of all the current players (in an array), and merges moves into your stuff
-// Also lets you join
-
-// Stick with a functional paradigm
-// but if you have SOME state, you're going to want classes or nested functions
import app = module("../app")
+import fb = module("./FB")
import av = module("./AppVersion")
// the angular service returns this IPlayerFactory
@@ -40,34 +32,34 @@ export interface IPlayers {
// hacky.. switch to some other pattern soon
// everyone needs to be able to call these function. They take the state into account!
- alivePlayers: Function;
+ alivePlayers():IPlayer[];
join(p:IPlayer);
- listen: Function;
- move: Function;
- killPlayer: Function;
- playerByName: Function;
- latestVersion: Function;
+ listen();
+ move(player:IPlayer);
+ killPlayer(player:IPlayer, killerName:string);
+ playerByName(name:string):IPlayer;
+ latestVersion():string;
}
-
var TAUNT_LIST = [
"Oooh yeah!"
, "I fart in your general direction."
, "Your mother was a hamster and your father smelt of elderberries."
, "All your base are belong to us!"
, "OK, next round, try it WITH your glasses on."
, "If your daddy's aim is as bad as yours, I'm surprised you're here at all!"
+ , "Boom!"
]
var STATE = {
DEAD: "dead",
ALIVE: "alive"
}
-app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion:av.IAppVersion) {
+app.main.factory('Players', function($rootScope:ng.IScope, FB:fb.FB, Board, AppVersion:av.IAppVersion) {
return function(gameId) {
- var gameRef = new FB(gameId)
+ var gameRef = FB.game(gameId)
var playersRef = gameRef.child('players')
var myname:string;
@@ -89,7 +81,7 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
}
// you need to define the functions in here, so they have access to the state!
- function join(player) {
+ function join(player:IPlayer) {
//console.log("Join:", player.name)
myname = player.name
player.x = Board.randomX()
@@ -100,7 +92,7 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
player.wins = player.wins || 0
player.losses = player.losses || 0
player.message = null
- player.version = AppVersion
+ player.version = AppVersion.num
var ref = playersRef.child(player.name)
ref.removeOnDisconnect();
@@ -119,14 +111,14 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
//gameRef.child('winner').on('value', FB.apply(onWinner))
}
- function onJoin(player) {
+ function onJoin(player:IPlayer) {
if (!players.current && player.name == myname) {
players.current = player
}
players.all.push(player)
}
- function onUpdate(remotePlayer) {
+ function onUpdate(remotePlayer:IPlayer) {
var player = playerByName(remotePlayer.name)
if (!player) {
return console.log("Error, player not found: "+remotePlayer.name)
@@ -150,7 +142,7 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
}
}
- function onQuit(player) {
+ function onQuit(player:IPlayer) {
players.all = players.all.filter((p) => p.name != player.name)
}
@@ -179,7 +171,7 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
// then turn everyone back to alive! (the winner does this?)
}
- function onWinner(player) {
+ function onWinner(player:IPlayer) {
// this can get called with null
if (!player) {
@@ -225,46 +217,45 @@ app.main.factory('Players', function($rootScope:ng.IScope, FB, Board, AppVersion
}
// killPlayer ONLY happens from the current player's perspective. yOu can only kill yourself
- function killPlayer(player, killerName) {
+ function killPlayer(player:IPlayer, killerName:string) {
player.state = STATE.DEAD
player.losses += 1
player.killer = killerName
- console.log("KILL PLAYER", player)
FB.update(playersRef.child(player.name), player)
}
- function move(player) {
+ function move(player:IPlayer) {
var playerRef = getPlayerRef(player.name)
FB.update(playerRef, player)
}
- function getPlayerRef(name) {
+ function getPlayerRef(name:string):any {
return playersRef.child(name)
}
// just make them class members?
// your function stuff is CRAP if you don't pass it in. no better than a class
// property of PLAYERS
- function playerByName(name) {
+ function playerByName(name:string):IPlayer {
return players.all.filter((p) => (p.name == name))[0]
}
- function isAlive(p) {
+ function isAlive(p:IPlayer):bool {
return (p.state == STATE.ALIVE)
}
// this is a property of PLAYERS
- function alivePlayers() {
+ function alivePlayers():IPlayer[] {
return players.all.filter(isAlive)
}
- function isPaid() {
+ function isPaid():bool {
return (localStorage.getItem("payment_status") == "paid");
//return FB.apply(function () {return (localStorage.getItem("payment_status") == "paid")});
}
- function latestVersion() {
+ function latestVersion():string {
return _.max(players.all, (player) => player.version)
}

0 comments on commit 83015af

Please sign in to comment.