Skip to content

Commit 27678bc

Browse files
author
Erwan
committed
docker-compose + atlas
1 parent e156d17 commit 27678bc

File tree

8 files changed

+870
-207
lines changed

8 files changed

+870
-207
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# formationNode
22

3-
4-
create mongo database 'formationNode'
5-
6-
git clone
7-
npm install
8-
npm start
3+
```
4+
docker-compose up
5+
```

app.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var users = require('./routes/users');
1010

1111
var app = express();
1212

13-
1413
// view engine setup
1514
app.set('views', path.join(__dirname, 'views'));
1615
app.set('view engine', 'ejs');
@@ -39,41 +38,39 @@ mongoose.set('debug', function (collectionName, method, query, doc) {
3938
});
4039
*/
4140

42-
4341
var timeout = 3000000;
4442

45-
mongoose.Promise = global.Promise;
46-
mongoose.connect('mongodb://127.0.0.1/formationNode', {
47-
uri_decode_auth: true,
48-
server: {socketOptions: {keepAlive: timeout, connectTimeoutMS: timeout}},
49-
replset: {socketOptions: {keepAlive: timeout, connectTimeoutMS: timeout}}
50-
}, function (err, db) {
51-
if(err){
43+
mongoose.connect(
44+
'mongodb+srv://formationNodeUser:formationNodePassword@cluster0-kt0tr.mongodb.net/test?retryWrites=true&w=majority',
45+
{
46+
promiseLibrary: global.Promise,
47+
useNewUrlParser: true
48+
}, function(err, db) {
49+
if (err) {
5250
console.error('Connection database : KO');
53-
console.log(err);
54-
} else{
51+
console.error(err);
52+
} else {
5553
console.log('Connection database : OK');
5654

57-
}
58-
});
59-
55+
}
56+
});
6057

6158
// catch 404 and forward to error handler
62-
app.use(function (req, res, next) {
63-
var err = new Error('Not Found');
64-
err.status = 404;
65-
next(err);
59+
app.use(function(req, res, next) {
60+
var err = new Error('Not Found');
61+
err.status = 404;
62+
next(err);
6663
});
6764

6865
// error handler
69-
app.use(function (err, req, res, next) {
70-
// set locals, only providing error in development
71-
res.locals.message = err.message;
72-
res.locals.error = req.app.get('env') === 'development' ? err : {};
73-
74-
// render the error page
75-
res.status(err.status || 500);
76-
res.render('error');
66+
app.use(function(err, req, res, next) {
67+
// set locals, only providing error in development
68+
res.locals.message = err.message;
69+
res.locals.error = req.app.get('env') === 'development' ? err : {};
70+
71+
// render the error page
72+
res.status(err.status || 500);
73+
res.render('error');
7774
});
7875

7976
module.exports = app;

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3'
2+
3+
services:
4+
5+
node:
6+
image: thecodingmachine/nodejs:10
7+
volumes:
8+
- ./:/usr/src/app
9+
environment:
10+
STARTUP_COMMAND_1: npm install
11+
STARTUP_COMMAND_2: npm start
12+
ports:
13+
- "3000:3000"

model/gameModel.js

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,65 @@
11
var mongoose = require('mongoose');
22

3-
43
var schema = new mongoose.Schema(
54
{
6-
user0: mongoose.Schema.Types.Mixed,
7-
user1: mongoose.Schema.Types.Mixed,
8-
caseArray: [mongoose.Schema.Types.Mixed],
9-
currentPlayer : String
5+
user0: mongoose.Schema.Types.Mixed,
6+
user1: mongoose.Schema.Types.Mixed,
7+
caseArray: [mongoose.Schema.Types.Mixed],
8+
currentPlayer: Number
109

1110
}
1211
);
1312

14-
schema.methods.initGame = function (user0, user1) {
15-
if(user0 && user1){
16-
this.user0 = user0;
17-
this.user1 = user1;
18-
this.caseArray = [];
19-
var rand = Math.floor(Math.random()*100);
20-
if(rand % 2 == 0){
21-
this.currentPlayer = this.user0.id;
22-
this.user0.color = 'red';
23-
this.user1.color = 'yellow';
24-
} else {
25-
this.currentPlayer = this.user1.id;
26-
this.user0.color = 'yellow';
27-
this.user1.color = 'red';
28-
}
13+
schema.methods.initGame = function(user0, user1) {
14+
if (user0 && user1) {
15+
this.user0 = user0;
16+
this.user1 = user1;
17+
this.caseArray = [];
18+
var rand = Math.floor(Math.random() * 100);
19+
if (rand % 2 === 0) {
20+
this.currentPlayer = this.user0.id;
21+
this.user0.color = 'red';
22+
this.user1.color = 'yellow';
23+
} else {
24+
this.currentPlayer = this.user1.id;
25+
this.user0.color = 'yellow';
26+
this.user1.color = 'red';
2927
}
28+
}
3029
};
3130

32-
33-
34-
35-
schema.methods.gameAction = function (userId, caseName) {
36-
if(userId == this.currentPlayer){
37-
if(!this.caseArray){
38-
this.caseArray = [];
39-
}
40-
if(this.caseArray.filter(function (item) {
41-
return item.caseName == caseName
42-
}).length != 0){
43-
console.error('try to overwrite');
44-
} else{
45-
this.caseArray.push({
46-
caseName : caseName,
47-
userId : userId,
48-
})
49-
if(this.currentPlayer){
50-
if(this.currentPlayer == this.user0.id){
51-
this.currentPlayer = this.user1.id
52-
} else if(this.currentPlayer == this.user1.id){
53-
this.currentPlayer = this.user0.id
54-
} else{
55-
console.error('the value of current player is incorrect');
56-
}
57-
} else{
58-
console.error('currentPlayer must be init')
59-
}
31+
schema.methods.gameAction = function(userId, caseName) {
32+
if (userId === parseInt(this.currentPlayer)) {
33+
if (!this.caseArray) {
34+
this.caseArray = [];
35+
}
36+
if (this.caseArray.filter(function(item) {
37+
return item.caseName === caseName;
38+
}).length !== 0) {
39+
console.error('try to overwrite');
40+
} else {
41+
this.caseArray.push({
42+
caseName: caseName,
43+
userId: userId
44+
});
45+
if (this.currentPlayer) {
46+
if (parseInt(this.currentPlayer) === this.user0.id) {
47+
this.currentPlayer = this.user1.id;
48+
} else if (parseInt(this.currentPlayer) === this.user1.id) {
49+
this.currentPlayer = this.user0.id;
50+
} else {
51+
console.error('the value of current player is incorrect');
6052
}
61-
} else{
62-
console.error('wrong current player')
53+
} else {
54+
console.error('currentPlayer must be init');
55+
}
6356
}
64-
65-
57+
} else {
58+
console.error('wrong current player');
59+
}
6660

6761
};
6862

69-
7063
schema.set('versionKey', false);
7164
module.exports = mongoose.model('Game', schema);
7265

0 commit comments

Comments
 (0)