-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaygame.ts
71 lines (69 loc) · 2.71 KB
/
playgame.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Player, Event, PlayerType, Position } from "./Interfaces/index";
import { Server } from "socket.io";
import gameArray, { timers } from "./game";
import { comparePosition, isNearbyPosition } from "./check";
function playGame(updatedPlayer: Player, io: Server) {
if (updatedPlayer.playerType !== PlayerType.SPECTATOR) {
const updatedPlayerId = updatedPlayer.id;
const gameIndex = gameArray.findIndex((game) =>
game.players.find((player) => updatedPlayer.id === player.id)
);
const currentGame = gameArray[gameIndex];
let playerIndex = currentGame?.players.findIndex(
(player) => player.id === updatedPlayerId
);
const nearbyPosition = isNearbyPosition(
updatedPlayer.position as Position,
currentGame.players[playerIndex].position as Position
);
console.log(
"Position",
playerIndex,
nearbyPosition,
updatedPlayer.position,
currentGame.players[playerIndex].position
);
if (
playerIndex !== undefined &&
nearbyPosition &&
!currentGame.obstaclePositions.find((obsPosition) =>
comparePosition(updatedPlayer.position, obsPosition.x, obsPosition.y)
) &&
currentGame?.currentPlayer === updatedPlayer.playerType &&
!(updatedPlayer.playerType === PlayerType.WARDER && comparePosition(updatedPlayer.position, currentGame.exitPosition.x, currentGame.exitPosition.y))
) {
currentGame!.players[playerIndex as number] = updatedPlayer;
currentGame.currentPlayer = 1 - currentGame.currentPlayer;
if (
comparePosition(
currentGame!.players[1 - playerIndex].position,
updatedPlayer.position!.x,
updatedPlayer.position!.y
)
) {
clearInterval(timers[gameIndex] as NodeJS.Timeout);
timers[gameIndex] = null;
currentGame.winner = PlayerType.WARDER;
const winnerIndex = currentGame.players.findIndex(player => currentGame.winner === player.playerType)
currentGame.players[winnerIndex].victory++;
}
if (
updatedPlayer.playerType === PlayerType.PRISONER &&
comparePosition(
currentGame.exitPosition,
updatedPlayer.position!.x,
updatedPlayer.position!.y
)
) {
clearInterval(timers[gameIndex] as NodeJS.Timeout);
timers[gameIndex] = null;
currentGame.winner = PlayerType.PRISONER;
const winnerIndex = currentGame.players.findIndex(player => currentGame.winner === player.playerType)
currentGame.players[winnerIndex].victory++;
}
currentGame.timer = currentGame.maxTimer;
io.to(currentGame?.roomCode || "").emit(Event.PLAY_GAME, currentGame);
}
}
}
export default playGame;