diff --git a/index.html b/index.html new file mode 100644 index 0000000..59126a0 --- /dev/null +++ b/index.html @@ -0,0 +1,248 @@ + + + + + + Server + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ p1 + +
+
+ p2 + +
+
+ +
+
---- Helper Action ----
+
+ + + +
+
+ +
+ + +
+ + + + + + diff --git a/test.html b/test.html index 3d15c97..1e36c0c 100644 --- a/test.html +++ b/test.html @@ -191,37 +191,10 @@ function down (content, filename = 'down.txt') { - - + -
- Deck: - -
-
- - - -
-
- - - -
-
-
- - - - + \ No newline at end of file diff --git a/testHelper.js b/testHelper.js index ae4596c..f9d9dae 100644 --- a/testHelper.js +++ b/testHelper.js @@ -2,88 +2,111 @@ // browser only -var game; // in-play game - function TestHelper() { this.deckNames = this.readDeckNames(); - this.deckName = ''; + this.hostDeck = ''; + this.ghostDeck = ''; this.cfg = { disableAudio: true, + } } - -TestHelper.prototype.disableAudio = function (doc) { +TestHelper.prototype.disableAudio = function(doc) { // disable BGM - if (!this.cfg.disableAudio) return; + if (!this.cfg.disableAudio) { + return; + } var bgm = doc.getElementById('checkbox-bgm'); var sound = doc.getElementById('checkbox-sound-effect'); - if (bgm.checked) bgm.click(); - if (sound.checked) sound.click(); + if (bgm && bgm.checked) { + bgm.click(); + } + if (sound && sound.checked) { + sound.click(); + } } -TestHelper.prototype.initClient = function (win) { +TestHelper.prototype.initClient = function(win) { var doc = win.document; var self = this; var socket = new FakeSocket(win); - win.addEventListener('unload',function () { - socket._dEomit('disconnect'); + win.addEventListener('unload', function() { + socket._doEmit('disconnect'); }); win.document.title = 'Client' + socket.id; io._handler(socket); this.disableAudio(doc); } -TestHelper.prototype.readDeckNames = function () { - return JSON.parse(localStorage.getItem('deck_filenames')); +TestHelper.prototype.readDeckNames = function() { + return JSON.parse(localStorage.getItem('deck_filenames')) || []; } -TestHelper.prototype.readDeckByName = function (name) { - if (typeof name === 'undefined') - if (this.deckName === '') - name = this.deckNames[0]; // use WHITE_HOPE - else - name = this.deckName; // (default) use selected deck - return JSON.parse(localStorage.getItem('deck_file_'+ name)); +TestHelper.prototype.getDeckPids = function(player) { + var name; + if (player === 'host') { + name = this.hostDeck; + } else { + name = this.ghostDeck; + } + if (name === '') { + name = this.deckNames[0]; // use WHITE_HOPE + } + return JSON.parse(localStorage.getItem('deck_file_' + name)); } var helper = new TestHelper(); -global.window.newClient = function () { - var win = window.open('./webxoss-client/?local=true'); - win.addEventListener('load',function () { +function startBattle() { + if (sockets.length) { + sockets.forEach(function(target) { + target._win.close(); + }) + location.reload(); + return; + } + var win = window.open('../webxoss-client/?local=true'); + win.addEventListener('load', function() { + var win2 = window.open('../webxoss-client/?local=true'); + win2.addEventListener('load', function() { + helper.initClient(win2); + oben(); + }); helper.initClient(win); }); } -global.window.oben = function () { +function oben() { if (sockets.length !== 2) { - console.log("two client needed"); + console.log('two client needed'); return; }; var createRoomMsg = { - "roomName": "test", - "nickname": "player", - "password": "", - "mayusRoom": true -, } + 'roomName': 'test', + 'nickname': 'host', + 'password': '', + 'mayusRoom': true, + } sockets[0]._doEmit('createRoom', createRoomMsg); var joinRoomMsg = { - "roomName": "test", - "nickname": "player", - "password": "", + 'roomName': 'test', + 'nickname': 'ghost', + 'password': '', } sockets[1]._doEmit('joinRoom', joinRoomMsg); - var deck = helper.readDeckByName(); - sockets[1]._doEmit('ready',deck); - sockets[0]._doEmit('startGame',deck); + sockets[1]._doEmit('ready', helper.getDeckPids('host')); + sockets[0]._doEmit('startGame', helper.getDeckPids('ghost')); + updateBattle(); } -global.window.updateBattle = function () { + +var game; // in-play game +function updateBattle() { if (roomManager.rooms.length === 0) { console.log('no in-play game found'); return; } game = roomManager.rooms[0].game; - console.log('update game infomation successfully'); + console.log('update game information successfully'); } -global.window.upgrade = function () { +function upgrade() { var p = game.turnPlayer; var cards = p.lrigDeck.cards.concat(p.lrigTrashZone.cards); var lrigCards = []; @@ -94,129 +117,41 @@ global.window.upgrade = function () { }); game.moveCards(lrigCards, p.lrigZone); } -global.window.addToHand = function () { +function addToHand() { var cardName = document.getElementById('card-name').value; if (game.turnPlayer.getCard(cardName)) console.log('add ' + cardName + ' to hand'); - else + else console.log('no matched card'); } -global.window.addToLifeCloth = function () { +function addToLifeCloth() { var cardName = document.getElementById('card-name').value; if (game.turnPlayer.putCardToLifeCloth(cardName)) console.log('put ' + cardName + ' to life cloth'); - else + else console.log('no matched card'); } - -window.onload = function() { - var deckSelect = document.getElementById('deck-select'); - helper.deckNames.forEach(function(name){ - var deckNameItem = document.createElement('option'); - deckNameItem.setAttribute('value',name); - deckNameItem.innerHTML = name; - deckSelect.appendChild(deckNameItem); +function initDeckSelect() { + var hostDecks = document.getElementById('host-decks'); + var ghostDecks = document.getElementById('ghost-decks'); + hostDecks.innerHTML = ''; + ghostDecks.innerHTML = ''; + helper.deckNames.forEach(function(name) { + var deckname = document.createElement('option'); + deckname.setAttribute('value', name); + deckname.innerHTML = name; + hostDecks.appendChild(deckname); + ghostDecks.appendChild(deckname.cloneNode(true)); }) - helper.deckName = deckSelect.value; - deckSelect.onchange = function() { - helper.deckName = this.value; + helper.hostDeck = hostDecks.value; + helper.ghostDeck = ghostDecks.value; + hostDecks.onchange = function() { + helper.hostDeck = this.value; + } + ghostDecks.onchange = function() { + helper.ghostDeck = this.value; } } -// copy from test.js - -var io = { - on: function (name,handler) { - io._handler = handler; - }, - use: function () {} -};; -var cfg = { - MAX_ROOMS: 100, - MAX_CLIENTS: 500, - MAX_ROOM_NAME_LENGTH: 15, - MAX_NICKNAME_LENGTH: 10, - MAX_PASSWORD_LENGTH: 15 -}; -var roomManager = new RoomManager(cfg); -var MAX_SOCKETS = 500; -// var MAX_SOCKETS_PER_IP = 50; -// var ipTable = {}; -function getSocketCount () { - if (!io.sockets) return 0; - return Object.keys(io.sockets.connected).length; -} -io.use(function (socket,next) { - if (getSocketCount() >= MAX_SOCKETS) { - next(new Error('MAX_SOCKETS')); - return; - } - // var handshake = socket.request; - // var ip = handshake.connection.remoteAddress; - // if (!ip) { - // next(); - // return; - // } - // if (ip in ipTable) { - // if (ipTable[ip] >= MAX_SOCKETS_PER_IP) { - // console.log('MAX_SOCKETS_PER_IP: %s',ip); - // next(new Error('MAX_SOCKETS_PER_IP')); - // return; - // } else { - // ipTable[ip]++; - // } - // } else { - // ipTable[ip] = 1; - // } - // socket.on('disconnect',function () { - // console.log('disconnect: %s, count: %s',ip,getSocketCount()); - // if (ip in ipTable) { - // if (ipTable[ip] <= 1) { - // delete ipTable[ip]; - // } else { - // ipTable[ip]--; - // } - // } - // }); - next(); -}); -io.on('connect',function (socket) { - if (global.window) { - return roomManager.createClient(socket); - } - var req = socket.request; - if (req.connection.destroyed) { - console.log('req.connection.destroyed'); - return; - } - var query = require('url').parse(req.url,true).query; - // console.log('connect: %s, count: %s',req.connection.remoteAddress,getSocketCount()); - // console.log(query.clientId); - roomManager.createClient(socket,+query.clientId); - - // test - // socket.on('force disconnect',function () { - // socket.disconnect(); - // }); - - // for debug - if (typeof process === 'undefined') return; - var password = ''; - process.argv.slice(2).forEach(function (arg) { - var match = arg.match(/^debug_password=(\S+)/) - if (match) { - password = match[1]; - } - }); - if (!password) return; - socket.on('debug',function (psw) { - if (psw !== password) return; - try { - var path = require('path'); - var filePath = './debug.js'; - delete require.cache[path.resolve(filePath)]; - require(filePath); - } catch (e) { - console.log(e); - } - }); -}); \ No newline at end of file +window.onload = function() { + initDeckSelect(); +} \ No newline at end of file