refactor : PitAnimator and Home

This commit is contained in:
Halit Aksoy 2022-05-22 17:57:33 +03:00
parent f2bb16e4e7
commit 2a600d6357
2 changed files with 8 additions and 9 deletions

View File

@ -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);
};

View File

@ -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);