mirror of
https://github.com/webxoss/webxoss-core.git
synced 2025-02-13 16:13:26 +01:00
improve test helper
refactor test helper grow lrig to max level and put other lrig card to the bottom of lrig zone aviod use innerHTML
This commit is contained in:
parent
3b98b12593
commit
012308b4a4
1 changed files with 85 additions and 42 deletions
127
testHelper.js
127
testHelper.js
|
@ -4,12 +4,11 @@
|
||||||
|
|
||||||
var $ = document.getElementById.bind(document);
|
var $ = document.getElementById.bind(document);
|
||||||
|
|
||||||
function TestHelper() {
|
var noBGM = true;
|
||||||
this.noBGM = true;
|
|
||||||
}
|
function disableAudio(doc) {
|
||||||
TestHelper.prototype.disableAudio = function(doc) {
|
|
||||||
// disable BGM
|
// disable BGM
|
||||||
if (!this.noBGM) {
|
if (!noBGM) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var bgm = doc.getElementById('checkbox-bgm');
|
var bgm = doc.getElementById('checkbox-bgm');
|
||||||
|
@ -20,8 +19,8 @@ TestHelper.prototype.disableAudio = function(doc) {
|
||||||
if (sound && sound.checked) {
|
if (sound && sound.checked) {
|
||||||
sound.click();
|
sound.click();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
TestHelper.prototype.initClient = function(win) {
|
function initClient(win) {
|
||||||
var doc = win.document;
|
var doc = win.document;
|
||||||
var socket = new FakeSocket(win);
|
var socket = new FakeSocket(win);
|
||||||
win.addEventListener('unload', function() {
|
win.addEventListener('unload', function() {
|
||||||
|
@ -30,11 +29,9 @@ TestHelper.prototype.initClient = function(win) {
|
||||||
win.document.title = 'Client' + socket.id;
|
win.document.title = 'Client' + socket.id;
|
||||||
io._handler(socket);
|
io._handler(socket);
|
||||||
|
|
||||||
this.disableAudio(doc);
|
disableAudio(doc);
|
||||||
log(win.document.title + ' added.');
|
log(win.document.title + ' added.');
|
||||||
};
|
}
|
||||||
|
|
||||||
var helper = new TestHelper();
|
|
||||||
|
|
||||||
// prepare and start game
|
// prepare and start game
|
||||||
function startBattle() {
|
function startBattle() {
|
||||||
|
@ -49,16 +46,16 @@ function startBattle() {
|
||||||
win.addEventListener('load', function() {
|
win.addEventListener('load', function() {
|
||||||
var win2 = window.open('../webxoss-client/?local=true');
|
var win2 = window.open('../webxoss-client/?local=true');
|
||||||
win2.addEventListener('load', function() {
|
win2.addEventListener('load', function() {
|
||||||
helper.initClient(win2);
|
initClient(win2);
|
||||||
oben();
|
oben();
|
||||||
});
|
});
|
||||||
helper.initClient(win);
|
initClient(win);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function oben() {
|
function oben() {
|
||||||
if (sockets.length !== 2) {
|
if (sockets.length !== 2) {
|
||||||
log('two client needed');
|
log('two client needed.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var createRoomMsg = {
|
var createRoomMsg = {
|
||||||
|
@ -66,7 +63,7 @@ function oben() {
|
||||||
'nickname': 'host',
|
'nickname': 'host',
|
||||||
'password': '',
|
'password': '',
|
||||||
'mayusRoom': true,
|
'mayusRoom': true,
|
||||||
}
|
};
|
||||||
sockets[0]._doEmit('createRoom', createRoomMsg);
|
sockets[0]._doEmit('createRoom', createRoomMsg);
|
||||||
log('Client<1> create room.');
|
log('Client<1> create room.');
|
||||||
|
|
||||||
|
@ -74,7 +71,7 @@ function oben() {
|
||||||
'roomName': 'test',
|
'roomName': 'test',
|
||||||
'nickname': 'ghost',
|
'nickname': 'ghost',
|
||||||
'password': '',
|
'password': '',
|
||||||
}
|
};
|
||||||
sockets[1]._doEmit('joinRoom', joinRoomMsg);
|
sockets[1]._doEmit('joinRoom', joinRoomMsg);
|
||||||
log('Client<2> join room.');
|
log('Client<2> join room.');
|
||||||
|
|
||||||
|
@ -83,16 +80,17 @@ function oben() {
|
||||||
sockets[0]._doEmit('startGame', getDeckPids('ghost'));
|
sockets[0]._doEmit('startGame', getDeckPids('ghost'));
|
||||||
log('oben!');
|
log('oben!');
|
||||||
|
|
||||||
updateBattle();
|
handleBattle();
|
||||||
}
|
}
|
||||||
|
|
||||||
var game; // in-play game
|
var game; // in-play game
|
||||||
function updateBattle() {
|
function handleBattle() {
|
||||||
if (roomManager.rooms.length === 0) {
|
if (roomManager.rooms.length === 0) {
|
||||||
log('no in-play game found.');
|
log('no in-play game found.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
game = roomManager.rooms[0].game;
|
game = roomManager.rooms[0].game;
|
||||||
|
enableButtons();
|
||||||
log('Handle game successfully.');
|
log('Handle game successfully.');
|
||||||
log('Now you can use helper function.');
|
log('Now you can use helper function.');
|
||||||
}
|
}
|
||||||
|
@ -102,13 +100,22 @@ function grow() {
|
||||||
var p = game.turnPlayer;
|
var p = game.turnPlayer;
|
||||||
var cards = p.lrigDeck.cards.concat(p.lrigTrashZone.cards);
|
var cards = p.lrigDeck.cards.concat(p.lrigTrashZone.cards);
|
||||||
var lrigCards = [];
|
var lrigCards = [];
|
||||||
|
var maxLevel = 0;
|
||||||
cards.forEach(function(card) {
|
cards.forEach(function(card) {
|
||||||
if (card.type === 'LRIG') {
|
if (card.type === 'LRIG') {
|
||||||
lrigCards.push(card);
|
if (maxLevel < card.level) {
|
||||||
|
lrigCards.push(card);
|
||||||
|
maxLevel = card.level;
|
||||||
|
} else {
|
||||||
|
lrigCards.unshift(card);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
game.moveCards(lrigCards, p.lrigZone);
|
lrigCards.pop().moveTo(p.lrigZone);
|
||||||
log('grow lrig to level max.');
|
lrigCards.forEach(function(card) {
|
||||||
|
card.moveTo(p.lrigZone, {bottom: true});
|
||||||
|
});
|
||||||
|
log('grow lrig to max level.');
|
||||||
}
|
}
|
||||||
function draw(num) {
|
function draw(num) {
|
||||||
if (!num) {
|
if (!num) {
|
||||||
|
@ -134,28 +141,27 @@ function matchCard(arg) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var cid = 0;
|
var cid = 0;
|
||||||
game.cards.forEach(function(card) {
|
for (var i = 0; i < game.cards.length; i++) {
|
||||||
|
var card = game.cards[i];
|
||||||
var info = CardInfo[card.cid];
|
var info = CardInfo[card.cid];
|
||||||
var matched = info.pid === arg ||
|
var matched = info.name === arg ||
|
||||||
info.cid === arg ||
|
info.name_zh_CN === arg ||
|
||||||
info.wxid === arg;
|
info.cid === arg ||
|
||||||
|
info.wxid === arg;
|
||||||
if (matched) {
|
if (matched) {
|
||||||
cid = card.cid;
|
cid = card.cid;
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
if (!cid) return null;
|
if (!cid) return null;
|
||||||
var player = game.turnPlayer;
|
var p = selectPlayer();
|
||||||
var cards = concat(player.mainDeck.cards,
|
var cards = concat(p.mainDeck.cards,p.trashZone.cards,p.enerZone.cards,p.lifeClothZone.cards);
|
||||||
player.trashZone.cards,
|
for (var j = 0; j < cards.length; j++) {
|
||||||
player.enerZone.cards,
|
var card = cards[j];
|
||||||
player.lifeClothZone.cards);
|
|
||||||
|
|
||||||
cards.forEach(function(card) {
|
|
||||||
if (card.cid === cid) {
|
if (card.cid === cid) {
|
||||||
return card;
|
return card;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,28 +177,37 @@ function addTo(zone) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var input = $('card-name').value;
|
var input = $('card-name').value;
|
||||||
if (input && matchCard(input)) {
|
if (input) {
|
||||||
var matchedCard = matchCard(input);
|
var matchedCard = matchCard(input);
|
||||||
if (!matchedCard) {
|
if (!matchedCard) {
|
||||||
log('no matched card');
|
log('no matched card');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
matchedCard.moveTo(game.turnPlayer[zone]);
|
var p = selectPlayer();
|
||||||
|
matchedCard.moveTo(p[zone]);
|
||||||
log('add <' + matchedCard.name + '> to ' + zone + '.');
|
log('add <' + matchedCard.name + '> to ' + zone + '.');
|
||||||
|
} else {
|
||||||
|
log('card\'s wxid / pid / cid needed.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetLrigDeck() {
|
function resetLrigDeck() {
|
||||||
game.moveCards(game.turnPlayer.lrigTrashZone.cards, game.turnPlayer.lrigDeck);
|
var p = selectPlayer();
|
||||||
|
game.moveCards(p.lrigTrashZone.cards, p.lrigDeck);
|
||||||
log('reset lrig deck.');
|
log('reset lrig deck.');
|
||||||
}
|
}
|
||||||
// log
|
// log
|
||||||
function log(text) {
|
function log(text) {
|
||||||
var logger = $('log');
|
var logger = $('log');
|
||||||
logger.innerHTML += text;
|
logger.textContent += text;
|
||||||
logger.innerHTML += '\n';
|
logger.textContent += '\n';
|
||||||
}
|
}
|
||||||
// dom
|
// dom
|
||||||
|
function selectPlayer() {
|
||||||
|
return $('target-player').value === 'opponent' ?
|
||||||
|
game.turnPlayer.opponent :
|
||||||
|
game.turnPlayer;
|
||||||
|
}
|
||||||
function getDeckPids(player) {
|
function getDeckPids(player) {
|
||||||
var name = {
|
var name = {
|
||||||
'host': $('host-decks').value || '',
|
'host': $('host-decks').value || '',
|
||||||
|
@ -216,7 +231,7 @@ function initDeckSelect() {
|
||||||
deckNames.forEach(function(name) {
|
deckNames.forEach(function(name) {
|
||||||
var deckName = document.createElement('option');
|
var deckName = document.createElement('option');
|
||||||
deckName.setAttribute('value', name);
|
deckName.setAttribute('value', name);
|
||||||
deckName.innerHTML = name;
|
deckName.textContent = name;
|
||||||
hostDeckSelect.appendChild(deckName);
|
hostDeckSelect.appendChild(deckName);
|
||||||
ghostDeckSelect.appendChild(deckName.cloneNode(true));
|
ghostDeckSelect.appendChild(deckName.cloneNode(true));
|
||||||
});
|
});
|
||||||
|
@ -227,8 +242,36 @@ function changeLanguage() {
|
||||||
log('set language to ' + lang + '.');
|
log('set language to ' + lang + '.');
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
|
function resizeIFrameToFitContent() {
|
||||||
|
var iFrame = $('deck-editor');
|
||||||
|
iFrame.width = iFrame.contentWindow.document.body.scrollWidth;
|
||||||
|
iFrame.height = iFrame.contentWindow.document.body.scrollHeight;
|
||||||
|
}
|
||||||
|
function enableButtons() {
|
||||||
|
var buttons = document.getElementsByTagName('button');
|
||||||
|
for (var i = 0; i < buttons.length; i++) {
|
||||||
|
if (buttons[i].id !== 'oben') {
|
||||||
|
buttons[i].disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function disableButtons() {
|
||||||
|
var buttons = document.getElementsByTagName('button');
|
||||||
|
for (var i = 0; i < buttons.length; i++) {
|
||||||
|
if (buttons[i].id !== 'oben') {
|
||||||
|
buttons[i].disabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
$('select-language').value = localStorage.getItem('language');
|
$('select-language').value = localStorage.getItem('language');
|
||||||
initDeckSelect();
|
initDeckSelect();
|
||||||
|
resizeIFrameToFitContent();
|
||||||
|
disableButtons();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.onunload = function() {
|
||||||
|
sockets.forEach(function(socket) {
|
||||||
|
socket._win.close();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue