diff --git a/api/signature-tool/signature-tool.js b/api/signature-tool/signature-tool.js index d6655ff..3a249eb 100644 --- a/api/signature-tool/signature-tool.js +++ b/api/signature-tool/signature-tool.js @@ -126,30 +126,78 @@ let addDecorationsAndSave = (userId, loadedImage, res, next) => { let ribbonPx = 598; let ribbonPy = 95; - AwardingModel.find({'userId': userId, 'confirmed': 1}, ['decorationId', 'date'], - {sort: {date: 'asc'}}).populate('decorationId', ['isMedal']) + AwardingModel.find({ + 'userId': userId, + 'confirmed': 1 + }, ['decorationId', 'date']).populate('decorationId', ['isMedal', 'fraction']) .exec((err, awardings) => { if (err) { return next(err); } if (awardings.length > 0) { + + //TODO: simplify this sorting hell + awardings.sort((a1, a2) => { + if (!a1.decorationId.isMedal && !a2.decorationId.isMedal) { + if (a1.decorationId.fraction === 'GLOBAL' && a2.decorationId.fraction === 'GLOBAL') { + 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; + } + }); + // use synchronized call to keep correct order of decorations async.eachSeries(awardings, (award, callback) => { jimp.read(resourceDir + 'decoration/' + award.decorationId._id + fileExt).then((decorationImage) => { - if (award.decorationId.isMedal) { decorationImage.resize(medalW, medalH); loadedImage.composite(decorationImage, medalPx, medalPy); - medalPx = medalPx - 1 - medalW; + if (medalPy === 5) { + medalPx = medalPx - 1 - medalW; + } else { + medalPx = medalPx + 1 + medalW; + } if (medalPx <= 300) { medalPy = medalPy + 3 + medalH; - medalPx = 621; } } else { decorationImage.resize(ribbonW, ribbonH); loadedImage.composite(decorationImage, ribbonPx, ribbonPy); ribbonPx = ribbonPx - 2 - ribbonW; - if (ribbonPx <= 174) { + if (ribbonPx <= 154) { ribbonPy = ribbonPy - 3 - ribbonH; ribbonPx = 598; }