MenuOnlineRoom.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <b-container class="gamesListContainer">
  3. <b-row>
  4. <b-col cols="auto">
  5. <h1>
  6. Existing Games
  7. </h1>
  8. <div id="online-games">
  9. <div v-if="loading">
  10. <h3>{{ loadingMessage }}</h3>
  11. </div>
  12. <div v-on-clickaway="unselect" v-else>
  13. <game-item
  14. v-for="(game, index) in gamesList"
  15. :key="game.id"
  16. :game-element="game"
  17. @click.native="selectedIndex = index"
  18. ></game-item>
  19. </div>
  20. </div>
  21. <div id="online-room-buttons">
  22. <div>
  23. <button class="btn" @click="$emit('enter-game-creation')">
  24. New game
  25. <br />
  26. <b-icon icon="plus-circle"></b-icon>
  27. </button>
  28. <button class="btn" :disabled="resumeDisabled" @click="joinGame()">
  29. Join
  30. <br />
  31. <b-icon icon="controller"></b-icon>
  32. </button>
  33. <button class="btn btn_primary" @click="$emit('back')">
  34. Return
  35. <br />
  36. <b-icon icon="arrow-counterclockwise"></b-icon>
  37. </button>
  38. </div>
  39. </div>
  40. </b-col>
  41. </b-row>
  42. </b-container>
  43. </template>
  44. <script>
  45. import GameItem from './components/GameItem';
  46. import { mixin as clickaway } from 'vue-clickaway';
  47. import { socketService, socketEventBus } from '../../main';
  48. export default {
  49. props: ['username'],
  50. data() {
  51. return {
  52. selectedIndex: -1,
  53. resumeDisabled: true,
  54. loading: true,
  55. loadingMessage: 'Loading...',
  56. gamesList: []
  57. };
  58. },
  59. components: {
  60. GameItem
  61. },
  62. watch: {
  63. selectedIndex() {
  64. console.log('index selected: ' + this.selectedIndex);
  65. if (this.selectedIndex >= 0) {
  66. this.resumeDisabled = false;
  67. } else {
  68. this.resumeDisabled = true;
  69. }
  70. }
  71. },
  72. methods: {
  73. unselect() {
  74. this.selectedIndex = -1;
  75. },
  76. joinGame() {
  77. this.$emit('back');
  78. this.$emit('join-game-id', {
  79. id: this.gamesList[this.selectedIndex].id,
  80. username: this.username
  81. });
  82. },
  83. updateGamesList() {
  84. socketService
  85. .getGamesList(this.username)
  86. .then(games => {
  87. this.gamesList = games;
  88. if (this.gamesList.length === 0) {
  89. this.loadingMessage = 'No games found, wait a new or create one !';
  90. } else {
  91. this.loading = false;
  92. }
  93. })
  94. .catch(() => {
  95. this.loadingMessage = 'Error reaching server';
  96. });
  97. }
  98. },
  99. mixins: [clickaway],
  100. created() {
  101. this.updateGamesList();
  102. socketEventBus.$on('reload-games', () => {
  103. this.updateGamesList();
  104. });
  105. }
  106. };
  107. </script>
  108. <style lang="scss" scoped>
  109. .gamesListContainer {
  110. margin-left: 15%;
  111. }
  112. .gamesListContainer {
  113. font-family: MedievalSharp, cursive;
  114. color: #a41515;
  115. }
  116. </style>