-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
378 lines (337 loc) · 10.7 KB
/
index.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import getAllGamesData from "./src/data/AllGames.js";
import allDevelopers from "./src/data/AllDevelopers.js";
import allTags from "./src/data/AllTags.js";
import allUsers from "./src/data/AllUsers.js";
import allReviewText from "./src/data/AllReviewTexts.js";
import { getRandomList, getRandomBoolean } from "./src/data/helpers.js";
import IdGenerator from "./src/model/IdGenerator.js";
import User from "./src/model/User.js";
import Review from "./src/model/Review.js";
import PageInfo from "./src/model/PageInfo.js";
import { DraftUser, DraftPurchase, CardInfo, DraftReview } from "./src/model/Drafts.js";
/**
* Get a paginated list of elements.
*
* @param {Array} list The list of elements.
* @param {number} page The page number.
* @return {PageInfo} A pagination object for the elements.
* @throws {PageException} If the page number is less than 1.
*/
function getPage(list, page) {
if (page < 1) throw new PageException("Page must be 1 or more");
// Chunk the list into groups of 10
const chunkedList = [];
for (let i = 0; i < list.length; i += 10) {
chunkedList.push(list.slice(i, i + 10));
}
return new PageInfo(
page,
chunkedList[page - 1] || [],
list.length,
chunkedList.length
);
}
class SteamSystem {
/**
* @param {Array<Game>} games
* @param {Array<Developer>} developers
* @param {Array<Tag>} tags
* @param {Array<User>} users
*/
constructor(games, developers, tags, users) {
this.games = games;
this.developers = developers;
this.tags = tags;
this.users = users;
this.idGenerator = new IdGenerator();
}
/**
* Add a new user to the system.
*
* @param {DraftUser} user The user to add.
* @return {User} The newly created user.
* @throws {UserException} If the email address is already in use.
*/
addNewUser(user) {
for (const existingUser of this.users) {
if (existingUser.email === user.email)
throw new UserException("Email is taken");
}
const newUser = new User(
this.idGenerator.nextUserId(),
user.email,
user.password,
user.name,
user.image,
user.backgroundImage,
[],
[]
);
this.users.push(newUser);
return newUser;
}
/**
* Get a tag by its ID.
*
* @param {string} id The ID of the tag.
* @return {Tag} The tag with the given ID.
* @throws {NotFoundTag} If the tag with the given ID does not exist.
*/
getTag(id) {
const tag = this.tags.find((t) => t.id === id);
if (!tag) throw new NotFoundTag();
return tag;
}
/**
* Get a user by its ID.
*
* @param {string} id The ID of the user.
* @return {User} The user with the given ID.
* @throws {NotFoundUser} If the user with the given ID does not exist.
*/
getUser(id) {
const user = this.users.find((u) => u.id === id);
if (!user) throw new NotFoundUser();
return user;
}
/**
* Get a game by its ID.
*
* @param {string} id The ID of the game.
* @return {Game} The game with the given ID.
* @throws {NotFoundGame} If the game with the given ID does not exist.
*/
getGame(id) {
const game = this.games.find((g) => g.id === id);
if (!game) throw new NotFoundGame();
return game;
}
/**
* Get a developer by its ID.
*
* @param {string} id The ID of the developer.
* @return {Developer} The developer with the given ID.
* @throws {NotFoundDeveloper} If the developer with the given ID does not exist.
*/
getDeveloper(id) {
const developer = this.developers.find((d) => d.id === id);
if (!developer) throw new NotFoundDeveloper();
return developer;
}
/**
* Get the reviews written by the user with the given ID.
*
* @param {string} userId The ID of the user.
* @return {Array<Review>} The list of reviews written by the user.
* @throws {NotFoundUser} If the user with the given ID does not exist.
*/
getUserReviews(userId) {
const user = this.getUser(userId);
return this.games
.flatMap((game) => game.reviews)
.filter((review) => review.user === user);
}
/**
* Get a list of recommended games.
*
* The recommended games are the games that have the most reviews that are marked as recommended.
*
* @return {Array<Game>} A list of recommended games.
*/
getRecommendedGames() {
return [...this.games]
.sort((a, b) => {
const aRecommended = a.reviews.filter(
(review) => review.isRecommended
).length;
const bRecommended = b.reviews.filter(
(review) => review.isRecommended
).length;
return bRecommended - aRecommended;
})
.slice(0, 10);
}
/**
* Get a list of games.
*
* @param {number} page The page number.
* @return {PageInfo<Game>} A list of games.
* @throws {PageException} If the page number is less than 1.
*/
getGames(page = 1) {
return getPage(this.games, page);
}
/**
* Get a list of games by tag.
*
* @param {string} tagId The ID of the tag.
* @param {number} page The page number.
* @return {PageInfo<Game>} A list of games.
* @throws {NotFoundTag} If the tag with the given ID does not exist.
* @throws {PageException} If the page number is less than 1.
*/
getGamesByTag(tagId, page = 1) {
const tag = this.getTag(tagId);
const filteredGames = this.games.filter((game) => game.tags.includes(tag));
return getPage(filteredGames, page);
}
/**
* Get a list of games by developer.
*
* @param {string} developerId The ID of the developer.
* @param {number} page The page number.
* @return {PageInfo<Game>} A list of games.
* @throws {NotFoundDeveloper} If the developer with the given ID does not exist.
* @throws {PageException} If the page number is less than 1.
*/
getGamesByDeveloper(developerId, page = 1) {
const developer = this.getDeveloper(developerId);
const filteredGames = this.games.filter(
(game) => game.developer === developer
);
return getPage(filteredGames, page);
}
/**
* Search for games by name.
*
* @param {string} name The name of the game.
* @param {number} page The page number.
* @return {PageInfo<Game>} A list of games.
* @throws {PageException} If the page number is less than 1.
*/
searchGame(name, page = 1) {
const filteredGames = this.games.filter((game) =>
game.name.toLowerCase().includes(name.toLowerCase())
);
return getPage(filteredGames, page);
}
/**
* Search for users by name.
*
* @param {string} name The name of the user.
* @param {number} page The page number.
* @return {PageInfo<User>} A list of users.
* @throws {PageException} If the page number is less than 1.
*/
searchUser(name, page = 1) {
const filteredUsers = this.users.filter((user) =>
user.name.toLowerCase().includes(name.toLowerCase())
);
return getPage(filteredUsers, page);
}
/**
* Add a review for a game.
*
* @param {string} userId The ID of the user who is submitting the review.
* @param {DraftReview} draftReview The draft review object.
* @return {Game} The game that was reviewed.
* @throws {NotFoundUser} If the user with the given ID does not exist.
* @throws {NotFoundGame} If the game with the given ID does not exist.
* @throws {ReviewException} If the user does not own the game or if the user has already submitted a review for the game.
*/
addReview(userId, draftReview) {
const user = this.getUser(userId);
const game = this.getGame(draftReview.gameId);
if (!user.games.includes(game)) {
throw new ReviewException("You need to own the game to leave a review");
}
const existingReview = game.reviews.find((review) => review.user === user);
if (existingReview) {
throw new ReviewException(
"You've already submitted a review for this game"
);
}
game.reviews.push(
new Review(
this.idGenerator.nextReviewId(),
user,
game,
draftReview.isRecommended,
draftReview.text
)
);
return game;
}
/**
* Purchase a game.
*
* @param {string} userId The ID of the user who is purchasing the game.
* @param {DraftPurchase} draftPurchase The draft purchase object.
* @return {User} The user who purchased the game.
* @throws {NotFoundUser} If the user with the given ID does not exist.
* @throws {NotFoundGame} If the game with the given ID does not exist.
* @throws {PurchaseException} If the user already owns the game.
*/
purchaseGame(userId, draftPurchase) {
const user = this.getUser(userId);
const game = this.getGame(draftPurchase.gameId);
if (user.games.includes(game)) {
throw new PurchaseException("You already have the game");
}
user.games.push(game);
return user;
}
/**
* Add or remove a friend.
*
* @param {string} userId The ID of the user who is adding or removing the friend.
* @param {string} friendId The ID of the friend.
* @return {User} The user who added or removed the friend.
* @throws {NotFoundUser} If the user or the friend with the given ID does not exist.
* @throws {UserException} If the user is trying to self-add.
*/
addOrRemoveFriend(userId, friendId) {
if (userId === friendId) {
throw new UserException("You cannot self-add.");
}
const user = this.getUser(userId);
const friend = this.getUser(friendId);
const index = user.friends.indexOf(friend);
if (index !== -1) {
// Remove friend if already exists
user.friends.splice(index, 1);
const friendIndex = friend.friends.indexOf(user);
if (friendIndex !== -1) {
friend.friends.splice(friendIndex, 1);
}
} else {
// Add as friend
user.friends.push(friend);
friend.friends.push(user);
}
return user;
}
}
function initSteamSystem() {
const random = Math.random();
const games = getAllGamesData(random).sort((a, b) => {
return b.releaseDate.getTime() - a.releaseDate.getTime();
});
const steam = new SteamSystem(games, allDevelopers, allTags, []);
allUsers.forEach((user) => steam.addNewUser(user));
steam.users.forEach((user) => {
getRandomList(random, steam.games, 40).forEach((game) => {
steam.purchaseGame(
user.id,
new DraftPurchase(game.id, new CardInfo("a", 1234, new Date(), 123))
);
});
getRandomList(random, steam.users, 5).forEach((friend) => {
if (user.id !== friend.id) {
steam.addOrRemoveFriend(user.id, friend.id);
}
});
getRandomList(random, user.games, 30).forEach((game) => {
steam.addReview(
user.id,
new DraftReview(
game.id,
getRandomBoolean(random),
getRandomList(random, allReviewText, 1)[0]
)
);
});
});
return steam;
}
export { initSteamSystem };