-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminesweeper.js
115 lines (102 loc) · 3.23 KB
/
minesweeper.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
const express = require("express");
const app = express();
const http = require("http");
const port = 3000;
const path = require("path");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const { createMap, calculateMinesPercentage } = require("./gameLogic/mapGenerator.js");
const { runGameLogic } = require("./gameLogic/gameLogic.js");
let clientAmount = 0;
let fullMap = [];
let clientMap = [];
/**
* Sends current gamestate to all clients
*/
const updateStateToClients = () => {
io.emit("gamestate updated", clientMap, gameStatus);
};
/**
* Sends the new map to all clients
*/
const updateNewGameToClients = () => {
io.emit("new game activated", clientMap);
};
/**
* Sends a number to all clients representing amount of connected clients
*/
const updatePlayerCount = () => {
io.emit("playercount updated", clientAmount);
};
/**
* Initializes game map into fullMap and clientMap global arrays with given parameters
* @param {int} gameWidth Width of game map in tiles
* @param {int} gameHeight Height of game map in tiles
* @param {int} mineDensity Percentage of mines in map
*/
const initGame = (gameWidth, gameHeight, mineDensity) => {
const maps = createMap(gameHeight, gameWidth, mineDensity);
fullMap = maps.fullMap;
clientMap = maps.clientMap;
};
initGame(24, 16, 15);
app.use(express.static("public"));
app.get("/", (req, res) => {
return res.redirect("/game.html");
});
io.on("connection", (socket) => {
clientAmount += 1;
gameStatus = 0;
updatePlayerCount();
updateStateToClients(clientMap, gameStatus);
socket.on("disconnect", () => {
clientAmount -= 1;
updatePlayerCount();
});
socket.on("clicked", (data) => {
console.log({ data })
console.log("Player clicked on ", { x: data.x, y: data.y, tile: clientMap[data.y][data.x] });
const state = runGameLogic(clientMap, fullMap, data.x, data.y);
if (state === 1) {
console.log("Game won")
gameStatus = 1;
} else if (state === -1) {
console.log("Game over")
clientMap = fullMap // reveal map if game over
gameStatus = -1
} else if (state === 2) {
//first click needs to be safe
var done = false;
while (!done) {
let minesPercentage = calculateMinesPercentage(fullMap);
initGame(clientMap[0].length, clientMap.length, minesPercentage);
updateNewGameToClients(clientMap);
if (runGameLogic(clientMap, fullMap, data.x, data.y) != 2) {
done = true;
}
}
}
if (state != null) {
updateStateToClients();
}
});
socket.on("rightClicked", (data) => {
console.log({ data })
console.log("Player right clicked on ", { x: data.x, y: data.y, tile: clientMap[data.y][data.x] });
if (clientMap[data.y][data.x] === 10) {
clientMap[data.y][data.x] = 9;
} else if (clientMap[data.y][data.x] === 9) {
clientMap[data.y][data.x] = 10;
}
updateStateToClients(clientMap);
});
socket.on("newGame", (data) => {
console.log("Starting a new game...")
initGame(data.gameWidth, data.gameHeight, data.mineDensity);
updateNewGameToClients(clientMap, gameStatus=0);
});
});
server.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});