forked from ErwanLP/formationNode
-
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
Erwan Le Poder
committed
Jul 10, 2017
0 parents
commit 9136eb1
Showing
12 changed files
with
544 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://docs.npmjs.com/cli/shrinkwrap#caveats | ||
node_modules | ||
|
||
# Debug log from npm | ||
npm-debug.log | ||
|
||
|
||
.idea | ||
|
||
npm-debug.log.* |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
var express = require('express'); | ||
var path = require('path'); | ||
var favicon = require('serve-favicon'); | ||
var logger = require('morgan'); | ||
var cookieParser = require('cookie-parser'); | ||
var bodyParser = require('body-parser'); | ||
|
||
var index = require('./routes/index'); | ||
var users = require('./routes/users'); | ||
|
||
var app = express(); | ||
|
||
|
||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'ejs'); | ||
|
||
// uncomment after placing your favicon in /public | ||
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | ||
//app.use(logger('dev')); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({extended: false})); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.use(express.static(path.join(__dirname, 'node_modules'))); | ||
|
||
app.use('/', index); | ||
app.use('/users', users); | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
/* | ||
mongoose.set('debug', function (collectionName, method, query, doc) { | ||
console.log('______________________________'); | ||
console.log('collectionName', collectionName); | ||
console.log('method', method); | ||
console.log('query', query); | ||
console.log('doc', doc); | ||
}); | ||
*/ | ||
|
||
|
||
var timeout = 3000000; | ||
|
||
mongoose.Promise = global.Promise; | ||
mongoose.connect('mongodb://127.0.0.1/formationNode', { | ||
uri_decode_auth: true, | ||
server: {socketOptions: {keepAlive: timeout, connectTimeoutMS: timeout}}, | ||
replset: {socketOptions: {keepAlive: timeout, connectTimeoutMS: timeout}} | ||
}, function (err, db) { | ||
if(err){ | ||
console.error('Connection database : KO'); | ||
console.log(err); | ||
} else{ | ||
console.log('Connection database : OK'); | ||
|
||
} | ||
}); | ||
|
||
|
||
// catch 404 and forward to error handler | ||
app.use(function (req, res, next) { | ||
var err = new Error('Not Found'); | ||
err.status = 404; | ||
next(err); | ||
}); | ||
|
||
// error handler | ||
app.use(function (err, req, res, next) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render('error'); | ||
}); | ||
|
||
module.exports = app; |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('formationnode:server'); | ||
var http = require('http'); | ||
var socket = require('./../socket'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Init Socket | ||
*/ | ||
|
||
socket.init(server); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
|
||
var schema = new mongoose.Schema( | ||
{ | ||
user0: mongoose.Schema.Types.Mixed, | ||
user1: mongoose.Schema.Types.Mixed, | ||
caseArray: [mongoose.Schema.Types.Mixed], | ||
currentPlayer : String | ||
|
||
} | ||
); | ||
|
||
schema.methods.initGame = function (user0, user1) { | ||
if(user0 && user1){ | ||
this.user0 = user0; | ||
this.user1 = user1; | ||
this.caseArray = []; | ||
var rand = Math.floor(Math.random()*100); | ||
if(rand % 2 == 0){ | ||
this.currentPlayer = this.user0.id; | ||
this.user0.color = 'red'; | ||
this.user1.color = 'yellow'; | ||
} else { | ||
this.currentPlayer = this.user1.id; | ||
this.user0.color = 'yellow'; | ||
this.user1.color = 'red'; | ||
} | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
schema.methods.gameAction = function (userId, caseName) { | ||
if(userId == this.currentPlayer){ | ||
if(!this.caseArray){ | ||
this.caseArray = []; | ||
} | ||
if(this.caseArray.filter(function (item) { | ||
return item.caseName == caseName | ||
}).length != 0){ | ||
console.error('try to overwrite'); | ||
} else{ | ||
this.caseArray.push({ | ||
caseName : caseName, | ||
userId : userId, | ||
}) | ||
if(this.currentPlayer){ | ||
if(this.currentPlayer == this.user0.id){ | ||
this.currentPlayer = this.user1.id | ||
} else if(this.currentPlayer == this.user1.id){ | ||
this.currentPlayer = this.user0.id | ||
} else{ | ||
console.error('the value of current player is incorrect'); | ||
} | ||
} else{ | ||
console.error('currentPlayer must be init') | ||
} | ||
} | ||
} else{ | ||
console.error('wrong current player') | ||
} | ||
|
||
|
||
|
||
}; | ||
|
||
|
||
schema.set('versionKey', false); | ||
module.exports = mongoose.model('Game', schema); | ||
|
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "formationnode", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./bin/www" | ||
}, | ||
"dependencies": { | ||
"body-parser": "~1.17.1", | ||
"bootstrap": "^3.3.7", | ||
"cookie-parser": "~1.4.3", | ||
"debug": "~2.6.3", | ||
"ejs": "~2.5.6", | ||
"express": "~4.15.2", | ||
"mongoose": "~4.6.6", | ||
"morgan": "~1.8.1", | ||
"serve-favicon": "~2.4.2", | ||
"ws": "^3.0.0" | ||
} | ||
} |
Oops, something went wrong.