tabooServices.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. angular.module('tabooServices', [])
  2. .factory('Timer', function($interval) {
  3. var service = {
  4. count: 0,
  5. start: function(duration) {
  6. service.count = duration;
  7. $interval(function() {
  8. service.count -= 1;
  9. }, 1000, duration);
  10. }
  11. }
  12. return service;
  13. })
  14. .factory('Taboo', function($rootScope, Chat, Timer) {
  15. var game = {};
  16. game.startRound = function() {
  17. Chat.send('/start');
  18. };
  19. game.status = function() {
  20. Chat.send('/status');
  21. };
  22. game.taboo = function() {
  23. Chat.send('/taboo');
  24. };
  25. game.pass = function() {
  26. Chat.send('/pass');
  27. };
  28. game.roundStart = function() {
  29. game.pendingRound = false;
  30. game.roundStarted = true;
  31. game.points = 0;
  32. game.timer.start(60);
  33. };
  34. game.roundEnd = function() {
  35. game.pendingRound = false;
  36. game.roundStarted = false;
  37. game.card = null;
  38. game.monitors = [];
  39. };
  40. game.isMonitor = function() {
  41. return game.monitors.indexOf(Chat.username) >= 0;
  42. };
  43. game.isPlayer = function() {
  44. return game.player == Chat.username;
  45. };
  46. $rootScope.$on('ws:connected', init);
  47. $rootScope.$on('ws:message', onmessage);
  48. function init() {
  49. game.teamA = null;
  50. game.teamB = null;
  51. game.card = null;
  52. game.points = 0;
  53. game.pendingRound = false;
  54. game.roundStarted = false;
  55. game.player = '';
  56. game.monitors = [];
  57. game.timer = Timer;
  58. game.status();
  59. }
  60. if(Chat.isConnected()) {
  61. init();
  62. }
  63. function onmessage(event, message) {
  64. if(message.kind == "point") {
  65. var text = "";
  66. if(message.action == "correct") {
  67. text = message.user + " got it!";
  68. }
  69. else if(message.action == "invalid") {
  70. text = "Uh-uh! You said a taboo word.";
  71. }
  72. else if(message.action == "pass") {
  73. text = "Tsk tsk. You passed.";
  74. }
  75. else if(message.action == "taboo") {
  76. text = "Oh no! "+message.user+" has called you out.";
  77. }
  78. text += " The last word was "+message.card.word+".";
  79. gmMessage(text);
  80. game.points = message.points;
  81. }
  82. else if(message.kind == "roundReady") {
  83. gmMessage("Next round, the player will be "+message.player);
  84. if(message.player == Chat.username) {
  85. game.pendingRound = true;
  86. }
  87. }
  88. else if(message.kind == "roundStart") {
  89. gmMessage("Start game!");
  90. game.roundStart();
  91. game.player = message.round.team.player;
  92. game.monitors = message.round.monitors;
  93. }
  94. else if(message.kind == "roundEnd") {
  95. if(message.card) {
  96. gmMessage("Time's up! The last word was "+message.card.word+".");
  97. }
  98. gmMessage("The round has ended. The team got "+message.points+".");
  99. game.roundEnd();
  100. }
  101. else if(message.kind == "card") {
  102. game.card = message.card;
  103. }
  104. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  105. updateStatus(message);
  106. }
  107. }
  108. function updateStatus(message) {
  109. game.teamA = message.teamA;
  110. game.teamB = message.teamB;
  111. }
  112. function gmMessage(message) {
  113. Chat.receive("gm", "*GM", message);
  114. }
  115. return game;
  116. });