2023-12-17 19:06:53 -05:00
|
|
|
import packageJSON from './package.json' with { type: "json" }
|
|
|
|
|
2023-05-18 11:57:15 -04:00
|
|
|
import { textFromBoard } from './src/logic.js'
|
|
|
|
import { Board } from './src/backend.js';
|
2021-01-06 08:44:34 -05:00
|
|
|
|
2023-05-18 11:57:15 -04:00
|
|
|
import express from 'express';
|
|
|
|
import http from 'http';
|
|
|
|
import { Server } from 'socket.io';
|
2021-01-06 08:44:34 -05:00
|
|
|
|
|
|
|
const port = process.env.PORT || 4001;
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2023-12-17 19:06:53 -05:00
|
|
|
app.get('/status', (req, res) => {
|
|
|
|
res.send('OK. Version: ' + packageJSON.version + '\n')
|
|
|
|
})
|
|
|
|
|
2021-01-06 08:44:34 -05:00
|
|
|
const server = http.createServer(app);
|
|
|
|
|
2023-05-18 11:57:15 -04:00
|
|
|
const io = new Server(server, {
|
2021-01-06 08:44:34 -05:00
|
|
|
cors: {
|
|
|
|
cors: true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let interval;
|
|
|
|
|
2021-01-06 12:05:57 -05:00
|
|
|
let games = new Map();
|
2021-01-06 08:44:34 -05:00
|
|
|
|
|
|
|
io.on("connection", (socket) => {
|
|
|
|
console.log("New client connected");
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
//interval = setInterval(() => getApiAndEmit(socket), 5000);
|
|
|
|
socket.on("disconnect", () => {
|
2023-05-18 11:57:15 -04:00
|
|
|
const game = socket.connectedGame
|
|
|
|
const gameMessage = game ? `from game '${game.key}'` : "and was not attached to a game"
|
|
|
|
console.log("Client disconnected " + gameMessage);
|
2021-01-06 08:44:34 -05:00
|
|
|
clearInterval(interval);
|
|
|
|
});
|
|
|
|
|
2022-10-12 22:01:09 -04:00
|
|
|
socket.on("find", key => {
|
|
|
|
key = key?.toLowerCase()
|
2021-01-06 12:05:57 -05:00
|
|
|
console.log(`Finding game '${key}'`);
|
|
|
|
let game = games.get(key);
|
|
|
|
if(game) {
|
|
|
|
console.log(`Found game '${key}'`);
|
|
|
|
game.sockets.push(socket);
|
|
|
|
} else {
|
|
|
|
game = {
|
2023-05-18 11:57:15 -04:00
|
|
|
key,
|
2021-01-06 12:05:57 -05:00
|
|
|
sockets: [socket],
|
|
|
|
board: new Board(),
|
|
|
|
moveDate: null,
|
|
|
|
};
|
|
|
|
console.log(`Created game '${key}'`);
|
|
|
|
}
|
2023-05-18 11:57:15 -04:00
|
|
|
socket.connectedGame = game;
|
2021-01-06 12:05:57 -05:00
|
|
|
games.set(key, game);
|
|
|
|
getApiAndEmit(socket, game.board);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("move", (key, move) => {
|
|
|
|
console.log(`Received move at '${key}'`);
|
|
|
|
let game = games.get(key);
|
2022-10-12 23:12:43 -04:00
|
|
|
if(move && game.board.makeMove(move.from, move.to) === 0) {
|
2021-01-06 12:05:57 -05:00
|
|
|
games.set(key, game);
|
2022-10-12 23:12:43 -04:00
|
|
|
for (const s of game.sockets) {
|
2021-01-06 12:05:57 -05:00
|
|
|
getApiAndEmit(s, game.board);
|
2021-01-06 08:44:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-06 12:05:57 -05:00
|
|
|
const getApiAndEmit = (socket, board) => {
|
2022-10-12 23:44:29 -04:00
|
|
|
const response = textFromBoard(board)
|
2021-01-06 08:44:34 -05:00
|
|
|
console.log(response);
|
|
|
|
// Emitting a new message. Will be consumed by the client
|
|
|
|
socket.emit("FromAPI", response);
|
|
|
|
};
|
|
|
|
|
|
|
|
server.listen(port, () => console.log(`Listening on port ${port}`));
|