CT-38 #17

Merged
mitch merged 2 commits from CT-38 into master 2022-04-19 12:36:10 +00:00
2 changed files with 35 additions and 50 deletions
Showing only changes of commit 96ea2e212a - Show all commits

View File

@ -21,6 +21,7 @@
], ],
"@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-non-null-assertion": "off",
"no-console": "warn", "no-console": "warn",
"@typescript-eslint/ban-ts-ignore": "off" "@typescript-eslint/ban-ts-ignore": "off",
"guard-for-in": "off"
} }
} }

View File

@ -34,54 +34,46 @@ export class ForceComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.getArmyId(); this.loadArmy().then();
} }
getArmyId(): void { async loadArmy(): Promise<HttpResponse<number>> {
if (this.forceList[0] && this.forceList[0].unit.id) { // @ts-ignore
const armyIdOrNull: Observable<HttpResponse<number>> = this.unitArmyService.getArmy(this.forceList[0].unit.id); const armyIdPromise: Observable<HttpResponse<number>> = this.unitArmyService.getArmy(this.forceList[0].unit.id);
armyIdOrNull.toPromise().then( await armyIdPromise.toPromise().then(
res => {
Promise.resolve(res.body);
this.armyId = res.body? res.body : 0;
}
)
}
}
getArmyIdPromise(): Promise<number | void> | null {
if (this.forceList[0] && this.forceList[0].unit.id) {
const armyIdOrNull: Observable<HttpResponse<number>> = this.unitArmyService.getArmy(this.forceList[0].unit.id);
return armyIdOrNull.toPromise().then(
res => {
Promise.resolve(res.body);
}
);
}
return null;
}
getArmyPromise(): Promise<HttpResponse<Army>> {
this.getArmyId();
const armyObs: Observable<HttpResponse<Army>> = this.armyService.find(this.armyId);
const armyPromise: Promise<HttpResponse<Army>> = armyObs.toPromise().then(
res => { res => {
Promise.resolve(res.body); Promise.resolve(res.body);
return res; if (res.body) {
this.armyId = res.body;
}
} }
) );
return armyPromise;
}
loadArmy(): void { const armyPromise: Observable<HttpResponse<Army>> = this.armyService.find(this.armyId);
this.getArmyId(); await armyPromise.toPromise().then(
const armyObs: Observable<HttpResponse<Army>> = this.armyService.find(this.armyId);
armyObs.toPromise().then(
res => { res => {
Promise.resolve(res.body); Promise.resolve(res.body);
this.army = res.body; this.army = res.body;
} }
) );
for (let i = 0; i < this.forceList.length; i++) {
if (this.army && this.army.units) {
this.army.units.forEach(
armyyUnit => {
if (this.forceList[i].unit.id === armyyUnit.id) {
for (const key in armyyUnit) {
if (key !== "army") {
this.forceList[i].unit[key] = armyyUnit[key];
}
}
}
}
)
}
}
localStorage.setItem('forceAddedListStored', JSON.stringify(this.forceList));
return armyIdPromise.toPromise();
} }
resetAll(): void { resetAll(): void {
@ -141,17 +133,7 @@ export class ForceComponent implements OnInit {
localStorage.setItem('forceAddedListStored', JSON.stringify(this.forceList)); localStorage.setItem('forceAddedListStored', JSON.stringify(this.forceList));
} }
async saveList(): Promise<void> { saveList(): void {
const armyPromise: Promise<HttpResponse<Army>> = this.getArmyPromise();
await armyPromise.then(
res => {
Promise.resolve(res.body);
this.army = res.body;
return this.army;
}
)
for (const fu of this.forceList) { for (const fu of this.forceList) {
let newExperience: number let newExperience: number
let newUnitsDestroyed: number let newUnitsDestroyed: number
@ -194,6 +176,7 @@ export class ForceComponent implements OnInit {
fu.unit.enemyUnitsDestroyed = newUnitsDestroyed; fu.unit.enemyUnitsDestroyed = newUnitsDestroyed;
fu.unit.experiencePoints = newExperience; fu.unit.experiencePoints = newExperience;
// console.log(fu.unit);
this.unitArmyService.update(fu.unit).toPromise().then(); this.unitArmyService.update(fu.unit).toPromise().then();
} }
@ -216,6 +199,7 @@ export class ForceComponent implements OnInit {
} }
if (this.army) { if (this.army) {
// console.log(this.army)
this.armyService.update(this.army).toPromise().then() this.armyService.update(this.army).toPromise().then()
} }