1
0

tabooServices.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.isMonitor = function() {
  52. return game.round.monitors.indexOf(Chat.username) >= 0;
  53. };
  54. game.isPlayer = function() {
  55. return game.round.team.player == Chat.username;
  56. };
  57. game.roundRunning = function() {
  58. return game.round != null;
  59. }
  60. $rootScope.$on('ws:connected', init);
  61. $rootScope.$on('ws:message', onmessage);
  62. function init() {
  63. game.teamA = null;
  64. game.teamB = null;
  65. game.card = null;
  66. game.points = 0;
  67. game.pendingRound = false;
  68. game.roundStarted = false;
  69. game.round = null;
  70. game.timer = Timer;
  71. game.status();
  72. }
  73. if(Chat.isConnected()) {
  74. init();
  75. }
  76. function onmessage(event, message) {
  77. if(message.kind == "point") {
  78. var text = "";
  79. if(message.action == "correct") {
  80. text = message.user + " got it!";
  81. }
  82. else if(message.action == "correctp") {
  83. text = "Let's take "+message.user+"'s word that someone got it.";
  84. }
  85. else if(message.action == "invalid") {
  86. text = "Uh-uh! You said a taboo word.";
  87. }
  88. else if(message.action == "pass") {
  89. text = "Tsk tsk. "+game.round.team.player+" passed.";
  90. }
  91. else if(message.action == "taboo") {
  92. text = "Oh no! "+message.user+" has called "+game.round.team.player+" out.";
  93. }
  94. text += " The last word was "+message.card.word+".";
  95. gmMessage(text);
  96. game.points = message.points;
  97. }
  98. else if(message.kind == "abuse") {
  99. var text = "";
  100. if(message.action == "correctp") {
  101. text = message.user+" is abusing the correct button. Nobody has even tried to guess yet.";
  102. }
  103. else if(message.action == "taboo") {
  104. text = message.user+" is abusing the taboo button. "+game.round.team.player+" hasn't even said anything yet.";
  105. }
  106. gmMessage(text);
  107. }
  108. else if(message.kind == "roundReady") {
  109. gmMessage("Next round, the player will be "+message.player);
  110. if(message.player == Chat.username) {
  111. game.pendingRound = true;
  112. }
  113. }
  114. else if(message.kind == "roundStart") {
  115. gmMessage("Start game!");
  116. game.roundStart(message.round);
  117. }
  118. else if(message.kind == "roundEnd") {
  119. if(message.card) {
  120. gmMessage("Time's up! The last word was "+message.card.word+".");
  121. }
  122. gmMessage("The round has ended. The team got "+message.points+".");
  123. game.roundEnd();
  124. }
  125. else if(message.kind == "card") {
  126. game.card = message.card;
  127. }
  128. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  129. updateStatus(message);
  130. }
  131. }
  132. function updateStatus(message) {
  133. game.teamA = message.teamA;
  134. game.teamB = message.teamB;
  135. game.round = message.round;
  136. game.pendingRound = (message.pendingPlayer == Chat.username);
  137. if(game.round) {
  138. Timer.start(game.round.remainingTime);
  139. }
  140. }
  141. function gmMessage(message) {
  142. Chat.receive("gm", "*GM", message);
  143. }
  144. return game;
  145. });