|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- var socket = new WebSocket('ws://192.168.2.131: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'
- }
- };
- // container creates the startBtn
- 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);
-
- // disable startBtn and nameInput before website finished loading
- 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 cupId
- */
- board.onMark = function(cupId){
- socket.send(makeMessage(events.outgoing.MARK, {playerId: hero.id, cupId: cupId}));
- };
-
- /**
- *
- */
- function start() {
- while (container.lastChild) {
- container.removeChild(container.lastChild);
- }
-
- if (board.players.length === 1) {
- scoreBoard[1].textContent = 'waiting...';
- }
-
- board.bindTo(container);
- }
-
-
- // if startBtn is pressed
- 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:
- // change Scoreboard
- board.highlightScoreboard(msg.data.id);
- board.ready = true;
- if (msg.data.id === hero.id) {
- // remove opponent cups
- board.enableTurn();
- }
-
- break;
- case events.incoming.MARK:
- board.doMark(msg.data.cupId, 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:
- // close
- socket.close();
-
- break;
- }
- };
-
- // if socket is open, show startBtn and nameInput
- socket.onopen = function(event) {
- startBtn.removeAttribute('disabled');
- nameInput.removeAttribute('disabled');
- nameInput.removeAttribute('placeholder');
- nameInput.focus();
- };
|