Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] - Move player functionality to Room component, player join and … #20

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server
Submodule server updated from 37e534 to a4228c
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default {
body{
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-weight: 500;
font-size: 16px;
}
#app {
-webkit-font-smoothing: antialiased;
Expand Down
59 changes: 9 additions & 50 deletions src/components/Board.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<template>
<div>
<div class="container" :class="{ active: activeBlock !== false}">

<!-- Player information -->
<div class="player-info">
Player ID: {{ player.id }} <br />
Team: {{ player.team }} <br />
<button @click="leaveRoom()">leave game</button>
</div>

<!-- Click Overlay -->
<div class="click-overlay" v-if="activeBlock" @click="unselect()">
</div>
Expand Down Expand Up @@ -39,7 +31,7 @@
import intersectionWith from 'lodash/intersectionWith'
import isEqual from 'lodash/isEqual'
import cursorPosition from '../utilities/cursorPosition'
import roomService from '../services/room.service'
// import roomService from '../services/room.service'
import block from './block'
import { GRID_SIZE } from '../../config'
const userId = window.localStorage.getItem('userId')
Expand All @@ -51,12 +43,6 @@ export default {
block
},
sockets: {
connected: function () {
console.log('socket connected')
},
disconnected: function () {
console.log('socket disconnected')
},
blockMove: function (res) {
console.log('SOCKET SERVER BLOCK MOVE', res)
this.moveBlock(res.block)
Expand All @@ -65,6 +51,13 @@ export default {
const blockToEdit = this.blocks.find(x => x._id === res.block._id)
blockToEdit.grid = res.block.grid
blockToEdit.pieces = res.block.pieces
},
playerJoined: function (res) {
console.log('played joined board!', res)
this.blocks = res.room.blocks
},
roomJoined: function (res) {
this.blocks = res.room.blocks
}
},
data () {
Expand All @@ -73,12 +66,10 @@ export default {
blocks: [],
rows: Array.apply(null, Array(20)).map(function (x, i) { return i }),
cols: Array.apply(null, Array(20)).map(function (x, i) { return i }),
activeBlock: false,
player: {}
activeBlock: false
}
},
mounted () {
this.joinRoom()
document.addEventListener('mousemove', (e) => {
this.mouseController(e)
}, false)
Expand Down Expand Up @@ -160,29 +151,6 @@ export default {
})
}
},
joinRoom: function () {
roomService.join({
roomId: this.roomId,
userId: window.localStorage.getItem('userId')
}).then(res => {
window.localStorage.setItem('userId', res.data.player._id)
this.player = {
id: res.data.player._id,
team: res.data.player.team
}
this.blocks = res.data.room.blocks
})
},
leaveRoom: async function () {
await this.$confirm('Are you sure you want to leave?', 'Warning', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'warning',
center: true
}).then(() => {
this.$router.push({ path: `/` })
})
},
findNearbyBlocks: function (block) {
const areaRadius = 1
const area = {
Expand Down Expand Up @@ -277,15 +245,6 @@ $block-size: 20px;
}
}

.player-info {
position: fixed;
right: 0;
text-align: right;
bottom: 0px;
text-transform: capitalize;
font-size: 12px;
}

.dragging .piece, .block:hover {
opacity: 1;
}
Expand Down
17 changes: 9 additions & 8 deletions src/components/Lobby.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<script>
import RoomService from '../services/room.service'
import { setItem, getItem } from '../utilities/localStorage'
import { setItem } from '../utilities/localStorage'
import { distanceInWordsStrict } from 'date-fns'

export default {
Expand All @@ -81,13 +81,14 @@ export default {
return distanceInWordsStrict(date, new Date())
},
joinRoom: function (id) {
RoomService.join({roomId: id, userId: getItem('userId')}).then(res => {
setItem('userId', res.data.player._id)
this.$message({ type: 'success', message: 'You have joined a room' })
this.$router.push({ path: `/room/${res.data.room._id}` })
}).catch(e => {
this.$message({ type: 'error', message: e })
})
this.$router.push({ path: `/room/${id}` })
// RoomService.join({roomId: id, userId: getItem('userId')}).then(res => {
// setItem('userId', res.data.player._id)
// this.$message({ type: 'success', message: 'You have joined a room' })
// this.$router.push({ path: `/room/${res.data.room._id}` })
// }).catch(e => {
// this.$message({ type: 'error', message: e })
// })
},
createRoom: function (roomName) {
RoomService.create(roomName).then(res => {
Expand Down
90 changes: 88 additions & 2 deletions src/components/Room.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,103 @@
<template>
<div>
<Board :roomId="$route.params.id" />
<Board :roomId="$route.params.id" :player="player" />
<!-- Player information -->
<div class="player-info">
Player ID: {{ player.id }} <br />
Team: {{ player.team }} <br />
<button @click="leaveRoom()">leave game</button>
</div>
</div>
</template>
<script>
import Board from './Board'
const userId = window.localStorage.getItem('userId')

export default {
components: {
Board
},
sockets: {
connected: function () {
console.log('socket connected')
},
disconnected: function () {
console.log('socket disconnected')
},
roomJoined: function (res) {
console.log('room joined', res)

this.player = res.player
window.localStorage.setItem('userId', this.player._id)
switch (res.status) {
case 'select': this.playerSelect(); break
case 'joined': this.joinGame(); break
case 'full': this.spectate(); break
default: this.kick()
}
}
},
data () {
return {
player: {},
players: []
}
},
mounted () {
this.joinRoom()
},
methods: {
joinRoom: function () {
// connect to the room
this.$socket.emit('room_connect', { roomId: this.$route.params.id, userId })
// roomService.join({
// roomId: this.roomId,
// userId: window.localStorage.getItem('userId')
// }).then(res => {
// window.localStorage.setItem('userId', res.data.player._id)
// this.player = {
// id: res.data.player._id,
// team: res.data.player.team
// }
// this.blocks = res.data.room.blocks
// })
},
playerSelect: function () {
// window.localStorage.setItem('userId', this.player.id)
console.log('playerselect')
this.$confirm('Team Selection here', 'Info', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'warning',
center: true
}).then(() => {
// choose nickname and colour here
this.$socket.emit('player_connect', { roomId: this.$route.params.id, userId, colour: 'red', nickName: 'Cain' })
})
},
leaveRoom: async function () {
await this.$confirm('Are you sure you want to leave?', 'Warning', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'warning',
center: true
}).then(() => {
this.$router.push({ path: `/` })
})
},
joinGame: function (room) {

}
}
}

</script>
<style lang="scss">
.player-info {
position: fixed;
right: 0;
text-align: right;
bottom: 0px;
text-transform: capitalize;
font-size: 12px;
}
</style>
4 changes: 2 additions & 2 deletions src/components/block.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export default {
}
.helper {
position: absolute;
// outline: 1px solid rgba(255,0,0,0.2);
outline: 1px solid rgba(255,0,0,0.2);
background-size: 20px 20px;
// background-image: linear-gradient(to right, #e7e7e7 1px, transparent 1px), linear-gradient(to bottom, #e7e7e7 1px, transparent 1px);
background-image: linear-gradient(to right, #e7e7e7 1px, transparent 1px), linear-gradient(to bottom, #e7e7e7 1px, transparent 1px);
}
.block-id {
position: absolute;
Expand Down