Skip to content

Small refactor: nearbyUnits readonly UnitType[]#3236

Merged
jrouillard merged 1 commit intomainfrom
nearbyunits-acceptreadonly
Feb 18, 2026
Merged

Small refactor: nearbyUnits readonly UnitType[]#3236
jrouillard merged 1 commit intomainfrom
nearbyunits-acceptreadonly

Conversation

@VariableVince
Copy link
Contributor

@VariableVince VariableVince commented Feb 18, 2026

Description:

PR 4/x in effort to break up PR #3220. Follows on already merged #3235. Precedes #3237.

Please see if these can be merged for v30.

  • Game/GameImpl/GameView: nearbyUnits required "UnitType | UnitType[]" for tiles, but calls UnitGrid nearbyUnits which requires "UnitType | readonly UnitType[]". Made the requirement the same for Game/GameImpl/GameView nearbyUnits. This way, we don't have make a shallow copy of the StructureTypes array everytime we want to send it as an argument. Other callers than listNukeBreakAlliance in Util.ts are unaffected.
  • Util.ts: listNukeBreakAlliance needs no shallow copy of StructureTypes anymore as argument for NearbyUnits

Please complete the following:

  • I have added screenshots for all UI updates
  • I process any text displayed to the user through translateText() and I've added it to the en.json file
  • I have added relevant tests to the test directory
  • I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced

Please put your Discord username so you can be contacted if a bug or regression is found:

tryout33

…(or others in the future) is needed anymore when sending it as argument.
@VariableVince VariableVince added this to the v30 milestone Feb 18, 2026
@VariableVince VariableVince self-assigned this Feb 18, 2026
@VariableVince VariableVince requested a review from a team as a code owner February 18, 2026 21:38
@VariableVince VariableVince added the Refactor Code cleanup, technical debt, refactoring, and architecture improvements. label Feb 18, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 18, 2026

Walkthrough

The pull request updates the nearbyUnits method signature across multiple files to accept readonly array types for the types parameter instead of mutable arrays, improving type safety. One call site is adjusted to pass the argument directly rather than spreading it.

Changes

Cohort / File(s) Summary
nearbyUnits method signatures
src/core/game/Game.ts, src/core/game/GameImpl.ts, src/core/game/GameView.ts
Updated nearbyUnits method signatures to accept UnitType | readonly UnitType[] instead of mutable arrays, tightening the type system to prefer immutable collections while preserving runtime behavior.
Call site update
src/core/execution/Util.ts
Changed nearbyUnits invocation from spreading array argument to passing it directly, aligning with the updated signature.

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🎯 Arrays march in single file,

Readonly and neat all the while,

No spreading, no mess—just right,

TypeScript keeps code tight! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: updating nearbyUnits to accept readonly UnitType[] arrays, which directly matches the core refactor across Game, GameImpl, and GameView.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description check ✅ Passed The pull request description clearly explains the changes: updating nearbyUnits to accept readonly arrays to align with UnitGrid's signature and avoid unnecessary shallow copies of arrays.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/core/game/Game.ts (1)

785-799: Optional: make anyUnitNearby and nearbyUnits consistent with a single-type shorthand.

anyUnitNearby (line 788) accepts only readonly UnitType[], but nearbyUnits (line 796) accepts UnitType | readonly UnitType[] — so a caller with a single UnitType must wrap it in an array for anyUnitNearby but not for nearbyUnits. This asymmetry pre-dates this PR. If you want to eliminate the inconsistency, align both to UnitType | readonly UnitType[]:

♻️ Align anyUnitNearby with nearbyUnits
  anyUnitNearby(
    tile: TileRef,
    searchRange: number,
-   types: readonly UnitType[],
+   types: UnitType | readonly UnitType[],
    predicate: (unit: Unit) => boolean,
    playerId?: PlayerID,
    includeUnderConstruction?: boolean,
  ): boolean;

The same change would propagate to GameImpl.ts, GameView.ts, and UnitGrid.ts for consistency — but that is entirely separate from this PR's scope.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/game/Game.ts` around lines 785 - 799, The two APIs are inconsistent:
change the anyUnitNearby signature to accept UnitType | readonly UnitType[]
(matching nearbyUnits) so callers can pass a single UnitType without wrapping;
update the declaration in Game.ts (anyUnitNearby) and then update all
implementations and overrides (e.g., GameImpl.anyUnitNearby,
GameView.anyUnitNearby, UnitGrid.anyUnitNearby) to accept the same union type,
adjust internal handling to treat a single UnitType as a one-element array where
needed, and update any call sites that assumed the old type accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/core/game/Game.ts`:
- Around line 785-799: The two APIs are inconsistent: change the anyUnitNearby
signature to accept UnitType | readonly UnitType[] (matching nearbyUnits) so
callers can pass a single UnitType without wrapping; update the declaration in
Game.ts (anyUnitNearby) and then update all implementations and overrides (e.g.,
GameImpl.anyUnitNearby, GameView.anyUnitNearby, UnitGrid.anyUnitNearby) to
accept the same union type, adjust internal handling to treat a single UnitType
as a one-element array where needed, and update any call sites that assumed the
old type accordingly.

@github-project-automation github-project-automation bot moved this from Triage to Final Review in OpenFront Release Management Feb 18, 2026
@jrouillard
Copy link
Contributor

Lgtm

@jrouillard jrouillard added this pull request to the merge queue Feb 18, 2026
Merged via the queue into main with commit 4f08e57 Feb 18, 2026
16 checks passed
@jrouillard jrouillard deleted the nearbyunits-acceptreadonly branch February 18, 2026 21:53
@github-project-automation github-project-automation bot moved this from Final Review to Complete in OpenFront Release Management Feb 18, 2026
github-merge-queue bot pushed a commit that referenced this pull request Feb 18, 2026
## Description:

PR 5/x in effort to break up PR
#3220. Follows on already
merged #3236.

Please see if these can be merged for v30.

**NationStructureBehavior**:
- maybeSpawnStructure: cache this.game to be used twice.
- maybeSpawnStructure: instead of hardcoded ruling out Defense Post for
upgrade check, check dynamically if type is upgradable. That way if
defense posts ever do become upgradable, we don't run into a bug right
away.

- maybeUpgradeStructure: removed canUpgradeUnit check. Since it already
checked this right before in findBestStructureToUpgrade, so only
upgradable units are returned. And canUpgradeUnit is also checked right
after in UpgradeStructureExecution. So we're going from 3 times to 2
times canUpgradeUnit, small perf win too.

- findBestStructureToUpgrade: cache this.game to be used thrice.

- shouldBuildStructure: cache this.game.config() to be used twice.

- getTotalStructureDensity: this.player.units can handle an array of
unit types to count. Input StructureTypes like this so we don't need a
loop and count, and only have to get an array length once.
getTotalStructureDensity needs to ignore unit levels so we can't make
use of other pre-defined functions in PlayerImpl (which were created to
avoid array length calls), but at least this saves a few.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

tryout33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Refactor Code cleanup, technical debt, refactoring, and architecture improvements.

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

2 participants

Comments