|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- var socket = new WebSocket('ws://192.168.188.23:2667');
- var events = {
- outgoing: {
- JOIN_GAME: 'csJoinGame',
- MARK: 'csMark',
- QUIT: 'csQuit'
- },
- incoming: {
- JOIN_GAME: 'scJoinGame',
- MARK: 'scMark',
- SET_TURN: 'scSetTurn',
- OPPONENT_READY: 'scOpponentReady',
- GAME_OVER: 'scGameOver',
- ERROR: 'scError',
- QUIT: 'scQuit'
- }
- };
-
- var container = document.querySelector('#gameBoard');
- var startBtn = document.querySelector('#startBtn');
- var nameInput = document.querySelector('#nickname');
-
- var scoreBoard = [
- document.querySelector('#p1Score'),
- document.querySelector('#p2Score')
- ];
-
- var hero = {};
- var board = new Board(scoreBoard);
-
- startBtn.setAttribute('disabled', true);
- nameInput.setAttribute('disabled', true);
- nameInput.setAttribute('placeholder', 'Loading...');
-
- /**
- *
- * @param action
- * @param data
- * @returns {*}
- */
- function makeMessage(action, data){
- var resp = {
- action: action,
- data: data
- };
-
- return JSON.stringify(resp);
- }
-
- /**
- *
- * @param cellId
- */
- board.onMark = function(cellId){
- socket.send(makeMessage(events.outgoing.MARK, {playerId: hero.id, cellId: cellId}));
- };
-
- /**
- *
- */
- function start() {
- while (container.lastChild) {
- container.removeChild(container.lastChild);
- }
-
- if (board.players.length === 1) {
- scoreBoard[1].textContent = 'waiting...';
- }
-
- board.bindTo(container);
- }
-
- startBtn.addEventListener('click', function(event) {
- var name = nameInput.value.trim();
-
- if (name.length > 0) {
- hero.name = name;
- socket.send(makeMessage(events.outgoing.JOIN_GAME, name));
- }
- });
-
- socket.onmessage = function(event){
- var msg = JSON.parse(event.data);
-
- switch (msg.action) {
- case events.incoming.ERROR:
- alert('Error: ' + msg.data);
- break;
- case events.incoming.JOIN_GAME:
- board.addPlayer(msg.data);
- if (msg.data.name === hero.name) {
- hero = msg.data;
- start();
- }
-
- break;
- case events.incoming.SET_TURN:
- board.highlightScoreboard(msg.data.id);
- board.ready = true;
- if (msg.data.id === hero.id) {
- board.enableTurn();
- }
-
- break;
- case events.incoming.MARK:
- board.doMark(msg.data.cellId, msg.data.player.label);
-
- break;
-
- case events.incoming.GAME_OVER:
- if (msg.data.player) {
- board.doWinner(msg.data.pos);
- } else {
- board.doDraw();
- }
-
- socket.send(makeMessage(events.outgoing.QUIT, hero.id));
-
- break;
-
- case events.incoming.QUIT:
- socket.close();
-
- break;
- }
- };
-
- socket.onopen = function(event) {
- startBtn.removeAttribute('disabled');
- nameInput.removeAttribute('disabled');
- nameInput.removeAttribute('placeholder');
- nameInput.focus();
- };
|