From 665d08170711cf9fbf9e361753b2f978299e0d1d Mon Sep 17 00:00:00 2001 From: Alex Ilchenko Date: Wed, 6 Mar 2024 16:43:03 +0100 Subject: [PATCH] refactor(engine): removed unused processing functions --- .../engine/utils/process-merge/index.ts | 1 - .../utils/process-merge/process-merge.test.ts | 46 --- .../utils/process-merge/process-merge.ts | 37 -- .../engine/utils/process-move/index.ts | 1 - .../utils/process-move/process-move.test.ts | 339 ------------------ .../engine/utils/process-move/process-move.ts | 118 ------ 6 files changed, 542 deletions(-) delete mode 100644 src/features/engine/utils/process-merge/index.ts delete mode 100644 src/features/engine/utils/process-merge/process-merge.test.ts delete mode 100644 src/features/engine/utils/process-merge/process-merge.ts delete mode 100644 src/features/engine/utils/process-move/index.ts delete mode 100644 src/features/engine/utils/process-move/process-move.test.ts delete mode 100644 src/features/engine/utils/process-move/process-move.ts diff --git a/src/features/engine/utils/process-merge/index.ts b/src/features/engine/utils/process-merge/index.ts deleted file mode 100644 index 62f9e27..0000000 --- a/src/features/engine/utils/process-merge/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './process-merge'; diff --git a/src/features/engine/utils/process-merge/process-merge.test.ts b/src/features/engine/utils/process-merge/process-merge.test.ts deleted file mode 100644 index f191dbf..0000000 --- a/src/features/engine/utils/process-merge/process-merge.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { expect, test } from 'vitest'; - -import { createEntity } from '../create-entity/create-entity'; -import { processMerge } from '.'; - -test('should merge dices', () => { - const dices = [ - createEntity('dice', { id: 'dice-1', position: { x: 0, y: 0 }, value: 1 }), - createEntity('dice', { id: 'dice-2', position: { x: 0, y: 0 }, value: 1 }), - ]; - - const { dices: result } = processMerge({ dices }); - - expect(result).toHaveLength(3); - expect(result).toEqual( - expect.arrayContaining([ - expect.objectContaining({ isFresh: true, value: 2 }), - expect.objectContaining({ id: 'dice-1', isRemoved: true }), - expect.objectContaining({ id: 'dice-2', isRemoved: true }), - ]), - ); -}); - -test('should not merge dices on different position', () => { - const dices = [ - createEntity('dice', { position: { x: 0, y: 0 }, value: 1 }), - createEntity('dice', { position: { x: 0, y: 1 }, value: 1 }), - ]; - - const { dices: result } = processMerge({ dices }); - - expect(result).toHaveLength(2); - expect(result).toEqual(dices); -}); - -test('should not merge dices of different value', () => { - const dices = [ - createEntity('dice', { position: { x: 0, y: 0 }, value: 1 }), - createEntity('dice', { position: { x: 0, y: 0 }, value: 2 }), - ]; - - const { dices: result } = processMerge({ dices }); - - expect(result).toHaveLength(2); - expect(result).toEqual(dices); -}); diff --git a/src/features/engine/utils/process-merge/process-merge.ts b/src/features/engine/utils/process-merge/process-merge.ts deleted file mode 100644 index 0eb7573..0000000 --- a/src/features/engine/utils/process-merge/process-merge.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Dice } from '../../types/entities'; -import { createEntity } from '../create-entity/create-entity'; -import { getIsSameVector } from '../get-is-same-vector'; - -export const processMerge = ({ dices }: { dices: Dice[] }) => { - const merged = JSON.parse(JSON.stringify(dices)) as Dice[]; - - for (const dice of merged) { - if (dice.isRemoved) { - continue; - } - - const index = merged.findIndex( - (item) => - getIsSameVector(item.position, dice.position) && - item.value === dice.value && - item.id !== dice.id, - ); - - if (index === -1) { - continue; - } - - dice.isRemoved = true; - merged[index].isRemoved = true; - - merged.push( - createEntity('dice', { - isFresh: true, - position: dice.position, - value: dice.value + 1, - }), - ); - } - - return { dices: merged }; -}; diff --git a/src/features/engine/utils/process-move/index.ts b/src/features/engine/utils/process-move/index.ts deleted file mode 100644 index 822be24..0000000 --- a/src/features/engine/utils/process-move/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './process-move'; diff --git a/src/features/engine/utils/process-move/process-move.test.ts b/src/features/engine/utils/process-move/process-move.test.ts deleted file mode 100644 index f143ed8..0000000 --- a/src/features/engine/utils/process-move/process-move.test.ts +++ /dev/null @@ -1,339 +0,0 @@ -import { expect, test } from 'vitest'; - -import { createEntity } from '../create-entity/create-entity'; -import { processMove } from '.'; - -test('should not move dices with no velocity', () => { - const dice = createEntity('dice', { - velocity: { x: 0, y: 0 }, - }); - - const movables = [dice]; - const floors = [ - createEntity('floor'), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - expect(processMove({ floors, movables })).toEqual([dice]); -}); - -test('should not move and stop when there is no floor', () => { - const dice = createEntity('dice', { - velocity: { x: 1, y: 0 }, - }); - - const movables = [dice]; - const floors = [createEntity('floor')]; - - expect( - processMove({ - floors, - movables, - }), - ).toEqual([{ ...dice, velocity: { x: 0, y: 0 } }]); -}); - -test('should move and preserve velocity when there is a floor', () => { - const dice = createEntity('dice', { - velocity: { x: 1, y: 0 }, - }); - - const movables = [dice]; - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - expect( - processMove({ - floors, - movables, - }), - ).toEqual([{ ...dice, position: { x: 1, y: 0 }, velocity: { x: 1, y: 0 } }]); -}); - -test('should pass velocity to the next movable and move it if possible', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 2, - velocity: { x: 0, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - createEntity('floor', { position: { x: 2, y: 0 } }), - ]; - - const expectedResult = [ - { ...dice1, position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 2, y: 0 }, velocity: { x: 1, y: 0 } }, - ]; - - // dice1 first - const result1 = processMove({ - floors, - movables: [dice1, dice2], - }); - - expect(result1).toHaveLength(expectedResult.length); - expect(result1).toEqual(expect.arrayContaining(expectedResult)); - - // dice2 first - const result2 = processMove({ - floors, - movables: [dice2, dice1], - }); - - expect(result2).toHaveLength(expectedResult.length); - expect(result2).toEqual(expect.arrayContaining(expectedResult)); -}); - -test('should stop when next movable can not be moved further', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 2, - velocity: { x: 0, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - ]; - - // dice1 first - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); - - // dice2 first - expect( - processMove({ - floors, - movables: [dice2, dice1], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should move last in a row of movables', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 2, - velocity: { x: 0, y: 0 }, - }); - const dice3 = createEntity('dice', { - id: 'dice-3', - position: { x: 2, y: 0 }, - value: 3, - velocity: { x: 0, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - createEntity('floor', { position: { x: 2, y: 0 } }), - createEntity('floor', { position: { x: 3, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice3, position: { x: 3, y: 0 }, velocity: { x: 1, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2, dice3], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should stop both movables when they are moving towards each other and are not dices of same value', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 0, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 1, - velocity: { x: -1, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should merge dices when they are moving towards each other and are dices of the same value', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 1, - velocity: { x: -1, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should move both movables when they are moving in the same direction', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - velocity: { x: 1, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - createEntity('floor', { position: { x: 2, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 1, y: 0 }, velocity: { x: 1, y: 0 } }, - { ...dice2, position: { x: 2, y: 0 }, velocity: { x: 1, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should move when next is dice of same value', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - position: { x: 1, y: 0 }, - value: 1, - velocity: { x: 0, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); -}); - -test('should not move into a fresh dice of same value', () => { - const dice1 = createEntity('dice', { - id: 'dice-1', - position: { x: 0, y: 0 }, - value: 1, - velocity: { x: 1, y: 0 }, - }); - const dice2 = createEntity('dice', { - id: 'dice-2', - isFresh: true, - position: { x: 1, y: 0 }, - value: 1, - velocity: { x: 0, y: 0 }, - }); - - const floors = [ - createEntity('floor', { position: { x: 0, y: 0 } }), - createEntity('floor', { position: { x: 1, y: 0 } }), - ]; - - const result = [ - { ...dice1, position: { x: 0, y: 0 }, velocity: { x: 0, y: 0 } }, - { ...dice2, position: { x: 1, y: 0 }, velocity: { x: 0, y: 0 } }, - ]; - - expect( - processMove({ - floors, - movables: [dice1, dice2], - }), - ).toEqual(expect.arrayContaining(result)); -}); diff --git a/src/features/engine/utils/process-move/process-move.ts b/src/features/engine/utils/process-move/process-move.ts deleted file mode 100644 index 0626992..0000000 --- a/src/features/engine/utils/process-move/process-move.ts +++ /dev/null @@ -1,118 +0,0 @@ -/* eslint-disable sonarjs/cognitive-complexity */ -import { VECTOR_ZERO } from '../../constants'; -import { Floor, isDice, Movable } from '../../types/entities'; -import { getClone } from '../get-clone'; -import { getIsOppositeVector } from '../get-is-opposite-vector'; -import { getIsSameVector } from '../get-is-same-vector'; -import { getSumVector } from '../get-sum-vector'; - -export const processMove = ({ - floors, - movables, -}: { - floors: Floor[]; - movables: Movable[]; -}): Movable[] => { - const result = getClone(movables); - const toResolve = [...result]; - - while (toResolve.length > 0) { - const current = toResolve.shift() as Movable; - - if (current.isRemoved) { - continue; - } - - const currentFloor = floors.find((floor) => - getIsSameVector(current.position, floor.position), - ); - - if (!currentFloor) { - continue; - } - - if (getIsSameVector(current.velocity, VECTOR_ZERO)) { - // resolve if it's not moving - continue; - } - - const nextPosition = getSumVector(current.position, current.velocity); - - const nextFloor = floors.find((floor) => - getIsSameVector(nextPosition, floor.position), - ); - - if (!nextFloor) { - // stop and resolve if there is no floor at the next position - current.velocity = getClone(VECTOR_ZERO); - - continue; - } - - const nextMovable = result.find((movable) => - getIsSameVector(nextPosition, movable.position), - ); - - if (!nextMovable) { - // move and resolve if there is no movable at the next position - current.position = nextPosition; - - // apply the direction of the next floor if it has one - if (nextFloor.direction) { - current.velocity = nextFloor.direction; - } - - continue; - } - - if (toResolve.includes(nextMovable)) { - if (!getIsOppositeVector(current.velocity, nextMovable.velocity)) { - // resolve later if the next movable is not resolved yet - toResolve.push(current); - - continue; - } - - // stop and resolve both if the next movable is moving to the opposite direction - current.velocity = getClone(VECTOR_ZERO); - nextMovable.velocity = getClone(VECTOR_ZERO); - toResolve.splice(toResolve.indexOf(nextMovable), 1); - - if ( - isDice(nextMovable) && - isDice(current) && - nextMovable.value === current.value - ) { - // merge and resolve if the next movable is a dice and has the same value - current.position = nextPosition; - } - - continue; - } - - if (getIsSameVector(current.velocity, nextMovable.velocity)) { - // move and resolve if the next movable is moving to the same direction - continue; - } - - if ( - isDice(nextMovable) && - isDice(current) && - nextMovable.value === current.value && - !nextMovable.isFresh - ) { - current.position = nextPosition; - current.velocity = getClone(VECTOR_ZERO); - nextMovable.velocity = getClone(VECTOR_ZERO); - - continue; - } - - // stop current and unresolve next movable - nextMovable.velocity = getClone(current.velocity); - current.velocity = getClone(VECTOR_ZERO); - toResolve.push(nextMovable); - } - - return result; -};