Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
crainbows committed Aug 17, 2018
2 parents cfd3a05 + ecdf182 commit 668fd3e
Show file tree
Hide file tree
Showing 23 changed files with 52,804 additions and 5,819 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
env: {
browser: true,
node: true,
'jest/globals': true
},
Expand All @@ -11,5 +12,10 @@ module.exports = {
],
rules: {
'no-console': ['warn']
}
},
parser: "babel-eslint",
rules: {
'no-console': ['warn'],
"indent": ["error", 2]
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
mix-manifest.json

# Compiled source #
###################
Expand Down
261 changes: 133 additions & 128 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ var busboy = require('connect-busboy');

// Used to generate session keys
var generateKey = function () {
var sha = crypto.createHash('sha256');
sha.update(Math.random().toString());
return sha.digest('hex');
var sha = crypto.createHash('sha256');
sha.update(Math.random().toString());
return sha.digest('hex');
};

var mostRecentImageData = null,
mostRecentRawImagePath = null,
UPLOADS_DIR = path.join(__dirname, '/public/uploads/'),
GENERATED_IMAGE_PATH = path.join(UPLOADS_DIR + 'generatedMap.png');
mostRecentRawImagePath = null,
UPLOADS_DIR = path.join(__dirname, '/public/uploads/'),
GENERATED_IMAGE_PATH = path.join(UPLOADS_DIR + 'generatedMap.png');


app.use(busboy());
Expand Down Expand Up @@ -54,203 +54,208 @@ app.use(session({secret: generateKey()}));
// Routes
// TODO: Move interior logic somewhere else
app.get('/', function (req, res) {
res.render('player', {dm: false, title: 'Dungeon Revealer'});
res.render('player', {dm: false, title: 'Dungeon Revealer'});
});

app.get('/dm', function (req, res) {
/* console.log( req.headers ); */
if( req.headers['host'] == 'localhost:3000' ){
res.render('dm', {dm: true, title: 'Dungeon Revealer DM Console'});
}
else
{
res.sendStatus(403);
}
/* console.log( req.headers ); */
if( req.headers['host'] == 'localhost:3000' ){
res.render('dm', {dm: true, title: 'Dungeon Revealer DM Console'});
}
else
{
res.sendStatus(403);
}
});


app.get('/map', function (req, res) {
res.sendFile(GENERATED_IMAGE_PATH);
res.sendFile(GENERATED_IMAGE_PATH);
});


app.get('/quit', function (req, res) {
if( req.headers['host'] == 'localhost:3000' ){
res.render('quit');
setTimeout(function () {
process.exit( );
}, 1000);
if( req.headers['host'] == 'localhost:3000' ){
res.render('quit');
setTimeout(function () {
process.exit( );
}, 1000);

}
else
{
res.sendStatus(403);
}
}
else
{
res.sendStatus(403);
}
});

app.get('/dm/map', function (req, res) {

var mapSent = false;
var mapSent = false;

if( req.headers['host'] == 'localhost:3000' ){
if( req.headers['host'] == 'localhost:3000' ){

if (mostRecentRawImagePath) {
res.sendFile(mostRecentRawImagePath);
if (mostRecentRawImagePath) {
res.sendFile(mostRecentRawImagePath);
mapSent = true;
} else {
console.log(UPLOADS_DIR);
// Look in the dir for a file named map.* and return the first one found
// Because we are deleting the previous files on upload this logic is mostly useless now
var files = fs.readdirSync(UPLOADS_DIR);
files.filter(function(file) {
return file.indexOf('map.') > -1;
}).forEach(function(file) {
var filePath = path.join(UPLOADS_DIR + file);
if (!mapSent) {
mapSent = true;
} else {
console.log(UPLOADS_DIR);
// Look in the dir for a file named map.* and return the first one found
// Because we are deleting the previous files on upload this logic is mostly useless now
var files = fs.readdirSync(UPLOADS_DIR);
files.filter(function(file) {
return file.indexOf('map.') > -1;
}).forEach(function(file) {
var filePath = path.join(UPLOADS_DIR + file);
if (!mapSent) {
mapSent = true;
mostRecentRawImagePath = filePath;
res.sendFile(mostRecentRawImagePath);
}
});
}
mostRecentRawImagePath = filePath;
res.sendFile(mostRecentRawImagePath);
}
});
}
}

if (!mapSent) {
res.sendStatus(403);
}
if (!mapSent) {
res.sendStatus(403);
}
});


// For DM map uploads. These are the raw images without any fog of war.
app.post('/upload', function (req, res) {

req.pipe(req.busboy);
req.pipe(req.busboy);

req.busboy.on('file', function (fieldname, file, filename) {
req.busboy.on('file', function (fieldname, file, filename) {

var fileExtension = filename.split('.').pop(),
uploadedImageSavePath = path.join(UPLOADS_DIR + 'map.' + fileExtension),
fstream;
var fileExtension = filename.split('.').pop(),
uploadedImageSavePath = path.join(UPLOADS_DIR + 'map.' + fileExtension),
fstream;

deleteExistingMapFilesSync();
deleteExistingMapFilesSync();

fstream = fs.createWriteStream(uploadedImageSavePath);
fstream = fs.createWriteStream(uploadedImageSavePath);

file.pipe(fstream);
fstream.on('close', function () {
console.log('map uploaded');
mostRecentRawImagePath = uploadedImageSavePath;
res.sendStatus(200);
});
// should do something for a failure as well
file.pipe(fstream);
fstream.on('close', function () {
console.log('map uploaded');
mostRecentRawImagePath = uploadedImageSavePath;
res.sendStatus(200);
});
// should do something for a failure as well
});

});

// For the DM sending out fogged maps to be distributed to players
app.post('/send', function (req, res) {
if( req.headers['host'] == 'localhost:3000' ){
var imageDataString = req.body.imageData;

if (imageDataString) {
var imageData = decodeBase64Image(imageDataString).data;

fs.writeFile(GENERATED_IMAGE_PATH, imageData, function (err) {
if(err) throw err;
console.log('sent map saved');
});

// Cache the data for future requests
mostRecentImageData = imageDataString;

// ACK for DM
res.json({
'success': true,
'responseText': 'Image successfully uploaded'
});
var imageData = decodeBase64Image(imageDataString).data;

fs.writeFile(GENERATED_IMAGE_PATH, imageData, function (err) {
if(err) throw err;
console.log('sent map saved');
});

// Send the map update to players
io.emit('map update', {
'imageData': imageDataString
});
// Cache the data for future requests
mostRecentImageData = imageDataString;

// ACK for DM
res.json({
'success': true,
'responseText': 'Image successfully uploaded'
});

// Send the map update to players
io.emit('map update', {
'imageData': imageDataString
});
} else {
res.json({
'success': false,
'responseText': 'Image not uploaded successfully'
});
res.json({
'success': false,
'responseText': 'Image not uploaded successfully'
});
}

}
else
{
res.sendStatus(403);
}
});


// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
app.use(function (err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res) {
console.log(err);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
console.log(err);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

io.on('connection', function(socket) {
console.log('a user connected');
console.log('a user connected');

if (mostRecentImageData) {
console.log('sending current map to newly connected user');
socket.emit('map update', {
'imageData': mostRecentImageData
});
}
if (mostRecentImageData) {
console.log('sending current map to newly connected user');
socket.emit('map update', {
'imageData': mostRecentImageData
});
}

socket.on('disconnect', function() {
console.log('a user disconnected');
});
socket.on('disconnect', function() {
console.log('a user disconnected');
});
});

function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+/]+);base64,(.+)$/),
response = {};
var matches = dataString.match(/^data:([A-Za-z-+/]+);base64,(.+)$/),
response = {};

if (matches.length !== 3) {
return new Error('Invalid input string');
}
if (matches.length !== 3) {
return new Error('Invalid input string');
}

response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');

return response;
return response;
}

function deleteExistingMapFilesSync() {
var files = fs.readdirSync(UPLOADS_DIR);
files.filter(function(file) {
return file.indexOf('map.') > -1;
}).forEach(function(file) {
var filePath = path.join(UPLOADS_DIR + file);
fs.unlinkSync(filePath);
});
var files = fs.readdirSync(UPLOADS_DIR);
files.filter(function(file) {
return file.indexOf('map.') > -1;
}).forEach(function(file) {
var filePath = path.join(UPLOADS_DIR + file);
fs.unlinkSync(filePath);
});
}

module.exports = app;
Loading

0 comments on commit 668fd3e

Please sign in to comment.