Skip to content

refactor: migrate battle reducer to RTK slice#2632

Open
KagamiChan wants to merge 2 commits intomasterfrom
refactor/battle-rtk-slice
Open

refactor: migrate battle reducer to RTK slice#2632
KagamiChan wants to merge 2 commits intomasterfrom
refactor/battle-rtk-slice

Conversation

@KagamiChan
Copy link
Copy Markdown
Member

What

  • Migrates the legacy views/redux/battle.es reducer logic to an RTK slice in views/redux/battle.ts.
  • Adds views/redux/middlewares/battle-slice.ts to bridge existing @@Response/kcsapi/* actions into the slice reducers.
  • Updates store wiring to use the slice reducer + middleware.
  • Adds Jest coverage for key battle/result behaviors.

Why

  • Keeps battle state updates type-safe and easier to evolve alongside other RTK-migrated reducers.

Notes

  • Adds a minimal poi-lib-battle d.ts shim to keep TS strict without pulling runtime code into tests.

Moves battle handling behind a middleware-driven slice to drop the legacy store-arg reducer and make behavior easier to test.
Run battle slice middleware after next(action) so it reads updated info/sortie state; add regression test covering same-action upstream updates.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Migrates the legacy battle reducer to a Redux Toolkit slice and introduces middleware to translate existing @@Response/kcsapi/* actions into slice actions, with Jest tests added for core battle/result behaviors.

Changes:

  • Replaced the legacy views/redux/battle.es reducer with an RTK slice in views/redux/battle.ts.
  • Added views/redux/middlewares/battle-slice.ts and wired it into the Redux store.
  • Added Jest tests covering map/battle/result flows and a minimal poi-lib-battle TS shim.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
views/redux/reducer-factory.es Switches battle reducer wiring to battleSlice.reducer.
views/redux/create-store.es Adds battleSliceMiddleware to the store middleware chain.
views/redux/middlewares/battle-slice.ts Bridges legacy @@Response/kcsapi/* actions into RTK slice actions.
views/redux/battle.ts New RTK slice implementation + dispatchBattleResult subscriber helper.
views/redux/battle.es Removes legacy battle reducer implementation.
views/redux/tests/battle.spec.ts Adds tests for battle slice + middleware + dispatchBattleResult behavior.
shims/vendor/poi-lib-battle.d.ts Adds minimal typings shim for poi-lib-battle to keep TS strict in tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +31
case '@@Response/kcsapi/api_req_map/start':
store.dispatch(
battleActions.mapStart({
body: a.body,
postBody: a.postBody,
}),
)
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

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

battleSliceMiddleware reads body/postBody from top-level fields (a.body/a.postBody). Actions created via RTK createAction (used widely elsewhere in the repo) typically carry these on action.payload instead; in that case this dispatch would pass undefined postBody and battleSlice.mapStart will throw when accessing postBody.api_deck_id. Consider normalizing by reading from action.payload (with fallback to top-level) before dispatching slice actions.

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +60
store.dispatch(
battleActions.battle({
body: a.body,
path: a.path,
time: a.time,
rootState: store.getState() as unknown,
}),
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

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

Same as mapStart: battle dispatch uses a.body/a.path/a.time, but RTK action creators provide these under action.payload. Consider pulling body/path/time from action.payload when present (or switching the middleware to exclusively use payload), so the bridge works for both onGameResponse actions and createAction-produced actions.

Copilot uses AI. Check for mistakes.
Comment on lines +660 to +676
it('dispatches @@BattleResult and a browser event for valid result', () => {
const dispatch = jest.fn()

const win = { dispatchEvent: jest.fn() }
// @ts-expect-error jest env is not a browser; provide minimal window stub
globalThis.window = win

const result = {
valid: true,
deckShipId: [1],
deckHp: [1],
deckInitHp: [2],
enemyShipId: [501],
enemyHp: [10],
}

dispatchBattleResult(dispatch, result)
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

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

This test suite relies on CustomEvent being present in the Jest runtime when calling dispatchBattleResult, but it only stubs globalThis.window. To make the test deterministic across environments, consider stubbing/polyfilling globalThis.CustomEvent here (and restoring any previous window/CustomEvent after the test).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants