Skip to content

Commit

Permalink
Take into account aliases when comparing userIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmburg committed Apr 27, 2024
1 parent 3b6ca7e commit 5d2b5d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ export function primaryTeam(player: PlayerSummary): Team {
}
}

export type UserIdAliases = Record<number, number>;

export type GameSummary = {
local_user_id: UserId;
highlights: HighlightEvent[];
Expand All @@ -216,7 +218,7 @@ export type GameSummary = {
interval_per_tick: number;
players: PlayerSummary[];
num_rounds: number;
aliases: Record<number, number>;
aliases: UserIdAliases;
};

export type Scoreboard = {
Expand Down
52 changes: 31 additions & 21 deletions src/routes/demos/demoDir/demoDetails/Highlights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
HighlightPlayerSnapshot,
PlayerSummary,
TaggedHighlight,
UserIdAliases,
destructureHighlight,
} from "@/demo";
import HighlightBox from "./HighlightBox";
Expand All @@ -23,66 +24,74 @@ export type TimelineProps = {

function samePlayer(
playerId: number,
player: PlayerSummary | HighlightPlayerSnapshot | null
player: PlayerSummary | HighlightPlayerSnapshot | null,
aliases: UserIdAliases
): boolean {
return playerId === player?.user_id;
if (player === null) {
return false;
} else {
return playerId === player.user_id || playerId === aliases[player.user_id];
}
}

function doesHighlightIncludePlayer(
highlight: TaggedHighlight,
playerId: number
playerId: number,
aliases: UserIdAliases
): boolean {
switch (highlight.type) {
case "Airshot":
return (
samePlayer(playerId, highlight.highlight.victim) ||
samePlayer(playerId, highlight.highlight.attacker)
samePlayer(playerId, highlight.highlight.victim, aliases) ||
samePlayer(playerId, highlight.highlight.attacker, aliases)
);
case "ChatMessage":
return samePlayer(playerId, highlight.highlight.sender);
return samePlayer(playerId, highlight.highlight.sender, aliases);
case "CrossbowAirshot":
return (
samePlayer(playerId, highlight.highlight.healer) ||
samePlayer(playerId, highlight.highlight.target)
samePlayer(playerId, highlight.highlight.healer, aliases) ||
samePlayer(playerId, highlight.highlight.target, aliases)
);
case "Kill":
return (
samePlayer(playerId, highlight.highlight.killer) ||
samePlayer(playerId, highlight.highlight.assister) ||
samePlayer(playerId, highlight.highlight.victim)
samePlayer(playerId, highlight.highlight.killer, aliases) ||
samePlayer(playerId, highlight.highlight.assister, aliases) ||
samePlayer(playerId, highlight.highlight.victim, aliases)
);
case "KillStreak":
return samePlayer(playerId, highlight.highlight.player);
return samePlayer(playerId, highlight.highlight.player, aliases);
case "KillStreakEnded":
return (

Check notice on line 64 in src/routes/demos/demoDir/demoDetails/Highlights.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/routes/demos/demoDir/demoDetails/Highlights.tsx#L30-L64

Complex Method
samePlayer(playerId, highlight.highlight.killer) ||
samePlayer(playerId, highlight.highlight.victim)
samePlayer(playerId, highlight.highlight.killer, aliases) ||
samePlayer(playerId, highlight.highlight.victim, aliases)
);
case "PlayerConnected":
return samePlayer(playerId, highlight.highlight.player);
return samePlayer(playerId, highlight.highlight.player, aliases);
case "PlayerDisconnected":
return samePlayer(playerId, highlight.highlight.player);
return samePlayer(playerId, highlight.highlight.player, aliases);
case "PointCaptured":
return highlight.highlight.cappers.some((capper) =>
samePlayer(playerId, capper)
samePlayer(playerId, capper, aliases)
);
case "PlayerTeamChange":
return samePlayer(playerId, highlight.highlight.player);
return samePlayer(playerId, highlight.highlight.player, aliases);
default:
return true;
}
}

function filterHighlights(
highlights: HighlightEvent[],
filters: Filters
filters: Filters,
aliases: UserIdAliases
): HighlightEvent[] {
if (filters.playerIds.length > 0) {
highlights = highlights.filter((h) =>
filters.playerIds.find((p) =>
doesHighlightIncludePlayer(
destructureHighlight(h.event),
Number.parseInt(p, 10)
Number.parseInt(p, 10),
aliases
)
)
);
Expand Down Expand Up @@ -182,7 +191,8 @@ export default function HighlightsList({ gameSummary }: TimelineProps) {
});

const highlights = useMemo(
() => filterHighlights(gameSummary.highlights, filters),
() =>
filterHighlights(gameSummary.highlights, filters, gameSummary.aliases),
[gameSummary.highlights, filters]

Check warning on line 196 in src/routes/demos/demoDir/demoDetails/Highlights.tsx

View workflow job for this annotation

GitHub Actions / Check PR

React Hook useMemo has a missing dependency: 'gameSummary.aliases'. Either include it or remove the dependency array

Check warning on line 196 in src/routes/demos/demoDir/demoDetails/Highlights.tsx

View workflow job for this annotation

GitHub Actions / Check PR

React Hook useMemo has a missing dependency: 'gameSummary.aliases'. Either include it or remove the dependency array

Check notice on line 196 in src/routes/demos/demoDir/demoDetails/Highlights.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/routes/demos/demoDir/demoDetails/Highlights.tsx#L196

React Hook useMemo has a missing dependency: 'gameSummary.aliases'. Either include it or remove the dependency array. (react-hooks/exhaustive-deps)
);

Expand Down

0 comments on commit 5d2b5d1

Please sign in to comment.