This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
156 lines (136 loc) · 3.42 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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var uuid = require("node-uuid");
var people = [
{
id: uuid.v4(),
sex: "male",
topic: "piwo",
position: {
lat: 51.084812,
lng: 17.013667
}
},
{
id: uuid.v4(),
sex: "female",
topic: "piwo",
position: {
lat: 51.083953,
lng: 17.009837
}
},
{
id: uuid.v4(),
sex: "male",
topic: "piwo",
position: {
lat: 51.082915,
lng: 17.007123
}
},
{
id: uuid.v4(),
sex: "female",
topic: "piwo",
position: {
lat: 51.082184,
lng: 17.011994
}
},
{
id: uuid.v4(),
sex: "female",
topic: "piwo",
position: {
lat: 51.087775,
lng: 17.013924
}
},
{
id: uuid.v4(),
sex: "male",
topic: "piwo",
position: {
lat: 51.087406,
lng: 17.007773
}
}
];
var places = [
{
id: uuid.v4(),
name: "Szynkarnia",
position: {
lat: 51.109328,
lng: 17.025007
}
},
{
id: uuid.v4(),
name: "Cukiernia Furtak",
position: {
lat: 51.085025,
lng: 17.010706
}
}
];
function broadcast(people, event, payload) {
people.forEach(person => {
if (person.socket) person.socket.emit(event, payload);
});
}
function filterPeopleByTopic(people, topic) {
return people.filter(person => person.topic === topic);
}
function findById(collection, id) {
return collection.find(item => item.id === id);
}
function getNearestPlace(places, sourcePos, destinationPos) {
return places[1];
}
function serializePerson(person) {
var newPerson = Object.assign({}, person);
delete newPerson.socket;
return newPerson;
}
function handleDisconnect(socket) {
console.log("on disconnect");
people = people.filter(person => person.socket !== socket);
broadcast(people, 'people', people.map(serializePerson));
}
function handleIntro(socket, payload) {
console.log("on intro");
people.push(Object.assign({}, payload, { socket: socket }));
const peopleFromTopic = filterPeopleByTopic(people, payload.topic);
broadcast(peopleFromTopic, "people", peopleFromTopic.map(serializePerson));
}
function handleNotify(socket, payload) {
console.log("on notify");
const source = findById(people, payload.sourceId);
const destination = findById(people, payload.destinationId);
console.log(source.id, destination.id);
destination.socket.emit("getNotified", serializePerson(source));
}
function handleAcceptNotification(socket, payload) {
console.log("on acceptNotification");
if (!payload.accepted) return;
const source = findById(people, payload.sourceId);
const destination = findById(people, payload.destinationId);
const place = getNearestPlace(places, source.position, destination.position);
source.socket.emit("place", place);
destination.socket.emit("place", place);
}
io.on('connection', function(socket) {
socket.on('intro', payload => handleIntro(socket, payload));
socket.on('notify', payload => handleNotify(socket, payload));
socket.on('disconnect', payload => handleDisconnect(socket, payload));
socket.on('acceptNotification', payload => handleAcceptNotification(socket, payload));
});
http.listen(process.env.PORT || 3000, function() {
console.log('listening on *:', process.env.PORT || 3000);
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});