Skip to content

Commit

Permalink
完善log
Browse files Browse the repository at this point in the history
  • Loading branch information
guanyuxin committed Jan 6, 2016
1 parent 74cc657 commit e3af19d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
# node.js
#
node_modules/
logs/
npm-debug.log
29 changes: 28 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@ var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var Game = require('./game/game.js');
var log4js = require('log4js');

var arg = process.argv.splice(2);

server.listen(arg[0] || 8030);

log4js.configure({
appenders: [{
type: 'console'
}, {
type: 'file',
filename: 'logs/access.log',
maxLogSize: 1024,
backups: 3,
category: 'access'
}, {
type: 'file',
filename: 'logs/game.log',
maxLogSize: 1024,
backups: 3,
category: 'game'
}]
});

var loggerAccess = log4js.getLogger('access');
loggerAccess.setLevel('INFO');
var loggerGame = log4js.getLogger('game');
loggerGame.setLevel('INFO');

app.use('/static', express.static('static'));

app.use(log4js.connectLogger(loggerAccess, {
level:log4js.levels.INFO
}));
//游戏地址
app.get('/', function (req, res) {
res.sendFile(__dirname + '/static/index.html');
Expand All @@ -20,7 +47,7 @@ app.get('/admin', function (req, res) {
res.sendFile(__dirname + '/static/admin.html');
});

var game = new Game(arg[1] || 'admin');
var game = new Game(arg[1] || 'admin', loggerGame);

io.on('connection', function (socket, data) {
game.addCon(socket);
Expand Down
12 changes: 9 additions & 3 deletions game/con.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ var Con = function (socket, game) {
this.name = '无名小卒';
this.joinTime = new Date().getTime();
this.ip = socket.handshake.address;

this.kill = 0;
this.death = 0;
this.highestKill = 0;

if (banedip[this.ip]) {
this.banned = true;
} else {
this.banned = false;
}

//初始化数据
var bodiesData = [];
for (var i = 0; i < this.game.bodies.length; i++) {
bodiesData.push(this.game.bodies[i].getData());
}

//初始化数据
socket.emit("init", {
props: game.props,
map: game.map.getData(),
bodies: bodiesData
});

//接收初始化数据
socket.on('init', function (data) {
if (data.code != undefined) {
Expand Down Expand Up @@ -66,7 +72,7 @@ var Con = function (socket, game) {
if (data.p1 && _this.p1 && !_this.p1.dieing && !_this.p1.dead) {return}
if (data.p2 && _this.p2 && !_this.p2.dieing && !_this.p2.dead) {return}
_this.name = data.userName.replace(/[<>]/g, '').substring(0, 8);
var u = game.addUser(_this.name);
var u = game.addUser(_this);
if (data.p1) {
_this.p1 = u;
} else {
Expand Down
20 changes: 14 additions & 6 deletions game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function eatItem (a, b, game) {
}
}

var Game = function (adminCode) {
var Game = function (adminCode, logger) {
this.props = {
w: 1100,
h: 600,
Expand All @@ -166,6 +166,7 @@ var Game = function (adminCode) {
tileH: 8
}
this.adminCode = adminCode;
this.logger = logger;
this.users = [];
this.cons = [];
this.items = [];
Expand All @@ -179,8 +180,8 @@ var Game = function (adminCode) {
}, 17);
}
//增加玩家
Game.prototype.addUser = function (name) {
var u = new User(this, name);
Game.prototype.addUser = function (con) {
var u = new User(this, con);
var place = this.map.born();
u.x = place.x;
u.y = place.y + this.props.blockHeight/2;
Expand Down Expand Up @@ -253,8 +254,9 @@ Game.prototype.checkShot = function (u) {
}
Game.prototype.award = function (u) {
u.score++;
if (u.score == 5 && !u.dead) {
this.announce('winner', u.getData());
u.con.kill++;
if (u.con.kill > u.con.highestKill) {
u.con.highestKill = u.con.kill;
}
}
Game.prototype.addMine = function (user) {
Expand Down Expand Up @@ -288,7 +290,10 @@ Game.prototype.addCon = function (socket) {
Game.prototype.removeCon = function (con) {
for (var i = 0; i < this.cons.length; i++) {
if (this.cons[i] == con) {
console.log('socket <' + con.name + '> ' + con.ip + ' ['+con.joinTime+':'+con.leaveTime+']');
this.logger.info('User <' + con.name + '> '
+ con.ip
+ ' ['+con.joinTime+':'+con.leaveTime+':'+Math.floor((con.joinTime/con.leaveTime)/60)+']'
+ ' ['+con.kill+','+con.death+','+con.highestKill+']');
this.cons.splice(i, 1);
return;
}
Expand Down Expand Up @@ -352,6 +357,9 @@ Game.prototype.clean = function () {
} else {
this.users.splice(i, 1);
this.bodies.push(user);
if (this.bodies.length > 100) {
this.bodies = this.bodies.slice(0, 50);
}
}
};
}
Expand Down
7 changes: 4 additions & 3 deletions game/user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

var userCount = 0;
var User = function (game, name) {
var User = function (game, con) {
this.id = userCount++;
this.game = game;
this.name = name;
this.con = con;
this.name = con.name;
this.onFloor = false;
this.onPilla = false;
this.nearPilla = false;
Expand Down Expand Up @@ -234,6 +235,7 @@ User.prototype.killed = function (action, byUser) {
this.killer = byUser && byUser.id;
this.dieing = true;
this.killedBy = action;
this.con.death++;

if (action == 'power') {
this.vy = 10;
Expand All @@ -245,7 +247,6 @@ User.prototype.killed = function (action, byUser) {
} else if (action == 'mine') {
this.vy = 10;
} else if (action == 'bomb') {
this.vy = 8;
} else {
this.killer = this.lastTouch;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"cookie-parser": ">=0.1.2",
"ejs": ">=1.0.0",
"express": ">=4.0",
"log4js": "^0.6.29",
"serve-static": ">=0.0.0",
"socket.io": "^1.3.7"
}
Expand Down
2 changes: 1 addition & 1 deletion static/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script src="/static/js/zepto.js"></script>
<style>
.btn {
display: block;;
display: block;
border: 1px solid #004;
padding: 10px 20px;
border-radius: 5px;
Expand Down

0 comments on commit e3af19d

Please sign in to comment.