Compare commits

..

No commits in common. "c9fce51ef60603f3e8b6f0b025364a0aa19ae40c" and "8e46b6c6f1e4b7838dc172b88e37e09e43277bed" have entirely different histories.

6 changed files with 57 additions and 71 deletions

View File

@ -2,20 +2,14 @@ import {Injectable} from '@angular/core';
import {Campaign} 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';
import {RequestMethod, RequestOptions} from '@angular/http';
@Injectable()
export class CampaignService {
campaigns$: Observable<Campaign[]>;
campaignStore = new Store<Campaign>();
campaigns: Campaign[];
constructor(private http: HttpClient,
private config: AppConfig) {
this.campaigns$ = this.campaignStore.items$;
}
getAllCampaigns() {
@ -24,48 +18,23 @@ export class CampaignService {
}
getAllCampaignsWithWars() {
this.http.get(this.config.apiWarPath)
.map(res => res.json())
.do((ranks) => {
this.campaignStore.dispatch({type: LOAD, data: ranks});
}).subscribe(_ => {
});
return this.campaigns$;
return this.http.get(this.config.apiWarPath)
.map(res => res.json());
}
submitCampaign(campaign: Campaign) {
let requestUrl: string;
let requestMethod: RequestMethod
let accessType;
if (campaign._id) {
requestUrl = this.config.apiCampaignPath + '/' + campaign._id;
requestMethod = RequestMethod.Patch;
accessType = EDIT;
return this.http.patch(this.config.apiCampaignPath + '/' + campaign._id, campaign)
.map(res => res.json());
} else {
requestUrl = this.config.apiCampaignPath;
requestMethod = RequestMethod.Post;
accessType = ADD;
return this.http.post(this.config.apiCampaignPath, campaign)
.map(res => res.json());
}
const options = new RequestOptions({
body: campaign,
method: requestMethod
});
return this.http.request(requestUrl, options)
.map(res => res.json())
.do(savedCampaign => {
const action = {type: accessType, data: savedCampaign};
this.campaignStore.dispatch(action);
});
}
deleteCampaign(campaign: Campaign) {
return this.http.delete(this.config.apiCampaignPath + '/' + campaign._id)
.do(res => {
this.campaignStore.dispatch({type: REMOVE, data: campaign});
});
deleteCampaign(id: string) {
return this.http.delete(this.config.apiCampaignPath + '/' + id)
.map(res => res.json());
}
getCampaign(id: string) {

View File

@ -1,4 +1,14 @@
import {Component, ElementRef, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild} from '@angular/core';
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
ViewChild
} from '@angular/core';
import {Campaign} from '../../../models/model-interfaces';
import {LoginService} from '../../../services/app-user-service/login-service';
@ -37,10 +47,8 @@ export class CampaignNavigationComponent implements OnChanges {
}
select(campaign) {
if (campaign && campaign._id) {
this.selectedCampaignId = campaign._id;
this.campaignSelect.emit(campaign);
}
this.selectedCampaignId = campaign._id;
this.campaignSelect.emit(campaign);
}
edit(campaign) {

View File

@ -16,6 +16,8 @@ import {PlayerUtils} from '../../../utils/player-utils';
})
export class StatisticHighScoreComponent implements OnInit {
@Input() campaigns;
id = '';
searchTerm = new FormControl();
@ -29,7 +31,8 @@ export class StatisticHighScoreComponent implements OnInit {
readonly fraction = Fraction;
constructor(private route: ActivatedRoute,
private playerService: PlayerService) {
private playerService: PlayerService,
private campaignService: CampaignService) {
}
ngOnInit() {
@ -37,7 +40,14 @@ export class StatisticHighScoreComponent implements OnInit {
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
this.initData();
if (this.campaignService.campaigns) {
this.initData();
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.campaignService.campaigns = campaigns;
this.initData();
});
}
});
const searchTermStream = this.searchTerm.valueChanges.debounceTime(400);

View File

@ -46,9 +46,13 @@ export class StatisticOverviewComponent implements OnInit {
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.initWars(campaigns);
});
if (this.campaignService.campaigns) {
this.initWars(this.campaignService.campaigns);
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.initWars(campaigns);
});
}
});
}
@ -91,40 +95,36 @@ export class StatisticOverviewComponent implements OnInit {
const playersObj = ChartUtils.getMultiDataArray(Fraction.BLUFOR, Fraction.OPFOR);
for (let i = wars.length - 1; i >= 0; i--) {
const war = wars[i];
if (!war) {
continue;
}
const j = wars.length - i - 1;
const warDate = (this.id === 'all') ? new Date(war.date) : ChartUtils.getShortDateString(war.date);
const warDate = (this.id === 'all') ? new Date(wars[i].date) : ChartUtils.getShortDateString(wars[i].date);
pointsObj[0].series.push({
name: warDate,
value: war.ptBlufor
value: wars[i].ptBlufor
});
pointsObj[1].series.push({
name: warDate,
value: war.ptOpfor
value: wars[i].ptOpfor
});
pointsSumObj[0].series.push({
name: warDate,
value: pointsSumObj[0].series[j - 1] ?
pointsSumObj[0].series[j - 1].value + war.ptBlufor :
war.ptBlufor
pointsSumObj[0].series[j - 1].value + wars[i].ptBlufor :
wars[i].ptBlufor
});
pointsSumObj[1].series.push({
name: warDate,
value: pointsSumObj[1].series[j - 1]
? pointsSumObj[1].series[j - 1].value + war.ptOpfor :
war.ptOpfor
? pointsSumObj[1].series[j - 1].value + wars[i].ptOpfor :
wars[i].ptOpfor
});
playersObj[0].series.push({
name: warDate,
value: war.playersBlufor
value: wars[i].playersBlufor
});
playersObj[1].series.push({
name: warDate,
value: war.playersOpfor
value: wars[i].playersOpfor
});
}

View File

@ -12,7 +12,7 @@ import {SettingsService} from '../services/settings.service';
})
export class StatisticComponent implements OnInit {
selectedCampaign: Campaign;
selectedCampaign: Campaign = {};
campaigns: Campaign[] = [];
@ -29,16 +29,14 @@ export class StatisticComponent implements OnInit {
ngOnInit() {
this.campaignService.getAllCampaignsWithWars().subscribe((campaigns) => {
this.campaigns = campaigns;
// TODO: next line has to die!
this.campaignService.campaigns = campaigns;
this.selectedCampaign = this.resolveCampaignFromUrl();
this.switchCampaign(this.selectedCampaign);
});
}
resolveCampaignFromUrl() {
if (this.campaigns.length === 0) {
return {};
}
const url = this.router.url;
const idFetchPattern = /right:.*\/(.*)\)$/;
@ -82,8 +80,9 @@ export class StatisticComponent implements OnInit {
deleteCampaign(campaign) {
this.translate.get('stats.campaign.manage.delete.confirm', {title: campaign.title}).subscribe((confirmQuestion: string) => {
if (confirm(confirmQuestion)) {
this.campaignService.deleteCampaign(campaign)
this.campaignService.deleteCampaign(campaign._id)
.subscribe((res) => {
this.campaigns.splice(this.campaigns.indexOf(campaign), 1);
});
}
});

View File

@ -19,7 +19,7 @@
id="campaign"
[(ngModel)]="war.campaign"
required>
<option *ngFor="let campaign of campaignService.campaigns$ | async" [ngValue]="campaign._id">
<option *ngFor="let campaign of campaignService.campaigns" [ngValue]="campaign._id">
{{campaign.title}}
</option>
</select>