Compare commits

..

3 Commits

Author SHA1 Message Date
HardiReady e8ea7dc6a9 Finish icons for scoreboard 2018-05-04 21:42:12 +02:00
HardiReady 8a78b695b7 Add generic method for get by id 2018-05-04 20:49:45 +02:00
HardiReady bdb07ecc1e Add generic patch method for reuse 2018-05-04 20:37:35 +02:00
15 changed files with 121 additions and 150 deletions

53
api/routes/_generic.js Normal file
View File

@ -0,0 +1,53 @@
"use strict";
// HTTP status codes by name
const codes = require('./http-codes');
const genericGetById = (req, res, next, modelClass) => {
modelClass.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;
return next(err);
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = item;
return next();
});
};
const genericPatch = (req, res, next, modelClass) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
req.body._id);
err.status = codes.notfound;
next(err);
return; // prevent node to process this function further after next() has finished.
}
req.body.updatedAt = new Date();
req.body.$inc = {__v: 1};
if (req.body.hasOwnProperty('__v')) {
delete req.body.__v;
}
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
// reset attributes that are missing.
modelClass.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
} else {
res.locals.items = item;
}
next(err);
});
};
exports.genericGetById = genericGetById;
exports.genericPatch = genericPatch;

View File

@ -16,6 +16,10 @@ const idValidator = require('../middleware/validators').idValidator;
const CampaignModel = require('../models/campaign'); const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war'); const WarModel = require('../models/war');
// util
const genericGetById = require('./_generic').genericGetById;
const genericPatch = require('./_generic').genericPatch;
const campaigns = new express.Router(); const campaigns = new express.Router();
// routes ********************** // routes **********************
@ -41,49 +45,11 @@ campaigns.route('/')
campaigns.route('/:id') campaigns.route('/:id')
.get(idValidator, (req, res, next) => { .get(idValidator, (req, res, next) => {
CampaignModel.findById(req.params.id, (err, item) => { return genericGetById(req, res, next, CampaignModel);
if (err) {
err.status = codes.servererror;
return next(err);
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = item;
return next();
});
}) })
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => { .patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) { return genericPatch(req, res, next, CampaignModel);
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
req.body._id);
err.status = codes.notfound;
next(err);
return; // prevent node to process this function further after next() has finished.
}
req.body.updatedAt = new Date();
req.body.$inc = {__v: 1};
if (req.body.hasOwnProperty('__v')) {
delete req.body.__v;
}
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
// reset attributes that are missing.
CampaignModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
} else {
res.locals.items = item;
}
next(err);
});
}) })
.delete((req, res, next) => { .delete((req, res, next) => {

View File

@ -21,6 +21,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
const DecorationModel = require('../models/decoration'); const DecorationModel = require('../models/decoration');
const AwardingsModel = require('../models/awarding'); const AwardingsModel = require('../models/awarding');
// util
const genericGetById = require('./_generic').genericGetById;
const decoration = new express.Router(); const decoration = new express.Router();
// routes ********************** // routes **********************
@ -81,18 +84,7 @@ decoration.route('/')
decoration.route('/:id') decoration.route('/:id')
.get(idValidator, (req, res, next) => { .get(idValidator, (req, res, next) => {
DecorationModel.findById(req.params.id, (err, item) => { return genericGetById(req, res, next, DecorationModel);
if (err) {
err.status = codes.servererror;
return next(err);
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = item;
next();
});
}) })
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => { .patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {

View File

@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const RankModel = require('../models/rank'); const RankModel = require('../models/rank');
// util
const genericGetById = require('./_generic').genericGetById;
const ranks = new express.Router(); const ranks = new express.Router();
// routes ********************** // routes **********************
@ -73,18 +76,7 @@ ranks.route('/')
ranks.route('/:id') ranks.route('/:id')
.get(idValidator, (req, res, next) => { .get(idValidator, (req, res, next) => {
RankModel.findById(req.params.id, (err, item) => { return genericGetById(req, res, next, RankModel);
if (err) {
err.status = codes.servererror;
return next(err);
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = item;
next();
});
}) })
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => { .patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {

View File

@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const SquadModel = require('../models/squad'); const SquadModel = require('../models/squad');
// util
const genericGetById = require('./_generic').genericGetById;
const squads = new express.Router(); const squads = new express.Router();
// routes ********************** // routes **********************
@ -77,18 +80,7 @@ squads.route('/')
squads.route('/:id') squads.route('/:id')
.get(idValidator, (req, res, next) => { .get(idValidator, (req, res, next) => {
SquadModel.findById(req.params.id, (err, item) => { return genericGetById(req, res, next, SquadModel);
if (err) {
err.status = codes.servererror;
return next(err);
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = item;
next();
});
}) })
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => { .patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {

View File

@ -35,6 +35,9 @@ const LogFlagModel = require('../models/logs/flag');
const LogBudgetModel = require('../models/logs/budget'); const LogBudgetModel = require('../models/logs/budget');
const LogPointsModel = require('../models/logs/points'); const LogPointsModel = require('../models/logs/points');
// util
const genericPatch = require('./_generic').genericPatch;
const wars = new express.Router(); const wars = new express.Router();
// routes ********************** // routes **********************
@ -169,34 +172,7 @@ wars.route('/:id')
}) })
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => { .patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) { return genericPatch(req, res, next, WarModel);
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
req.body._id);
err.status = codes.notfound;
next(err);
return; // prevent node to process this function further after next() has finished.
}
req.body.updatedAt = new Date();
req.body.$inc = {__v: 1};
if (req.body.hasOwnProperty('__v')) {
delete req.body.__v;
}
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
// reset attributes that are missing.
WarModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
} else if (!item) {
err = new Error('item not found');
err.status = codes.notfound;
} else {
res.locals.items = item;
}
next(err);
});
}) })
.delete(apiAuthenticationMiddleware, checkMT, (req, res, next) => { .delete(apiAuthenticationMiddleware, checkMT, (req, res, next) => {

View File

@ -137,48 +137,7 @@ let addDecorationsAndSave = (userId, loadedImage, res, next) => {
return next(err); return next(err);
} }
if (awardings.length > 0) { if (awardings.length > 0) {
// TODO: simplify this sorting hell awardings.sort((a1, a2) => sortAwardingsForSignature(a1, a2));
awardings.sort((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;
}
});
// use synchronized call to keep correct order of decorations // use synchronized call to keep correct order of decorations
async.eachSeries(awardings, (award, callback) => { async.eachSeries(awardings, (award, callback) => {
@ -262,5 +221,47 @@ let saveJimpImageAndCompress = (image, userId, res, next) => {
}, 3000); }, 3000);
}; };
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;
}
};
module.exports = createSignature; module.exports = createSignature;

View File

@ -39,7 +39,6 @@ const timeStringToDecimal = (timeString) => {
return hour + (sek / 3600); return hour + (sek / 3600);
}; };
const decimalToTimeString = (decimal) => { const decimalToTimeString = (decimal) => {
const hours = parseInt(decimal.toString().split('.')[0]); const hours = parseInt(decimal.toString().split('.')[0]);
const minutesFloat = ((decimal % 1) * 3600) / 60; const minutesFloat = ((decimal % 1) * 3600) / 60;

View File

@ -20,10 +20,10 @@ export class ScoreboardComponent implements OnChanges {
@Output() playerTabSwitch = new EventEmitter(); @Output() playerTabSwitch = new EventEmitter();
tableHead = [ tableHead = [
{prop: 'name', head: 'Spieler'}, {prop: 'fraction', head: 'Fraktion'}, {prop: 'kill', head: 'Kills'}, {prop: 'name', head: 'Spieler'}, {prop: 'fraction', head: 'Fraktion'}, {prop: 'kill', head: 'Abschüsse'},
{prop: 'friendlyFire', head: 'Friendly Fire'}, {prop: 'vehicleLight', head: 'Fahrzeug (leicht)'}, {prop: 'friendlyFire', head: 'Friendly Fire'}, {prop: 'vehicleLight', head: 'Fahrzeug (leicht)'},
{prop: 'vehicleHeavy', head: 'Fahrzeug (schwer)'}, {prop: 'vehicleAir', head: 'Fahrzeug (Luft)'}, {prop: 'vehicleHeavy', head: 'Fahrzeug (schwer)'}, {prop: 'vehicleAir', head: 'Fahrzeug (Luft)'},
{prop: 'revive', head: 'Revive'}, {prop: 'flagTouch', head: 'Flagge'}, {prop: 'death', head: 'Tod'}, {prop: 'revive', head: 'Revive'}, {prop: 'flagTouch', head: 'Eroberungen'}, {prop: 'death', head: 'Tode'},
{prop: 'respawn', head: 'Respawn'} {prop: 'respawn', head: 'Respawn'}
]; ];

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 678 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB