Advertisement
Guest User

App.vue

a guest
Feb 17th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. <template>
  2. <div id="app">
  3. <div class="debug" v-if="isAdmin">
  4. <h1>debug toolbar</h1>
  5. <p>
  6. <router-link to="/">Home</router-link>
  7. <router-link to="/queue">Queue</router-link>
  8. <router-link to="/game">Game</router-link>
  9. <router-link to="/admin">Admin</router-link>
  10. </p>
  11. </div>
  12. <router-view></router-view>
  13. </div>
  14. </template>
  15. <script>
  16. import store from './components/store'
  17. import assert from 'assert'
  18.  
  19. var request = require('superagent')
  20. var socketlib = require('socket.io-client')
  21. var socket = socketlib()
  22. var VueRouter = require('vue-router')
  23. var router = new VueRouter()
  24.  
  25. function getAdminKey() {
  26. return window.localStorage.getItem('writeio-admin-key')
  27. }
  28.  
  29. function isAdmin() {
  30. return !!getAdminKey()
  31. }
  32.  
  33. // Format the story as HTML
  34. function formatStory(story) {
  35. // TODO Security - make sure this is escaped & tested for XSS
  36. let result = ''
  37. story.forEach(contribution => {
  38. result += `<div>${contribution.text}</div>`
  39. })
  40. return result
  41. }
  42.  
  43. export default {
  44. name: 'app',
  45. components: {
  46. // Add any custom components here
  47. },
  48. data: function () {
  49. return {
  50. sharedState: store.state,
  51. consts: store.consts,
  52. isAdmin: isAdmin()
  53. }
  54. },
  55. created: function () {
  56. var self = this
  57.  
  58. function updateStateForPlayer(playerId, callback) {
  59. return request.get('/api/state')
  60. .set('Accept', 'application/json')
  61. .query({
  62. playerId
  63. })
  64. .end((err, res) => {
  65. assert(!err)
  66. assert(res.status === 200)
  67. let state = res.body
  68. assert(state)
  69.  
  70. console.log(
  71. `Got server state (updated num players from ${self.sharedState.players.length} to ${state.players.length})`
  72. )
  73. self.sharedState.players = state.players
  74. if (state.game) {
  75. self.sharedState.story = state.game.story
  76. self.sharedState.storyHtml = formatStory(state.game.story)
  77. }
  78.  
  79. // Populate iVotedFor boolean
  80. let myPlayer = self.sharedState.players.find(player => player.id === self.sharedState.playerId)
  81. if (myPlayer) {
  82. self.sharedState.players = self.sharedState.players.map(player => {
  83. player.iVotedFor = (player.id === myPlayer.votedForId)
  84. return player
  85. })
  86. }
  87.  
  88. assert(state.players.length <= self.consts.maxPlayers)
  89. // TODO Read current location from VueRouter here. router.history.current shows '/' for some reason
  90. if (state.players.length === self.consts.maxPlayers && window.location.hash.endsWith('/queue')) {
  91. router.replace('/game')
  92. }
  93.  
  94. if (callback) {
  95. callback()
  96. }
  97. })
  98. }
  99.  
  100. function updateAdminState(callback) {
  101. let adminKey = getAdminKey()
  102. if (!adminKey) {
  103. return
  104. }
  105.  
  106. return request.get('/api/adminState')
  107. .set('Accept', 'application/json').query({
  108. adminKey
  109. }).end((err, res) => {
  110. assert(!err)
  111. assert(res.status === 200)
  112. self.sharedState.adminState = res.body
  113. if (callback) {
  114. callback()
  115. }
  116. })
  117. }
  118.  
  119. // Returns future
  120. function updateState(callback) {
  121. // We only ask for state if we're logged in or admin
  122. if (self.sharedState.playerId) {
  123. return updateStateForPlayer(self.sharedState.playerId, callback)
  124. } else {
  125. return updateAdminState(callback)
  126. }
  127. }
  128.  
  129. updateState()
  130.  
  131. // Update on change when server state changes
  132. socket.on('server:state', function () {
  133. updateState()
  134. })
  135. socket.on('server:round-over', function () {
  136. updateState(() => {
  137. console.log('Round over')
  138. // If I won the last round, clear my suggestion box
  139. if (self.sharedState.story && self.sharedState.story.length > 0) {
  140. var lastRound = self.sharedState.story[self.sharedState.story.length - 1]
  141. if (lastRound.playerId === self.sharedState.playerId) {
  142. console.log('I won last round!')
  143. // TODO Animate
  144. self.sharedState.suggestionText = ''
  145. }
  146. }
  147. })
  148. })
  149.  
  150. // Routes
  151. // If we're not on the admin page, reload --> home
  152. if (!window.location.href.endsWith('admin')) {
  153. router.replace('/')
  154. }
  155. }
  156. }
  157.  
  158. </script>
  159. <style>
  160. #app {
  161. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  162. -webkit-font-smoothing: antialiased;
  163. -moz-osx-font-smoothing: grayscale;
  164. text-align: center;
  165. color: #2c3e50;
  166. margin-top: 60px;
  167. }
  168.  
  169. .debug {
  170. background-color: lightgray
  171. }
  172.  
  173. a {
  174. margin-right: 10px;
  175. }
  176.  
  177. .center {
  178. top: 50%;
  179. left: 50%;
  180. transform: translate3d(-50%, -50%, 0);
  181. position: absolute;
  182. }
  183.  
  184. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement