tabooServices.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 == "roundReady") {
  99. gmMessage("Next round, the player will be "+message.player);
  100. if(message.player == Chat.username) {
  101. game.pendingRound = true;
  102. }
  103. }
  104. else if(message.kind == "roundStart") {
  105. gmMessage("Start game!");
  106. game.roundStart(message.round);
  107. }
  108. else if(message.kind == "roundEnd") {
  109. if(message.card) {
  110. gmMessage("Time's up! The last word was "+message.card.word+".");
  111. }
  112. gmMessage("The round has ended. The team got "+message.points+".");
  113. game.roundEnd();
  114. }
  115. else if(message.kind == "card") {
  116. game.card = message.card;
  117. }
  118. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  119. updateStatus(message);
  120. }
  121. }
  122. function updateStatus(message) {
  123. game.teamA = message.teamA;
  124. game.teamB = message.teamB;
  125. game.round = message.round;
  126. game.pendingRound = (message.pendingPlayer == Chat.username);
  127. if(game.round) {
  128. Timer.start(game.round.remainingTime);
  129. }
  130. }
  131. function gmMessage(message) {
  132. Chat.receive("gm", "*GM", message);
  133. }
  134. return game;
  135. });