|
|
|
@ -1,55 +1,37 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import { HttpClient } from '@angular/common/http'; |
|
|
|
import { Observable, Subject, take } from 'rxjs'; |
|
|
|
import { map, Observable, pipe, shareReplay, Subject, take, tap } from 'rxjs'; |
|
|
|
import { environment } from '../../../environments/environment.development'; |
|
|
|
import { GamePlan } from '../models/plan.model'; |
|
|
|
import { GamePlan, PokemonFamilyEntry } from '../models/plan.model'; |
|
|
|
import { PokemonService } from './pokemon.service'; |
|
|
|
|
|
|
|
@Injectable({ |
|
|
|
providedIn: 'root' |
|
|
|
}) |
|
|
|
export class PlanService { |
|
|
|
private caughtPokemon = new Set<string>(); |
|
|
|
private gameUpdates = new Subject<{gameId: number, total: number}>(); |
|
|
|
|
|
|
|
gameUpdates$ = this.gameUpdates.asObservable(); |
|
|
|
|
|
|
|
constructor(private http: HttpClient) {} |
|
|
|
private gamePlanCache: Observable<(GamePlan[])> | null = null; |
|
|
|
private gamePlan: GamePlan[] = []; |
|
|
|
|
|
|
|
getPlan(): Observable<GamePlan[]> { |
|
|
|
return this.http.get<GamePlan[]>(`${environment.apiUrl}/plan`); |
|
|
|
} |
|
|
|
constructor( |
|
|
|
private http: HttpClient, |
|
|
|
private pokemonService: PokemonService |
|
|
|
) {} |
|
|
|
|
|
|
|
updateCaughtStatus(pfic: string, caught: boolean) { |
|
|
|
if (caught) { |
|
|
|
this.caughtPokemon.add(pfic); |
|
|
|
} else { |
|
|
|
this.caughtPokemon.delete(pfic); |
|
|
|
getPlan(): Observable<GamePlan[]> { |
|
|
|
if (this.gamePlanCache) { |
|
|
|
return this.gamePlanCache; |
|
|
|
} |
|
|
|
// Trigger recalculation of affected games
|
|
|
|
this.recalculateAffectedGames(pfic); |
|
|
|
} |
|
|
|
|
|
|
|
private recalculateAffectedGames(pfic: string) { |
|
|
|
return |
|
|
|
// This would need to check all games for the affected Pokemon
|
|
|
|
// and update their totals accordingly
|
|
|
|
/* |
|
|
|
this.getPlan().pipe(take(1)).subscribe(games => { |
|
|
|
games.forEach(game => { |
|
|
|
const affectedPokemon = game.pokemon.find(p => |
|
|
|
p.pfic === pfic || |
|
|
|
p.evolve_to.some(e => e.pfic === pfic) || |
|
|
|
p.breed_for.some(b => b.pfic === pfic) |
|
|
|
); |
|
|
|
if (affectedPokemon) { |
|
|
|
this.gameUpdates.next({ |
|
|
|
gameId: game.game_id, |
|
|
|
total: this.calculateGameTotal(game) |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
*/ |
|
|
|
this.gamePlanCache = this.http.get<GamePlan[]>(`${environment.apiUrl}/plan`).pipe( |
|
|
|
tap(game_plan => { |
|
|
|
this.gamePlan = game_plan as GamePlan[]; |
|
|
|
}), |
|
|
|
shareReplay(1) |
|
|
|
); |
|
|
|
return this.gamePlanCache; |
|
|
|
} |
|
|
|
|
|
|
|
private calculateGameTotal(game: GamePlan): number { |
|
|
|
@ -61,4 +43,27 @@ export class PlanService { |
|
|
|
return sum |
|
|
|
} |
|
|
|
|
|
|
|
updateCaughtCount(family: PokemonFamilyEntry) { |
|
|
|
for(const plan of this.gamePlan) { |
|
|
|
if(family.family_pfic && family.family_pfic in plan.pokemon) { |
|
|
|
let pokemon_family = plan.pokemon[family.family_pfic] |
|
|
|
let count = 0; |
|
|
|
|
|
|
|
for( const pfic of pokemon_family.evolve_to) { |
|
|
|
if (this.pokemonService.isTargetCompleted(pfic)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for( const pfic of pokemon_family.breed_for) { |
|
|
|
if (this.pokemonService.isTargetCompleted(pfic)) { |
|
|
|
count += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pokemon_family.caught_count = count; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |