From 2a600d6357a1248273c9cbc8cc6632dd6b48c105 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sun, 22 May 2022 17:57:33 +0300 Subject: [PATCH] refactor : PitAnimator and Home --- src/Home.tsx | 2 ++ src/animation/PitAnimator.ts | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Home.tsx b/src/Home.tsx index a4e225a..8d832b1 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -17,6 +17,7 @@ import { CommonMancalaGame, MancalaGame, Pit } from "mancala.js"; import { GameMove } from "./models/GameMove"; import PitAnimator from "./animation/PitAnimator"; import BoardViewModel from "./viewmodel/BoardViewModel"; +import { v4 } from "uuid"; type ConnectionState = "connecting" | "error" | "connected" | "reconnecting"; @@ -100,6 +101,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { }; const updateBoardViewModel = (boardViewModel: BoardViewModel) => { + boardViewModel.id = v4(); setBoardId(boardViewModel.id); setBoardViewModel(boardViewModel); }; diff --git a/src/animation/PitAnimator.ts b/src/animation/PitAnimator.ts index d9357d2..8182ed5 100644 --- a/src/animation/PitAnimator.ts +++ b/src/animation/PitAnimator.ts @@ -38,7 +38,7 @@ export default class PitAnimator { public setNewGame(game: MancalaGame) { this.reset(); this.game = game; - this.updateBoardViewModel(this.getBoardViewModelFromGame(this.game)); + this.onBoardViewModelUpdate?.(this.getBoardViewModelFromGame(this.game)); } public setUpdatedGame(game: MancalaGame, forceClear = false) { @@ -69,12 +69,12 @@ export default class PitAnimator { onAnimate() { if (this.animationIndex === this.currentHistoryItem.gameSteps.length) { this.clearCurrentInterval(); - this.updateBoardViewModel(this.getBoardViewModelFromGame(this.game)); + this.onBoardViewModelUpdate?.(this.getBoardViewModelFromGame(this.game)); } else { const gameStep = this.currentHistoryItem.gameSteps[this.animationIndex]; const index = this.game.board.getPitIndexCircularly(gameStep.index); this.animatePit(index, this.oldBoardViewModel, gameStep); - this.updateBoardViewModel(this.oldBoardViewModel); + this.onBoardViewModelUpdate?.(this.oldBoardViewModel); } this.animationIndex++; } @@ -143,7 +143,9 @@ export default class PitAnimator { stopCurrentAnimation() { this.clearCurrentInterval(); if (this.oldGame) { - this.updateBoardViewModel(this.getBoardViewModelFromGame(this.oldGame)); + this.onBoardViewModelUpdate?.( + this.getBoardViewModelFromGame(this.oldGame) + ); } this.resetAnimationState(); } @@ -154,11 +156,6 @@ export default class PitAnimator { } } - updateBoardViewModel(boardViewModel: BoardViewModel) { - boardViewModel.id = v4(); - this.onBoardViewModelUpdate?.(boardViewModel); - } - private getBoardViewModelFromGame(game: MancalaGame): BoardViewModel { const pitViewModels = this.createPitViewModelsFromGame(game); return BoardViewModelFactory.create(v4(), pitViewModels);