chatRoom.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var h = require('cyclejs').h;
  2. function renderLogin() {
  3. var disconnected = true;
  4. var error = null;
  5. var hasRoom = false;
  6. return (
  7. h("div.row", [
  8. error && h("div.alert.alert-danger", [
  9. h("strong", [ "Oops!" ]), error + "."
  10. ]),
  11. h("div#login", [
  12. disconnected && h("form.form-inline#login-form", [
  13. h("input#username.form-control", {
  14. "name": "username",
  15. "type": "text",
  16. "ng-model": "username",
  17. "placeholder": "Username"
  18. }),
  19. h("br"),
  20. h("br"),
  21. h("button.btn", {
  22. "type": "submit",
  23. "ng-click": "service.connect(username); username=''"
  24. }, [ hasRoom ? 'Join Room' : 'Create New Room' ])
  25. ])
  26. ])
  27. ])
  28. )
  29. }
  30. function renderChat(messages, me) {
  31. return (
  32. h("div#main.col-md-9", [
  33. h("div#messages", [
  34. h("table", messages.map(function(message) {
  35. var classes = '.message';
  36. classes += '.'+message.kind;
  37. if(message.user == me) {
  38. classes += '.me';
  39. }
  40. return h("tr"+classes, [
  41. h("td.user", [ message.user ]),
  42. h("td", [ message.message ])
  43. ]);
  44. })),
  45. h("input#talk.form-control", {
  46. "type": "text",
  47. "ng-model": "text",
  48. "ng-keypress": "onType($event)"
  49. })
  50. ])
  51. ])
  52. )
  53. }
  54. function renderSidebar() {
  55. return (
  56. h("div.col-md-3", {
  57. "ng-controller": "GameCtrl"
  58. }, [
  59. h("button.btn.btn-primary", {
  60. "ng-show": "game.pendingRound",
  61. "ng-click": "game.startRound()"
  62. }, [ "Start" ]),
  63. h("div", {
  64. "ng-if": "game.roundRunning()"
  65. }, [
  66. h("h2", [ h("ng-pluralize", {
  67. "count": "game.timer.count",
  68. "when": "{'0': 'Time\\'s up!', 'one': '1 second', 'other': '{} seconds'}"
  69. }) ]),
  70. h("h2", [ h("ng-pluralize", {
  71. "count": "game.points",
  72. "when": "{'one': '1 point', 'other': '{} points'}"
  73. }) ]),
  74. h("hr")
  75. ]),
  76. h("div", {
  77. "ng-if": "game.roundRunning()"
  78. }, [
  79. h("h3", {
  80. "ng-show": "game.isPlayer()"
  81. }, [ "you are the giver" ]),
  82. h("h3", {
  83. "ng-show": "game.isMonitor()"
  84. }, [ "you are a monitor" ]),
  85. h("h3", {
  86. "ng-show": "game.isGuesser()"
  87. }, [ "you are a guesser" ]),
  88. h("button.btn.btn-warning", {
  89. "ng-show": "game.isPlayer()",
  90. "ng-click": "game.pass()"
  91. }, [ "Pass" ]),
  92. h("button.btn.btn-danger", {
  93. "ng-show": "game.isMonitor()",
  94. "ng-click": "game.taboo()"
  95. }, [ "Uh-uh!" ]),
  96. h("button.btn.btn-success", {
  97. "ng-show": "game.isMonitor() || game.isPlayer()",
  98. "ng-click": "game.correct()"
  99. }, [ "Correct" ])
  100. ]),
  101. h("div", {
  102. "ng-show": "game.card"
  103. }, [
  104. h("h2", [ "Card" ]),
  105. h("h3", [ "{{game.card.word}}" ]),
  106. h("ul.taboo", [
  107. h("li", {
  108. "ng-repeat": "word in game.card.taboo"
  109. }, [ "{{word}}" ])
  110. ])
  111. ]),
  112. h("h2", [ "Team A" ]),
  113. h("ul.members", [
  114. h("li", {
  115. "ng-repeat": "member in game.teamA.members"
  116. }, [ "{{member}}" ])
  117. ]),
  118. h("h2", [ "Team B" ]),
  119. h("ul.members", [
  120. h("li", {
  121. "ng-repeat": "member in game.teamB.members"
  122. }, [ "{{member}}" ])
  123. ])
  124. ])
  125. )
  126. }
  127. function renderMain() {
  128. return h("div.row", [
  129. renderChat(),
  130. renderSideBar()
  131. ])
  132. }
  133. module.exports = function() {
  134. var isConnected = false;
  135. return (
  136. h("div", [
  137. h("div.page-header", [
  138. h("h1", isConnected ?
  139. [ "Welcome ", h("small", [ "You are playing as {{service.username}}" ]) ]
  140. :
  141. [ "Welcome ", h("small", [ "login to play" ]) ]
  142. )
  143. ]),
  144. isConnected ? renderMain() : renderLogin()
  145. ])
  146. );
  147. }