tabooServices.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.roundStart = function() {
  11. game.pendingRound = false;
  12. game.startTime = new Date();
  13. game.points = 0;
  14. };
  15. game.roundEnd = function() {
  16. game.pendingRound = false;
  17. game.card = null;
  18. };
  19. $rootScope.$on('ws:connected', init);
  20. $rootScope.$on('ws:message', onmessage);
  21. function init() {
  22. game.teamA = null;
  23. game.teamB = null;
  24. game.card = null;
  25. game.points = 0;
  26. game.pendingRound = false;
  27. game.status();
  28. }
  29. function onmessage(event, message) {
  30. if(message.kind == "point") {
  31. var text = "";
  32. if(message.action == "correct") {
  33. text = message.user + " got it!";
  34. }
  35. else if(message.action == "invalid") {
  36. text = "Uh-uh! You said a taboo word.";
  37. }
  38. else if(message.action == "pass") {
  39. text = "Tsk tsk. You passed.";
  40. }
  41. else if(message.action == "taboo") {
  42. text = "Oh no! "+message.user+" has called you out.";
  43. }
  44. text += " The last word was "+message.card.word+".";
  45. gmMessage(text);
  46. game.points = message.points;
  47. }
  48. else if(message.kind == "roundReady") {
  49. gmMessage("Next round, the player will be "+message.player);
  50. if(message.player == Chat.username) {
  51. game.pendingRound = true;
  52. }
  53. }
  54. else if(message.kind == "roundStart") {
  55. gmMessage("Start game!");
  56. game.roundStart();
  57. }
  58. else if(message.kind == "roundEnd") {
  59. if(message.card) {
  60. gmMessage("Time's up! The last word was "+message.card.word+".");
  61. }
  62. gmMessage("The round has ended. The team got "+message.points+".");
  63. game.roundEnd();
  64. }
  65. else if(message.kind == "card") {
  66. game.card = message.card;
  67. }
  68. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  69. updateStatus(message);
  70. }
  71. }
  72. function updateStatus(message) {
  73. game.teamA = message.teamA;
  74. game.teamB = message.teamB;
  75. }
  76. function gmMessage(message) {
  77. Chat.receive("gm", "*GM", message);
  78. }
  79. return game;
  80. })