1
0

tabooServices.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. angular.module('tabooServices', [])
  2. .factory('Timer', function($interval) {
  3. var promise;
  4. var service = {
  5. count: 0,
  6. endTime: 0,
  7. remainingTime: function() {
  8. var ms = service.endTime - new Date().getTime();
  9. return Math.round(Math.max(0, ms) / 1000);
  10. },
  11. start: function(duration) {
  12. if(service.remainingTime() > 0) $interval.cancel(promise);
  13. var now = new Date().getTime();
  14. service.endTime = now + duration * 1000;
  15. service.count = duration;
  16. promise = $interval(function() {
  17. service.count = service.remainingTime();
  18. }, 1000, duration);
  19. }
  20. }
  21. return service;
  22. })
  23. .factory('Taboo', function($rootScope, Chat, Timer) {
  24. var game = {};
  25. game.startRound = function() {
  26. Chat.send('/start');
  27. };
  28. game.status = function() {
  29. Chat.send('/status');
  30. };
  31. game.taboo = function() {
  32. Chat.send('/taboo');
  33. };
  34. game.correct = function() {
  35. Chat.send('/correct');
  36. };
  37. game.pass = function() {
  38. Chat.send('/pass');
  39. };
  40. game.roundStart = function(round) {
  41. game.pendingRound = false;
  42. game.round = round;
  43. game.points = 0;
  44. Timer.start(game.round.remainingTime);
  45. };
  46. game.roundEnd = function() {
  47. game.pendingRound = false;
  48. game.round = null;
  49. game.card = null;
  50. };
  51. game.isGuesser = function() {
  52. return game.round.team.guessers.indexOf(Chat.username) >= 0;
  53. };
  54. game.isMonitor = function() {
  55. return game.round.monitors.indexOf(Chat.username) >= 0;
  56. };
  57. game.isPlayer = function() {
  58. return game.round.team.player == Chat.username;
  59. };
  60. game.roundRunning = function() {
  61. return game.round != null;
  62. }
  63. $rootScope.$on('ws:connected', init);
  64. $rootScope.$on('ws:message', onmessage);
  65. function init() {
  66. game.teamA = null;
  67. game.teamB = null;
  68. game.card = null;
  69. game.points = 0;
  70. game.pendingRound = false;
  71. game.roundStarted = false;
  72. game.round = null;
  73. game.timer = Timer;
  74. game.status();
  75. }
  76. if(Chat.isConnected()) {
  77. init();
  78. }
  79. function onmessage(event, message) {
  80. if(message.kind == "point") {
  81. var text = "";
  82. if(message.action == "correct") {
  83. text = message.user + " got it!";
  84. }
  85. else if(message.action == "correctp") {
  86. text = "Let's take "+message.user+"'s word that someone got it.";
  87. }
  88. else if(message.action == "invalid") {
  89. text = "Uh-uh! You said a taboo word.";
  90. }
  91. else if(message.action == "pass") {
  92. text = "Tsk tsk. "+game.round.team.player+" passed.";
  93. }
  94. else if(message.action == "taboo") {
  95. text = "Oh no! "+message.user+" has called "+game.round.team.player+" out.";
  96. }
  97. text += " The last word was "+message.card.word+".";
  98. gmMessage(text);
  99. game.points = message.points;
  100. }
  101. else if(message.kind == "abuse") {
  102. var text = "";
  103. if(message.action == "correctp") {
  104. text = message.user+" is abusing the correct button. Nobody has even tried to guess yet.";
  105. }
  106. else if(message.action == "taboo") {
  107. text = message.user+" is abusing the taboo button. "+game.round.team.player+" hasn't even said anything yet.";
  108. }
  109. gmMessage(text);
  110. }
  111. else if(message.kind == "roundReady") {
  112. gmMessage("Next round, the player will be "+message.player);
  113. if(message.player == Chat.username) {
  114. game.pendingRound = true;
  115. }
  116. }
  117. else if(message.kind == "roundStart") {
  118. gmMessage("Start game!");
  119. game.roundStart(message.round);
  120. }
  121. else if(message.kind == "roundEnd") {
  122. if(message.card) {
  123. gmMessage("Time's up! The last word was "+message.card.word+".");
  124. }
  125. gmMessage("The round has ended. The team got "+message.points+".");
  126. game.roundEnd();
  127. }
  128. else if(message.kind == "card") {
  129. game.card = message.card;
  130. }
  131. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  132. updateStatus(message);
  133. }
  134. }
  135. function updateStatus(message) {
  136. game.teamA = message.teamA;
  137. game.teamB = message.teamB;
  138. game.round = message.round;
  139. game.pendingRound = (message.pendingPlayer == Chat.username);
  140. if(game.round) {
  141. Timer.start(game.round.remainingTime);
  142. }
  143. }
  144. function gmMessage(message) {
  145. Chat.receive("gm", "*GM", message);
  146. }
  147. return game;
  148. });