opt-cc/server/tools/signature-tool.js

269 lines
9.1 KiB
JavaScript
Raw Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-05-10 11:04:06 +02:00
// modules used for graphic manipulation
const jimp = require('jimp');
const imagemin = require('imagemin');
const imageminpngquant = require('imagemin-pngquant');
const sha1 = require('node-sha1');
2017-05-10 11:04:06 +02:00
const async = require('async');
// Mongoose Model using mongoDB
const UserModel = require('../models/user');
const AwardingModel = require('../models/awarding');
const RankModel = require('../models/rank');
// http error code table
const codes = require('../routes/http-codes');
// standard input/output file extension
const fileExt = '.png';
2018-04-01 16:01:43 +02:00
const resourceDir = require('../middleware/resource-location').resourceLocation().concat('/');
2017-05-10 11:04:06 +02:00
let createSignature = (userId, res, next) => {
let loadedImage;
let user;
if (!String(userId).match(/^[0-9a-fA-F]{24}$/)) {
let error = new Error('no valid ID ' + userId);
error.status = codes.wrongdatatyperequest;
return next(error);
}
UserModel.findById(userId, ['username', 'rankLvl', 'squadId'])
2018-02-26 09:04:27 +01:00
.populate('squadId', ['name', 'fraction'])
.exec()
.then((resUser) => {
user = resUser;
let platePath;
if (resUser && resUser.squadId && resUser.squadId.fraction === 'BLUFOR') {
platePath = __dirname + '/backplate/blufor' + fileExt;
} else if (resUser && resUser.squadId && resUser.squadId.fraction === 'OPFOR') {
platePath = __dirname + '/backplate/opfor' + fileExt;
}
2017-05-10 11:04:06 +02:00
2018-02-26 09:04:27 +01:00
// kill process on undefined platePath
if (!platePath) {
throw new Error('Fraction not defined for user with id ' + userId);
}
2018-03-12 09:26:44 +01:00
return jimp.read(platePath);
2018-02-26 09:04:27 +01:00
})
.then((image) => {
loadedImage = image;
}).then(() => {
2020-03-08 13:46:55 +01:00
return jimp.loadFont(jimp.FONT_SANS_16_BLACK);
2018-02-26 09:04:27 +01:00
})
.then((font) => {
2020-03-08 13:46:55 +01:00
loadedImage.print(font, 118, 22, user.username);
2018-02-26 09:04:27 +01:00
})
.then(() => {
return jimp.loadFont(__dirname + '/font/DEJAVU_SANS_13.fnt');
})
.then((font) => {
2020-03-08 13:46:55 +01:00
loadedImage.print(font, 118, 49, user.squadId.name);
2018-02-26 09:04:27 +01:00
return font;
})
.then((font) => {
let rankH;
let rankW;
let rankX;
let rankY;
2017-05-10 11:04:06 +02:00
2018-02-26 09:04:27 +01:00
RankModel.findOne({'level': user.rankLvl, 'fraction': user.squadId.fraction}, (err, result) => {
if (err) {
2018-03-12 09:26:44 +01:00
return next(err);
2018-02-26 09:04:27 +01:00
}
2017-05-10 11:04:06 +02:00
2018-02-26 09:04:27 +01:00
if (result) {
if (user.squadId.fraction === 'BLUFOR') {
2020-03-08 13:46:55 +01:00
rankW = 33;
rankH = 80;
rankX = 38;
rankY = 20;
2018-02-26 09:04:27 +01:00
} else {
2020-03-08 13:46:55 +01:00
rankW = 38;
rankH = 60;
rankX = 35;
rankY = 36;
2018-02-26 09:04:27 +01:00
}
2018-02-26 09:04:27 +01:00
jimp.read(resourceDir + 'rank/' + result._id + fileExt)
.then((rankImage) => {
rankImage.resize(rankW, rankH);
loadedImage
2020-03-08 13:46:55 +01:00
.print(font, 118, 69, result.name)
2018-03-12 09:26:44 +01:00
.composite(rankImage, rankX, rankY);
2018-02-26 09:04:27 +01:00
})
.then(() => {
addDecorationsAndSave(userId, loadedImage, res, next);
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
} else {
// user has not any assignable rank in his fraction at this point,
// e.g. assigned rank has been deleted or switched fraction so rankLvl is not defined
addDecorationsAndSave(userId, loadedImage, res, next);
}
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
})
.catch((err) => {
next(err);
2018-03-12 09:26:44 +01:00
});
2017-05-10 11:04:06 +02:00
};
/**
* query decorations according to user id and
* add all graphics into given image
*
* @param {number} userId
* @param {Object} loadedImage - background image
* @param {function} res
* @param {function} next
2017-05-10 11:04:06 +02:00
*/
let addDecorationsAndSave = (userId, loadedImage, res, next) => {
const medalW = 21;
const medalH = 40;
const ribbonW = 53;
const ribbonH = 15;
2020-03-08 13:46:55 +01:00
let medalPx = 690;
let medalPy = 9;
let ribbonPx = 658;
let ribbonPy = 107;
2017-05-10 11:04:06 +02:00
AwardingModel.find({
'userId': userId,
2018-03-12 09:26:44 +01:00
'confirmed': 1,
}, ['decorationId', 'date']).populate('decorationId', ['isMedal', 'fraction'])
2018-02-26 09:04:27 +01:00
.exec((err, awardings) => {
if (err) {
return next(err);
}
if (awardings.length > 0) {
2018-05-04 20:37:35 +02:00
awardings.sort((a1, a2) => sortAwardingsForSignature(a1, a2));
2018-02-26 09:04:27 +01:00
// use synchronized call to keep correct order of decorations
async.eachSeries(awardings, (award, callback) => {
2018-03-12 09:59:43 +01:00
jimp.read(resourceDir + 'decoration/' + award.decorationId._id + fileExt)
.then((decorationImage) => {
if (award.decorationId.isMedal) {
decorationImage.resize(medalW, medalH);
loadedImage.composite(decorationImage, medalPx, medalPy);
2020-03-08 13:46:55 +01:00
if (medalPy === 9) {
2018-03-12 09:59:43 +01:00
medalPx = medalPx - 1 - medalW;
} else {
medalPx = medalPx + 1 + medalW;
}
if (medalPx <= 300) {
2020-03-08 13:46:55 +01:00
medalPx = medalPx + 1 + medalW;
2018-03-12 09:59:43 +01:00
medalPy = medalPy + 3 + medalH;
}
} else {
decorationImage.resize(ribbonW, ribbonH);
loadedImage.composite(decorationImage, ribbonPx, ribbonPy);
ribbonPx = ribbonPx - 2 - ribbonW;
2020-03-08 13:46:55 +01:00
if (ribbonPx <= 200) {
2018-03-12 09:59:43 +01:00
ribbonPy = ribbonPy - 3 - ribbonH;
2020-03-08 13:46:55 +01:00
ribbonPx = 657;
2018-03-12 09:59:43 +01:00
}
}
callback();
});
2018-02-26 09:04:27 +01:00
}, (err) => {
if (err) {
throw err;
}
compareImagesAndSave(loadedImage, userId, res, next);
});
} else {
compareImagesAndSave(loadedImage, userId, res, next);
}
}
2018-03-12 09:26:44 +01:00
);
2017-05-10 11:04:06 +02:00
};
let compareImagesAndSave = (generatedImage, userId, res, next) => {
return jimp.read(resourceDir + 'signature/big/' + userId + fileExt)
2018-02-26 09:04:27 +01:00
.then((oldImage) => {
// compare hashes of image map to recognize difference
const sig1 = sha1(generatedImage.bitmap.data);
const sig2 = sha1(oldImage.bitmap.data);
2018-02-26 09:04:27 +01:00
if (sig1 !== sig2) {
2018-03-12 09:26:44 +01:00
saveJimpImageAndCompress(generatedImage, userId, res, next);
2018-02-26 09:04:27 +01:00
} else {
res.locals.items = {status: 'nothing to do'};
next();
}
})
.catch((err) => {
saveJimpImageAndCompress(generatedImage, userId, res, next);
2018-03-12 09:26:44 +01:00
});
2017-05-10 11:04:06 +02:00
};
/**
* Write Jimp image to file system
* Gets minified after 3 seconds timeout
*
* @param {Object} image - Jimp image
* @param {number} userId - user id of signature owner
* @param {function} res - express response object
* @param {function} next - express next-function
2017-05-10 11:04:06 +02:00
*/
let saveJimpImageAndCompress = (image, userId, res, next) => {
image.write(resourceDir + 'signature/big/' + userId + fileExt);
setTimeout(() => {
imagemin([resourceDir + 'signature/big/' + userId + fileExt], resourceDir + 'signature/', {
plugins: [
2018-03-12 09:26:44 +01:00
imageminpngquant({quality: '65-80'}),
],
2017-05-10 11:04:06 +02:00
}).then((files) => {
res.locals.items = {status: 'success'};
return next();
2017-06-10 22:07:32 +02:00
}).catch((error) => {
2018-03-12 09:26:44 +01:00
console.log(error);
});
2017-05-10 11:04:06 +02:00
}, 3000);
};
2018-05-04 20:37:35 +02:00
const sortAwardingsForSignature = (a1, a2) => {
if (!a1.decorationId.isMedal && !a2.decorationId.isMedal) {
if (a1.decorationId.fraction === a2.decorationId.fraction) {
if (a1.date !== a2.date) {
if (a1.date < a2.date) {
return -1;
}
if (a1.date > a2.date) {
return 1;
}
}
return 0;
} else {
if (a1.decorationId.fraction === 'GLOBAL' && a2.decorationId.fraction !== 'GLOBAL') {
return -1;
}
if (a2.decorationId.fraction === 'GLOBAL' && a1.decorationId.fraction !== 'GLOBAL') {
return 1;
}
}
}
if (a1.decorationId.isMedal !== a2.decorationId.isMedal) {
if (a1.decorationId.isMedal && !a2.decorationId.isMedal) {
return 1;
}
if (!a1.decorationId.isMedal && a2.decorationId.isMedal) {
return -1;
}
}
if (a1.decorationId.isMedal && a2.decorationId.isMedal) {
if (a1.date !== a2.date) {
if (a1.date < a2.date) {
return -1;
}
if (a1.date > a2.date) {
return 1;
}
}
return 0;
}
};
2017-05-10 11:04:06 +02:00
module.exports = createSignature;