This commit is contained in:
Evert Prants 2017-04-04 19:59:15 +03:00
parent 624e47af63
commit 74cbfd9eac
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 18 additions and 19 deletions

View File

@ -554,6 +554,7 @@
msgElem += '<span class="line">' + escapeHtml(message) + '</span>' msgElem += '<span class="line">' + escapeHtml(message) + '</span>'
Battleship.DOM.chatbox.innerHTML += msgElem Battleship.DOM.chatbox.innerHTML += msgElem
Battleship.DOM.chatbox.scrollTop = Battleship.DOM.chatbox.scrollHeight
} }
window.onload = () => { window.onload = () => {

View File

@ -5,7 +5,6 @@ const path = require('path')
// TODO LIST: // TODO LIST:
// * Timer // * Timer
// * Chat box
const ships = [ const ships = [
{name: 'aircraft_carrier1', tiles: 5, destCount: 4}, {name: 'aircraft_carrier1', tiles: 5, destCount: 4},
@ -70,6 +69,16 @@ function clientsBySocketID (id) {
return result return result
} }
function determineOpponent (myIndex) {
let opponent = 'player2'
if (myIndex === 'player2') {
opponent = 'player1'
}
return opponent
}
function killGamesClientIsIn (uid) { function killGamesClientIsIn (uid) {
for (let gameId in games) { for (let gameId in games) {
let game = games[gameId] let game = games[gameId]
@ -108,7 +117,8 @@ function createNewGame (uid) {
player2: null, player2: null,
isWaiting: true, isWaiting: true,
turn: 1, turn: 1,
started: new Date() created: new Date(),
started: null
} }
} }
@ -136,6 +146,7 @@ function joinGame (uid, gameId) {
} }
games[gameId].isWaiting = false games[gameId].isWaiting = false
games[gameId].started = new Date()
let opponent = clients[games[gameId].player1.uid] let opponent = clients[games[gameId].player1.uid]
@ -427,11 +438,7 @@ io.on('connection', (socket) => {
if (meObj.ships.length === ships.length) { if (meObj.ships.length === ships.length) {
meObj.placed = true meObj.placed = true
let opponent = 'player2' let opponent = determineOpponent(playerInGame)
if (playerInGame === 'player2') {
opponent = 'player1'
}
if (game[opponent].placed) { if (game[opponent].placed) {
game.turn = 'player1' game.turn = 'player1'
@ -467,12 +474,7 @@ io.on('connection', (socket) => {
return return
} }
let opponent = 'player2' let opponent = determineOpponent(playerInGame)
if (playerInGame === 'player2') {
opponent = 'player1'
}
let result = attemptToBombTile(playerInGame, opponent, game, data.x, data.y) let result = attemptToBombTile(playerInGame, opponent, game, data.x, data.y)
let opponentObj = game[opponent] let opponentObj = game[opponent]
@ -512,6 +514,7 @@ io.on('connection', (socket) => {
opponentShipsLeft: ships.length - opponentObj.destructions, opponentShipsLeft: ships.length - opponentObj.destructions,
myShipsLeft: ships.length - me.destructions myShipsLeft: ships.length - me.destructions
}) })
clients[opponentObj.uid].socket.emit('current_stats', { clients[opponentObj.uid].socket.emit('current_stats', {
opponentShipsLeft: ships.length - me.destructions, opponentShipsLeft: ships.length - me.destructions,
myShipsLeft: ships.length - opponentObj.destructions myShipsLeft: ships.length - opponentObj.destructions
@ -535,12 +538,7 @@ io.on('connection', (socket) => {
return return
} }
let opponent = 'player2' let opponent = determineOpponent(playerInGame)
if (playerInGame === 'player2') {
opponent = 'player1'
}
let opponentObj = game[opponent] let opponentObj = game[opponent]
let me = game[playerInGame] let me = game[playerInGame]