-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
190 lines (146 loc) · 4.36 KB
/
app.js
File metadata and controls
190 lines (146 loc) · 4.36 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
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
const mongoose = require("mongoose");
const cors = require("cors");
const express = require("express");
const Schema = mongoose.Schema;
const app = express();
const corsOptions = {
origin: '*',
optionsSuccessStatus: 200
}
app.use(cors())
const hostileSchema = new Schema({
image_URL: String,
id: Number,
immunity: Array,
name: String,
research_count: Number,
scannable_powers: Array,
scientific_name: String,
weaknesses: Array
}, {versionKey: false});
const Hostile = mongoose.model("Hostile", hostileSchema);
const abilitySchema = new Schema({
name: String,
id: Number,
type: String,
includes: Array
}, {versionKey: false});
const Ability = mongoose.model("Ability", abilitySchema);
const chipsetSchema = new Schema({
name: String,
id: Number,
type: String,
description: String
}, {versionKey: false});
const Chipset = mongoose.model("Chipset", chipsetSchema);
const locationSchema = new Schema({
name: String,
id: Number,
rooms: Array,
quests: Array,
connects: Array,
airlock: Boolean,
med_bay: Boolean,
crew_assigned: Number,
hostiles: Array
}, {versionKey: false});
const Location = mongoose.model("Location", locationSchema);
const weaponSchema = new Schema({
name: String,
id: Number,
primary_fire: String,
firing_range: String,
rate_of_fire: String,
material_cost: Map,
material_yield: Map,
secondary_fire: String
}, {versionKey: false});
const Weapon = mongoose.model("Weapon", weaponSchema);
(async () => {
try {
await mongoose.connect("mongodb://127.0.0.1:27017/Prey");
app.listen(3001);
console.log('Connection');
} catch(err) {
return console.log(err);
}
})();
app.get("/api/", async(req, res) => {
res.send([{
"hostiles": "http:/localhost:3001/api/hostiles/",
"abilities": "http:/localhost:3001/api/abilities/",
"chipsets": "http:/localhost:3001/api/chipsets/",
"locations": "http:/localhost:3001/api/locations/",
"weapons": "http:/localhost:3001/api/weapons/"
}]);
});
app.get("/api/hostiles", async(req, res) => {
const hostiles = await Hostile.find({});
res.send(hostiles);
});
app.get("/api/hostiles/:id", async(req, res) => {
const id = req.params.id;
const hostile = await Hostile.find({id: id});
if(!(hostile.length === 0)) res.send(hostile);
else res.sendStatus(404);
});
app.get("/api/abilities", async(req, res) => {
const abilities = await Ability.find({});
res.send(abilities);
});
app.get("/api/abilities/:id", async(req, res) => {
const id = req.params.id;
const ability = await Ability.find({id: id});
if(!(ability.length === 0)) res.send(ability);
else res.sendStatus(404);
});
app.get("/api/chipsets", async(req, res) => {
const chipsets = await Chipset.find({});
res.send(chipsets);
});
app.get("/api/chipsets/:id", async(req, res) => {
const id = req.params.id;
const chipset = await Chipset.find({id: id});
if (!(chipset.length === 0)) res.send(chipset);
else res.sendStatus(404);
});
app.get("/api/locations", async(req, res) => {
const locations = await Location.find({});
const response = await Promise.all(
locations.map(location => getHostiles(location))
);
res.send(response);
});
app.get("/api/locations/:id", async(req, res) => {
const id = req.params.id;
const location = await Location.find({id: id});
if(!(location.length === 0)) {
const response = await getHostiles(location[0]);
res.send(response);
}
else res.sendStatus(404);
});
app.get("/api/weapons", async(req, res) => {
const weapons = await Weapon.find({});
res.send(weapons);
});
app.get("/api/weapons/:id", async(req, res) => {
const id = req.params.id;
const weapon = await Weapon.find({id: id});
if (!(weapon.length === 0)) res.send(weapon);
else res.sendStatus(404);
});
async function getHostiles(location) {
const hostilesArray = await Promise.all(location.hostiles.map(
async (hostileObjectId) => {
return await Hostile.findById(hostileObjectId.toHexString());
}
));
location.hostiles = hostilesArray;
return location;
};
process.on("SIGINT", async() => {
await mongoose.disconnect();
console.log('No connection');
process.exit();
});