-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (83 loc) · 3.52 KB
/
server.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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
let players = [];
let currentPlayerIndex = 0;
const TOTAL_BOXES = 39;
const specialBoxes = {
2: { action: 'move', value: 3 },
4: { action: 'set', value: 0 },
10: { action: 'skip', value: 0 },
13: { action: 'set', value: 15},
21: { action: 'move', value: 2},
24: { action: 'set', value: 22},
36: { action: 'move', value: -2},
};
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('join', (playerName) => {
if (players.length < 6) {
players.push({ id: socket.id, name: playerName, position: 0});
socket.emit('gameState', { players, currentPlayerIndex });
socket.broadcast.emit('playerJoined', { id: socket.id, name: playerName });
if (players.length === 1) {
// First player joined, start their turn
io.to(players[currentPlayerIndex].id).emit('yourTurn');
}
} else {
socket.emit('gameFull');
}
});
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
socket.on('rollDice', async (diceRoll) => {
const playerIndex = players.findIndex(p => p.id === socket.id);
if (playerIndex === currentPlayerIndex) {
players[playerIndex].position = (players[playerIndex].position + diceRoll) //Update position based on dice roll
let difference_to_end = TOTAL_BOXES - players[playerIndex].position; //Ensure that if position is bigger than TOTAL_BOX, pawn goes back
if(difference_to_end < 0){
players[playerIndex].position = TOTAL_BOXES + difference_to_end;
}
io.emit('gameState', { players, currentPlayerIndex }); //Update players position
if(specialBoxes[players[playerIndex].position]){ //Test for special cases
const specialAction = specialBoxes[players[playerIndex].position];
if(specialAction.action == "move"){
players[playerIndex].position = Math.min(players[playerIndex].position + specialAction.value, TOTAL_BOXES);
await delay(500);
}
if(specialAction.action == "set"){
players[playerIndex].position = specialAction.value;
await delay(500);
}
if(specialAction.action == "skip"){
io.to(players[playerIndex].id).emit('addSkip');
}
}
currentPlayerIndex = (currentPlayerIndex + 1) % players.length; //Set next turn to next player
io.to(players[currentPlayerIndex].id).emit('yourTurn'); //Goes to next turn
io.emit('gameState', { players, currentPlayerIndex }); //Update game state for every player
}
});
socket.on('disconnect', () => {
const disconnectedPlayerIndex = players.findIndex(p => p.id === socket.id);
if (disconnectedPlayerIndex !== -1) {
players.splice(disconnectedPlayerIndex, 1);
if (disconnectedPlayerIndex < currentPlayerIndex) {
currentPlayerIndex--;
} else if (disconnectedPlayerIndex === currentPlayerIndex) {
currentPlayerIndex = currentPlayerIndex % players.length;
}
currentPlayerIndex = Math.max(0, Math.min(currentPlayerIndex, players.length - 1));
io.emit('playerLeft', socket.id);
io.emit('gameState', { players, currentPlayerIndex });
if (players.length > 0) {
io.to(players[currentPlayerIndex].id).emit('yourTurn');
}
}
});
});
http.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});