This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
247 lines (216 loc) · 9.84 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//Express JS setup
let express = require("express")
let app = express();
let router = require('express-router');
const serverPort = 8000;
let ip = process.argv[2];
//Socket.io setup
let io = require('socket.io')();
//Models
let User = require("./models/user");
let Quest = require("./models/quest");
// SendGrid Setup
let sendEmail = require("./emailNotifications/email")
//Seeding the DB
let seed = require("./seed/seed");
let seeData = require("./seed/seedData")
seed();
//Sending data to new users.
seeData.forEach(function(user){
sendEmail(user.email,"Welcom to Hill Runner","Aw shucks! Thanks for signing up :) \n Regards, \n The Hill Runner Team");
});
//Passport JS setup
//Not yet
//BodyParser set up
bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
//Mongoose set up
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/HillRunner");
//MethodOverride set up
let methodOverride = require('method-override');
app.use(methodOverride('_method'));
//Route configuration
// app.use('/public', express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public'));
app.use(require('./routes/landing'));
//App settings
app.set('port', serverPort);
app.set('view engine', 'ejs');
app.set('host',ip);
app.set('views', 'views');
app.set('sockets', []);
//Server setup
let server = app.listen(app.get('port'), function() {
console.log('Listening on port ' + app.get('port'));
});
app.set('isLocal', false);
console.log(`Serving on ${app.get('host')}:${app.get('port')}`)
//SOCKET CODE:
io.attach(server);
io.on('connection', (client) => {
console.log('client connected');
//******************************Events for everyone ***************************//
//set uniquely identifying username (user's id). This is for referring the socket to a user.
client.on('add_username', (username) => {
/*if (!io.sockets.sockets.map((user) => {
return user.username;
}).includes(username)) {
*/
client.username = username;
client.emit('username_added', { 'status': 'ok' });
/*} else {
client.emit('username_added', { 'status': 'declined' });
}*/
});
//**************************events for hillrunners:*****************************//
client.on('accept_quest', (object) => {
//note that object is : {id: 'ID OF QUEST',hillrunner:'_id OF HILLRUNNER.'}
// 1. update quest object's state field:
//CANCER STAGE 5
object.hillrunner =mongoose.Types.ObjectId(object.hillrunner);
object.id = mongoose.Types.ObjectId(object.id);
//CANCER
Quest.findByIdAndUpdate(object.id, {
state: "accepted",
hillrunner: object.hillrunner
}, function(error, foundQuest) {
if (error) {
console.log(`app.js: ACCEPT IN FINDANDUPDATE FOR ${object.id} FAILED.`)
} else {
//2. update map for other users:
User.findById( object.hillrunner /* TODO: Find the user's ID*/ , function(error, hillRunner) {
if (error) {
console.log(error)
console.log("app.js FAILED TO FIND HILL RUNNER AND SEND HIM/HER EMAIL CONFIRMATION FOR ACCEPTING");
} else {
User.findById(foundQuest.requester, function(error, requester) {
if (error) {
console.log("app.js FAILED TO FIND REQUESTER AND SEND HIM/HER EMAIL CONFIRMATION FOR ACCEPTING");
} else {
let subjectRequester = "Your quest has been accepted";
let textRequester = `Your quest has been accepted.\n
Details:\n
description: ${foundQuest.description}\n
Accepted by ${hillRunner.name}\n
Reward to be paid ${foundQuest.reward}\n
Accepted at ${foundQuest.updatedAt}\n
\n
Thank-you for using Hill Runner!
`
sendEmail(requester.email, subjectRequester, textRequester);
let subjectHillRunner = "Your have accepted a quest";
let textHillRunner = `Your have accepted a quest from ${requester.name}.\n
description : ${foundQuest.description}\n
Your reward for completion will be ${foundQuest.reward}.\n
You will earn 100 experience points.\n
\n
Thank-you for using Hill Runner!
`
sendEmail(hillRunner.email, subjectHillRunner, textHillRunner);
}
});
}
});
io.emit('user_accepted_quest', object.id);
}
});
});
//*************************Events for quest assigners***********************//
client.on('assign_quest', (quest) => {
console.log(quest);
console.log("Check0", quest.requester);
quest.requester = mongoose.Types.ObjectId(quest.requester);
console.log("Check1", quest.requester);
/*
NOTE. MAKE SURE THAT THE OBJECT PASSED IS IN THE
CORRECT FORMAT
*/
//1. Add new quest to DB's quest collection:
Quest.create(quest, function(error, createdQuest) {
if (error) {
console.log("app.js: ERROR CREATING QUEST")
} else {
console.log(`created a new quest called: ${quest.name}`);
//2. Update user db to add this new quest:
createdQuest.save(function(error, savedQuest) {
if (error) {
console.log("app.js ERROR SAVING QUEST");
} else {
console.log("Requester: " ,savedQuest.requester)
//attempting to find user
User.findByIdAndUpdate(savedQuest.requester, {
$push: {
quests: savedQuest,
},
}, function(error, updatedPlayer) {
console.log(updatedPlayer);
if (error) {
console.log("app.js: ERROR UPDATING PLAYER WITH QUEST");
} else {
//3. update map for other users:
io.emit('user_assigned_quest', quest);
}
});
}
});
}
});
});
client.on('complete_quest', (quest) => {
quest.requester = mongoose.Types.ObjectId(quest.requester);
quest.hillRunner = mongoose.Types.ObjectId(quest.hillRunner);
console.log(`quest ${quest.name} completed.`);
//1. Update completion in quest collection
Quest.findAndUpdate({ id: quest._id }, {
state: "completed"
}, function(error, foundQuest) {
if (error) {
console.log(`app.js: completion in FINDANDUPDATE FOR ${quest.name} FAILED.`)
} else {
//2 update reward into hillrunner's moneyearned.
User.findAndUpdate({ id: quest.hillrunner /* TODO: Find the user's ID*/ }, {
$inc: {
moneyearned: quest.reward,
experience: 100,
level: 1,
}
}, function(error, hillRunner) {
if (error) {
console.log("app.js FAILED TO FIND HILL RUNNER AND SEND HIM/HER EMAIL CONFIRMATION");
} else {
User.findById(quest.requester, function(error, requester) {
if (error) {
console.log("app.js FAILED TO FIND REQUESTER AND SEND HIM/HER EMAIL CONFIRMATION");
} else {
let subjectRequester = "Your quest has been completed";
let textRequester = `Your quest has been completed.\n
Details\n
Fulfiled by ${hillRunner.name}\n
Reward paid ${quest.reward}\n
Completed at ${quest.updatedAt}\n
\n
Thank-you for using Hill Runner!
`
sendEmail(requester.email, subjectRequester, textRequester);
let subjectHillRunner = "Congragulations on quest completion";
let textHillRunner = `You completed a quest for ${requester.name}.\n
Your reward is ${quest.reward}.\n
You completed the quest at ${quest.updatedAt}.\n
You earned 100 experience points.\n
\n
Thank-you for using Hill Runner!
`
sendEmail(hillRunner.email, subjectHillRunner, textHillRunner);
}
});
}
});
}
});
//TODO : find a way to get the socket from the id, and emit 'quest_completed to it
/* io.sockets.sockets[io.sockets.sockets.map(function(sockets) {
return sockets.username;
}).indexOf(quest.hillrunner)].emit('quest_completed', quest); */ //emit to the hillrunner, and hillrunner can update exp, earned money, in their own view.
});
});