-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
100 lines (84 loc) · 2.42 KB
/
server.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
'use strict'
const add = (a, b) => a + b;
const avg = (arr) => arr.reduce(add, 0) / arr.length;
const axios = require("axios").default;
const config = require("./config");
const websocket = new require("ws");
const PORT = config.port;
console.log("port: ", PORT);
const wss = new websocket.Server({ port: PORT });
const ratings = {};
const api = config.api;
const getEndpoint = (genre) => `${api.url}?api_key=${api.key}&with_genres=${api.codes[genre]}`;
const requestForFilms = async (genre) => {
if (messageData.data[genre] > 2) {
let response = await axios.get(
getEndpoint(genre),
{
headers: {
"Content-Type": "application/json",
},
}
);
let data = response.data.results;
return (
data.map((film) => {
const record = ratings[film.id];
const result = record ? avg(record) : "No rating";
return {
img: `https://www.themoviedb.org/t/p/w220_and_h330_face${film.poster_path}`,
title: film.title,
description: film.overview,
rating: result,
id: film.id,
};
})
)
}
}
const commands = {
CLIENTDATA: (ws, messageData) => {
const films = {};
const genresRatings = messageData.data;
const requests = new Promise(async (resolve) => {
if (genresRatings.comedy > 2) {
films.comedy = await requestForFilms('comedy');
}
resolve();
})
.then(async () => {
if (genresRatings["sci-fi"] > 2) {
films["sci-fi"] = await requestForFilms("sci-fi");
}
})
.then(async () => {
if (genresRatings.entertainment > 2) {
films.entertainment = await requestForFilms("entertainment");
}
})
.then(async () => {
if (genresRatings.adventure > 2) {
films.adventure = await requestForFilms("adventure");
}
})
.then(async () => {
if (genresRatings.drama > 2) {
films.drama = await requestForFilms("drama");
}
})
.then(() => {
ws.send(JSON.stringify(films));
});
},
NEWRATING: (ws, messageData) => {
const { id, newRating } = messageData.data;
if (ratings[id]) ratings.push(newRating);
else ratings[id] = [newRating];
},
};
wss.on("connection", (ws) => {
ws.on("message", (message) => {
const messageData = JSON.parse(message);
commands[messageData.type](ws, messageData)
});
});