-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (56 loc) · 1.66 KB
/
app.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
const express = require("express");
const { createServer } = require("node:http");
const { join } = require("node:path");
const { Server } = require("socket.io");
const fs = require("fs");
const { randomUUID } = require("node:crypto");
const net = require("./src/ai");
const app = express();
const server = createServer(app);
const io = new Server(server);
const indexRouter = require("./routes/index");
app.set("views", join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(join(__dirname, "public")));
app.use("/", indexRouter);
io.on("connection", async (socket) => {
const roomID = socket.id;
socket.join(roomID);
socket.on(
"direction_request",
(snakeLength, snakeHeadX, snakeHeadY, foodX, foodY, fullSnakeXY) => {
const fullSnakeSquareWidth = 1;
const fullSnakeSquareHeight = 1;
// fullSnakeXY[0].X;
// fullSnakeXY[0].Y;
// fullSnakeXY[fullSnakeXY.lenght - 1].Y;
// fullSnakeXY[fullSnakeXY.lenght - 1].X;
const data = {
snakeLength,
snakeHeadX,
snakeHeadY,
foodX,
foodY,
// fullSnakeXY,
fullSnakeSquareWidth,
fullSnakeSquareHeight,
};
const output = net.run(data);
let maxValue = Number.NEGATIVE_INFINITY;
let highestKey;
for (const key in output) {
if (output[key] > maxValue) {
maxValue = output[key];
highestKey = key;
}
}
io.to(roomID).emit("direction_output", highestKey);
},
);
socket.on("disconnect", () => {
socket.leave("roomID");
});
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});