-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuccessfulMovementEvent.ts
30 lines (26 loc) · 1.38 KB
/
SuccessfulMovementEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { SuccessfulMovementEventData } from "@/core/@typings/EventDataTypes";
import { DispatchedEventNameTypes } from "@/core/@typings/EventTypes";
import { PlayerBoardStatus } from "@/core/@typings/PlayerTypes";
import Event from "@/core/events/Event";
import MovementEvent from "@/core/events/Movement/MovementEvent";
import GameState from "@/core/GameState";
import PlayerBoardBuilder from "@/core/player/PlayerBoardBuilder";
import * as R from "ramda";
export default class SuccessfulMovementEvent extends MovementEvent {
public type: DispatchedEventNameTypes = "SuccessfulMovementEvent";
public data: SuccessfulMovementEventData;
constructor(successfulMovementEventData: SuccessfulMovementEventData) {
super();
this.types = R.append(this.type, this.types);
this.data = successfulMovementEventData;
}
public handle(gameState: GameState) {
const playerBoard = gameState.getPlayerBoard(this.data.playerID, this.data.boardID);
const board = gameState.boards[this.data.boardID];
const boardIsSolved = R.equals(this.data.newCharacterPosition, board.endPoint);
return gameState.replacePlayerBoard(PlayerBoardBuilder.mergeWithOptions(playerBoard, {
characterPosition: this.data.newCharacterPosition,
boardStatus: (boardIsSolved ? PlayerBoardStatus.Solved : PlayerBoardStatus.Playing)
}));
}
}