-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
89 lines (73 loc) · 2.53 KB
/
Copy pathapp.js
File metadata and controls
89 lines (73 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const fs = require('fs');
const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const PORT = process.env.PORT || 5000;
// Use connect method to connect to the server loccal
//MongoClient.connect("mongodb://localhost:27017", function (err, client) {
//Use connect method to connect to the server online
MongoClient.connect("mongodb://heroku_h7xk0tmx:em7skah8t4vlj4tfmd10efda68@ds247077.mlab.com:47077/heroku_h7xk0tmx", function (err, client) {
if(err) throw err;
//server
const points = client.db('heroku_h7xk0tmx');
//local
//const points = client.db('points');
const scores = points.collection('scores');
scores.insertOne({name: "Felix", score: 9000, clicks: 3, time: 3000})
fs.readFile('views/pages/main.html', (err, html) => {
app.get('/', function (req, res) {
res.send(html.toString());
});
});
fs.readFile('views/pages/game.html', (err, html) => {
app.get('/game', function (req, res) {
res.send(html.toString());
});
});
app.get('/saveUser', function(req, res){
let objId;
let rank;
var newInsert = {
name: req.query.name,
score: parseInt(req.query.score),
clicks: parseInt(req.query.clicks),
time: req.query.time
};
scores.insertOne(newInsert, function(err, docsInserted) {
if (err) throw err;
objId = newInsert._id;
scores.find().sort({score: -1}).toArray(function(err, allScores) {
if (err) throw err;
for (var i = 0; i < allScores.length; i++) {
console.log(i + " " + objId)
if(allScores[i]._id.toString() == objId.toString()) {
rank = i + 1;
}
}
scores.find({ score: { $gt: newInsert.score } } ).sort({score : 1}).limit(2).toArray(function(err, higher) {
if (err) throw err;
scores.find({ score: { $lt: newInsert.score } } ).sort({score : -1}).limit(2).toArray(function(err, lower) {
if (err) throw err;
res.send({
lower: lower,
higher: higher,
rank: rank
});
});
});
});
});
});
app.get('/showTop', function (req, res) {
scores.find().sort({score : -1}).limit(5).toArray(function(err, data) {
if (err) throw err;
res.send(data);
});
});
app.use('/public', express.static(path.join(__dirname, 'public')));
app.listen(PORT);
console.log('listening on port ' + PORT)
});