/views/games/index.jade

https://github.com/jwalter/zombiedice · Jade · 223 lines · 209 code · 14 blank · 0 comment · 4 complexity · 3563c283036b9ca220fe1ab1305bd9c8 MD5 · raw file

  1. script(src='/socket.io/socket.io.js')
  2. script(src='/javascripts/ICanHaz.min.js')
  3. script(src='http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js')
  4. script(src='/javascripts/backbone.js')
  5. script(id='die', type='text/html')
  6. li A {{ color }} die with the {{ side }} side up
  7. script(id='player', type='text/html')
  8. li {{ name }} -
  9. div(id='{{ id }}'){{ score }}
  10. script
  11. var playerId = '#{playerid}';
  12. var socket = io.connect();
  13. socket.on('connect', function(data) {
  14. socket.emit('join game', { gameId: '#{gameId}'}, function(joined, serverPlayers, scores) {
  15. _.each(serverPlayers, function(p) {
  16. p.score = scores[p.id];
  17. players.add(new Player(p));
  18. })
  19. $('#msgArea').html('Joined game: ' + joined);
  20. setButtonVisibility(joined);
  21. });
  22. });
  23. socket.on('news', function (data) {
  24. socket.emit('my other event', { my: 'data' });
  25. });
  26. socket.on('addPlayer', function (data) {
  27. console.log('Add player: ');
  28. console.log(data);
  29. players.add(new Player(data));
  30. });
  31. socket.on('updateTable', function (data) {
  32. console.log(data);
  33. table.setBrains(data.tableBrains);
  34. table.setShotguns(data.tableShotguns);
  35. table.setActivePlayer(data.activePlayer);
  36. tableview.render();
  37. $('#tableDice').html('');
  38. _.each(data.dice, function(die) {
  39. var die = ich.die(die);
  40. $('#tableDice').append(die);
  41. });
  42. });
  43. socket.on('resetTable', function (data) {
  44. table.reset();
  45. $('#tableDice').html('');
  46. table.setActivePlayer(data.activePlayer);
  47. tableview.render();
  48. updateScore(data.scoreUpdate);
  49. setButtonVisibility(data.activePlayer);
  50. });
  51. var object = {};
  52. _.extend(object, Backbone.Events);
  53. function setButtonVisibility(activePlayerId) {
  54. if (activePlayerId === playerId) {
  55. showButtons();
  56. } else {
  57. hideButtons();
  58. }
  59. }
  60. function showButtons() {
  61. onTurnButtons(function(name) {
  62. $(name).removeClass('hidden');
  63. });
  64. }
  65. function hideButtons() {
  66. onTurnButtons(function(name) {
  67. $(name).addClass('hidden');
  68. });
  69. }
  70. function onTurnButtons(fn) {
  71. fn('#rollBtn');
  72. fn('#endTurnBtn');
  73. }
  74. function roll() {
  75. $.getJSON('/game/#{game.cid}/roll', function(dice) {
  76. <!--$('#msgArea').html('Server says: ' + data + '<br>');-->
  77. }
  78. );
  79. }
  80. function endTurn() {
  81. $.getJSON('/game/#{game.cid}/endTurn', function(data) {
  82. }
  83. );
  84. }
  85. function updateScore(data) {
  86. console.log(data);
  87. var p = players.get(data.playerId);
  88. p.set({score: data.score});
  89. }
  90. h1 #{title}
  91. div(id='playArea')
  92. Your game: #{game.cid}
  93. div(id='tablePlayer')
  94. div(id='tableBrains')
  95. div(id='tableShotguns')
  96. div(id='tableDice')
  97. button(id='rollBtn', class='hidden') Roll
  98. button(id='endTurnBtn', class='hidden') End turn
  99. script
  100. (function ($) {
  101. Table = Backbone.Model.extend({
  102. initialize: function() {
  103. this.brains = 0;
  104. this.shotguns = 0;
  105. },
  106. setBrains: function(brains) {
  107. this.brains = brains;
  108. },
  109. setShotguns: function(shotguns) {
  110. this.shotguns = shotguns;
  111. },
  112. setActivePlayer: function(player) {
  113. this.activePlayer = player;
  114. },
  115. reset: function() {
  116. this.brains = 0;
  117. this.shotguns = 0;
  118. }
  119. });
  120. TableView = Backbone.View.extend({
  121. el: $('#playArea'),
  122. initialize: function () {
  123. },
  124. events: {
  125. "click #rollBtn": "rollAction",
  126. "click #endTurnBtn": "endTurnAction"
  127. },
  128. rollAction: function () {
  129. roll();
  130. },
  131. endTurnAction: function () {
  132. endTurn();
  133. },
  134. render: function() {
  135. $('#tablePlayer').html('Current player: ' + table.activePlayer);
  136. $('#tableBrains').html('Brains: ' + table.brains);
  137. $('#tableShotguns').html('Shotguns: ' + table.shotguns);
  138. }
  139. });
  140. tableview = new TableView;
  141. table = new Table();
  142. })(jQuery);
  143. div(id='msgArea')
  144. div(id='scoreBoard')
  145. button(id='add-friend') Add Friend
  146. script
  147. (function ($) {
  148. Player = Backbone.Model.extend({
  149. name: 'unnamed',
  150. initialize: function(options) {
  151. if (options.score) {
  152. this.score = options.score;
  153. }
  154. if (options.name) {
  155. this.name = options.name;
  156. }
  157. if (options.id) {
  158. this.id = options.id;
  159. }
  160. console.log('Player.initialize');
  161. console.log(options);
  162. this.bind("change:score", function(model) {
  163. $("#" + model.id).html(model.get('score'));
  164. });
  165. }
  166. });
  167. Players = Backbone.Collection.extend({
  168. model: Player,
  169. initialize: function (models, options) {
  170. this.bind("add", options.view.addFriendLi);
  171. },
  172. addByName: function(name) {
  173. console.log('Add by name: ' + name);
  174. var p = new Player({name: name});
  175. console.log('Add: ' + p + '(' + p.get('name') + ')');
  176. this.add(p);
  177. }
  178. });
  179. ScoreView = Backbone.View.extend({
  180. el: $('#scoreBoard'),
  181. initialize: function () {
  182. this.friends = new Players( null, { view: this });
  183. },
  184. events: {
  185. "click #add-friend": "showPrompt",
  186. "click #rollBtn": "performRoll",
  187. },
  188. showPrompt: function () {
  189. var friend_name = prompt("Who is your friend?");
  190. var friend_model = new Player({ name: friend_name });
  191. this.friends.addByName( friend_name );
  192. },
  193. performRoll: function () {
  194. roll();
  195. },
  196. addFriendLi: function (model) {
  197. var playerTemplate = ich.player({name: model.get('name'), score: model.get('score'), id: model.id});
  198. $("#scoreTable > ul").append(playerTemplate);
  199. },
  200. render: function(model) {
  201. $("#scoreTable > ul").append("<li>" + model.get('name') + ", " + model.get('score') + "</li>");
  202. }
  203. });
  204. scoreview = new ScoreView;
  205. players = new Players( null, { view: scoreview });
  206. })(jQuery);
  207. Current score
  208. div(id='scoreTable')
  209. ul