tabooServices.js 2.7 KB

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