|
| 1 | +<template> |
| 2 | + <div class="game-uttt"> |
| 3 | + <GameHead :game="game" :gameId="gameId" :players="players"></GameHead> |
| 4 | + <div class="board-parent"> |
| 5 | + <div class="board uttt-big-board"> |
| 6 | + <div class="smaller-board" v-for="boardIndex in 9"> |
| 7 | + <div class="pieces pieces-bg"> |
| 8 | + <div v-for="tileIndex in 9" class="piece piece-bg" |
| 9 | + :class="{ 'moveable': movesMade % 2 == yourIndex }" |
| 10 | + @click="onClick({ boardIndex: boardIndex - 1, tileIndex: tileIndex - 1 })"> |
| 11 | + </div> |
| 12 | + </div> |
| 13 | + <div class="pieces player-pieces"> |
| 14 | + <UrPiece v-for="piece in gamePieces" |
| 15 | + v-if="piece.boardIndex == boardIndex - 1" |
| 16 | + :key="piece.key" |
| 17 | + :mouseover="doNothing" :mouseleave="doNothing" |
| 18 | + class="piece" |
| 19 | + :class="'piece-' + piece.player" |
| 20 | + :piece="{ x: piece.tileIndex % 3, y: Math.floor(piece.tileIndex / 3) }"> |
| 21 | + </UrPiece> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + <GameResult :yourIndex="yourIndex"></GameResult> |
| 27 | + </div> |
| 28 | +</template> |
| 29 | +<script> |
| 30 | +import Socket from "../../socket"; |
| 31 | +import UrPiece from "../ur/UrPiece"; |
| 32 | +import GameHead from "./common/GameHead"; |
| 33 | +import GameResult from "./common/GameResult"; |
| 34 | +
|
| 35 | +function globalToBoardTile(position) { |
| 36 | + return { |
| 37 | + boardIndex: Math.floor(position.x / 3) + Math.floor(position.y / 3) * 3, |
| 38 | + tileIndex: Math.floor(position.x % 3) + Math.floor(position.y % 3) * 3 |
| 39 | + }; |
| 40 | +} |
| 41 | +
|
| 42 | +function boardTileToGlobal(piece) { |
| 43 | + let x = (piece.boardIndex % 3) * 3 + piece.tileIndex % 3; |
| 44 | + let y = |
| 45 | + Math.floor(piece.boardIndex / 3) * 3 + Math.floor(piece.tileIndex / 3); |
| 46 | + return { x: x, y: y }; |
| 47 | +} |
| 48 | +
|
| 49 | +export default { |
| 50 | + name: "UTTT", |
| 51 | + props: ["yourIndex", "game", "gameId", "players"], |
| 52 | + data() { |
| 53 | + return { |
| 54 | + movesMade: 0, |
| 55 | + gamePieces: [], |
| 56 | + gameOverMessage: null |
| 57 | + }; |
| 58 | + }, |
| 59 | + created() { |
| 60 | + if (this.yourIndex < 0) { |
| 61 | + Socket.send( |
| 62 | + `v1:{ "type": "observer", "game": "${this.game}", "gameId": "${ |
| 63 | + this.gameId |
| 64 | + }", "observer": "start" }` |
| 65 | + ); |
| 66 | + } |
| 67 | + Socket.$on("type:GameMove", this.messageMove); |
| 68 | + Socket.$on("type:IllegalMove", this.messageIllegal); |
| 69 | + }, |
| 70 | + beforeDestroy() { |
| 71 | + Socket.$off("type:GameMove", this.messageMove); |
| 72 | + Socket.$off("type:IllegalMove", this.messageIllegal); |
| 73 | + }, |
| 74 | + components: { |
| 75 | + GameHead, |
| 76 | + GameResult, |
| 77 | + UrPiece |
| 78 | + }, |
| 79 | + methods: { |
| 80 | + doNothing: function() {}, |
| 81 | + action: function(name, data) { |
| 82 | + if (Socket.isConnected()) { |
| 83 | + let json = `{ "game": "${this.game}", "gameId": "${ |
| 84 | + this.gameId |
| 85 | + }", "type": "move", "moveType": "${name}", "move": ${JSON.stringify( |
| 86 | + data |
| 87 | + )} }`; |
| 88 | + Socket.send(json); |
| 89 | + } |
| 90 | + }, |
| 91 | + onClick: function(piece) { |
| 92 | + this.action("move", boardTileToGlobal(piece)); |
| 93 | + }, |
| 94 | + messageMove(e) { |
| 95 | + console.log(`Recieved move: ${e.moveType}: ${e.move}`); |
| 96 | + this.movesMade++; |
| 97 | + const boardTile = globalToBoardTile(e.move); |
| 98 | + this.gamePieces.push({ |
| 99 | + x: e.move.x, |
| 100 | + y: e.move.y, |
| 101 | + player: e.player, |
| 102 | + boardIndex: boardTile.boardIndex, |
| 103 | + tileIndex: boardTile.tileIndex |
| 104 | + }); |
| 105 | + }, |
| 106 | + messageIllegal(e) { |
| 107 | + console.log("IllegalMove: " + JSON.stringify(e)); |
| 108 | + } |
| 109 | + }, |
| 110 | + computed: { |
| 111 | + moveableTiles: function() { |
| 112 | + let result = []; |
| 113 | + for (let i = 0; i < 7 * 6; i++) { |
| 114 | + result.push(false); |
| 115 | + } |
| 116 | + for (let x = 0; x < 7; x++) { |
| 117 | + for (let y = 5; y >= 0; y--) { |
| 118 | + if (!this.gamePieces.find(e => e.y == y && e.x == x)) { |
| 119 | + result[y * 7 + x] = true; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return result; |
| 125 | + } |
| 126 | + } |
| 127 | +}; |
| 128 | +</script> |
| 129 | +<style> |
| 130 | +@import "../../assets/games-style.css"; |
| 131 | +
|
| 132 | +.uttt-big-board { |
| 133 | + width: 640px; |
| 134 | + height: 640px; |
| 135 | + display: grid; |
| 136 | + grid-template-columns: 1fr 1fr 1fr; |
| 137 | + grid-template-rows: 1fr 1fr 1fr; |
| 138 | +} |
| 139 | +
|
| 140 | +.smaller-board { |
| 141 | + width: 196px; |
| 142 | + height: 196px; |
| 143 | + position: relative; |
| 144 | +} |
| 145 | +
|
| 146 | +.smaller-board .pieces { |
| 147 | + display: grid; |
| 148 | + grid-template-columns: 1fr 1fr 1fr; |
| 149 | + grid-template-rows: 1fr 1fr 1fr; |
| 150 | +} |
| 151 | +</style> |
0 commit comments