Skip to content

Commit

Permalink
[WIP] lobby creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Mar 25, 2024
1 parent acdb3c8 commit e5fa928
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 17 deletions.
229 changes: 226 additions & 3 deletions multiplayerservice/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions multiplayerservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"body-parser": "^1.20.2",
"express": "^4.18.2",
"mongoose": "^8.0.4",
"socket.io": "^4.7.5",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
73 changes: 59 additions & 14 deletions multiplayerservice/server.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,70 @@
const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const port = 8006;
const server = http.createServer(app);
const io = socketIo(server);

// app.use(bodyParser.json());
app.use(express.json());
const parties = {}; // Store active parties and their corresponding sockets

const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);
// Generate a random code for the party
function generatePartyCode() {
return Math.random().toString(36).substr(2, 6).toUpperCase();
}

app.get('/', (req, res) => {
res.json({
"hi": "multiplayer service"
// Create a new party
function createParty() {
const partyCode = generatePartyCode();
parties[partyCode] = [];
return partyCode;
}

// Join a party
function joinParty(partyCode, socket) {
if (parties[partyCode]) {
parties[partyCode].push(socket);
return true;
}
return false;
}

io.on('connection', socket => {
console.log('Client connected');

// Create a new party
socket.on('createParty', () => {
const partyCode = createParty();
socket.emit('partyCreated', partyCode);
});
});

// Join an existing party
socket.on('joinParty', partyCode => {
if (joinParty(partyCode, socket)) {
socket.join(partyCode);
socket.emit('joinedParty', partyCode);
console.log(`User joined party: ${partyCode}`);
} else {
socket.emit('partyNotFound');
}
});

const server = app.listen(port, () => {
console.log(`Multiplayer Service listening at http://localhost:${port}`);
socket.on('disconnect', () => {
console.log('Client disconnected');

// Remove the socket from all parties
for (const partyCode in parties) {
const index = parties[partyCode].indexOf(socket);
if (index !== -1) {
parties[partyCode].splice(index, 1);
console.log(`User left party: ${partyCode}`);
break; // Assuming user can only be in one party at a time
}
}
});
});

module.exports = server;
const PORT = process.env.PORT || 8006;
server.listen(PORT, () => {
console.log(`Multiplayer Service listening at http://localhost:${PORT}`);
});

0 comments on commit e5fa928

Please sign in to comment.