-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
226 lines (210 loc) · 8.33 KB
/
Copy pathgame.js
File metadata and controls
226 lines (210 loc) · 8.33 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { createPlayer } from "./player.js";
import { createDeck, shuffle } from "./deck.js";
import { createRound } from "./round.js";
export default function createGame(
onGameStarted,
onGameFinished,
onPlayersUpdated,
onRoundStarted,
onRoundFinished,
onTrickStarted,
onTrickFinished,
onPlayerPlayed,
onNextPlayerTurn
) {
const numberOfPlayers = 4;
const deck = createDeck();
const actions = {
gameStarted: function () {
state.started = true;
onGameStarted();
},
gameFinished: function () {
onGameFinished();
},
playersUpdated: function () {
onPlayersUpdated();
},
};
const listeners = {
onRoundFinished: function (round) {
onRoundFinished(round);
startNextRound();
},
onRoundStarted: function (round) {
onRoundStarted(round);
},
onTrickStarted: function (trick) {
onTrickStarted(trick);
autoPlayIfBot();
},
onTrickFinished: function (trick) {
onTrickFinished(trick);
},
onPlayerPlayed: function (playerIdx, card) {
const player = state.players[playerIdx];
onPlayerPlayed(player, card);
},
onNextPlayerTurn: function (playerIdx){
onNextPlayerTurn(playerIdx)
}
};
const state = {
rounds: [],
players: [],
started: false,
};
function addPlayer(id, name) {
if (
state.players.length >= 4 ||
state.players.filter((x) => x.id === id).length > 0
) {
return {
error: "Erro ao inserir jogador. Sala cheia",
};
}
const player = createPlayer(id, name);
state.players.push(player);
actions.playersUpdated();
return true;
}
function removePlayer(id) {
const playerIdx = state.players.findIndex(
(x) => x.id === id
);
if (playerIdx < 0) {
return;
}
if (state.rounds.length > 0) {
state.players[playerIdx].robot = true;
state.players[playerIdx].name = "BOT " + playerIdx;
} else {
state.players.splice(playerIdx, 1);
}
actions.playersUpdated();
}
function fillMissingPlayersWithBots(players) {
for (
let i = players.length, j = 1;
i < numberOfPlayers;
i++
) {
players[i] = createPlayer(i, "BOT " + j++, true);
}
}
function start() {
fillMissingPlayersWithBots(state.players);
// state.players = shuffle(state.players);
actions.gameStarted();
startNextRound();
}
function checkGameFinished() {
return state.rounds.length >= 13;
}
function startNextRound() {
if (checkGameFinished()) {
gameFinished();
return;
}
const roundNumber = state.rounds.length + 1;
const round = createRound(
deck,
state,
roundNumber,
listeners.onPlayerPlayed,
listeners.onRoundStarted,
listeners.onRoundFinished,
listeners.onTrickStarted,
listeners.onTrickFinished,
listeners.onNextPlayerTurn
);
state.rounds.push(round);
round.start();
}
function getCurrentRound() {
const rounds = state.rounds;
return rounds[rounds.length - 1];
}
function getCurrentPlayer() {
const currentTrick = getCurrentRound().getCurrentTrick()
.trick;
const playerTurnIdx = currentTrick.playerTurnIdx;
const round = currentTrick.round;
return {
...state.players[playerTurnIdx],
idx: playerTurnIdx,
hand: round.playersHands[playerTurnIdx],
};
}
function getCurrentState(){
const currentRound = getCurrentRound()
const round = {number: currentRound.round.number,
tricks: currentRound.round.tricks.map(x=>({
number: x.trick.number,
playerTurnIdx:x.trick.playerTurnIdx,
cardPlays: x.trick.cardPlays
}))}
const currentTrick = currentRound.getCurrentTrick()
const trick = {
number: currentTrick.trick.number,
playerTurnIdx: currentTrick.trick.playerTurnIdx,
cardPlays: currentTrick.trick.cardPlays,
};
const currentState = {
round, trick
}
return currentState
}
function getValidPlayOrError(playerId, cardIdx) {
const currentPlayer = getCurrentPlayer();
if (currentPlayer.id !== playerId) {
const error = {
error: "não é a sua vez de jogar",
};
console.log(error);
return error;
}
const currentRound = getCurrentRound();
return currentRound.getValidPlayOrError(
currentPlayer.idx,
cardIdx
);
}
function playCard(playerId, index) {
const error = getValidPlayOrError(playerId, index);
if (error !== true) {
console.error(error);
return error;
}
const currentRound = getCurrentRound();
const playerIdx = getCurrentPlayer().idx;
currentRound.playCard(playerIdx, index);
return true;
}
function autoPlayIfBot() {
const currentRound = getCurrentRound();
const player = getCurrentPlayer();
if (!player.robot) {
return;
}
const hand = currentRound.round.playersHands[player.idx];
const numberCardsInHand = hand.length;
const handIdxs = [...Array(numberCardsInHand).keys()];
const shuffledIdxs = shuffle(handIdxs);
const cardIdx = shuffledIdxs.find(
(idx) =>
currentRound.getValidPlayOrError(player.idx, idx) ===
true
);
playCard(player.id, cardIdx);
}
return {
state,
addPlayer,
removePlayer,
start,
playCard,
autoPlayIfBot,
getCurrentState
};
}