-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.js
220 lines (196 loc) · 5.34 KB
/
api.js
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
const { getCard } = require("./DB/Cards");
const {
getServer,
setServer,
deleteServer,
getAllServers,
} = require("./DB/Servers");
const { addPlayer, removePlayer, getPlayer } = require("./DB/PlayersSockets");
const { io } = require("./server");
const { getBots } = require("./DB/Bots");
const GameServer = require("./GameServer");
function joinServer({
serverId,
serverPassword = "",
player,
socket,
cb = () => {},
}) {
const server = getServer(serverId);
if (!server) throw new Error("Server Doesn't Exist");
if (server.serverPassword !== serverPassword)
throw new Error("Invalid Server Password");
if (player.name.trim().length <= 1) throw new Error("Player Name too short");
if (server.players.length >= server.numberOfPlayers)
throw new Error("Server is Already full");
let playerId;
if (socket) {
player.socketId = socket.id;
socket.join(serverId);
playerId = server.joinPlayer(player);
addPlayer(socket.id, playerId, serverId);
} else {
playerId = server.joinPlayer(player);
}
cb(null, playerId);
io.to(serverId).emit("players-changed", server.players);
// TO BE REMOVED (ONLY FOR DEVELOPMENT)
if (server.players.length === 2) {
setTimeout(() => {
const botsToAdd = getBots(server.numberOfPlayers - server.players.length);
for (const bot of botsToAdd) {
joinServer({
serverId,
serverPassword: server.serverPassword,
player: {
...bot,
isBot: true,
},
});
}
}, 5000);
return;
}
if (server.players.length === server.numberOfPlayers) {
initGame(server);
}
}
function createServer({ serverName, serverPassword }) {
if (serverName.trim().length < 2) throw new Error();
const server = new GameServer(serverName, serverPassword);
const serverId = server.serverId;
setServer(server);
server.init();
return serverId;
}
function addBots({ socket }) {
const { playerId, serverId } = getPlayer(socket.id);
const server = getServer(serverId);
if (!server.isAdmin(playerId)) throw new Error("Only Admin Can Add Bots");
const botsToAdd = getBots(server.numberOfPlayers - server.players.length);
for (const bot of botsToAdd) {
joinServer({
serverId,
serverPassword: server.serverPassword,
player: { ...bot, isBot: true },
});
}
io.to(serverId).emit("players-changed", server.players);
if (server.players.length === server.numberOfPlayers) {
initGame(server);
}
}
function initGame(server) {
setTimeout(() => {
server.start();
const playersToSend = server.players.map((player) => ({
...player,
cards: [], // just empty cards objects
}));
for (const player of server.players) {
if (player.socketId) {
io.to(player.socketId).emit("init-game", {
players: playersToSend,
cards: player.cards,
});
}
}
}, 2000);
}
function startGame(serverId) {
const server = getServer(serverId);
if (!server.gameRunning) {
server.gameRunning = true;
if (
server.players[server.curPlayer].disconnected ||
server.players[server.curPlayer].isBot
) {
setTimeout(() => {
moveBot(server);
}, 1500);
}
server.onFinish((playersOrdered) => {
io.to(serverId).emit("finished-game", playersOrdered);
});
}
}
function moveBot(server) {
if (!server) return;
const { card, nxtPlayer, draw } = server.moveBot();
io.to(server.serverId).emit("move", {
nxtPlayer,
card,
draw,
});
if (
server.players[server.curPlayer] &&
(server.players[server.curPlayer].disconnected ||
server.players[server.curPlayer].isBot)
) {
setTimeout(() => {
moveBot(server);
}, 1500);
}
}
function move({ socket, cardId, draw }) {
const { playerId, serverId } = getPlayer(socket.id);
const server = getServer(serverId);
const card = getCard(cardId);
// Check if its my turn
if (server.players[server.curPlayer].id !== playerId)
throw new Error("Not Your Turn");
// Make the move
const { nxtPlayer, cardsToDraw } = server.move(draw, card);
//broadcast to all OTHER players
socket.broadcast.to(serverId).emit("move", {
nxtPlayer,
card,
draw: cardsToDraw?.length,
});
//send to my player
socket.emit("move", {
nxtPlayer,
card,
draw: cardsToDraw?.length,
cardsToDraw,
});
if (
server.players[server.curPlayer].disconnected ||
server.players[server.curPlayer].isBot
) {
setTimeout(() => {
moveBot(server);
}, 1500);
}
}
function leaveServer(socket) {
try {
const player = getPlayer(socket.id);
const { playerId, serverId } = player;
const server = getServer(serverId);
server.leavePlayer(playerId);
let connectedPlayer = 0;
for (const p of server.players) {
if (!p.disconnected && !p.isBot) connectedPlayer++;
}
if (connectedPlayer === 0) deleteServer(serverId);
socket.leave(serverId);
if (server.gameRunning) io.to(serverId).emit("player-left", playerId);
else io.to(serverId).emit("players-changed", server.players);
if (
server.players[server.curPlayer].isBot &&
server.players[server.curPlayer].id === playerId
)
moveBot(server);
removePlayer(socket.id);
} catch (error) {}
}
module.exports = {
joinServer,
createServer,
addBots,
initGame,
startGame,
move,
leaveServer,
};