Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

reworking organization

  • Loading branch information...
commit f3a4ca5ba0d9babcb489ac539f91cd65f6d23e24 1 parent c0a94b3
authored February 22, 2010
19  js/Transformers/Game.js
... ...
@@ -0,0 +1,19 @@
  1
+dojo.provide('Transformers.Game');
  2
+
  3
+dojo.require('dijit.Widget');
  4
+dojo.require('Transformers.Referee');
  5
+
  6
+(function(d) {
  7
+	d.declare('Transformers.Game', [ dijit._Widget ], {
  8
+		
  9
+		constructor : function(args) {
  10
+			this.teams = (args && args.teams) || ['autobots', 'decepticons'],
  11
+			this.controller = new Transformers.Referee();
  12
+			
  13
+			d.forEach(teams, function(t) { new Transformers.Team(t); });
  14
+			
  15
+			d.publish('/game/new');
  16
+			d.publish('/game/start');
  17
+		}
  18
+	});
  19
+})(dojo);
63  js/Transformers/Referee.js
... ...
@@ -1,63 +1,28 @@
1  
-dojo.provide('Transformers.Controller');
2  
-
3  
-dojo.require('dijit._Widget');
4  
-dojo.require('dijit._Templated');
5  
-
6  
-dojo.require('Transformers.Team');
  1
+dojo.provide('Transformers.Referee');
7 2
 
8 3
 (function(d) {
9  
-	d.declare('Transformers.Controller', [ dijit._Widget, dijit._Templated ], {
  4
+	d.declare('Transformers.Referee', null, {
10 5
 		rate : 100, 	// ms
11  
-		templatePath : d.moduleUrl('Transformers', 'templates/controller.html'),
12  
-		
13  
-		constructor : function(args) {
14  
-			this.teamNames = args.teamNames || ['autobots', 'decepticons'];
15  
-		},
16  
-		
17  
-		postCreate : function() {
18  
-			this.setupTeams();
19  
-
20  
-			this.connect(this.generateUserGame, 'click', 'userGenerated');
21  
-			this.connect(this.generateRandomGame, 'click', 'randomGenerated');
22  
-
23  
-			d.subscribe('/game/end', this, 'displayResults');
24  
-		},
25 6
 		
26  
-		setupTeams : function() {
27  
-			this.teams = d.map(this.teamNames, function(team) {
28  
-				return new Transformers.Team({ team : team }).placeAt(this.domNode, 'last');
29  
-			}, this);
30  
-			console.log(this.teams);
31  
-		},
  7
+		constructor : function() {
  8
+			d.subscribe('/team/join', this, '_registerTeam');
  9
+			d.subscribe('/bot/ping', this, '_handleBotPing');
32 10
 
33  
-		generate : function(type) {
34  
-			d.forEach([ 'reset', 'new', 'start' ], function(topic) {
35  
-				d.publish('/game/' + topic, [ { type : type }]);
36  
-			}, this);
  11
+			d.subscribe('/game/start', this, '_doTurn');
  12
+			
  13
+			this.firstTurn = Math.random() > 0.5 ? 0 : 1;
37 14
 		},
38 15
 		
39  
-		userGenerated : function(e) {
40  
-			e.preventDefault();
41  
-			this.generate('user');
42  
-		},
43  
-
44  
-		randomGenerated : function(e) {
45  
-			e.preventDefault();
46  
-			this.generate('random');
  16
+		_doTurn : function() {
  17
+			
47 18
 		},
48 19
 		
49  
-		_randomTeamSize : function() {
50  
-			return Math.floor(Math.random() * this.maxTeamSize) + 1;
  20
+		_registerTeam : function(team) {
  21
+			this.teams.push(team);
51 22
 		},
52 23
 		
53  
-		displayResults : function() {
54  
-			var results = [];
55  
-			d.forEach(this.teams, function(team) {
56  
-				results.push(team.getResults());
57  
-			});
58  
-			
59  
-			console.log(results);
  24
+		_handleBotPing : function(ping) {
  25
+			console.dir(ping);
60 26
 		}
61 27
 	});
62  
-	
63 28
 })(dojo);
64  js/Transformers/Team.js
@@ -7,69 +7,31 @@ dojo.require('Transformers.Autobot');
7 7
 dojo.require('Transformers.Decepticon');
8 8
 
9 9
 (function(d) {
10  
-	d.declare('Transformers.Team', [ dijit._Widget, dijit._Templated ], {
11  
-		rate : 100,
12  
-		maxTeamSize : 5,
13  
-		creators : {
14  
-			'autobots' : Transformers.Autobot,
15  
-			'decepticons' : Transformers.Decepticon,
16  
-			'default' : Transformers._Transformer
17  
-		},
18  
-		templatePath : d.moduleUrl('Transformers', 'templates/team.html'),
  10
+	d.declare('Transformers.Team', [ dijit._Widget ], {
  11
+		defaultTeamSize : 5,
19 12
 		
20 13
 		constructor : function(args) {
21 14
 			this.team = args.team || 'anonymous';
22  
-			this.creator = this.creators[this.name] || this.creators.default;
23  
-			
24 15
 			this.score = 0;
25  
-			this.players = [];
26  
-		},
27  
-		
28  
-		postCreate : function() {
29  
-			d.subscribe('/game/new', this, '_setup');
30  
-			d.subscribe('/game/reset', this, '_reset');
31  
-			this.teamName.innerHTML = this.team;
32  
-		},
33  
-		
34  
-		getResults : function() {
35  
-			return 'TODO';
  16
+			this._setup();
  17
+
  18
+			d.subscribe('/game/start', this, '_setup');
  19
+			d.subscribe('/status/request', this, '_sendStatus');
  20
+
  21
+			d.publish('/game/join', [ this ]);			
36 22
 		},
37 23
 		
38 24
 		_setup : function(config) {
39  
-			this.teamSize = config.type == 'random' ? this._randomTeamSize() : this.playerCount.value || 1;
40  
-			
41  
-			if (this.teamSize > this.maxTeamSize) { 
42  
-				this.teamSize = this.maxTeamSize; 
43  
-				this.playerCount.value = this.maxTeamSize;
44  
-			}
45  
-			
46  
-			this.playerCount.disabled = true;
47  
-			this.playerCount.value = this.teamSize;
48  
-			this._buildTeam();
49  
-		},
50  
-		
51  
-		_buildTeam : function() {
52  
-			var size = this.teamSize;
  25
+			var size = this.teamSize || this.defaultTeamSize;
53 26
 			
  27
+			this.members = [];
54 28
 			while (size--) {
55  
-				var t = new this.creator({ team : this.name }).placeAt(this.playerStatus, 'last')
56  
-				this.players.push(t);
  29
+				this.members.push(new this.creator({ team : this.name }));
57 30
 			}
58 31
 		},
59 32
 		
60  
-		_reset : function() {
61  
-			this.players = d.filter(this.players, function(player) { 
62  
-				player.destroy(); 
63  
-				return false;
64  
-			}, this);
65  
-
66  
-			this.playerCount.disabled = false;
67  
-			this.playerCount.value = '';
68  
-			this.playerCount.innerHTML = '0';
69  
-		},
70  
-		
71  
-		_randomTeamSize : function() {
72  
-			return Math.floor(Math.random() * this.maxTeamSize) + 1;
  33
+		_sendStatus : function() {
  34
+			d.publish('/team/ping', [ this ]);
73 35
 		}
74 36
 	});
75 37
 })(dojo);
9  js/Transformers/_Transformer.js
@@ -11,14 +11,13 @@ dojo.require('dijit._Widget');
11 11
 			this.health = this.startingHealth;
12 12
 			
13 13
 			d.subscribe('/status/request', this, '_sendStatus');
14  
-		},
15  
-		
16  
-		postCreate : function() {
17  
-			this._updateHealth();
  14
+			d.subscribe('/game/new', this, 'destroy');
  15
+			
  16
+			d.publish('/bot/join', [ this ]);
18 17
 		},
19 18
 		
20 19
 		_sendStatus : function() {
21 20
 			d.publish('/status', [ this ]);
22  
-		}		
  21
+		}
23 22
 	});	
24 23
 })(dojo);
9  topics.txt
@@ -5,15 +5,18 @@
5 5
 /game/reset					ends the game and destroys all bot instances
6 6
 /game/pause					pauses play; bots remain intact
7 7
 /game/end					game over 
8  
-/game/join					team joins a game
9 8
 
10 9
 /team/[a|d]/attack			attack the opposing team; bigger results, bigger risk
11 10
 /team/[a|d]/defend			defend; can't cause much damage, but won't take much either
12 11
 /team/[a|d]/transform		change state; bots in docile state can't attack but are hard to damage
13 12
 /team/[a|d]/retreat			run; can't cause any damage, won't take much either; costs some health
  13
+/team/join					team joins a game
14 14
 
15  
-/ping						controller listens here for bots to send updates; do other bots listen too?
  15
+/status/request				bots and teams listen here for status requests; respond on /ping
  16
+/ping/team					controller listens here for teams to send updates
  17
+/ping/bot					controller listens here for bots to send updates
16 18
 
17 19
 /bot/join					bot joins the game
18 20
 /bot/die					bot leaves the game
19  
-/bot/msg					bot says something
  21
+/bot/msg					bot says something
  22
+

0 notes on commit f3a4ca5

Please sign in to comment.
Something went wrong with that request. Please try again.