-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
183 lines (158 loc) · 5.97 KB
/
Copy pathapp.js
File metadata and controls
183 lines (158 loc) · 5.97 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
import express from 'express';
import { config } from 'dotenv';
import { connectToCluster, migrateAll } from './public/js/establish_db.js';
import {ObjectId} from "mongodb";
//set up the server, config and initiate migrations
const app = express();
config();
await migrateAll();
//create a mongo client instance for later use
let mc = await connectToCluster();
//start the express server
app.use(express.static("public"));
app.listen(3000);
// View engine setup
app.set('view engine', 'ejs');
/**
* API Call - Fetch character sheet of a player _id
*/
app.get('/character-sheet/:participantId', async (req, res, next) => {
let participantId = new ObjectId(req.params.participantId);
const character = await mc.db("TRBS").collection("player").find({"_id" : participantId}).toArray();
const skills = await mc.db("TRBS").collection("skill").find().toArray();
const items = await mc.db("TRBS").collection("item").find().toArray();
res.locals.character = character[0];
res.locals.skills = skills;
res.locals.items = items;
res.locals.specialClasses = [];
if(res.locals.character === undefined) res.sendStatus("404");
res.render("character-sheet");
});
/**
* API Call - Fetch all players from the player collection
*/
app.get('/players/', async (req, res, next) => {
const players = await mc.db("TRBS").collection("player").find().toArray();
res.send(players);
});
/**
* API Call - Fetch all enemies from the enemy collection
*/
app.get('/enemies/', async (req, res, next) => {
const enemies = await mc.db("TRBS").collection("enemy").find().toArray();
res.send(enemies);
});
/**
* API Call - Fetch all items from the item collection
*/
app.get('/items/', async (req, res, next) => {
const items = await mc.db("TRBS").collection("item").find().toArray();
res.send(items);
});
/**
* API Call - Fetch all skills from the skill collection
*/
app.get('/skills/', async (req, res, next) => {
const skills = await mc.db("TRBS").collection("skill").find().toArray();
res.send(skills);
});
/**
* API Call - Fetch all spells from the spell collection
*/
app.get('/spells/', async (req, res, next) => {
const spells = await mc.db("TRBS").collection("spell").find().toArray();
res.send(spells);
});
/**
* API Call - Fetch all statuses from the status collection
*/
app.get('/statuses/', async (req, res, next) => {
const statuses = await mc.db("TRBS").collection("status").find().toArray();
res.send(statuses);
});
/**
* API Call - Insert a new participant into player/enemy collection and return the _id
*/
app.put('/participants/:participant/:type', async (req, res, next) => {
const newParticipantId = await mc.db("TRBS").collection(req.params.type).insertOne(JSON.parse(req.params.participant));
res.send(newParticipantId.insertedId);
});
/**
* API Call - Insert a new status into status collection and return the _id
*/
app.put('/statuses/:statusToInsert/', async (req, res, next) => {
const newStatusId = await mc.db("TRBS").collection("status").insertOne(JSON.parse(req.params.statusToInsert));
res.send(newStatusId.insertedId);
});
/**
* API Call - Delete a player/enemy from the database
*/
app.delete('/dropParticipant/:participant/:type', async (req, res, next) => {
let participant = JSON.parse(req.params.participant);
participant._id = new ObjectId(participant._id);
await mc.db("TRBS").collection(req.params.type).deleteOne({ "_id" : participant._id });
res.sendStatus("200");
});
/**
* API Call - Delete a status from the database
*/
app.delete('/dropStatus/:statusToDrop/', async (req, res, next) => {
let statusToDrop = JSON.parse(req.params.statusToDrop);
statusToDrop._id = new ObjectId(statusToDrop._id);
await mc.db("TRBS").collection("status").deleteOne({ "_id" : statusToDrop._id });
res.sendStatus("200");
});
/**
* API Call - Update a player/enemy in their corresponding collection
*/
app.put('/updateParticipant/:participant/:type', async (req, res, next) => {
let participant = JSON.parse(req.params.participant);
participant._id = new ObjectId(participant._id); //need to update in-system id for the replacement not to overwrite in-db ID
await mc.db("TRBS").collection(req.params.type).replaceOne({ "_id" : participant._id }, participant);
res.sendStatus("200");
});
/**
* API Call - Update a status in the status collection
*/
app.put('/updateStatus/:statusToUpdate/', async (req, res, next) => {
let statusToUpdate = JSON.parse(req.params.statusToUpdate);
statusToUpdate._id = new ObjectId(statusToUpdate._id);
await mc.db("TRBS").collection("status").replaceOne({ "_id" : statusToUpdate._id }, statusToUpdate);
res.sendStatus("200");
});
/**
* API Call - Increase player experience by 1 (called after battle ends with player victory)
*/
app.put('/grantExperience/:player', async (req, res, next) => {
let participant = JSON.parse(req.params.player);
participant._id = new ObjectId(participant._id);
await mc.db("TRBS").collection("player").updateOne(
{ "_id" : participant._id },
{ $set: {"experience": (parseInt(participant.experience) + 1)} }
);
res.sendStatus("200");
});
/**
* API Call - Increase player gold by 1 (called after battle ends with player victory)
*/
app.put('/payday/:player', async (req, res, next) => {
let participant = JSON.parse(req.params.player);
participant._id = new ObjectId(participant._id);
await mc.db("TRBS").collection("player").updateOne(
{ "_id" : participant._id },
{ $set: {"gold": (parseInt(participant.gold))} }
);
res.sendStatus("200");
});
/**
* API Call - Updates a participant's (player) inventory
*/
app.put('/updateInventory/:player', async (req, res, next) => {
let participant = JSON.parse(req.params.player);
participant._id = new ObjectId(participant._id);
await mc.db("TRBS").collection("player").updateOne(
{ "_id" : participant._id },
{ $set: {"inventory": (participant.inventory)} }
);
res.sendStatus("200");
});