diff --git a/api/routes/campaigns.js b/api/routes/campaigns.js index a9ae52f..e292624 100644 --- a/api/routes/campaigns.js +++ b/api/routes/campaigns.js @@ -63,6 +63,21 @@ campaigns.route('/') routerHandling.httpMethodNotAllowed ); +campaigns.route('/with/war/:id') + .get((req, res, next) => { + WarModel.findById(req.params.id, (err, item) => { + if (err) { + err.status = codes.servererror; + return next(err); + } + return genericGetById({params: {id: item.campaign}}, res, next, CampaignModel); + }); + }) + + .all( + routerHandling.httpMethodNotAllowed + ); + campaigns.route('/:id') .get(idValidator, (req, res, next) => { return genericGetById(req, res, next, CampaignModel); diff --git a/api/routes/wars.js b/api/routes/wars.js index cf57b6a..692e37f 100644 --- a/api/routes/wars.js +++ b/api/routes/wars.js @@ -23,7 +23,6 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca const parseWarLog = require('../tools/log-parse-tool'); // Mongoose Model using mongoDB -const CampaignModel = require('../models/campaign'); const WarModel = require('../models/war'); const PlayerModel = require('../models/player'); const LogKillModel = require('../models/logs/kill'); @@ -43,36 +42,18 @@ const wars = new express.Router(); // routes ********************** wars.route('/') .get((req, res, next) => { - let result = []; - CampaignModel.find({}, {}, {sort: {timestamp: 'desc'}}, (err, campaigns) => { + const filter = {}; + if (req.query.campaignId) { + filter.campaign = req.query.campaignId; + } + WarModel.find(filter, {}, {sort: {date: 'desc'}}, (err, wars) => { if (err) { err.status = codes.servererror; return next(err); } - if (campaigns) { - WarModel.find({}, {}, {sort: {date: 'desc'}}, (err, wars) => { - if (err) { - err.status = codes.servererror; - return next(err); - } - if (wars) { - campaigns.forEach((campaign) => { - let entry = {_id: campaign._id, title: campaign.title, wars: []}; - wars.forEach((war) => { - if (String(campaign._id) === String(war.campaign)) { - entry.wars.push(war); - } - }); - result.push(entry); - }); - res.locals.items = result; - } - res.locals.processed = true; - next(); - } - ) - ; - } + res.locals.items = wars; + res.locals.processed = true; + next(); }); }) diff --git a/static/src/app/models/model-interfaces.ts b/static/src/app/models/model-interfaces.ts index 97232d0..9bb52de 100644 --- a/static/src/app/models/model-interfaces.ts +++ b/static/src/app/models/model-interfaces.ts @@ -1,3 +1,5 @@ +import {Observable} from 'rxjs'; + export interface AppUser { _id?: string; username?: string; @@ -43,7 +45,7 @@ export interface CampaignPlayer { export interface Campaign { _id?: string; title?: string; - wars?: War[]; + wars$?: Observable; } export interface War { diff --git a/static/src/app/services/logs/campaign.service.ts b/static/src/app/services/logs/campaign.service.ts index 81d5619..4a9878b 100644 --- a/static/src/app/services/logs/campaign.service.ts +++ b/static/src/app/services/logs/campaign.service.ts @@ -20,20 +20,20 @@ export class CampaignService { getAllCampaigns() { return this.http.get(this.config.apiCampaignPath) - .map(res => res.json()); - } - - getAllCampaignsWithWars() { - return this.http.get(this.config.apiWarPath) - .map(res => res.json()) - .do((ranks) => this.campaignStore.dispatch({type: LOAD, data: ranks})) + .map(res => res.json()) + .do((ranks) => this.campaignStore.dispatch({type: LOAD, data: ranks})); } getCampaign(id: string) { - return this.http.get(this.config.apiCampaignPath + '/' + id) + return this.http.get(`${this.config.apiCampaignPath}/${id}`) .map(res => res.json()); } + getCampaignByWarId(warId) { + return this.http.get(`${this.config.apiCampaignPath}/with/war/${warId}`) + .map((res) => res.json()); + } + submitCampaign(campaign: Campaign) { let requestUrl: string; let requestMethod: RequestMethod diff --git a/static/src/app/services/logs/war.service.ts b/static/src/app/services/logs/war.service.ts index b434a2e..f4d0a5c 100644 --- a/static/src/app/services/logs/war.service.ts +++ b/static/src/app/services/logs/war.service.ts @@ -2,12 +2,29 @@ import {Injectable} from '@angular/core'; import {War} from '../../models/model-interfaces'; import {AppConfig} from '../../app.config'; import {HttpClient} from '../http-client'; +import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; +import {Observable} from 'rxjs'; @Injectable() export class WarService { + wars$: Observable; + + warStore = new Store(); + constructor(private http: HttpClient, private config: AppConfig) { + this.wars$ = this.warStore.items$; + } + + getAllWars(campaignId?: string) { + let targetUrl = this.config.apiWarPath; + if (campaignId) { + targetUrl += `?campaignId=${campaignId}` + } + return this.http.get(targetUrl) + .map(res => res.json()) + .do((wars) => this.warStore.dispatch({type: LOAD, data: wars})); } getWar(warId: string) { @@ -15,7 +32,6 @@ export class WarService { .map(res => res.json()); } - submitWar(war: War, logFile?) { let body; @@ -30,17 +46,19 @@ export class WarService { } return this.http.post(this.config.apiWarPath, body) - .map(res => res.json()); - } - - deleteWar(id: string) { - return this.http.delete(this.config.apiWarPath + '/' + id) - .map(res => res.json()); + .map(res => res.json()) + .do((newWar) => this.warStore.dispatch({type: ADD, data: newWar})); } updateWar(war: War) { return this.http.patch(this.config.apiWarPath + '/' + war._id, war) - .map(res => res.json()); + .map(res => res.json()) + .do((updatedWar) => this.warStore.dispatch({type: EDIT, data: updatedWar})); + } + + deleteWar(war: War) { + return this.http.delete(this.config.apiWarPath + '/' + war._id) + .do((emptyRes) => this.warStore.dispatch({type: REMOVE, data: war})); } } diff --git a/static/src/app/statistic/campaign/overview/campaign-overview.component.html b/static/src/app/statistic/campaign/overview/campaign-overview.component.html index 5abdacb..780a3d4 100644 --- a/static/src/app/statistic/campaign/overview/campaign-overview.component.html +++ b/static/src/app/statistic/campaign/overview/campaign-overview.component.html @@ -14,7 +14,7 @@ -
+
params['id']) .subscribe((id) => { this.id = id; - this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => { - this.initWars(campaigns); - }); + this.initWars(); }); } - initWars(campaigns) { - let wars = []; - let itemsProcessed = 0; - campaigns = campaigns.filter(campaign => this.id === 'all' || campaign._id === this.id); - campaigns.forEach(campaign => { - wars = wars.concat(campaign.wars); - itemsProcessed++; - if (itemsProcessed === campaigns.length) { - if (this.id === 'all') { - this.timeline = true; - } + initWars() { + if (this.id === 'all') { + this.warService.getAllWars().subscribe((wars) => { this.initChart(wars); - } - }); + }); + this.timeline = true; + } else { + this.warService.wars$.subscribe((wars) => { + this.initChart(wars); + }) + } } goToSlide(index) { @@ -137,5 +133,9 @@ export class StatisticOverviewComponent implements OnInit { this.goToSlide(1); } Object.assign(this, this.currentData); + + setTimeout(() => { + this.initialized = true; + }, 250); } } diff --git a/static/src/app/statistic/stats.component.ts b/static/src/app/statistic/stats.component.ts index 726e25e..ce6f896 100644 --- a/static/src/app/statistic/stats.component.ts +++ b/static/src/app/statistic/stats.component.ts @@ -4,6 +4,7 @@ import {CampaignService} from '../services/logs/campaign.service'; import {ActivatedRoute, Router} from '@angular/router'; import {TranslateService} from '@ngx-translate/core'; import {SettingsService} from '../services/settings.service'; +import {WarService} from '../services/logs/war.service'; @Component({ selector: 'cc-stats', @@ -19,6 +20,7 @@ export class StatisticComponent implements OnInit { collapsed = false; constructor(private campaignService: CampaignService, + private warService: WarService, private router: Router, private route: ActivatedRoute, private translate: TranslateService, @@ -27,11 +29,12 @@ export class StatisticComponent implements OnInit { } ngOnInit() { - this.campaignService.getAllCampaignsWithWars().filter(c => c.length !== 0).subscribe((campaigns) => { - this.campaigns = campaigns; - this.selectedCampaign = this.resolveCampaignFromUrl(); - this.switchCampaign(this.selectedCampaign); - }); + this.campaignService.getAllCampaigns() + .filter(campaigns => campaigns.length !== 0) + .subscribe((campaigns) => { + this.campaigns = campaigns; + this.resolveCampaignFromUrl(); + }); } resolveCampaignFromUrl() { @@ -42,25 +45,33 @@ export class StatisticComponent implements OnInit { if (url.includes('right:overview') || url.includes('right:highscore')) { const id = idFetchPattern.exec(url)[1]; if (id === 'all') { - return {_id: id} + this.switchCampaign({_id: id}); } const filteredCampaigns = this.campaigns.filter(c => c._id === id); if (filteredCampaigns.length === 1) { - return filteredCampaigns[0]; + this.switchCampaign(filteredCampaigns[0]); } - } - if (url.includes('right:war')) { + } else if (url.includes('right:war')) { const id = idFetchPattern.exec(url)[1]; - const filteredCampaigns = this.campaigns.filter(c => c.wars.filter(war => war._id === id).length > 0); - if (filteredCampaigns.length === 1) { - return filteredCampaigns[0]; - } + console.log(id) + this.campaignService.getCampaignByWarId(id).subscribe((campaign) => { + this.switchCampaign(campaign); + }); + } else { + this.switchCampaign(this.campaigns[0]); } - return this.campaigns[0] } switchCampaign(campaign) { - this.selectedCampaign = campaign; + if (campaign._id !== 'all') { + campaign.wars$ = this.warService.wars$; + this.warService.getAllWars(campaign._id).subscribe((wars) => { + this.selectedCampaign = campaign; + }); + } else { + this.selectedCampaign = campaign; + } + if (campaign._id === 'all' || this.router.url.includes('/overview/all')) { setTimeout(_ => { window.dispatchEvent(new Event('resize')); diff --git a/static/src/app/statistic/war/war-list/war-list.component.html b/static/src/app/statistic/war/war-list/war-list.component.html index eeef7f7..61486c2 100644 --- a/static/src/app/statistic/war/war-list/war-list.component.html +++ b/static/src/app/statistic/war/war-list/war-list.component.html @@ -37,7 +37,7 @@
-
+
{ if (confirm(confirmQuestion)) { - this.warService.deleteWar(war._id) + this.warService.deleteWar(war) .subscribe((res) => { if (this.selectedWarId === war._id) { this.selectOverview('all'); } - this.campaign.wars.splice(this.campaign.wars.indexOf(war), 1); }); } }); diff --git a/static/src/app/statistic/war/war-submit/war-submit.component.html b/static/src/app/statistic/war/war-submit/war-submit.component.html index c87a008..0f633eb 100644 --- a/static/src/app/statistic/war/war-submit/war-submit.component.html +++ b/static/src/app/statistic/war/war-submit/war-submit.component.html @@ -88,7 +88,7 @@