tabooServices.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if(Chat.isConnected()) {
  30. init();
  31. }
  32. function onmessage(event, message) {
  33. if(message.kind == "point") {
  34. var text = "";
  35. if(message.action == "correct") {
  36. text = message.user + " got it!";
  37. }
  38. else if(message.action == "invalid") {
  39. text = "Uh-uh! You said a taboo word.";
  40. }
  41. else if(message.action == "pass") {
  42. text = "Tsk tsk. You passed.";
  43. }
  44. else if(message.action == "taboo") {
  45. text = "Oh no! "+message.user+" has called you out.";
  46. }
  47. text += " The last word was "+message.card.word+".";
  48. gmMessage(text);
  49. game.points = message.points;
  50. }
  51. else if(message.kind == "roundReady") {
  52. gmMessage("Next round, the player will be "+message.player);
  53. if(message.player == Chat.username) {
  54. game.pendingRound = true;
  55. }
  56. }
  57. else if(message.kind == "roundStart") {
  58. gmMessage("Start game!");
  59. game.roundStart();
  60. }
  61. else if(message.kind == "roundEnd") {
  62. if(message.card) {
  63. gmMessage("Time's up! The last word was "+message.card.word+".");
  64. }
  65. gmMessage("The round has ended. The team got "+message.points+".");
  66. game.roundEnd();
  67. }
  68. else if(message.kind == "card") {
  69. game.card = message.card;
  70. }
  71. else if(message.kind == "status" || message.kind == "join" || message.kind == "quit") {
  72. updateStatus(message);
  73. }
  74. }
  75. function updateStatus(message) {
  76. game.teamA = message.teamA;
  77. game.teamB = message.teamB;
  78. }
  79. function gmMessage(message) {
  80. Chat.receive("gm", "*GM", message);
  81. }
  82. return game;
  83. })