Skip to content

Commit

Permalink
Change the signature of randomEntry to ensure empty arrays do not b…
Browse files Browse the repository at this point in the history
…reak callsites.

GitOrigin-RevId: 7da31a3794740cf31f4eab917d21d60c080578fb
  • Loading branch information
cpojer committed Nov 5, 2024
1 parent 6ebc172 commit c593db6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion athena/generator/MapGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export function generateRandomMap(
];
const length = size.width * size.height;
for (let i = 0; i < length; i++) {
map.push(randomEntry(tiles).id);
map.push((randomEntry(tiles) || tiles[0]).id);
}

return MapData.createMap({
Expand Down
8 changes: 5 additions & 3 deletions dionysus/DionysusAlpha.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class DionysusAlpha extends BaseAI {
}

if (potentialSkills.length) {
const [skill] = randomEntry(potentialSkills);
const [skill] = randomEntry(potentialSkills) || potentialSkills[0];
const currentMap = this.execute(map, ActivatePowerAction(skill));
if (currentMap) {
this.tryAttacking();
Expand Down Expand Up @@ -718,8 +718,10 @@ export default class DionysusAlpha extends BaseAI {
});

if (buyableSkills.length) {
const skill = randomEntry(buyableSkills);
return this.execute(map, BuySkillAction(from, skill));
return this.execute(
map,
BuySkillAction(from, randomEntry(buyableSkills) || buyableSkills[0]),
);
}

return null;
Expand Down
19 changes: 10 additions & 9 deletions docs/content/playground/PlaygroundDemoGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ import demo2, {
} from '@deities/hermes/map-fixtures/demo-2.tsx';
import PlaygroundGame from './PlaygroundGame.tsx';

const biome = randomEntry([
Biome.Grassland,
Biome.Desert,
Biome.Snow,
Biome.Swamp,
Biome.Volcano,
]);
const biome =
randomEntry([
Biome.Grassland,
Biome.Desert,
Biome.Snow,
Biome.Swamp,
Biome.Volcano,
]) || Biome.Grassland;

const [map, metadata] = randomEntry([
[demo1, metadata1],
[demo1, metadata1],
[demo1, metadata1],
[demo2, metadata2],
]);
]) || [demo1, metadata1];
const currentDemoMap = convertBiome(
map.copy({
config: map.config.copy({
fog: randomEntry([true, false, false, false, false]),
fog: randomEntry([true, false, false, false, false]) || false,
}),
}),
biome,
Expand Down
4 changes: 2 additions & 2 deletions hephaestus/randomEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random from './random.tsx';

export default function randomEntry<T>(array: ReadonlyArray<T>): T {
return array.at(random(0, array.length - 1))!;
export default function randomEntry<T>(array: ReadonlyArray<T>): T | null {
return array.at(random(0, array.length - 1)) || null;
}
2 changes: 1 addition & 1 deletion hera/hooks/useUnitState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const getNextUnitState = (unitState: UnitState, unit: Unit): UnitState => {
switch (stateType) {
case 'idle':
return {
direction: randomEntry(directions),
direction: randomEntry(directions) || directions[0],
highlightStyle: 'move-null',
type: 'move',
};
Expand Down
6 changes: 4 additions & 2 deletions hera/ui/demo/UnitPreviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ const UnitPreview = ({
</div>
);

const playerIDs = PlayerIDs.slice(1);

const map = MapData.createMap({
map: [1],
modifiers: [0],
teams: PlayerIDs.slice(1).map((id) => ({
teams: playerIDs.map((id) => ({
id,
name: '',
players: [{ funds: 0, id, name: `User-${id}`, teamId: id }],
Expand Down Expand Up @@ -84,7 +86,7 @@ export default function UnitPreviews() {
unit.characterDescription !== 'Unknown',
)
.map((unit) => {
const player = randomEntry(PlayerIDs.slice(1));
const player = randomEntry(playerIDs) || playerIDs[0];
return (
<UnitPreview
key={unit.id}
Expand Down

0 comments on commit c593db6

Please sign in to comment.