improve TestHelper

click Oben! == open 2 client, create 1 room, start battle with selected
deck
2 player can play with 2 separate deck
show deckEditor by iframe in server page
keep previous test html and js
This commit is contained in:
deardrops 2017-04-06 20:02:03 +08:00 committed by WEBXOSS
parent 9aff9d258d
commit 4273196754
3 changed files with 337 additions and 181 deletions

248
index.html Normal file
View file

@ -0,0 +1,248 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Server</title>
<script>
var global = window;
// function require (file) {
// return window[file.replace(/^\.[\/\\]/,'').replace(/.js$/,'')];
// }
function handleCardInfo() {
// var defaultValueMap = {
// "rarity": "LR",
// "cardType": "SIGNI",
// "color": "white",
// "level": 0,
// "limit": 0,
// "power": 0,
// "limiting": "",
// "classes": [],
// "costWhite": 0,
// "costBlack": 0,
// "costRed": 0,
// "costBlue": 0,
// "costGreen": 0,
// "costColorless": 0,
// "guardFlag": false,
// "multiEner": false,
// };
for (var x in CardInfo) {
var info = CardInfo[x];
delete info.timestamp;
delete info.kana;
delete info.imgUrl;
delete info.cardText;
delete info.cardText_zh_CN;
delete info.cardText_en;
delete info.constEffects;
delete info.actionEffects;
delete info.startUpEffects;
delete info.spellEffect;
delete info.artsEffect;
delete info.burstEffect
delete info.faqs;
delete info.cardSkills;
delete info.growCondition;
delete info.useCondition;
delete info.costChange;
delete info.costChangeBeforeUseAsyn;
delete info.resonaPhase;
delete info.resonaCondition;
delete info.resonaAsyn;
delete info.encore;
delete info.bettedCost;
if (info.rise) info.rise = true;
// for (var key in defaultValueMap) {
// if (info[key] === defaultValueMap[key]) {
// delete info[key];
// }
// }
}
// var textarea = document.createElement('textarea');
// textarea.value = JSON.stringify(CardInfo);
// document.body.appendChild(textarea);
// textarea.select();
}
function handleCardInfo_ko(min, max) {
var props = [
"name",
"actionEffectTexts",
"constEffectTexts",
"startUpEffectTexts",
"spellEffectTexts",
"artsEffectTexts",
"burstEffectTexts",
"attachedEffectTexts",
"extraTexts",
];
var suffix = [
"",
"_zh_CN",
"_en"
]
var arr = [];
for (var x in CardInfo) {
// if (x <= 1762) continue;
if (x < min || x > max) continue;
var info = CardInfo[x];
var obj = {
pid: info.pid,
wxid: info.wxid,
};
props.forEach(function(rawprop) {
// suffix.forEach(function (suf) {
// var prop = rawprop + suf;
// if (!info[prop]) return;
// obj[prop] = info[prop];
// });
obj[rawprop + "_ko"] = info[rawprop + "_en"];
});
arr.push(obj);
}
down(arr, `${min}-${max}.json`)
}
function fetchAndHandleRuJson(url) {
let CardInfo_ru = {};
fetch(url).then(res => res.json()).then(arr => {
arr.forEach(info => {
let info_ru = {};
for (let prop in info) {
if (!prop.match(/_ru$/)) continue;
info_ru[prop] = info[prop];
}
CardInfo_ru[info.pid] = info_ru;
});
window.ru = JSON.stringify(CardInfo_ru);
});
}
function getPrCards() {
let ids = [];
for (let pid in CardInfo) {
let card = CardInfo[pid];
let id = +card.wxid.replace('PR-', '');
if (id) ids.push(id);
}
ids.sort((a, b) => a - b);
let ranges = [];
let start = ids[0];
let end = ids[0];
ids.slice(1).concat(0).forEach(id => {
if ((id - end) === 1) {
end = id;
} else {
let range = `${('000'+start).slice(-3)}-${('000'+end).slice(-3)}`;
if (start === end) range = ('000' + start).slice(-3);
ranges.push(range);
start = end = id;
}
})
return ranges;
}
function getUntestedPr() {
let ids = [];
for (let pid in CardInfo) {
if (pid <= 1762) continue;
let card = CardInfo[pid];
if (card.pid !== card.cid) continue;
if (/^PR-/.test(card.wxid)) ids.push(card.wxid);
}
return ids;
}
function getNewCardNames() {
let names = [];
for (let pid in CardInfo) {
if (pid <= 1762) continue;
let card = CardInfo[pid];
if (card.pid !== card.cid) continue;
names.push(card.name_zh_CN);
}
return names;
}
function down(content, filename = 'down.txt') {
if (typeof content === 'object') {
content = JSON.stringify(content, null, ' ')
}
let blob = new Blob([content], {
type: 'application/octet-stream'
})
let url = URL.createObjectURL(blob)
let link = document.createElement('a')
link.href = url
link.download = filename
link.click()
}
</script>
<script src="util.js"></script>
<script src="random.min.js"></script>
<script src="Callback.js"></script>
<script src="Game.js"></script>
<script src="Phase.js"></script>
<script src="IO.js"></script>
<script src="Player.js"></script>
<script src="Card.js"></script>
<script src="Zone.js"></script>
<script src="CardInfo.js"></script>
<script src="Timming.js"></script>
<script src="Mask.js"></script>
<script src="ConstEffect.js"></script>
<script src="ConstEffectManager.js"></script>
<script src="Effect.js"></script>
<script src="EffectManager.js"></script>
<script src="FakeSocket.js"></script>
<script src="Client.js"></script>
<script src="Room.js"></script>
<script src="RoomManager.js"></script>
<script src="test.js"></script>
<script src="testHelper.js"></script>
</head>
<body>
<div class="float">
<div>
<span> p1 </span>
<select id="host-decks"></select>
</div>
<div>
<span> p2 </span>
<select id="ghost-decks"></select>
</div>
<div>
<button onclick="startBattle();">Oben!</button>
</div>
<div> ---- Helper Action ---- </div>
<div>
<button onclick="upgrade()">upgrade</button>
<button onclick="game.turnPlayer.draw(5)">draw</button>
<button onclick="game.turnPlayer.enerCharge(10);">ener charge</button>
</div>
<div>
<input id="card-name" type="text" placeholder="WX01-100">
</div>
<button onclick="addToHand()">add card</button>
<button onclick="addToLifeCloth()">add to life cloth</button>
</div>
</div>
<iframe class="float" src="../webxoss-client/DeckEditor/" width="1090px" height="690px" scrolling="no" frameborder="no" border="0"></iframe>
</body>
<style type="text/css">
a {
outline: none;
color: inherit;
text-decoration: none;
-webkit-tap-highlight-color: transparent;
}
.float {
float: left;
}
</style>
</html>

View file

@ -191,37 +191,10 @@ function down (content, filename = 'down.txt') {
<script src="Client.js"></script>
<script src="Room.js"></script>
<script src="RoomManager.js"></script>
<script src="testHelper.js"></script>
<!-- <script src="test.js"></script> -->
<script src="test.js"></script>
</head>
<body>
<div>
<a href="#" onclick="window.open('./webxoss-client/DeckEditor/')">Deck:</a>
<select id="deck-select"></select>
</div>
<div>
<button onclick="newClient();">new client</button>
<button onclick="oben();">oben!</button>
<button onclick="updateBattle();">update battle</button>
</div>
<div>
<button onclick="upgrade()">upgrade</button>
<button onclick="game.turnPlayer.draw(5)">draw</button>
<button onclick="game.turnPlayer.enerCharge(10);">ener charge</button>
</div>
<div>
<input id="card-name" type="text" placeholder="WX01-100"></div>
<button onclick="addToHand()">add card</button>
<button onclick="addToLifeCloth()">add to life cloth</button>
</div>
<style type="text/css">
a {
outline: none;
color: inherit;
text-decoration: none;
-webkit-tap-highlight-color: transparent;
}
</style>
<button onclick="newClient();">newClient</button>
</body>
</html>

View file

@ -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);
}
});
});
window.onload = function() {
initDeckSelect();
}