You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.1 KiB
121 lines
3.1 KiB
const express = require('express');
|
|
const { createServer } = require('node:http');
|
|
const { join } = require('node:path');
|
|
|
|
const Chance = require('chance');
|
|
const mongoose = require("mongoose");
|
|
|
|
const app = express();
|
|
const server = createServer(app);
|
|
const io = require('socket.io')(server, {
|
|
cors: { origin: "*" }
|
|
});
|
|
|
|
const Game = require("./schemas/game-schema");
|
|
const GameLogic = require('./game-logic.js');
|
|
|
|
const databaseUrl = "mongodb://dama:dama@localhost:27017/dama";
|
|
|
|
const chance = new Chance();
|
|
|
|
mongoose.connect(databaseUrl, { useUnifiedTopology: true });
|
|
|
|
io.on('connection', (socket) => {
|
|
console.log('a user ' + socket.id + ' connected');
|
|
|
|
socket.on('disconnect', async () => {
|
|
console.log('user disconnected');
|
|
await Game.deleteMany({owner:socket.id});
|
|
})
|
|
|
|
socket.on('leaveGame', async () => {
|
|
await Game.deleteMany({owner:socket.id});
|
|
})
|
|
|
|
socket.on('joinWithCodeRequest', async ({userName, inviteCode}, callback) => {
|
|
const game = await Game.findOne({"inviteCode":inviteCode});
|
|
|
|
if(!game){
|
|
callback({status:"error", message:"Nebyly nalezeny žádné hry"});
|
|
return;
|
|
}
|
|
|
|
if(game.player1 != null && game.player2 != null){
|
|
callback({status:"error", message:"Místnost je plná"})
|
|
}
|
|
|
|
other_player = null;
|
|
|
|
if(game.player1 == null){
|
|
game.player1 = socket.id;
|
|
game.player1_name = userName;
|
|
|
|
other_player = game.player2;
|
|
}else{
|
|
game.player2 = socket.id;
|
|
game.player2_name = userName;
|
|
|
|
other_player = game.player1;
|
|
}
|
|
|
|
await game.save();
|
|
|
|
|
|
const player1 = game.player1;
|
|
const player1_name = game.player1_name;
|
|
const player2 = game.player2_name;
|
|
const player2_name = game.player2_name;
|
|
const playerSwap = game.playerSwap;
|
|
|
|
callback({status:"success", player1:player1, player1_name:player1_name, player2:player2, player2_name:player2_name, playerSwap:playerSwap})
|
|
io.sockets.sockets.get(other_player).emit('playerJoinGame', {player1, player1_name, player2, player2_name, playerSwap});
|
|
socket.join(game.roomId);
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
socket.on('createGameRequest', ({userName}, callback) => {
|
|
|
|
const roomId = chance.guid();
|
|
const inviteCode = chance.string({ length: 5, casing: 'upper', alpha: true, numeric: true, symbols: false });
|
|
|
|
const gameLogic = new GameLogic();
|
|
gameLogic.generateDefaultPositions();
|
|
const pieceList = gameLogic.pieceList;
|
|
|
|
socket.join(roomId);
|
|
newGame = new Game({
|
|
owner: socket.id,
|
|
player1: socket.id,
|
|
player1_name: userName,
|
|
player2: null,
|
|
player2_name: null,
|
|
channel: roomId,
|
|
playerSwap: false,
|
|
public: false,
|
|
started: false,
|
|
inviteCode: inviteCode,
|
|
currentPlayer: null,
|
|
missedOportunities: [],
|
|
pieces: pieceList
|
|
});
|
|
|
|
newGame.save();
|
|
callback({ status: "success", inviteCode:inviteCode, socket:socket.id, roomId:roomId});
|
|
|
|
});
|
|
|
|
socket.on('joinGameRequest', () => {
|
|
|
|
});
|
|
|
|
socket.on('randomJoinRequest', () => {
|
|
|
|
});
|
|
});
|
|
|
|
server.listen(3000, () => {
|
|
console.log('server running at http://localhost:3000');
|
|
}); |