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.
76 lines
1.9 KiB
76 lines
1.9 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 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.on('createGameRequest', ({userName}, callback) => {
|
|
|
|
const roomId = chance.guid();
|
|
const inviteCode = chance.string({ length: 5, casing: 'upper', alpha: true, numeric: true, symbols: false });
|
|
|
|
socket.join(roomId);
|
|
newGame = new Game({
|
|
owner: socket.id,
|
|
player1: socket.id,
|
|
player1_name: userName,
|
|
player2: null,
|
|
player2_name: null,
|
|
channel: roomId,
|
|
white: 1,
|
|
black: 2,
|
|
public: false,
|
|
started: false,
|
|
inviteCode: inviteCode,
|
|
currentPlayer: null,
|
|
missedOportunities: [String],
|
|
pieces: [
|
|
{
|
|
id: Integer,
|
|
x: Integer,
|
|
y: Integer,
|
|
player: String,
|
|
type: String
|
|
}
|
|
]
|
|
});
|
|
|
|
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');
|
|
}); |