1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| "use strict";
const app = require('express')(); const http = require('http').Server(app); const io = require('socket.io')(http); const _ = require('lodash');
const Snake = require('./snake'); const Apple = require('./apple'); var ipaddr = require('ipaddr.js');
let autoId = 0; const GRID_SIZE = 40; let players = []; let apples = [];
app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); });
http.listen(3000, () => { console.log('listening on *:3000'); });
io.on('connection', (client) => { let player; let id;
client.on('auth', (opts, cb) => { id = ++autoId; player = new Snake(_.assign({ id, dir: 'right', gridSize: GRID_SIZE, snakes: players, apples }, opts)); players.push(player); cb({ id: autoId }); });
client.on('key', (key) => { if(player) { player.changeDirection(key); } });
client.on('disconnect', () => { _.remove(players, player); });
client.on('admin', (msg, cb) => { var ipString = client.handshake.headers['x-forwarded-for'] || client.request.connection.remoteAddress; if (ipaddr.IPv4.isValid(ipString)) {
} else if (ipaddr.IPv6.isValid(ipString)) { var ip = ipaddr.IPv6.parse(ipString); if (ip.isIPv4MappedAddress()) { ipString = ip.toIPv4Address().toString(); } else { } } else { }
console.log(ipString); if(ipString == "127.0.0.1") { cb("FLAG{xxxxxxxxxx}"); } });
});
for(var i=0; i < 3; i++) { apples.push(new Apple({ gridSize: GRID_SIZE, snakes: players, apples })); }
setInterval(() => { players.forEach((p) => { p.move(); }); io.emit('state', { players: players.map((p) => ({ x: p.x, y: p.y , id: p.id, nickname: p.nickname, points: p.points, tail: p.tail })), apples: apples.map((a) => ({ x: a.x, y: a.y })) }); }, 100);
|