Advertisement
emesten

Untitled

Jul 8th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template lang="pug">
  2.     #app
  3.         Spinner(v-if="isFetching" size="huge").spinner
  4.         .error(v-else-if="!isFetching && skunBugDown") {{ skunBugDown }}
  5.         .container(v-else-if="!isFetching && !gameStarted")
  6.             .room-data
  7.                 .room-box
  8.                     .room-fs
  9.                         router-link(:to=" { name: 'Room', params: { id: roomID, name: roomName }}").link Invite link (RMB)
  10.                     .room-sb link
  11.                 .room-box
  12.                     .room-fs {{ roomName }}
  13.                     .room-sb name
  14.                 .room-box
  15.                     .room-fs {{ creatorNick }}
  16.                     .room-sb creator
  17.                 .room-box
  18.                     .room-fs {{ spaceLimit }}
  19.                     .room-sb slots left
  20.                 .room-box
  21.                     .listed.room-fs(@click="changeListedState") {{ isListed }}
  22.                     .room-sb listed
  23.             .room-container
  24.                 Chat(:messages="messages").chat
  25.                 .room-clients
  26.                     .room-client(v-for="client in clients")
  27.                         .client-points {{ client.currentPoints }}
  28.                         div(:class="[client.nick === nick ? 'client-nick-you' : '', 'client-nick']")
  29.                             router-link(:to="{ name: 'Profile', params: { id: client.nick } }" target="_blank") {{ client.nick }}
  30.             .start-button
  31.                 button(v-if="isCreator" @click="startGame").btn START A GAME
  32.                 .start-info(v-else) Game will start soon
  33.         Game(v-if="gameStarted && !waitingForPlayers" :questions="questions" @gamedone="gameHasEnded")
  34.         .waiting-box(v-if="waitingForPlayers")
  35.             Spinner(size="large").waiting-spinner
  36.             .waiting-title Waiting for other players
  37.             .waiting-score-box
  38.                 .waiting-score-title Your total score
  39.                 .waiting-score {{ points }}
  40. </template>
  41.  
  42. <script>
  43.     import Spinner from '../../components/Spinner';
  44.     import Game from './components/Game';
  45.     import Chat from './components/Chat';
  46.     import Type from '../../enums/Type';
  47.     import Error from '../../enums/Error';
  48.     import { EventBus } from '../../EventBus/EventBus';
  49.  
  50.     export default {
  51.         name: 'Room',
  52.         components: {
  53.             Spinner,
  54.             Game,
  55.             Chat
  56.         },
  57.         data() {
  58.             return {
  59.                 isFetching: true,
  60.                 socket: null,
  61.                 exists: false,
  62.                 roomName: null,
  63.                 roomID: null,
  64.                 clients: [],
  65.                 spaceLimit: null,
  66.                 isCreator: false,
  67.                 questions: [],
  68.                 gameStarted: false,
  69.                 waitingForPlayers: false,
  70.                 creatorNick: null,
  71.                 skunBugDown: null,
  72.                 nick: null,
  73.                 messages: [],
  74.                 isListed: true
  75.             }
  76.         },
  77.         created() {
  78.             const socket = new WebSocket('ws://'+this.$ptw+':8080');
  79.             const roomID = this.$route.params.id;
  80.             EventBus.socket = socket;
  81.             EventBus.room.id = roomID;
  82.             const token = this.$cookie.get('mathimeauth');
  83.             this.socket = socket;
  84.             socket.onopen = () => {
  85.                 socket.send(
  86.                     JSON.stringify({
  87.                         type: Type.EXISTS,
  88.                         roomID: roomID,
  89.                         token: token
  90.                     })
  91.                 );
  92.             }
  93.  
  94.             socket.onmessage = (msg) => {
  95.                 const response = JSON.parse(msg.data);
  96.                 if (response.error === Error.ROOMLOADFAILED) {
  97.                     this.skunBugDown = Error.ROOMDOESNOTEXIST;
  98.                     this.isFetching = false;
  99.                 }
  100.                 else if (response.error === Error.USERNOTFOUND) {
  101.                     this.skunBugDown = Error.USERDOESNOTEXIST;
  102.                     this.isFetching = false;
  103.                 }
  104.                 if (response.type === Type.EXISTS) {
  105.                     this.exists = true;
  106.                     this.roomName = response.room.roomName;
  107.                     this.roomID = response.room.roomID;
  108.                     this.clients = response.room.clients;
  109.                     this.spaceLimit = response.spaceLimit;
  110.                     this.creatorNick = response.creatorNick;
  111.                     this.nick = response.nick;
  112.                     if (response.creator)
  113.                         this.isCreator = true;
  114.                     this.isFetching = false;
  115.  
  116.                     EventBus.$emit('LOGGED_IN', this.nick);
  117.                 }
  118.                 else if (response.type === Type.GAMEINIT) {
  119.                     if (response.error) {
  120.                         return; //error cos z pytaniami
  121.                     }
  122.                     this.questions = response.questions;
  123.                     this.gameStarted = true;
  124.                 }
  125.                 else if (response.type === Type.CLIENTSUPDATE) {
  126.                     if (response.error)
  127.                         return;
  128.                     this.clients = response.clients;
  129.                     this.spaceLimit = response.spaceLimit;
  130.                 }
  131.                 else if (response.type === Type.GAMESTOP) {
  132.                     if (response.error)
  133.                         return; //?
  134.                     this.clients = response.clients;
  135.                     this.waitingForPlayers = false;
  136.                     this.gameStarted = false;
  137.                 }
  138.                 else if (response.type === Type.UPDATECREATOR) {
  139.                     if (response.error)
  140.                         return;
  141.                     this.isCreator = response.creator;
  142.                     this.creatorNick = response.creatorNick;
  143.                 }
  144.                 else if (response.type === Type.NEWMESSAGE) {
  145.                     this.messages = response.messages;
  146.                 }
  147.                 else if (response.type === Type.LISTED) {
  148.                     this.isListed = response.isListed;
  149.                 }
  150.             }
  151.         },
  152.         methods: {
  153.             startGame() {
  154.                 this.socket.send(
  155.                     JSON.stringify({
  156.                         type: Type.GAMEINIT,
  157.                         roomID: this.roomID
  158.                     })
  159.                 );
  160.             },
  161.             gameHasEnded(result) {
  162.                 this.waitingForPlayers = true;
  163.                 this.points = result.points;
  164.                 this.socket.send(JSON.stringify({
  165.                     type: Type.GAMESTOP,
  166.                     roomID: this.roomID,
  167.                     token: this.$cookie.get('mathimeauth'),
  168.                     points: this.points,
  169.                     correct: result.correct,
  170.                     incorrect: result.incorrect
  171.                 }));
  172.             },
  173.             changeListedState() {
  174.                 if (!this.isCreator)
  175.                     return;
  176.                 this.socket.send(JSON.stringify({
  177.                     type: Type.LISTED,
  178.                     roomID: this.roomID,
  179.                     isListed: !this.isListed
  180.                 }));
  181.             }
  182.         },
  183.         beforeDestroy() {
  184.             this.socket.close();
  185.         }
  186.     }
  187. </script>
  188.  
  189. <style scoped>
  190.  
  191.     .spinner, .container, .error {
  192.         width: 100%;
  193.         height: 100%;
  194.         display: flex;
  195.         align-items: center;
  196.     }
  197.  
  198.     .error {
  199.         justify-content: center;
  200.     }
  201.  
  202.     .room-data {
  203.         width: 100%;
  204.         display: flex;
  205.         justify-content: space-around;
  206.         align-items: center;
  207.         margin-top: 2rem;
  208.     }
  209.  
  210.     .room-box {
  211.         width: 100%;
  212.         display: flex;
  213.         flex-flow: column;
  214.         align-items: center;
  215.     }
  216.  
  217.     .room-fs {
  218.         font-weight: bold;
  219.         color: var(--dec-0);
  220.     }
  221.  
  222.     .room-sb {
  223.         color: var(--dec-10);
  224.         font-size: 0.8rem;
  225.         margin-top: -.3rem;
  226.     }
  227.  
  228.     .container {
  229.         flex-flow: column;
  230.     }
  231.  
  232.     .chat {
  233.         max-width: 30%;
  234.     }
  235.  
  236.     .room-clients {
  237.         display: flex;
  238.         justify-content: center;
  239.         align-items: center;
  240.         width: 100%;
  241.         height: 100%;
  242.     }
  243.  
  244.     .room-container {
  245.         display: flex;
  246.         flex-flow: row;
  247.         width: 100%;
  248.         height: 100%;
  249.         justify-content: space-between;
  250.         align-items: center;
  251.     }
  252.  
  253.     .room-client {
  254.         display: flex;
  255.         flex-flow: row;
  256.         width: 50%;
  257.         justify-content: center;
  258.         align-items: center;
  259.     }
  260.  
  261.     .client-points {
  262.         cursor: pointer;
  263.         padding: 1rem;
  264.         background-color: var(--dec-10)
  265.     }
  266.  
  267.     .client-nick, .client-nick-you {
  268.         font-weight: bold;
  269.         margin-left: 2rem;
  270.         padding: 1rem;
  271.         border-bottom: 1px solid var(--dec-10);
  272.         cursor: default;
  273.     }
  274.  
  275.     .client-nick-you {
  276.         border-bottom: 1px solid var(--dec-0)
  277.     }
  278.  
  279.     .btn {
  280.         width: 30rem;
  281.         height: 3rem;
  282.         font-weight: bold;
  283.         color: var(--dec-0);
  284.         background-color: var(--dec-10);
  285.         border: none;
  286.         outline: none;
  287.         cursor: pointer;
  288.         transition-duration: 300ms;
  289.     }
  290.  
  291.     .listed {
  292.         cursor: pointer;
  293.     }
  294.  
  295.     .btn:hover {
  296.         background-color: var(--dec-8);
  297.         transition-duration: 300ms;
  298.     }
  299.  
  300.     a {
  301.         text-decoration: none;
  302.         color: var(--dec-0);
  303.     }
  304.  
  305.     a:hover {
  306.         color: var(--dec-1);
  307.     }
  308.  
  309.     .waiting-box {
  310.         width: 100%;
  311.         height: 100%;
  312.         display: flex;
  313.         justify-content: center;
  314.         align-items: center;
  315.         flex-flow: column;
  316.     }
  317.  
  318.     .waiting-score-box {
  319.         margin-top: 5rem;
  320.         display: flex;
  321.         flex-flow: column;
  322.         align-items: center;
  323.     }
  324.  
  325.     .waiting-score-title {
  326.         font-size: 1.3rem;
  327.         font-weight: bold;
  328.     }
  329.  
  330.     .waiting-score {
  331.         color: var(--dec-10);
  332.         font-weight: bold;
  333.     }
  334.  
  335.     @media only screen and (max-width: 600px) {
  336.         .room-data {
  337.             flex-wrap: wrap;
  338.         }        
  339.     }
  340.  
  341. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement