Skip to content

Commit 9136eb1

Browse files
author
Erwan Le Poder
committed
first commit
0 parents  commit 9136eb1

File tree

12 files changed

+544
-0
lines changed

12 files changed

+544
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://docs.npmjs.com/cli/shrinkwrap#caveats
27+
node_modules
28+
29+
# Debug log from npm
30+
npm-debug.log
31+
32+
33+
.idea
34+
35+
npm-debug.log.*

app.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var index = require('./routes/index');
9+
var users = require('./routes/users');
10+
11+
var app = express();
12+
13+
14+
// view engine setup
15+
app.set('views', path.join(__dirname, 'views'));
16+
app.set('view engine', 'ejs');
17+
18+
// uncomment after placing your favicon in /public
19+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
20+
//app.use(logger('dev'));
21+
app.use(bodyParser.json());
22+
app.use(bodyParser.urlencoded({extended: false}));
23+
app.use(cookieParser());
24+
app.use(express.static(path.join(__dirname, 'public')));
25+
app.use(express.static(path.join(__dirname, 'node_modules')));
26+
27+
app.use('/', index);
28+
app.use('/users', users);
29+
30+
var mongoose = require('mongoose');
31+
32+
/*
33+
mongoose.set('debug', function (collectionName, method, query, doc) {
34+
console.log('______________________________');
35+
console.log('collectionName', collectionName);
36+
console.log('method', method);
37+
console.log('query', query);
38+
console.log('doc', doc);
39+
});
40+
*/
41+
42+
43+
var timeout = 3000000;
44+
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){
52+
console.error('Connection database : KO');
53+
console.log(err);
54+
} else{
55+
console.log('Connection database : OK');
56+
57+
}
58+
});
59+
60+
61+
// 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);
66+
});
67+
68+
// 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');
77+
});
78+
79+
module.exports = app;

bin/www

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('formationnode:server');
9+
var http = require('http');
10+
var socket = require('./../socket');
11+
12+
/**
13+
* Get port from environment and store in Express.
14+
*/
15+
16+
var port = normalizePort(process.env.PORT || '3000');
17+
app.set('port', port);
18+
19+
/**
20+
* Create HTTP server.
21+
*/
22+
23+
var server = http.createServer(app);
24+
25+
/**
26+
* Init Socket
27+
*/
28+
29+
socket.init(server);
30+
31+
/**
32+
* Listen on provided port, on all network interfaces.
33+
*/
34+
35+
server.listen(port);
36+
server.on('error', onError);
37+
server.on('listening', onListening);
38+
39+
/**
40+
* Normalize a port into a number, string, or false.
41+
*/
42+
43+
function normalizePort(val) {
44+
var port = parseInt(val, 10);
45+
46+
if (isNaN(port)) {
47+
// named pipe
48+
return val;
49+
}
50+
51+
if (port >= 0) {
52+
// port number
53+
return port;
54+
}
55+
56+
return false;
57+
}
58+
59+
/**
60+
* Event listener for HTTP server "error" event.
61+
*/
62+
63+
function onError(error) {
64+
if (error.syscall !== 'listen') {
65+
throw error;
66+
}
67+
68+
var bind = typeof port === 'string'
69+
? 'Pipe ' + port
70+
: 'Port ' + port;
71+
72+
// handle specific listen errors with friendly messages
73+
switch (error.code) {
74+
case 'EACCES':
75+
console.error(bind + ' requires elevated privileges');
76+
process.exit(1);
77+
break;
78+
case 'EADDRINUSE':
79+
console.error(bind + ' is already in use');
80+
process.exit(1);
81+
break;
82+
default:
83+
throw error;
84+
}
85+
}
86+
87+
/**
88+
* Event listener for HTTP server "listening" event.
89+
*/
90+
91+
function onListening() {
92+
var addr = server.address();
93+
var bind = typeof addr === 'string'
94+
? 'pipe ' + addr
95+
: 'port ' + addr.port;
96+
debug('Listening on ' + bind);
97+
}

model/gameModel.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var mongoose = require('mongoose');
2+
3+
4+
var schema = new mongoose.Schema(
5+
{
6+
user0: mongoose.Schema.Types.Mixed,
7+
user1: mongoose.Schema.Types.Mixed,
8+
caseArray: [mongoose.Schema.Types.Mixed],
9+
currentPlayer : String
10+
11+
}
12+
);
13+
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+
}
29+
}
30+
};
31+
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+
}
60+
}
61+
} else{
62+
console.error('wrong current player')
63+
}
64+
65+
66+
67+
};
68+
69+
70+
schema.set('versionKey', false);
71+
module.exports = mongoose.model('Game', schema);
72+

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "formationnode",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "node ./bin/www"
7+
},
8+
"dependencies": {
9+
"body-parser": "~1.17.1",
10+
"bootstrap": "^3.3.7",
11+
"cookie-parser": "~1.4.3",
12+
"debug": "~2.6.3",
13+
"ejs": "~2.5.6",
14+
"express": "~4.15.2",
15+
"mongoose": "~4.6.6",
16+
"morgan": "~1.8.1",
17+
"serve-favicon": "~2.4.2",
18+
"ws": "^3.0.0"
19+
}
20+
}

0 commit comments

Comments
 (0)