-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
231 lines (190 loc) · 7.08 KB
/
app.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
224
225
226
227
228
229
230
231
require('dotenv').config();
const express = require("express");
const https = require("https");
const session = require("express-session");
const mongoose = require("mongoose");
const parseData = require(__dirname + "/parseData.js");
const sortTeam = require(__dirname + "/sortTeam.js");
const uri = process.env.MONGODB_URI;
const statNames = ["HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"];
const app = express();
// Allows us to use body-parser
app.use(express.urlencoded({
extended: true
}));
app.set("view engine", "ejs");
app.use(express.static("public"));
// Allows us to give users different session IDs so that their teams are different
app.use(session({
secret: "hello",
resave: false,
saveUninitialized: false
}));
mongoose.connect(uri, {useNewUrlParser: true});
const teamSchema = new mongoose.Schema ({
username: String,
team: [{
originalName: String,
name: String,
form: String,
spriteURL: String,
iconURL: String,
stats: [Number],
types: [String],
abilities: [{
ability: String,
url: String,
isHidden: Boolean
}],
height: String,
weight: Number,
notableMoves: [{
name: String,
moveLevel: Number,
url: String
}]
}]
});
const Team = mongoose.model("Team", teamSchema);
// This function renders index.ejs with the appropriate data (pokemonList, statNames,
// errorMessage, warningMessage, successMessage)
function render(request, response, error, warning, success) {
response.render("index.ejs", {
pokemonList: request.session.pokemonList,
statNames: statNames,
errorMessage: error,
warningMessage: warning,
successMessage: success
});
}
// This function determins if a pokemonList already contains a Pokémon
// with the exact name and form as the given Pokémon
function pokemonAlreadyExists(name, form, pokemonList) {
if (form === "shiny") {
name += "-shiny";
}
return pokemonList.some((pokemon) => pokemon.originalName === name);
}
// This function gets a HTTPResponse from the API. If we receive certain status codes,
// we will display an error message. Moreover, if a Pokémon has no moves, we will
// display a warning message. Otherwise, we parse the response and add a new Pokémon
function handleHTTPResponse(req, res, name, form, url) {
https.get(url, function (response) {
if (response.statusCode === 404) {
render(req, res, "This Pokémon could not be retrieved because we searched " +
"for the data in the wrong place! Please report this in the feedback form!", "", "");
} else if (response.statusCode !== 200) {
render(req, res, "We could not retrieve the data for this Pokémon. Please try again!", "", "");
} else {
let pokemonData = "";
response.on("data", (data) => pokemonData += data);
response.on("end", function () {
pokemonData = JSON.parse(pokemonData);
req.session.pokemonList.push(parseData.getPokemon(name, form, pokemonData));
if (pokemonData.moves.length === 0) {
render(req, res, "", "All Generation 8 Pokémon have a blank Notable Moves " +
"section because our data source currently does not provide any moves for " +
"these Pokémon. Hopefully this issue will be resolved soon!", "");
} else {
render(req, res, "", "", "");
}
});
}
});
}
app.get("/", function (req, res) {
// If a user has already opened the website, opening the website on a new tab with
// the same session ID should not reset their team
if (!req.session.pokemonList) {
req.session.pokemonList = [];
}
if (req.session.savedSuccessfully) {
req.session.savedSuccessfully = false;
render(req, res, "", "", "Your team was saved! Please remember your username so you can load the team in the future!");
} else if (req.session.usernameNotFound) {
req.session.usernameNotFound = false;
render(req, res, "That username does not exist!", "", "");
} else if (req.session.loadedSuccessfully) {
req.session.loadedSuccessfully = false;
render(req, res, "", "", "Your team has been loaded! If this is not your team, it is highly likely someone else is using " +
"your username; in that case, please use another username!");
} else {
render(req, res, "", "", "");
}
});
app.get("/about", function (req, res) {
res.render("about.ejs");
});
app.get("/announcements", function (req, res) {
res.render("announcements.ejs");
});
app.get("/feedback", function (req, res) {
res.render("feedback.ejs");
});
app.post("/", function (req, res) {
if (req.body.name === "") {
render(req, res, "You did not select any Pokémon!", "", "");
} else {
const name = req.body.name;
const form = req.body.form;
const url = "https://pokeapi.co/api/v2/pokemon/" + name;
if (pokemonAlreadyExists(name, form, req.session.pokemonList)) {
render(req, res, "You already added that exact Pokémon! Try another form instead!", "", "");
} else {
handleHTTPResponse(req, res, name, form, url);
}
}
});
app.post("/remove", function (req, res) {
req.session.pokemonList.splice(req.body.removeButton, 1);
res.redirect("/");
});
app.post("/save-team", function (req, res) {
const currentUsername = req.body.username;
Team.findOne({username: currentUsername}, function (err, team) {
if (err) {
console.log(err);
} else if (!team) {
const newTeam = new Team ({
username: currentUsername,
team: req.session.pokemonList
});
newTeam.save();
req.session.savedSuccessfully = true;
} else {
Team.updateOne({username: currentUsername}, {team: req.session.pokemonList}, function(err) {
if (err) {
console.log(err);
}
});
req.session.savedSuccessfully = true;
}
res.redirect("/");
});
});
app.post("/load-team", function (req, res) {
const currentUsername = req.body.username;
Team.findOne({username: currentUsername}, function (err, team) {
if (err) {
console.log(err);
} else if (!team) {
req.session.usernameNotFound = true;
res.redirect("/");
} else {
req.session.pokemonList = team.team;
req.session.loadedSuccessfully = true;
res.redirect("/");
}
});
});
app.post("/clear", function (req, res) {
req.session.pokemonList = [];
res.redirect("/");
});
app.post("/sort-team", function (req, res) {
sortTeam.sort(req.body.sortOrder, req.session.pokemonList);
res.redirect("/");
});
app.listen(process.env.PORT || 3000, function () {
console.log("Server started on port 3000.");
});