Skip to content

Commit

Permalink
DEV: prevent NPE in hp graph
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastientromp committed May 16, 2022
1 parent be81936 commit bc8e833
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ViewChild,
ViewRef,
} from '@angular/core';
import { CardIds } from '@firestone-hs/reference-data';
import { CardsFacadeService } from '@services/cards-facade.service';
import { Label } from 'aws-sdk/clients/cloudhsm';
import { ChartData, ChartDataSets, ChartOptions, ChartTooltipItem } from 'chart.js';
Expand Down Expand Up @@ -442,52 +443,55 @@ export class BgsChartHpComponent {
private buildPlayerOrder(leaderboardPositionOverTurn: {
[playerCardId: string]: readonly NumericTurnInfo[];
}): readonly string[] {
const lastTurn = leaderboardPositionOverTurn[0]?.length ?? 0;
return Object.keys(leaderboardPositionOverTurn)
if (!!leaderboardPositionOverTurn?.length) {
const lastTurn = leaderboardPositionOverTurn[0]?.length ?? 0;
return Object.keys(leaderboardPositionOverTurn)
.map((playerCardId) => {
const positionAtLastTurn = leaderboardPositionOverTurn[playerCardId][lastTurn];
return {
playerCardId: playerCardId,
position: positionAtLastTurn?.value ?? 0,
};
})
.sort((a, b) => a.position - b.position)
.map((info) => info.playerCardId);
}

// Fallback which uses the total health + armor instead of the leaderboard position
const turnAtWhichEachPlayerDies = Object.keys(leaderboardPositionOverTurn)
.filter((playerCardId) => playerCardId !== CardIds.BaconphheroHeroicBattlegrounds)
.map((playerCardId) => {
const positionAtLastTurn = leaderboardPositionOverTurn[playerCardId][lastTurn];
const info = leaderboardPositionOverTurn[playerCardId];
return {
playerCardId: playerCardId,
position: positionAtLastTurn?.value ?? 0,
turnDeath: info.find((turnInfo) => turnInfo.value <= 0)?.turn ?? 99,
lastKnownHp: (info[info.length - 1]?.value ?? 99) + ((info[info.length - 1] as any)?.armor ?? 0),
};
})
.sort((a, b) => a.position - b.position)
.map((info) => info.playerCardId);

// const turnAtWhichEachPlayerDies = Object.keys(leaderboardPositionOverTurn)
// .filter((playerCardId) => playerCardId !== CardIds.BaconphheroHeroicBattlegrounds)
// .map((playerCardId) => {
// const info = leaderboardPositionOverTurn[playerCardId];
// return {
// playerCardId: playerCardId,
// turnDeath: info.find((turnInfo) => turnInfo.value <= 0)?.turn ?? 99,
// lastKnownHp: (info[info.length - 1]?.value ?? 99) + ((info[info.length - 1] as any)?.armor ?? 0),
// };
// });
// let playerOrder: string[] = turnAtWhichEachPlayerDies
// .sort(
// (a, b) =>
// b.turnDeath - a.turnDeath ||
// // a.lastKnownPosition - b.lastKnownPosition ||
// b.lastKnownHp - a.lastKnownHp,
// )
// .map((playerInfo) => playerInfo.playerCardId);
// // Legacy issue - the heroes that were offered during the hero selection phase are
// // also proposed there
// if (playerOrder.length > 8) {
// const candidatesToRemove = turnAtWhichEachPlayerDies
// .filter((info) => info.turnDeath === 99)
// .filter((info) =>
// info.playerCardId === CardIds.PatchwerkBattlegrounds
// ? info.lastKnownHp === 55
// : info.lastKnownHp === 40,
// )
// .filter((info) => info.playerCardId !== this._mainPlayerCardId);
// playerOrder = playerOrder.filter(
// (playerCardId) => !candidatesToRemove.map((info) => info.playerCardId).includes(playerCardId),
// );
// }
// return playerOrder;
});
let playerOrder: string[] = turnAtWhichEachPlayerDies
.sort(
(a, b) =>
b.turnDeath - a.turnDeath ||
// a.lastKnownPosition - b.lastKnownPosition ||
b.lastKnownHp - a.lastKnownHp,
)
.map((playerInfo) => playerInfo.playerCardId);
// Legacy issue - the heroes that were offered during the hero selection phase are
// also proposed there
if (playerOrder.length > 8) {
const candidatesToRemove = turnAtWhichEachPlayerDies
.filter((info) => info.turnDeath === 99)
.filter((info) =>
info.playerCardId === CardIds.PatchwerkBattlegrounds
? info.lastKnownHp === 55
: info.lastKnownHp === 40,
)
.filter((info) => info.playerCardId !== this._mainPlayerCardId);
playerOrder = playerOrder.filter(
(playerCardId) => !candidatesToRemove.map((info) => info.playerCardId).includes(playerCardId),
);
}
return playerOrder;
}

private buildChartLabels(value: { [playerCardId: string]: readonly NumericTurnInfo[] }): Label[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class DuelsSignatureTreasureFilterDropdownComponent
return 0;
}

const aName = aCard.name.toLowerCase();
const bName = bCard.name.toLowerCase();
const aName = aCard.name?.toLowerCase();
const bName = bCard.name?.toLowerCase();
if (aName < bName) {
return -1;
}
Expand All @@ -124,8 +124,8 @@ export class DuelsSignatureTreasureFilterDropdownComponent
return 0;
}

const aClassName = aCard.playerClass.toLowerCase();
const bClassName = bCard.playerClass.toLowerCase();
const aClassName = aCard.playerClass?.toLowerCase();
const bClassName = bCard.playerClass?.toLowerCase();
if (aClassName < bClassName) {
return -1;
}
Expand Down

0 comments on commit bc8e833

Please sign in to comment.