tabooServices.js 3.4 KB

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