|
@ -3,16 +3,7 @@ |
|
|
import {Player, Game} from "./classes.js" |
|
|
import {Player, Game} from "./classes.js" |
|
|
import * as HTTP from "./services.js" |
|
|
import * as HTTP from "./services.js" |
|
|
import style from './style.scss' |
|
|
import style from './style.scss' |
|
|
|
|
|
|
|
|
let conn |
|
|
|
|
|
let gameOver = false |
|
|
|
|
|
let player = new Player() |
|
|
|
|
|
player.loadFromCookies() |
|
|
|
|
|
player.setCookies() |
|
|
|
|
|
let game = new Game(player) |
|
|
|
|
|
|
|
|
|
|
|
console.debug(player) |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
|
|
|
import { join } from "path"; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Initialize DOM variables |
|
|
* Initialize DOM variables |
|
@ -21,32 +12,57 @@ let name = document.getElementById('name') |
|
|
let nameLabel = document.getElementById('nameLabel') |
|
|
let nameLabel = document.getElementById('nameLabel') |
|
|
let playerH1 = document.getElementById('player') |
|
|
let playerH1 = document.getElementById('player') |
|
|
let status = document.getElementById('status') |
|
|
let status = document.getElementById('status') |
|
|
let waiting = document.getElementById('waiting') |
|
|
|
|
|
let turn = document.getElementById('turn') |
|
|
|
|
|
let winner = document.getElementById('winner') |
|
|
|
|
|
let draw = document.getElementById('draw') |
|
|
|
|
|
let gameTable = document.getElementById('gameTable') |
|
|
let gameTable = document.getElementById('gameTable') |
|
|
let instructions = document.getElementById('instructions') |
|
|
let instructions = document.getElementById('instructions') |
|
|
let input = document.getElementById('shareLink') |
|
|
|
|
|
|
|
|
let shareLink = document.getElementById('shareLink') |
|
|
let tooltip = document.getElementById("tooltip") |
|
|
let tooltip = document.getElementById("tooltip") |
|
|
let startGameForm = document.getElementById('startGameForm') |
|
|
let startGameForm = document.getElementById('startGameForm') |
|
|
let joinGameForm = document.getElementById('joinGameForm') |
|
|
let joinGameForm = document.getElementById('joinGameForm') |
|
|
let opponentTileColor = "#992222" |
|
|
let opponentTileColor = "#992222" |
|
|
let tileColor = "#229922" |
|
|
let tileColor = "#229922" |
|
|
|
|
|
let gameOver = false |
|
|
|
|
|
let firstgame = true |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Setup |
|
|
|
|
|
*/ |
|
|
|
|
|
function _setup() { |
|
|
|
|
|
if (player.getName()) { |
|
|
|
|
|
name.value = player.getName() |
|
|
|
|
|
name.classList.add("hide") |
|
|
|
|
|
nameLabel.classList.add("hide") |
|
|
|
|
|
playerH1.innerText = "Hi " + player.getName() |
|
|
|
|
|
playerH1.classList.remove("hide") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
let conn |
|
|
|
|
|
let player = new Player() |
|
|
|
|
|
console.debug(player) |
|
|
|
|
|
if (player.getName()) { |
|
|
|
|
|
name.value = player.getName() |
|
|
|
|
|
_hideConfig() |
|
|
|
|
|
_showGreeting() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let game = new Game() |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
if (game.getId()) { |
|
|
|
|
|
firstgame = false |
|
|
|
|
|
// we need to fetch the game data and resume the game
|
|
|
|
|
|
_retrieveGameFromServer().then(resp => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() success", resp) |
|
|
|
|
|
game.player1 = new Player(resp.player1.id, resp.player1.name) |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
game.matrix = resp.matrix |
|
|
|
|
|
if (!resp.player2 || typeof resp.player2 === "undefined") { |
|
|
|
|
|
_updateGameOnServer({ |
|
|
|
|
|
player2: player |
|
|
|
|
|
}).then(resp => { |
|
|
|
|
|
console.debug("_updateGame() success", resp) |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_updateGame() err", err) |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
game.player2 = new Player(resp.player2.id, resp.player2.name) |
|
|
|
|
|
} |
|
|
|
|
|
if (document.location.pathname != "/join") { |
|
|
|
|
|
_renderGame() |
|
|
|
|
|
_showGame() |
|
|
|
|
|
} |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() error", err) |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
_setup() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function Copy (context) { |
|
|
export function Copy (context) { |
|
@ -61,7 +77,7 @@ export function TooltipBlur () { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (window.WebSocket) { |
|
|
if (window.WebSocket) { |
|
|
conn = new WebSocket("wss://" + document.location.host + "/ws") |
|
|
|
|
|
|
|
|
conn = new WebSocket("ws://" + document.location.host + "/ws") |
|
|
conn.onclose = function (evt) { |
|
|
conn.onclose = function (evt) { |
|
|
console.debug("Websocket Closed") |
|
|
console.debug("Websocket Closed") |
|
|
} |
|
|
} |
|
@ -75,52 +91,49 @@ if (window.WebSocket) { |
|
|
console.error("Error parsing", data) |
|
|
console.error("Error parsing", data) |
|
|
} |
|
|
} |
|
|
console.debug("Message", data) |
|
|
console.debug("Message", data) |
|
|
if (data.sender === player.getId()) { |
|
|
|
|
|
console.debug("My own message received and ignored") |
|
|
|
|
|
|
|
|
if (data.sender.toUpperCase() === player.getId().toUpperCase()) { |
|
|
|
|
|
// console.debug("My own message received and ignored")
|
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
if (data.game_id.toUpperCase() !== game.getId().toUpperCase()) { |
|
|
|
|
|
// console.debug("Not my game")
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
switch(data.event) { |
|
|
switch(data.event) { |
|
|
case "joining": |
|
|
case "joining": |
|
|
console.debug("Event: joining") |
|
|
console.debug("Event: joining") |
|
|
instructions.classList.add("hide") |
|
|
instructions.classList.add("hide") |
|
|
game.players.push(data.player) |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
_yourTurn() |
|
|
|
|
|
|
|
|
_safeRetrieve() |
|
|
_showGame() |
|
|
_showGame() |
|
|
break |
|
|
break |
|
|
|
|
|
case "new": |
|
|
|
|
|
console.debug("Event: new") |
|
|
|
|
|
let newGame = confirm("Would you like to play again?") |
|
|
|
|
|
if (newGame) { |
|
|
|
|
|
history.replaceState(null, "", "?id=" + data.new_game_id) |
|
|
|
|
|
game.setId(data.new_game_id) |
|
|
|
|
|
_retrieveGameFromServer().then(resp => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() success", resp) |
|
|
|
|
|
game.player1 = new Player(resp.player1.id, resp.player1.name) |
|
|
|
|
|
game.player2 = new Player(resp.player2.id, resp.player2.name) |
|
|
|
|
|
game.matrix = resp.matrix |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
gameOver = false |
|
|
|
|
|
_renderGame() |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() error", err) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
break |
|
|
case "move": |
|
|
case "move": |
|
|
console.debug("Event: move") |
|
|
console.debug("Event: move") |
|
|
game.matrix[data.index] = data.player |
|
|
|
|
|
_updateTiles(data.index) |
|
|
|
|
|
_yourTurn() |
|
|
|
|
|
console.debug(game.matrix) |
|
|
|
|
|
|
|
|
_safeRetrieve() |
|
|
break |
|
|
break |
|
|
case "winner": |
|
|
case "winner": |
|
|
console.debug("Event: winner") |
|
|
console.debug("Event: winner") |
|
|
game.matrix[data.index] = data.player |
|
|
|
|
|
_updateTiles(data.index) |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
status.classList.add("hide") |
|
|
|
|
|
turn.classList.add("hide") |
|
|
|
|
|
winner.innerText = "You lose... " + game.getOpponentsName(player.getId()) + " Wins!" |
|
|
|
|
|
winner.classList.remove("hide") |
|
|
|
|
|
_highlightBoard(data.positions) |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
if (startGameForm) |
|
|
|
|
|
startGameForm.classList.remove("hide") |
|
|
|
|
|
break |
|
|
|
|
|
case "draw": |
|
|
|
|
|
console.debug("Event: draw") |
|
|
|
|
|
game.matrix[data.index] = data.player |
|
|
|
|
|
_updateTiles(data.index) |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
status.classList.add("hide") |
|
|
|
|
|
turn.classList.add("hide") |
|
|
|
|
|
winner.innerText = "Its a draw!" |
|
|
|
|
|
winner.classList.remove("hide") |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
if (startGameForm) |
|
|
|
|
|
startGameForm.classList.remove("hide") |
|
|
|
|
|
|
|
|
game.winner = data.sender |
|
|
|
|
|
_safeRetrieve() |
|
|
break |
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -138,24 +151,38 @@ if (startGameForm) { |
|
|
if (!_validateForm()) { |
|
|
if (!_validateForm()) { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
_createGame().then(resp => { |
|
|
|
|
|
name.classList.add("hide") |
|
|
|
|
|
nameLabel.classList.add("hide") |
|
|
|
|
|
startGameForm.classList.add("hide") |
|
|
|
|
|
console.debug("_createGame() success", resp) |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
game.matrix = resp.matrix |
|
|
|
|
|
console.debug("Game:", game) |
|
|
|
|
|
input.value = _getUrl() + "/join/" + game.getId() |
|
|
|
|
|
instructions.classList.remove("hide") |
|
|
|
|
|
_waiting() |
|
|
|
|
|
// _yourTurn()
|
|
|
|
|
|
// _showGame()
|
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_createGame() error", err) |
|
|
|
|
|
status.innerText = err.message.error |
|
|
|
|
|
status.classList.remove("hide") |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
if (firstgame) { |
|
|
|
|
|
_safeCreate() |
|
|
|
|
|
firstgame = false |
|
|
|
|
|
} else { |
|
|
|
|
|
_createGame().then(resp => { |
|
|
|
|
|
console.debug("_createGame() success", resp) |
|
|
|
|
|
history.replaceState(null, "", "?id=" + resp.id) |
|
|
|
|
|
let old_game_id = game.getId() |
|
|
|
|
|
game.setId(resp.id) |
|
|
|
|
|
_updateGameOnServer({ |
|
|
|
|
|
player2: game.player2, |
|
|
|
|
|
turn: game.player1.id == game.winner ? game.player2.id : game.player1.id, |
|
|
|
|
|
previous_game: old_game_id |
|
|
|
|
|
}).then(resp => { |
|
|
|
|
|
console.debug("_updateGameOnServer() success", resp) |
|
|
|
|
|
gameOver = false |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
game.matrix = resp.matrix |
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: old_game_id, |
|
|
|
|
|
event: "new", |
|
|
|
|
|
new_game_id: resp.id |
|
|
|
|
|
})) |
|
|
|
|
|
_renderGame() |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_updateGameOnServer() err", err) |
|
|
|
|
|
}) |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_createGame() err", err) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
if (joinGameForm) { |
|
|
if (joinGameForm) { |
|
@ -164,171 +191,188 @@ if (joinGameForm) { |
|
|
if (!_validateForm()) { |
|
|
if (!_validateForm()) { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
_addPlayer().then(resp => { |
|
|
|
|
|
name.classList.add("hide") |
|
|
|
|
|
nameLabel.classList.add("hide") |
|
|
|
|
|
joinGameForm.classList.add("hide") |
|
|
|
|
|
console.debug("_addPlayer() success", resp) |
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: "joining", |
|
|
|
|
|
player: { |
|
|
|
|
|
id: player.getId(), |
|
|
|
|
|
name: player.getName() |
|
|
|
|
|
} |
|
|
|
|
|
})) |
|
|
|
|
|
game.players.push(new Player(resp.players[0].id, resp.players[0].name)) |
|
|
|
|
|
_theirTurn() |
|
|
|
|
|
_showGame() |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_addPlayer() error", err) |
|
|
|
|
|
status.innerText = err.message.error |
|
|
|
|
|
status.classList.remove("hide") |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: "joining" |
|
|
|
|
|
})) |
|
|
|
|
|
joinGameForm.classList.add("hide") |
|
|
|
|
|
_theirTurn() |
|
|
|
|
|
_showGame() |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
if (gameTable) { |
|
|
|
|
|
document.querySelectorAll('.tile').forEach(element => { |
|
|
|
|
|
element.addEventListener('click', event => { |
|
|
|
|
|
if (gameOver) { |
|
|
|
|
|
console.debug("Game over") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
let pos = event.target.id.split('_')[1] |
|
|
|
|
|
// validate its my turn
|
|
|
|
|
|
if (game.turn !== player.getId()) { |
|
|
|
|
|
console.debug("Not my turn") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
// validate position available
|
|
|
|
|
|
if (game.matrix[pos]) { |
|
|
|
|
|
console.debug("Tile not free") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
// set move
|
|
|
|
|
|
game.matrix[pos] = player.getId() |
|
|
|
|
|
|
|
|
|
|
|
// show tile selected
|
|
|
|
|
|
event.target.style.backgroundColor = tileColor |
|
|
|
|
|
|
|
|
|
|
|
// calculate if 3 tiles in a row
|
|
|
|
|
|
let three = _threeInARow() |
|
|
|
|
|
if (three) { |
|
|
|
|
|
console.debug("You win!") |
|
|
|
|
|
_highlightBoard(three) |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
winner.innerText = "You Win!!!" |
|
|
|
|
|
winner.classList.remove("hide") |
|
|
|
|
|
status.classList.add("hide") |
|
|
|
|
|
turn.classList.add("hide") |
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: "winner", |
|
|
|
|
|
index: pos, |
|
|
|
|
|
player: player.getId(), |
|
|
|
|
|
winner: player.getId(), |
|
|
|
|
|
positions: three |
|
|
|
|
|
})) |
|
|
|
|
|
if (startGameForm) |
|
|
|
|
|
startGameForm.classList.remove("hide") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_draw()) { |
|
|
|
|
|
console.debug("Draw!") |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
winner.innerText = "Its a draw!" |
|
|
|
|
|
winner.classList.remove("hide") |
|
|
|
|
|
status.classList.add("hide") |
|
|
|
|
|
turn.classList.add("hide") |
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: "draw", |
|
|
|
|
|
index: pos, |
|
|
|
|
|
player: player.getId(), |
|
|
|
|
|
winner: player.getId() |
|
|
|
|
|
})) |
|
|
|
|
|
if (startGameForm) |
|
|
|
|
|
startGameForm.classList.remove("hide") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function tileListener (event) { |
|
|
|
|
|
if (gameOver) { |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
let pos = event.target.id.split('_')[1] |
|
|
|
|
|
// validate its my turn
|
|
|
|
|
|
if (game.turn !== player.getId()) { |
|
|
|
|
|
console.debug("Not my turn") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
// validate position available
|
|
|
|
|
|
if (game.matrix[pos]) { |
|
|
|
|
|
console.debug("Tile not free") |
|
|
|
|
|
event.preventDefault() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
// set move
|
|
|
|
|
|
game.matrix[pos] = player.getId() |
|
|
|
|
|
|
|
|
// send move via socket
|
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: "move", |
|
|
|
|
|
index: pos, |
|
|
|
|
|
player: player.getId() |
|
|
|
|
|
})) |
|
|
|
|
|
|
|
|
// calculate if 3 tiles in a row
|
|
|
|
|
|
let [done, winner, positions] = _threeInARow() |
|
|
|
|
|
|
|
|
_theirTurn() |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
_updateGameOnServer({ |
|
|
|
|
|
id: game.getId(), |
|
|
|
|
|
player1: game.player1, |
|
|
|
|
|
player2: game.player2, |
|
|
|
|
|
turn: game.player1.id == game.turn ? game.player2.id : game.player1.id, |
|
|
|
|
|
winner: winner, |
|
|
|
|
|
matrix: game.matrix |
|
|
|
|
|
}).then(resp => { |
|
|
|
|
|
console.debug("_updateGameOnServer() success", resp) |
|
|
|
|
|
conn.send(JSON.stringify({ |
|
|
|
|
|
sender: player.getId(), |
|
|
|
|
|
game_id: game.getId(), |
|
|
|
|
|
event: done ? "winner" : "move" |
|
|
|
|
|
})) |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
_renderGame() |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_updateGameOnServer() error", err) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
if (gameTable) { |
|
|
|
|
|
document.querySelectorAll('.tile').forEach(element => { |
|
|
|
|
|
element.addEventListener('click', tileListener) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
/** |
|
|
/** |
|
|
* END Document Listeners |
|
|
* END Document Listeners |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
function _validateForm() { |
|
|
function _validateForm() { |
|
|
if (!name.value) { |
|
|
if (!name.value) { |
|
|
status.innerText = "Name is required." |
|
|
status.innerText = "Name is required." |
|
|
status.classList.remove("hide") |
|
|
|
|
|
|
|
|
status.style.color = "#FF0000" |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
status.style.color = "#000000" |
|
|
player.setName(name.value) |
|
|
player.setName(name.value) |
|
|
player.setCookies() |
|
|
|
|
|
status.classList.add("hide") |
|
|
|
|
|
_greeting() |
|
|
|
|
|
|
|
|
_hideConfig() |
|
|
|
|
|
_showGreeting() |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
function _getUrl() { |
|
|
|
|
|
return document.location.protocol + '//' + document.location.host |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _showInstructions() { |
|
|
|
|
|
shareLink.value = _getUrl() + "/join?id=" + game.getId() |
|
|
|
|
|
instructions.classList.remove("hide") |
|
|
|
|
|
} |
|
|
|
|
|
function _hideInstructions () { |
|
|
|
|
|
instructions.classList.add("hide") |
|
|
} |
|
|
} |
|
|
function _greeting() { |
|
|
|
|
|
|
|
|
function _showGreeting() { |
|
|
if (player.getName()) { |
|
|
if (player.getName()) { |
|
|
playerH1.innerText = "Hi " + player.getName() |
|
|
playerH1.innerText = "Hi " + player.getName() |
|
|
playerH1.classList.remove("hide") |
|
|
playerH1.classList.remove("hide") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
function _createGame() { |
|
|
|
|
|
let url = _getUrl() |
|
|
|
|
|
return HTTP.POST(`${url}/game`, JSON.stringify(game)) |
|
|
|
|
|
|
|
|
function _showGame () { |
|
|
|
|
|
gameTable.classList.remove("hide") |
|
|
|
|
|
} |
|
|
|
|
|
function _resumeGame () { |
|
|
|
|
|
startGameForm.classList.add("hide") |
|
|
|
|
|
_showGame() |
|
|
} |
|
|
} |
|
|
function _addPlayer() { |
|
|
|
|
|
|
|
|
function _hideConfig () { |
|
|
|
|
|
name.classList.add("hide") |
|
|
|
|
|
nameLabel.classList.add("hide") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function _renderGame() { |
|
|
|
|
|
console.debug("Render Game Board") |
|
|
|
|
|
_dimBoard(true) |
|
|
|
|
|
for (let i = 0; i < game.matrix.length; i++) { |
|
|
|
|
|
let playerAt = game.matrix |
|
|
|
|
|
if (playerAt[i] === player.getId()) { |
|
|
|
|
|
document.getElementById('pos_' + i).style.backgroundColor = tileColor |
|
|
|
|
|
} else if (playerAt[i] === game.getOpponent().getId()) { |
|
|
|
|
|
document.getElementById('pos_' + i).style.backgroundColor = opponentTileColor |
|
|
|
|
|
} else { |
|
|
|
|
|
document.getElementById('pos_' + i).style.backgroundColor = "unset" |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (game.turn == player.getId()) { |
|
|
|
|
|
_yourTurn() |
|
|
|
|
|
} else { |
|
|
|
|
|
_theirTurn() |
|
|
|
|
|
} |
|
|
|
|
|
let [done, winner, positions] = _threeInARow() |
|
|
|
|
|
if (done) { |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
game.winner = winner |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
_highlightBoard(positions) |
|
|
|
|
|
if (winner == player.getId()) { |
|
|
|
|
|
status.innerText = "You Win!!!" |
|
|
|
|
|
} else { |
|
|
|
|
|
status.innerText = "You lose... " + game.getOpponent().getName() + " Wins!" |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (_draw()) { |
|
|
|
|
|
gameOver = true |
|
|
|
|
|
_dimBoard() |
|
|
|
|
|
status.innerText = "Its a draw!" |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function _getUrl () { |
|
|
|
|
|
return document.location.protocol + '//' + document.location.host |
|
|
|
|
|
} |
|
|
|
|
|
function _safeCreate() { |
|
|
|
|
|
_createGame().then(resp => { |
|
|
|
|
|
console.debug("_createGame() success", resp) |
|
|
|
|
|
game.setId(resp.id) |
|
|
|
|
|
game.player1 = new Player(resp.player1.id, resp.player1.name) |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
_showInstructions() |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_createGame() err", err) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
function _createGame () { |
|
|
let url = _getUrl() |
|
|
let url = _getUrl() |
|
|
let gid = game.getId() |
|
|
|
|
|
let pid = player.getId() |
|
|
|
|
|
return HTTP.POST(`${url}/game/${gid}/player/${pid}`, JSON.stringify({ name: document.getElementById('name').value })) |
|
|
|
|
|
|
|
|
return HTTP.POST(`${url}/game`, JSON.stringify({ player1: player })) |
|
|
} |
|
|
} |
|
|
function _showGame() { |
|
|
|
|
|
gameTable.classList.remove("hide") |
|
|
|
|
|
|
|
|
function _updateGameOnServer(updatePayload) { |
|
|
|
|
|
let url = _getUrl() + "/game/" + game.getId() |
|
|
|
|
|
return HTTP.POST(url, JSON.stringify(updatePayload)) |
|
|
|
|
|
} |
|
|
|
|
|
function _safeRetrieve() { |
|
|
|
|
|
_retrieveGameFromServer().then(resp => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() success", resp) |
|
|
|
|
|
game.player1 = new Player(resp.player1.id, resp.player1.name) |
|
|
|
|
|
game.player2 = new Player(resp.player2.id, resp.player2.name) |
|
|
|
|
|
game.matrix = resp.matrix || [null, null, null, null, null, null, null, null, null] |
|
|
|
|
|
game.turn = resp.turn |
|
|
|
|
|
_renderGame() |
|
|
|
|
|
console.debug(game) |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.debug("_retrieveGameFromServer() error", err) |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
function _waiting() { |
|
|
|
|
|
waiting.innerText = "Waiting for opponent" |
|
|
|
|
|
waiting.classList.remove("hide") |
|
|
|
|
|
|
|
|
function _retrieveGameFromServer() { |
|
|
|
|
|
let url = _getUrl() + "/game/" + game.getId() |
|
|
|
|
|
return HTTP.GET(url) |
|
|
} |
|
|
} |
|
|
function _yourTurn () { |
|
|
function _yourTurn () { |
|
|
if (waiting) |
|
|
|
|
|
waiting.classList.add("hide") |
|
|
|
|
|
turn.innerText = "Your Turn" |
|
|
|
|
|
turn.classList.remove("hide") |
|
|
|
|
|
|
|
|
status.innerText = "Your Turn" |
|
|
game.turn = player.getId() |
|
|
game.turn = player.getId() |
|
|
} |
|
|
} |
|
|
function _theirTurn () { |
|
|
function _theirTurn () { |
|
|
game.setOpponentsTurn(player.getId()) |
|
|
|
|
|
turn.innerText = _possessivizer(game.getOpponentsName(player.getId())) + " Turn" |
|
|
|
|
|
turn.classList.remove("hide") |
|
|
|
|
|
|
|
|
status.innerText = _possessivizer(game.getOpponent().getName()) + " Turn" |
|
|
} |
|
|
} |
|
|
function _possessivizer(name) { |
|
|
function _possessivizer(name) { |
|
|
if (name.charAt(name.length - 1) === "s") { |
|
|
if (name.charAt(name.length - 1) === "s") { |
|
@ -337,28 +381,25 @@ function _possessivizer(name) { |
|
|
return name + "'s" |
|
|
return name + "'s" |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
function _updateTiles(index) { |
|
|
|
|
|
document.getElementById('pos_' + index).style.backgroundColor = opponentTileColor |
|
|
|
|
|
} |
|
|
|
|
|
function _threeInARow() { |
|
|
function _threeInARow() { |
|
|
for (let i = 0; i <= 8; i) { |
|
|
for (let i = 0; i <= 8; i) { |
|
|
if (game.matrix[i] && (game.matrix[i] === game.matrix[i + 1] && game.matrix[i] === game.matrix[i + 2]) ) { |
|
|
if (game.matrix[i] && (game.matrix[i] === game.matrix[i + 1] && game.matrix[i] === game.matrix[i + 2]) ) { |
|
|
return [i, i + 1, i + 2] |
|
|
|
|
|
|
|
|
return [true, game.matrix[i], [i, i + 1, i + 2]] |
|
|
} |
|
|
} |
|
|
i = i + 3 |
|
|
i = i + 3 |
|
|
} |
|
|
} |
|
|
for (let i = 0; i <= 2; i++) { |
|
|
for (let i = 0; i <= 2; i++) { |
|
|
if (game.matrix[i] && (game.matrix[i] === game.matrix[i + 3] && game.matrix[i] === game.matrix[i + 6]) ) { |
|
|
if (game.matrix[i] && (game.matrix[i] === game.matrix[i + 3] && game.matrix[i] === game.matrix[i + 6]) ) { |
|
|
return [i, i + 3, i + 6] |
|
|
|
|
|
|
|
|
return [true, game.matrix[i], [i, i + 3, i + 6]] |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (game.matrix[0] && (game.matrix[0] === game.matrix[4] && game.matrix[0] === game.matrix[8])) { |
|
|
if (game.matrix[0] && (game.matrix[0] === game.matrix[4] && game.matrix[0] === game.matrix[8])) { |
|
|
return [0, 4, 8] |
|
|
|
|
|
|
|
|
return [true, game.matrix[0], [0, 4, 8]] |
|
|
} |
|
|
} |
|
|
if (game.matrix[2] && (game.matrix[2] === game.matrix[4] && game.matrix[2] === game.matrix[6])) { |
|
|
if (game.matrix[2] && (game.matrix[2] === game.matrix[4] && game.matrix[2] === game.matrix[6])) { |
|
|
return [2, 4, 6] |
|
|
|
|
|
|
|
|
return [true, game.matrix[2], [2, 4, 6]] |
|
|
} |
|
|
} |
|
|
return false |
|
|
|
|
|
|
|
|
return [false, null, null] |
|
|
} |
|
|
} |
|
|
function _draw() { |
|
|
function _draw() { |
|
|
for (let i = 0; i < game.matrix.length; i++) { |
|
|
for (let i = 0; i < game.matrix.length; i++) { |
|
@ -368,10 +409,14 @@ function _draw() { |
|
|
} |
|
|
} |
|
|
return true |
|
|
return true |
|
|
} |
|
|
} |
|
|
function _dimBoard() { |
|
|
|
|
|
document.querySelectorAll('td').forEach(el => el.classList.add("dim")) |
|
|
|
|
|
|
|
|
function _dimBoard(reverse) { |
|
|
|
|
|
if (reverse) { |
|
|
|
|
|
document.querySelectorAll('td').forEach(el => el.classList.remove("dim")) |
|
|
|
|
|
document.querySelectorAll('td').forEach(el => el.classList.remove("highlight")) |
|
|
|
|
|
} else { |
|
|
|
|
|
document.querySelectorAll('td').forEach(el => el.classList.add("dim")) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function _highlightBoard (positions) { |
|
|
function _highlightBoard (positions) { |
|
|
for (let pos of positions) { |
|
|
for (let pos of positions) { |
|
|
document.querySelector('#pos_' + pos).classList.add("highlight") |
|
|
document.querySelector('#pos_' + pos).classList.add("highlight") |
|
|