generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
286 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |