simplify armyoverview data structure

pull/32/head
Florian Hartwich 2018-04-01 23:29:10 +02:00
parent 193b136b0a
commit 0e571136bb
4 changed files with 25 additions and 42 deletions

View File

@ -17,16 +17,12 @@ const overview = new express.Router();
// routes ********************** // routes **********************
overview.route('/') overview.route('/')
.get((req, res, next) => { .get((req, res, next) => {
let countOpfor = 0; const fractions = ['BLUFOR', 'OPFOR'];
let countBlufor = 0; const fractionMemberCount = [0, 0];
const armyOverview = { const armyOverview = [
BLUFOR: { {squads: []},
squads: [], {squads: []},
}, ];
OPFOR: {
squads: [],
},
};
SquadModel.find({}, {'sortingNumber': 0, 'updatedAt': 0, 'timestamp': 0, '__v': 0}, { SquadModel.find({}, {'sortingNumber': 0, 'updatedAt': 0, 'timestamp': 0, '__v': 0}, {
sort: { sort: {
@ -71,15 +67,14 @@ overview.route('/')
const s = squad.toObject(); const s = squad.toObject();
s.members = squadMembers; s.members = squadMembers;
s.memberCount = squadMembers.length; s.memberCount = squadMembers.length;
if (s.fraction === 'BLUFOR') {
for (let i = 0; i < fractions.length; i++) {
if (s.fraction === fractions[i]) {
delete s.fraction; delete s.fraction;
armyOverview.BLUFOR.squads.push(s); armyOverview[i].squads.push(s);
countBlufor += s.members.length; fractionMemberCount[i] += s.members.length;
break;
} }
if (s.fraction === 'OPFOR') {
delete s.fraction;
armyOverview.OPFOR.squads.push(s);
countOpfor += s.members.length;
} }
} }
@ -90,8 +85,11 @@ overview.route('/')
if (err) { if (err) {
return next(err); return next(err);
} }
armyOverview.BLUFOR.memberCount = countBlufor;
armyOverview.OPFOR.memberCount = countOpfor; for (let i = 0; i < fractions.length; i++) {
armyOverview[i].memberCount = fractionMemberCount[i];
}
res.locals.items = armyOverview; res.locals.items = armyOverview;
res.locals.processed = true; res.locals.processed = true;
next(); next();

View File

@ -3,7 +3,7 @@
<div class="pull-left" style="width: 45%;"> <div class="pull-left" style="width: 45%;">
<h3 class="army-head" [style.color]="fraction.COLOR_BLUFOR">{{fraction.BLUFOR}}</h3> <h3 class="army-head" [style.color]="fraction.COLOR_BLUFOR">{{fraction.BLUFOR}}</h3>
<div class="squad-layout" *ngFor="let squad of army.BLUFOR.squads"> <div class="squad-layout" *ngFor="let squad of army[0].squads">
<div class="row colored-row title-row"> <div class="row colored-row title-row">
<div class="squad-cell pull-left"><img <div class="squad-cell pull-left"><img
src="resource/squad/{{squad._id}}.png"></div> src="resource/squad/{{squad._id}}.png"></div>
@ -27,12 +27,12 @@
</div> </div>
</div> </div>
</div> </div>
<div class="member-count">Armeemitglieder: {{army.BLUFOR.memberCount}}</div> <div class="member-count">Armeemitglieder: {{army[0].memberCount}}</div>
</div> </div>
<div class="pull-right" style="width: 45%;"> <div class="pull-right" style="width: 45%;">
<h3 class="army-head" [style.color]="fraction.COLOR_OPFOR">{{fraction.OPFOR}}</h3> <h3 class="army-head" [style.color]="fraction.COLOR_OPFOR">{{fraction.OPFOR}}</h3>
<div class="squad-layout" *ngFor="let squad of army.OPFOR.squads"> <div class="squad-layout" *ngFor="let squad of army[1].squads">
<div class="row colored-row title-row"> <div class="row colored-row title-row">
<div class="squad-cell pull-left"><img <div class="squad-cell pull-left"><img
src="resource/squad/{{squad._id}}.png"></div> src="resource/squad/{{squad._id}}.png"></div>
@ -56,7 +56,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="member-count">Armeemitglieder: {{army.OPFOR.memberCount}}</div> <div class="member-count">Armeemitglieder: {{army[1].memberCount}}</div>
</div> </div>
</div> </div>

View File

@ -15,7 +15,7 @@ import {CSSHelpers} from '../global.helpers';
}) })
export class ArmyComponent implements OnInit, OnDestroy { export class ArmyComponent implements OnInit, OnDestroy {
army: Army = {BLUFOR: {squads: [], memberCount: 0}, OPFOR: {squads: [], memberCount: 0}}; army: Army[];
readonly fraction = Fraction; readonly fraction = Fraction;

View File

@ -104,7 +104,6 @@ export interface Decoration {
} }
export interface Army { export interface Army {
BLUFOR: {
squads: { squads: {
_id, _id,
name, name,
@ -115,20 +114,6 @@ export interface Army {
rank rank
}[], }[],
}[], }[],
memberCount memberCount: number
};
OPFOR: {
squads: {
_id,
name,
memberCount,
members: {
_id,
username,
rank
}[],
}[],
memberCount
};
} }