Compare commits

..

2 Commits

6 changed files with 71 additions and 57 deletions

View File

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

View File

@ -1,14 +1,4 @@
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
ViewChild
} from '@angular/core';
import {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';
@ -47,9 +37,11 @@ export class CampaignNavigationComponent implements OnChanges {
}
select(campaign) {
if (campaign && campaign._id) {
this.selectedCampaignId = campaign._id;
this.campaignSelect.emit(campaign);
}
}
edit(campaign) {
this.campaignEdit.emit(campaign);

View File

@ -16,8 +16,6 @@ import {PlayerUtils} from '../../../utils/player-utils';
})
export class StatisticHighScoreComponent implements OnInit {
@Input() campaigns;
id = '';
searchTerm = new FormControl();
@ -31,8 +29,7 @@ export class StatisticHighScoreComponent implements OnInit {
readonly fraction = Fraction;
constructor(private route: ActivatedRoute,
private playerService: PlayerService,
private campaignService: CampaignService) {
private playerService: PlayerService) {
}
ngOnInit() {
@ -40,14 +37,7 @@ export class StatisticHighScoreComponent implements OnInit {
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
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,13 +46,9 @@ export class StatisticOverviewComponent implements OnInit {
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
if (this.campaignService.campaigns) {
this.initWars(this.campaignService.campaigns);
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.initWars(campaigns);
});
}
});
}
@ -95,36 +91,40 @@ 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(wars[i].date) : ChartUtils.getShortDateString(wars[i].date);
const warDate = (this.id === 'all') ? new Date(war.date) : ChartUtils.getShortDateString(war.date);
pointsObj[0].series.push({
name: warDate,
value: wars[i].ptBlufor
value: war.ptBlufor
});
pointsObj[1].series.push({
name: warDate,
value: wars[i].ptOpfor
value: war.ptOpfor
});
pointsSumObj[0].series.push({
name: warDate,
value: pointsSumObj[0].series[j - 1] ?
pointsSumObj[0].series[j - 1].value + wars[i].ptBlufor :
wars[i].ptBlufor
pointsSumObj[0].series[j - 1].value + war.ptBlufor :
war.ptBlufor
});
pointsSumObj[1].series.push({
name: warDate,
value: pointsSumObj[1].series[j - 1]
? pointsSumObj[1].series[j - 1].value + wars[i].ptOpfor :
wars[i].ptOpfor
? pointsSumObj[1].series[j - 1].value + war.ptOpfor :
war.ptOpfor
});
playersObj[0].series.push({
name: warDate,
value: wars[i].playersBlufor
value: war.playersBlufor
});
playersObj[1].series.push({
name: warDate,
value: wars[i].playersOpfor
value: war.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,14 +29,16 @@ 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:.*\/(.*)\)$/;
@ -80,9 +82,8 @@ 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._id)
this.campaignService.deleteCampaign(campaign)
.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" [ngValue]="campaign._id">
<option *ngFor="let campaign of campaignService.campaigns$ | async" [ngValue]="campaign._id">
{{campaign.title}}
</option>
</select>