1
0
Fork 0

clean up code

This commit is contained in:
deardrops 2017-04-10 21:15:52 +08:00 committed by WEBXOSS
parent 1d8fe4e4c6
commit 345c1750ab
6 changed files with 358 additions and 599 deletions

View file

@ -2274,46 +2274,6 @@ Player.prototype.setCoin = function(count) {
} }
}; };
// For test:
Player.prototype.matchCard = function (arg) {
var cid = 0;
for (var i = 0; i < this.game.cards.length; i++) {
var card = this.game.cards[i];
var info = CardInfo[card.cid];
var matched = info.name === arg ||
info.name_zh_CN === arg ||
info.cid === arg ||
info.wxid === arg;
if (matched) {
cid = card.cid;
break;
}
}
if (!cid) return null;
var cards = concat(this.mainDeck.cards,this.trashZone.cards,this.enerZone.cards,this.lifeClothZone.cards);
for (var i = 0; i < cards.length; i++) {
var card = cards[i];
if (card.cid === cid) {
return card;
}
}
return null;
};
Player.prototype.getCard = function (arg) {
var card = this.matchCard(arg);
if (!card) return null;
card.moveTo(this.handZone);
return card;
};
Player.prototype.putCardToLifeCloth = function (arg) {
var card = this.matchCard(arg);
if (!card) return null;
card.moveTo(this.lifeClothZone);
return card;
};
Player.prototype.getInfectedZones = function() { Player.prototype.getInfectedZones = function() {
return this.signiZones.filter(function (zone) { return this.signiZones.filter(function (zone) {
return zone.virus; return zone.virus;

173
dango.js Normal file
View file

@ -0,0 +1,173 @@
// 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()
}

View file

@ -6,179 +6,6 @@
<title>Server</title> <title>Server</title>
<script> <script>
var global = window; 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>
<script src="util.js"></script> <script src="util.js"></script>
<script src="random.min.js"></script> <script src="random.min.js"></script>
@ -200,8 +27,8 @@
<script src="Client.js"></script> <script src="Client.js"></script>
<script src="Room.js"></script> <script src="Room.js"></script>
<script src="RoomManager.js"></script> <script src="RoomManager.js"></script>
<script src="test.js"></script>
<script src="testHelper.js"></script> <script src="testHelper.js"></script>
<script src="dango.js"></script>
</head> </head>
<body> <body>

200
test.html
View file

@ -1,200 +0,0 @@
<!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>
</head>
<body>
<button onclick="newClient();">newClient</button>
</body>
</html>

33
test.js
View file

@ -1,6 +1,7 @@
'use strict'; 'use strict';
if (!global.window) { // node env only
global.Random = require("random-js"); global.Random = require("random-js");
require("./util.js"); require("./util.js");
require("./Callback.js"); require("./Callback.js");
@ -20,28 +21,7 @@ if (!global.window) {
require("./Client.js"); require("./Client.js");
require("./Room.js"); require("./Room.js");
require("./RoomManager.js"); require("./RoomManager.js");
}
var io;
if (global.window) {
io = {
on: function (name,handler) {
io._handler = handler;
},
use: function () {}
};
global.window.newClient = function () {
var win = window.open('./webxoss-client/?local=true');
win.addEventListener('load',function () {
var socket = new FakeSocket(win);
win.addEventListener('unload',function () {
socket._doEmit('disconnect');
});
win.document.title = 'Client' + socket.id;
io._handler(socket);
});
}
} else {
var noStaticServe = process.argv.slice(2).some(function(arg) { var noStaticServe = process.argv.slice(2).some(function(arg) {
return arg === 'no-static-server'; return arg === 'no-static-server';
}); });
@ -67,6 +47,7 @@ if (global.window) {
} }
}); });
server.listen(port); server.listen(port);
console.log('server listen on 127.0.0.1:' + port);
} }
// game server // game server
var getArg = function(key) { var getArg = function(key) {
@ -89,12 +70,11 @@ if (global.window) {
} else { } else {
gameServer = require('http').createServer(); gameServer = require('http').createServer();
} }
io = require('socket.io')(gameServer,{ var io = require('socket.io')(gameServer, {
pingTimeout: 30000, pingTimeout: 30000,
maxHttpBufferSize: 1024 * 1024, maxHttpBufferSize: 1024 * 1024,
}); });
gameServer.listen(2015); gameServer.listen(2015);
}
var cfg = { var cfg = {
MAX_ROOMS: 100, MAX_ROOMS: 100,
@ -146,9 +126,6 @@ io.use(function (socket,next) {
next(); next();
}); });
io.on('connect', function(socket) { io.on('connect', function(socket) {
if (global.window) {
return roomManager.createClient(socket);
}
var req = socket.request; var req = socket.request;
if (req.connection.destroyed) { if (req.connection.destroyed) {
console.log('req.connection.destroyed'); console.log('req.connection.destroyed');
@ -187,6 +164,4 @@ io.on('connect',function (socket) {
}); });
}); });
if (!global.window) {
exports.roomManager = roomManager; exports.roomManager = roomManager;
}

View file

@ -2,10 +2,39 @@
// browser only // browser only
var $ = document.getElementById.bind(document); // server
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;
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;
}
next();
});
io.on('connect',function (socket) {
return roomManager.createClient(socket);
});
// client
var noBGM = true; var noBGM = true;
function disableAudio(doc) { function disableAudio(doc) {
// disable BGM // disable BGM
if (!noBGM) { if (!noBGM) {
@ -20,7 +49,6 @@ function disableAudio(doc) {
sound.click(); sound.click();
} }
} }
function initClient(win) { function initClient(win) {
var doc = win.document; var doc = win.document;
var socket = new FakeSocket(win); var socket = new FakeSocket(win);
@ -58,7 +86,6 @@ function startBattle() {
initClient(win); initClient(win);
}); });
} }
function oben() { function oben() {
if (sockets.length !== 2) { if (sockets.length !== 2) {
log('two client needed.'); log('two client needed.');
@ -100,7 +127,6 @@ function handleBattle() {
log('Handle game successfully.'); log('Handle game successfully.');
log('Now you can use helper function.'); log('Now you can use helper function.');
} }
function skipDiscards() { function skipDiscards() {
Player.prototype.redrawAsyn = function() { Player.prototype.redrawAsyn = function() {
return new Callback.immediately(); return new Callback.immediately();
@ -176,13 +202,14 @@ function matchCard(arg) {
return null; return null;
} }
var $ = document.getElementById.bind(document);
var zones = [ var zones = [
'handZone', 'handZone',
'enerZone', 'enerZone',
'trashZone', 'trashZone',
'lifeClothZone', 'lifeClothZone',
]; ];
function addTo(zone) { function addTo(zone) {
if (zones.indexOf(zone) === -1) { if (zones.indexOf(zone) === -1) {
log('no such zone: ' + zone); log('no such zone: ' + zone);
@ -202,7 +229,6 @@ function addTo(zone) {
log('card\'s wxid / pid / cid needed.'); log('card\'s wxid / pid / cid needed.');
} }
} }
function resetLrigDeck() { function resetLrigDeck() {
var p = selectPlayer(); var p = selectPlayer();
game.moveCards(p.lrigTrashZone.cards, p.lrigDeck); game.moveCards(p.lrigTrashZone.cards, p.lrigDeck);
@ -258,7 +284,6 @@ function changeLanguage() {
log('set language to ' + lang + '.'); log('set language to ' + lang + '.');
location.reload(); location.reload();
} }
function handleDeckEditor() { function handleDeckEditor() {
var iFrame = $('deck-editor'); var iFrame = $('deck-editor');
@ -277,7 +302,6 @@ function handleDeckEditor() {
} }
}); });
} }
function enableButtons() { function enableButtons() {
var buttons = document.getElementsByTagName('button'); var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) { for (var i = 0; i < buttons.length; i++) {
@ -286,7 +310,6 @@ function enableButtons() {
} }
} }
} }
function disableButtons() { function disableButtons() {
var buttons = document.getElementsByTagName('button'); var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) { for (var i = 0; i < buttons.length; i++) {
@ -295,6 +318,7 @@ function disableButtons() {
} }
} }
} }
window.onload = function() { window.onload = function() {
$('select-language').value = localStorage.getItem('language'); $('select-language').value = localStorage.getItem('language');
updateDeckSelect(); updateDeckSelect();