2020-07-07 23:52:45 +00:00
|
|
|
// TODO const someSocketLogic = require('./middleware/sockets/...');
|
2020-05-30 23:01:13 +00:00
|
|
|
const socketIO = require("socket.io");
|
2020-01-21 22:33:40 +00:00
|
|
|
const io = socketIO({ cookie: false });
|
2020-01-08 19:24:24 +00:00
|
|
|
|
2020-05-12 01:18:42 +00:00
|
|
|
// const gameQueries = require('./data/queries/game');
|
2020-05-30 23:01:13 +00:00
|
|
|
const moveQueries = require("./data/queries/move");
|
2020-06-20 23:25:09 +00:00
|
|
|
const gameQueries = require("./data/queries/game");
|
|
|
|
const gameServices = require("./services/gameServices")({
|
|
|
|
moveQueries,
|
|
|
|
gameQueries,
|
|
|
|
});
|
2020-01-24 05:25:08 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
io.on("connection", async (socket) => {
|
|
|
|
socket.emit("connected", { message: "socket connected" });
|
|
|
|
socket.on("connect_room", async (data) => {
|
2020-01-28 06:40:03 +00:00
|
|
|
if (data.user && data.user.email) {
|
|
|
|
delete data.user.email;
|
|
|
|
}
|
2020-05-03 06:22:57 +00:00
|
|
|
const room = data.room;
|
2020-01-30 01:01:46 +00:00
|
|
|
const roomIo = io.of(room);
|
2020-05-30 23:01:13 +00:00
|
|
|
roomIo.on("connection", async (socket) => {
|
|
|
|
socket.emit("connected");
|
|
|
|
socket.emit("new_user", data);
|
|
|
|
socket.on("connect_game", (data) => {
|
2020-01-30 01:01:46 +00:00
|
|
|
const game = `game-${data.game.id}`;
|
2020-02-01 04:28:39 +00:00
|
|
|
socket.join(game, async () => {
|
2020-07-07 23:52:45 +00:00
|
|
|
// TODO move this logic into game service
|
2020-07-01 21:32:03 +00:00
|
|
|
const gameData = await gameQueries.findGameById(data.game.id);
|
|
|
|
const convertWinType = (winType) => {
|
|
|
|
if (winType.includes("B")) return 1;
|
|
|
|
if (winType.includes("W")) return -1;
|
|
|
|
if (winType.includes("0")) return "D";
|
|
|
|
return "?";
|
|
|
|
};
|
|
|
|
gameData.winner = gameData.win_type
|
|
|
|
? convertWinType(gameData.win_type)
|
|
|
|
: 0;
|
2020-05-12 01:18:42 +00:00
|
|
|
const gameRecord = await moveQueries.findGameRecord(data.game.id);
|
2020-07-01 21:32:03 +00:00
|
|
|
await gameServices.initGame({
|
|
|
|
id: data.game.id,
|
|
|
|
gameRecord,
|
|
|
|
gameData,
|
|
|
|
});
|
2020-05-30 23:01:13 +00:00
|
|
|
const { board, ...meta } = await gameServices.getDataForUI(
|
|
|
|
data.game.id
|
|
|
|
);
|
|
|
|
io.of(room).to(game).emit("game_connected", { board, meta });
|
2020-01-30 01:01:46 +00:00
|
|
|
});
|
|
|
|
});
|
2020-06-08 00:06:26 +00:00
|
|
|
|
|
|
|
// MAKE MOVE
|
2020-05-30 23:01:13 +00:00
|
|
|
socket.on("make_move", async (data) => {
|
2020-01-31 05:03:27 +00:00
|
|
|
const { user, move, board, game, room } = data;
|
2020-02-01 06:22:43 +00:00
|
|
|
const gameNsp = `game-${data.game.id}`;
|
|
|
|
try {
|
2020-05-30 23:01:13 +00:00
|
|
|
const { board, message, ...meta } = await gameServices.makeMove({
|
2020-05-30 23:43:41 +00:00
|
|
|
id: data.game.id,
|
2020-05-30 23:01:13 +00:00
|
|
|
move,
|
|
|
|
});
|
|
|
|
const socketAction = message ? "error" : "update_board";
|
2020-02-01 06:22:43 +00:00
|
|
|
socket.join(gameNsp, () => {
|
2020-05-30 23:01:13 +00:00
|
|
|
io.of(room)
|
|
|
|
.to(gameNsp)
|
|
|
|
.emit(socketAction, { board, meta, message });
|
2020-02-01 06:22:43 +00:00
|
|
|
});
|
2020-05-30 23:01:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2020-02-01 06:22:43 +00:00
|
|
|
socket.join(gameNsp, () => {
|
2020-05-30 23:01:13 +00:00
|
|
|
io.of(room).to(gameNsp).emit("error", e);
|
2020-02-01 06:22:43 +00:00
|
|
|
});
|
|
|
|
}
|
2020-05-30 23:01:13 +00:00
|
|
|
});
|
2020-06-08 00:06:26 +00:00
|
|
|
|
|
|
|
// RESIGN
|
2020-06-07 20:47:09 +00:00
|
|
|
socket.on("resign", async ({ game, player }) => {
|
|
|
|
const { id, room } = game;
|
|
|
|
const gameNsp = `game-${id}`;
|
|
|
|
try {
|
|
|
|
const meta = await gameServices.resign({
|
|
|
|
id,
|
|
|
|
player,
|
|
|
|
});
|
|
|
|
socket.join(gameNsp, () => {
|
|
|
|
io.of(room).to(gameNsp).emit("game_resign", meta);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2020-06-17 18:59:21 +00:00
|
|
|
|
2020-06-08 00:06:26 +00:00
|
|
|
// PASS
|
|
|
|
socket.on("pass", async ({ game, player }) => {
|
|
|
|
const { id, room } = game;
|
|
|
|
const gameNsp = `game${id}`;
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
board,
|
|
|
|
message,
|
|
|
|
territory,
|
|
|
|
...meta
|
|
|
|
} = await gameServices.pass({
|
|
|
|
id,
|
|
|
|
player,
|
|
|
|
});
|
|
|
|
socket.join(gameNsp, () => {
|
|
|
|
io.of(room)
|
|
|
|
.to(gameNsp)
|
|
|
|
.emit("update_board", { board, message, territory, meta });
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2020-06-17 18:59:21 +00:00
|
|
|
|
|
|
|
// TOGGLE TERRITORY
|
|
|
|
socket.on("toggle_territory", async ({ user, point, board, game }) => {
|
|
|
|
const { id, room } = game;
|
|
|
|
const gameNsp = `game${id}`;
|
|
|
|
try {
|
2020-06-26 23:24:55 +00:00
|
|
|
const {
|
|
|
|
board,
|
|
|
|
territory,
|
|
|
|
...meta
|
|
|
|
} = await gameServices.toggleTerritory({
|
2020-06-17 18:59:21 +00:00
|
|
|
id,
|
|
|
|
point,
|
|
|
|
});
|
|
|
|
socket.join(gameNsp, () => {
|
2020-06-26 23:24:55 +00:00
|
|
|
io.of(room)
|
|
|
|
.to(gameNsp)
|
|
|
|
.emit("update_board", { board, territory, meta });
|
2020-06-17 18:59:21 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2020-06-20 23:25:09 +00:00
|
|
|
|
|
|
|
// END GAME
|
|
|
|
socket.on("end_game", async ({ user, game }) => {
|
|
|
|
const { id, room } = game;
|
|
|
|
const gameNsp = `game${id}`;
|
|
|
|
try {
|
|
|
|
const { board, ...meta } = await gameServices.endGame({ id });
|
|
|
|
socket.join(gameNsp, () => {
|
|
|
|
io.of(room).to(gameNsp).emit("end_game", { board, meta });
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
});
|
2020-01-30 01:01:46 +00:00
|
|
|
});
|
2020-05-30 23:01:13 +00:00
|
|
|
});
|
|
|
|
});
|
2020-01-08 19:24:24 +00:00
|
|
|
|
2020-01-21 22:33:40 +00:00
|
|
|
module.exports = {
|
2020-05-30 23:01:13 +00:00
|
|
|
io,
|
|
|
|
};
|