Initial Websockets, Rooms & Players done
This commit is contained in:
3
API/DB.js
Normal file
3
API/DB.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
DB: 'mongodb://localhost:27017/stick-figure-game'
|
||||
}
|
||||
35
API/index.js
35
API/index.js
@@ -1,17 +1,42 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const webSockets = express();
|
||||
const bodyParser = require('body-parser');
|
||||
const PORT = 4000;
|
||||
const cors = require('cors');
|
||||
const axios = require('axios');
|
||||
const mongoose = require('mongoose');
|
||||
const socket = require('socket.io');
|
||||
const config = require('./DB.js');
|
||||
const roomRoute = require('./room.route');
|
||||
var expressWs = require('express-ws')(app);
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.connect(config.DB, { useNewUrlParser: true }).then(
|
||||
() => { console.log('Database is connected') },
|
||||
err => { console.log('Can not connect to the database' + err) }
|
||||
);
|
||||
|
||||
app.use(cors());
|
||||
app.use(bodyParser.urlencoded({extended: true}));
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use('/rooms', roomRoute);
|
||||
|
||||
app.listen(PORT, function(){
|
||||
console.log('Express server running on port:',PORT);
|
||||
});
|
||||
app.listen(PORT, function () {
|
||||
console.log('Express server running on port:', PORT);
|
||||
})
|
||||
|
||||
app.ws('/:id', function (ws, req) {
|
||||
|
||||
ws.on('message', function (msg) {
|
||||
msgJ = JSON.parse(msg);
|
||||
console.log(req.params.id);
|
||||
console.log(msgJ.playerId);
|
||||
ws.send(msg);
|
||||
});
|
||||
|
||||
ws.onclose = function (event) {
|
||||
console.log("Client disconnected from: " + req.params.id);
|
||||
};
|
||||
console.log("Client connected to: " + req.params.id);
|
||||
});
|
||||
1743
API/package-lock.json
generated
Normal file
1743
API/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,5 +7,16 @@
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
"express-ws": "^4.0.0",
|
||||
"mongoose": "^5.11.17",
|
||||
"socket.io": "^3.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
}
|
||||
}
|
||||
|
||||
13
API/player.model.js
Normal file
13
API/player.model.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
// Define collection and schema for Post
|
||||
let Player = new Schema({
|
||||
name: {
|
||||
type: String
|
||||
}
|
||||
}, {
|
||||
collection: 'players'
|
||||
});
|
||||
|
||||
module.exports = mongoose.model('Player', Player);
|
||||
0
API/player.route.js
Normal file
0
API/player.route.js
Normal file
16
API/room.model.js
Normal file
16
API/room.model.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
// Define collection and schema for Post
|
||||
let Room = new Schema({
|
||||
title: {
|
||||
type: String
|
||||
},
|
||||
members: {
|
||||
type: Array
|
||||
}
|
||||
}, {
|
||||
collection: 'rooms'
|
||||
});
|
||||
|
||||
module.exports = mongoose.model('Room', Room);
|
||||
69
API/room.route.js
Normal file
69
API/room.route.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const express = require('express');
|
||||
const postRoutes = express.Router();
|
||||
|
||||
let Room = require('./room.model');
|
||||
let Player = require('./player.model');
|
||||
|
||||
// Add a room
|
||||
postRoutes.route('/add').get(function (req, res) {
|
||||
let r = new Room();
|
||||
let p = new Player();
|
||||
r.members.push(p);
|
||||
r.save()
|
||||
.then(() => {
|
||||
let ret = {};
|
||||
ret._id = r._id;
|
||||
ret.playerId = p._id;
|
||||
res.send(ret);
|
||||
console.log("Created Room");
|
||||
})
|
||||
.catch(() => {
|
||||
console.log("Unable to save to db");
|
||||
res.status(400).send("Unable to save to the database");
|
||||
});
|
||||
});
|
||||
|
||||
// Join a room
|
||||
postRoutes.route('/join').post(function (req, res) {
|
||||
console.log(req.body);
|
||||
Room.findById(req.body.id, function (err, r) {
|
||||
if (err) {
|
||||
res.json(err);
|
||||
console.log("Error Joining Room");
|
||||
} else {
|
||||
let p = new Player();
|
||||
r.members.push(p);
|
||||
r.save().then(() => {
|
||||
res.send(p);
|
||||
console.log("Joined Room");
|
||||
console.log(r.members);
|
||||
}).catch(() => {
|
||||
console.log("Unable to save to db");
|
||||
res.status(400).send("Unable to save to the database")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
// Defined get data(index or listing) route
|
||||
postRoutes.route('/').get(function (req, res) {
|
||||
Post.find(function (err, posts) {
|
||||
if (err) {
|
||||
res.json(err);
|
||||
}
|
||||
else {
|
||||
res.json(posts);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Defined delete | remove | destroy route
|
||||
postRoutes.route('/delete/:id').delete(function (req, res) {
|
||||
Post.findByIdAndRemove({ _id: req.params.id }, function (err) {
|
||||
if (err) res.json(err);
|
||||
else res.json('Successfully removed');
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = postRoutes;
|
||||
Reference in New Issue
Block a user