tabooServices.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.pass = function() {
  35. Chat.send('/pass');
  36. };
  37. game.roundStart = function(round) {
  38. game.pendingRound = false;
  39. game.round = round;
  40. game.points = 0;
  41. Timer.start(game.round.remainingTime);
  42. };
  43. game.roundEnd = function() {
  44. game.pendingRound = false;
  45. game.round = null;
  46. game.card = null;
  47. };
  48. game.isMonitor = function() {
  49. return game.round.monitors.indexOf(Chat.username) >= 0;
  50. };
  51. game.isPlayer = function() {
  52. return game.round.team.player == Chat.username;
  53. };
  54. game.roundRunning = function() {
  55. return game.round != null;
  56. }
  57. $rootScope.$on('ws:connected', init);
  58. $rootScope.$on('ws:message', onmessage);
  59. function init() {
  60. game.teamA = null;
  61. game.teamB = null;
  62. game.card = null;
  63. game.points = 0;
  64. game.pendingRound = false;
  65. game.roundStarted = false;
  66. game.round = null;
  67. game.timer = Timer;
  68. game.status();
  69. }
  70. if(Chat.isConnected()) {
  71. init();
  72. }
  73. function onmessage(event, message) {
  74. if(message.kind == "point") {
  75. var text = "";
  76. if(message.action == "correct") {
  77. text = message.user + " got it!";
  78. }
  79. else if(message.action == "invalid") {
  80. text = "Uh-uh! You said a taboo word.";
  81. }
  82. else if(message.action == "pass") {
  83. text = "Tsk tsk. You passed.";
  84. }
  85. else if(message.action == "taboo") {
  86. text = "Oh no! "+message.user+" has called you out.";
  87. }
  88. text += " The last word was "+message.card.word+".";
  89. gmMessage(text);
  90. game.points = message.points;
  91. }
  92. else if(message.kind == "roundReady") {
  93. gmMessage("Next round, the player will be "+message.player);
  94. if(message.player == Chat.username) {
  95. game.pendingRound = true;
  96. }
  97. }
  98. else if(message.kind == "roundStart") {
  99. gmMessage("Start game!");
  100. game.roundStart(message.round);
  101. }
  102. else if(message.kind == "roundEnd") {
  103. if(message.card) {
  104. gmMessage("Time's up! The last word was "+message.card.word+".");
  105. }
  106. gmMessage("The round has ended. The team got "+message.points+".");
  107. game.roundEnd();
  108. }
  109. else if(message.kind == "card") {
  110. game.card = message.card;
  111. }
  112. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  113. updateStatus(message);
  114. }
  115. }
  116. function updateStatus(message) {
  117. game.teamA = message.teamA;
  118. game.teamB = message.teamB;
  119. game.round = message.round;
  120. game.pendingRound = (message.pendingPlayer == Chat.username);
  121. if(game.round) {
  122. Timer.start(game.round.remainingTime);
  123. }
  124. }
  125. function gmMessage(message) {
  126. Chat.receive("gm", "*GM", message);
  127. }
  128. return game;
  129. });