-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
223 lines (199 loc) · 5.22 KB
/
server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const express = require("express");
const cors = require("cors");
const async = require("async");
_ = require("underscore");
const path = require("path");
const app = express();
const helmet = require("helmet");
const compression = require("compression");
const port = process.env.PORT || 5000;
app.use(helmet({}));
app.use(compression());
var Game = require("./models/game");
var Player = require("./models/player");
var Count = require("./models/count");
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
app.use(express.static(path.join(__dirname, "client", "build")));
//Set up mongoose connection
var mongoose = require("mongoose");
require("dotenv").config();
var dev_db_url = process.env.MONGODB_URI;
// var mongoDB = process.env.MONGODB_URI || dev_db_url;
var mongoDB = dev_db_url;
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// Clear Database
// Player.deleteMany({}, function (err) {
// if (err) {
// console.log(err);
// }
// console.log("All players deleted");
// });
// Game.deleteMany({}, function (err) {
// if (err) {
// console.log(error);
// }
// console.log("All games deleted");
// });
// Count.deleteMany({}, function (err) {
// if (err) {
// console.log(err);
// }
// console.log("Count deleted");
// });
// let first = new Count({ count: 0 });
// first.save(function (err) {
// if (err) {
// console.log(err);
// }
// console.log("saved new count");
// console.log(first);
// });
// });
app.use(cors());
let count;
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.get("/count", (req, res) => {
// console.log("get count " + count);
if (!count) {
Count.find().exec(function (err, savedCount) {
if (err) {
console.log(err);
}
count = savedCount[0];
res.send({ count: count.count });
});
} else {
res.send({ count: count.count });
}
});
app.post("/count", (req, res) => {
// console.log("post count " + count);
if (count) {
count.count = count.count + 1;
count.save(function (err) {
if (err) {
console.log(err);
}
res.send({ count: count.count });
});
} else {
res.send({ count: 0 });
}
});
app.get("/players", (req, res) => {
// console.log("request for players made");
Player.find({}).exec(function (err, players) {
if (err) {
console.log(err);
}
res.send(players);
});
});
app.get("/recentGames", (req, res) => {
Game.find()
.sort({ _id: -1 })
.limit(5)
.populate("players")
.exec(function (err, games) {
if (err) {
console.log(err);
}
res.send(games);
});
});
app.get("/allGames", (req, res) => {
Game.find()
.sort({ _id: -1 })
.populate("players")
.exec(function (err, games) {
if (err) {
console.log(err);
}
res.send(games);
});
});
app.post("/game", (req, res) => {
// console.log("game posted");
// console.log(req.body);
//save game to database
//find or create players
let players = req.body.players;
players = players.map((player) => player.trim().toUpperCase());
// console.log(players);
async.forEachOf(
players,
function iteratee(name, index, callback) {
Player.findOne({ name: name }, function (err, player) {
if (err) {
console.log(err);
}
if (!player) {
player = new Player({
name: name,
gameCount: 0,
totalScore: 0,
totalHands: 0,
games: [],
});
// player.save();
}
players[index] = player;
callback();
});
},
function callback(err) {
if (err) {
console.log(err);
}
// once we have found or created all the players from the game in the database, we can create the game
// console.log(players);
// console.log(req.body);
// var newGameObject = Object.create(req.body);
// newGameObject.players = players;
// console.log({newGameObject})
var newGame = new Game(_.extend(req.body));
newGame.players = players;
players.forEach(async (player, i) => {
if (req.body.addToLeaderboard) {
player.gameCount = player.gameCount + 1;
player.totalScore = player.totalScore + newGame.totalScores[i];
player.totalHands = player.totalHands + newGame.rounds.length;
}
player.games.push(newGame._id);
player.save(function (err) {
if (err) {
console.log(err);
}
//saved
});
});
Game.countDocuments({}, function callback(err, count) {
if (err) {
console.log(err);
}
newGame.number = count + 1;
newGame.save(function (err) {
if (err) {
console.log(err);
res.send({ err });
} else {
res.send("Success");
}
});
});
}
);
});
app.get("/express_backend", (req, res) => {
res.send({ message: "YOUR EXPRESS BACKEND IS CONNECTED TO REACT" });
});
app.listen(port, () => console.log(`Listening on port ${port}`));