PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/public/client.js

http://github.com/cmpolis/Apples-and-Oranges
JavaScript | 215 lines | 173 code | 39 blank | 3 comment | 31 complexity | 871999c0ffb30eeaee95030b1e2c2f10 MD5 | raw file
  1. $(function() {
  2. function event_obj(event_name, data_obj) {
  3. return { event: event_name,
  4. data: data_obj };
  5. }
  6. // Socket.IO
  7. var socket = new io.Socket(null, {port: 3009});
  8. socket.connect();
  9. var user_id;
  10. var judging = false;
  11. // Backbone
  12. var Noun = Backbone.Model.extend();
  13. var NounCollection = Backbone.Collection.extend({
  14. model: Noun
  15. });
  16. var userNouns = new NounCollection();
  17. var NounView = Backbone.View.extend({
  18. tagName: 'li',
  19. events: {
  20. 'click span.play_noun': 'play'
  21. },
  22. initialize: function(){
  23. _.bindAll(this, 'render', 'unrender', 'play');
  24. this.model.bind('remove', this.unrender);
  25. },
  26. render: function(){
  27. $(this.el).html(this.model.get('word')+' <span class="play_noun">Play</span>');
  28. $(this.el).children('.play_noun').hide();
  29. return this;
  30. },
  31. unrender: function(){
  32. $(this.el).remove();
  33. },
  34. play: function(){
  35. userNounView.disablePlay();
  36. socket.send(event_obj('play_card', this.model.id));
  37. }
  38. });
  39. var NounListView = Backbone.View.extend({
  40. el: $('#my_nouns'),
  41. initialize: function(){
  42. _.bindAll(this, 'appendNoun');
  43. this.collection = userNouns;
  44. this.collection.bind('add', this.appendNoun);
  45. },
  46. appendNoun: function(noun) {
  47. var nounView = new NounView({
  48. model: noun
  49. });
  50. this.el.append(nounView.render().el);
  51. },
  52. enablePlay: function() {
  53. $('.play_noun').show();
  54. },
  55. disablePlay: function() {
  56. $('.play_noun').hide();
  57. }
  58. });
  59. var userNounView = new NounListView();
  60. var User = Backbone.Model.extend();
  61. var UserCollection = Backbone.Collection.extend({
  62. model: User
  63. });
  64. var users = new UserCollection();
  65. var UserView = Backbone.View.extend({
  66. tagName: 'li',
  67. initialize: function(){
  68. _.bindAll(this, 'render', 'unrender');
  69. this.model.bind('remove', this.unrender);
  70. },
  71. render: function(){
  72. $(this.el).html(this.model.get('name'));
  73. return this;
  74. },
  75. unrender: function(){
  76. $(this.el).remove();
  77. }
  78. });
  79. var UserListView = Backbone.View.extend({
  80. el: $('#user_list'),
  81. initialize: function(){
  82. _.bindAll(this, 'appendUser');
  83. this.collection = users;
  84. this.collection.bind('add', this.appendUser);
  85. },
  86. appendUser: function(user) {
  87. var userView = new UserView({
  88. model: user
  89. });
  90. this.el.append(userView.render().el);
  91. },
  92. });
  93. var usersListView = new UserListView();
  94. var PlayedNoun = Backbone.Model.extend();
  95. var PlayedNounCollection = Backbone.Collection.extend({
  96. model: PlayedNoun
  97. });
  98. var nounPile = new PlayedNounCollection();
  99. var PlayedNounView = Backbone.View.extend({
  100. tagName: 'li',
  101. events: {
  102. 'click span.pick_adj': 'pick'
  103. },
  104. initialize: function(){
  105. _.bindAll(this, 'render', 'unrender', 'reveal', 'pick');
  106. this.model.view = this;
  107. this.model.bind('remove', this.unrender);
  108. },
  109. render: function(){
  110. $(this.el).html('Facedown card');
  111. return this;
  112. },
  113. unrender: function(){
  114. $(this.el).remove();
  115. },
  116. reveal: function(){
  117. $(this.el).html(this.model.get('word')+' <span class="pick_adj">Pick</span>');
  118. if(!judging) $(this.el).children('.pick_adj').hide();
  119. },
  120. pick: function(){
  121. socket.send(event_obj('pick_card', this.model.id));
  122. $(this.el).children('.pick_adj').hide();
  123. }
  124. });
  125. var NounPileView = Backbone.View.extend({
  126. el: $('#played_nouns'),
  127. initialize: function(){
  128. _.bindAll(this, 'appendNoun');
  129. this.collection = nounPile;
  130. this.collection.bind('add', this.appendNoun);
  131. },
  132. appendNoun: function(noun) {
  133. var nounView = new PlayedNounView({
  134. model: noun
  135. });
  136. this.el.append(nounView.render().el);
  137. }
  138. });
  139. var nounPileView = new NounPileView();
  140. // Handle update from server
  141. socket.on('message', function(msg){
  142. if(msg.event == 'new_card') {
  143. var noun = new Noun({word: msg.data.word, id: msg.data.id});
  144. userNouns.add(noun);
  145. } else if(msg.event == 'add_user') {
  146. var user = new User({name: msg.data.name, id: msg.data.id});
  147. users.add(user);
  148. } else if(msg.event == 'remove_user') {
  149. users.remove(msg.data);
  150. } else if(msg.event == 'remove_card') {
  151. userNouns.remove(msg.data);
  152. } else if(msg.event == 'mode_playing') {
  153. nounPile.remove(nounPile.models.slice(0));
  154. judging = (user_id == msg.data.judge_id);
  155. if(judging) {
  156. $('#judge').html('You');
  157. alert('You are judging');
  158. } else {
  159. $('#judge').html(msg.data.judge_name);
  160. userNounView.enablePlay();
  161. }
  162. $('#adj').html(msg.data.word);
  163. } else if(msg.event == 'mode_judging') {
  164. userNounView.disablePlay();
  165. nounPile.each(function(noun){
  166. noun.view.reveal();
  167. });
  168. } else if(msg.event == 'add_to_pile') {
  169. nounPile.add(new PlayedNoun(msg.data));
  170. } else if(msg.event == 'winning_word') {
  171. alert('won: ' + msg.data);
  172. } else if(msg.event == 'init') {
  173. user_id = msg.data.id;
  174. } else { alert(msg) }
  175. });
  176. });