-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (85 loc) · 2.77 KB
/
Copy pathapp.js
File metadata and controls
99 lines (85 loc) · 2.77 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
const express = require("express");
const cors = require("cors");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, { cors: { origin: "https://localhost:3000" } });
app.use(cors({ origin: "https://localhost:3000" }));
app.get("/", (req, res, next) => {
res.json({ message: "Hello World" });
});
io.on("connection", (socket) => {
console.log("User Conntected", socket.id);
socket.on("room", (room) => {
socket.join(room);
console.log(
`${socket.handshake.auth.player} (${socket.id}) joind room ${room}`
);
});
socket.on("start-game", (room) => {
io.to(room).emit("start-game-seq", true);
console.log(`Starting game for ${room}`);
});
const gamefield = [
["", "", ""],
["", "", ""],
["", "", ""],
];
let moveCount = 0;
socket.on("player-moved", (data) => {
moveCount += 1;
console.log(moveCount);
const haruPosition = {
x: data.haruPosition.x + 1,
y: data.haruPosition.y + 1,
};
const player = socket.handshake.auth.player;
const currentPlayer = player === "player1" ? "player2" : "player1";
gamefield[haruPosition.x][haruPosition.y] = player;
io.to(socket.handshake.auth.room).emit("switch-player", {
currentPlayer: currentPlayer,
});
// if (moveCount >= 3) {
// if (
// (gamefield[0][0] == player &&
// gamefield[0][1] == player &&
// gamefield[0][2] == player) ||
// (gamefield[1][0] == player &&
// gamefield[1][1] == player &&
// gamefield[1][2] == player) ||
// (gamefield[2][0] == player &&
// gamefield[2][1] == player &&
// gamefield[2][2] == player) ||
// (gamefield[0][2] == player &&
// gamefield[1][2] == player &&
// gamefield[2][2] == player) ||
// (gamefield[0][1] == player &&
// gamefield[1][1] == player &&
// gamefield[2][1] == player) ||
// (gamefield[0][0] == player &&
// gamefield[1][0] == player &&
// gamefield[2][0] == player) ||
// (gamefield[0][0] == player &&
// gamefield[1][1] == player &&
// gamefield[2][2] == player) ||
// (gamefield[0][2] == player &&
// gamefield[1][1] == player &&
// gamefield[2][0] == player)
// ) {
// io.to(socket.handshake.auth.room).emit("winner-detected", {
// player: player,
// });
// }
// }
socket.to(socket.handshake.auth.room).emit("move-done", {
haruPosition: data.haruPosition,
});
});
socket.on("disconnect", () => {
console.log("user disconnected");
});
});
server.listen(8080, () => {
console.log("listening on *:8080");
});