chatRoomNg.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. angular.module('tabooServices', [])
  2. .factory('connectionService', function($rootScope) {
  3. var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket;
  4. var chatSocket = null;
  5. var service = {
  6. username: '',
  7. messages: [],
  8. teamA: {},
  9. teamB: {},
  10. error: null,
  11. isConnected: function() {
  12. return this.username != '';
  13. },
  14. };
  15. service.connect = function(username) {
  16. chatSocket = new WS(jsRoutes.controllers.Application.chat(username).webSocketURL());
  17. chatSocket.onmessage = onEvent;
  18. chatSocket.onopen = function() {
  19. service.send("/status");
  20. }
  21. service.username = username;
  22. }
  23. service.disconnect = function() {
  24. service.username = '';
  25. service.messages = [];
  26. service.members = [];
  27. chatSocket.close();
  28. chatSocket = null;
  29. }
  30. service.send = function(message) {
  31. chatSocket.send(JSON.stringify({text: message}));
  32. }
  33. function onEvent(event) {
  34. var message = JSON.parse(event.data);
  35. console.log(message);
  36. if(message.error) {
  37. service.error = message;
  38. }
  39. else if(message.kind == "talk") {
  40. service.messages.push(message);
  41. }
  42. else if(message.kind == "join") {
  43. addMessage("join", message.user, " has joined.");
  44. }
  45. else if(message.kind == "quit") {
  46. addMessage("quit", message.user, " has left.");
  47. }
  48. else if(message.kind == "point") {
  49. var text = "";
  50. if(message.action == "correct") {
  51. text = message.user + " got it!";
  52. }
  53. else if(message.action == "invalid") {
  54. text = "Uh-uh! You said a taboo word.";
  55. }
  56. else if(message.action == "pass") {
  57. text = "Tsk tsk. You passed.";
  58. }
  59. else if(message.action == "taboo") {
  60. text = "Oh no! "+message.user+" has called you out.";
  61. }
  62. text += " The last word was "+message.card.word+".";
  63. gmMessage(text);
  64. service.points = message.points;
  65. }
  66. else if(message.kind == "roundReady") {
  67. gmMessage("Next round, the player will be "+message.player);
  68. if(message.player == service.username) {
  69. service.startReady = true;
  70. }
  71. }
  72. else if(message.kind == "roundStart") {
  73. gmMessage("Start game!");
  74. service.startReady = false;
  75. service.startTime = new Date();
  76. service.points = 0;
  77. }
  78. else if(message.kind == "roundEnd") {
  79. if(message.card) {
  80. gmMessage("Time's up! The last word was "+message.card.word+".");
  81. }
  82. gmMessage("The round has ended. The team got "+message.points+".");
  83. service.startReady = false;
  84. }
  85. else if(message.kind == "card") {
  86. service.card = message.card;
  87. }
  88. else if(message.kind == "status") {
  89. updateStatus(message);
  90. }
  91. $rootScope.$apply();
  92. }
  93. function updateStatus(message) {
  94. service.teamA = message.teamA;
  95. service.teamB = message.teamB;
  96. }
  97. function addMessage(kind, user, message) {
  98. service.messages.push({
  99. kind: kind,
  100. user: user,
  101. message: message
  102. });
  103. }
  104. function gmMessage(message) {
  105. addMessage("talk", "*GM", message);
  106. }
  107. return service;
  108. });
  109. angular.module('taboo', ['tabooServices']);
  110. function partial(template) {
  111. return jsRoutes.controllers.Assets.at('partials/'+template+'.html').url;
  112. }
  113. function ViewCtrl($scope) {
  114. $scope.partial = partial;
  115. }
  116. function LoginCtrl($scope, connectionService) {
  117. $scope.service = connectionService;
  118. }
  119. function ChatCtrl($scope, connectionService) {
  120. $scope.service = connectionService;
  121. $scope.onType = function(event) {
  122. if(event.keyCode == 13) {
  123. connectionService.send($scope.text);
  124. $scope.text = '';
  125. event.originalEvent.preventDefault();
  126. }
  127. }
  128. }