-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
156 lines (119 loc) · 4.68 KB
/
index.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require('express');
// var mongo = require('mongodb');
//mongodb://heroku_kvdqb8k0:[email protected]:29085/heroku_kvdqb8k0
var mongoose = require("mongoose");
const url = 'mongodb://heroku_8r5bwnlw:[email protected]:25021/heroku_8r5bwnlw'
const connect = mongoose.connect(url, {});
connect.then((db) => {
var dbs = mongoose.connection;
console.log('Connected correctly to server');
}, (err) => { console.log("ERRR ", err);} );
const Color = require('./color');
const app = express();
var cors = require('cors');
app.use(cors());
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello World!')
});
app.get('/colors', (req, res) => {
Color.find({})
.then( (colors) => {
res.send(colors);
}, (err) => console.log(err))
.catch((err) => console.log(err));
});
app.get('/:color', (req, res) =>{
Color.find(req.params.color, function(err, color) {
if (err) res.status(500).send(err);
else {
res.json(color);
}
});
res.send(color);
});
app.put('/:color', (req, res) => {
// console.log(req);
console.log(req.query.distance);
if (req.query && req.query.distance) {
// res.send("have distance");
console.log(req.params.color);
// return res.send("fine");
Color.find({ color: req.params.color }, (err, me) => {
if (err) res.status(500).send(err);
else {
const player_2 = 'blue';
// return res.send(me);
Color.find({ color: player_2 }, (err, target) => {
if (err) return res.send(err);
else {
target = target[0];
me = me[0];
const planetWeight = target.planet;
if (planetWeight == 0 ) {
console.log("no planets available");
// return res.send(me);
}
else {
console.log('target ', target);
const targetWeight = target.weight;
const targetDistance = target.distance;
var weight = me.weight;
const distance = parseInt(req.query.distance);// - targetDistance;
const Fg = 9.8 * weight * planetWeight / (distance * distance);
const Fg_target = 9.8 * targetWeight * planetWeight / (targetDistance * targetDistance);
console.log('Fg = ', Fg);
console.log('Ft = ', Fg_target);
if (Fg > Fg_target) {
console.log('updating');
var planet = me.planet;
weight += planet;
planet = planetWeight;
target.planet = 0;
target.distance = 0;
target.save();
me.weight = weight;
me.planet = planet;
me.distance = distance;
me.save();
// res.send({can : true});
}
}
}
});
res.send(me);
// res.send({can : false});
}
})
.catch(err => console.log(err));
} else {
// res.send("no query");
}
});
app.post('/:color', (req, res) => {
console.log('in post');
const color_ = req.params.color;
//sun weights 1.989 * 10^30 kg = 1989000 * 10^24 kg;
var random = Math.floor(Math.random() * 10) + 1;
const weight = 1989 * random;
//earth weights 5.972 * 10^24 kg;
random = Math.floor(Math.random() * 10) + 1;
const planetweight = 5.972 * random;
//distance between the sun and the eart is 149.6 million km
random = Math.floor(Math.random() * 10) + 1;
const distance = 149.6 * random;
var color = new Color({
color: color_,
weight: weight,
planet: planetweight,
distance: distance
});
color.save();
res.send(color)
});
// const port = 3000;
const port = process.env.PORT || 3000;
// app.set('port', process.env.PORT || 3000 );
app.listen( port, () => console.log(`Example app listening on port ${port}!`))