-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (144 loc) · 5.67 KB
/
server.js
File metadata and controls
161 lines (144 loc) · 5.67 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
const { Server } = require('socket.io');
const http = require('http');
const app = require('./lib/app');
const server = http.createServer(app);
const io = new Server(server, { cors: true });
const PORT = process.env.PORT || 8080;
const users = {};
const games = [];
const { addUser, deleteUser } = require('./lib/socket/user-utils');
const onConnection = (socket) => {
// GAME MECHANICS
// on every connection, add a user a join that user's socket to the current room
addUser(socket, users, games);
const currentRoom = `${users[socket.id].room}`;
socket.join(currentRoom);
socket.broadcast.to(currentRoom).emit('new user', { [socket.id]: socket.id });
// socket.to(socket.id).emit('current users', Object.keys(users));
// take in cursor movement data and broadcast to other clients
const onMovement = (movementData) => {
movementData.id = socket.id;
socket.broadcast.to(currentRoom).emit('moving', movementData);
};
const onGameStart = () => {
if (!games.includes(currentRoom)) games.push(currentRoom);
};
// broadcast a remove cursor signal to other clients when a client disconnects, delete the user
const onDisconnect = () => {
deleteUser(socket, users);
socket.broadcast.to(currentRoom).emit('removeCursor', socket.id);
};
// take in chat message data and emit to all clients
const onMessage = (message) => {
socket.to(currentRoom).emit('socket message', message);
};
////////////////////////////////
// SEARCH + HEADER MECHANICS
// take in button state data and broadcast to other clients
const onButtonClick = (buttonState) => {
socket.broadcast.to(currentRoom).emit('socket search click', buttonState);
};
// take in input from search input field
const onInputChange = (inputValue) => {
socket.broadcast.to(currentRoom).emit('search input typing', inputValue);
};
//when 'duck' is entered into search bar duck data === TRUE
const onDuckInput = (duckData) => {
socket.to(currentRoom).emit('duck input', duckData);
};
// take in hover data from nav links (for now)
const onHover = (hoverData) => {
socket.to(currentRoom).emit('socket link hover', hoverData);
};
//take in user click on header to all users
const onHeaderClick = (clickCount) => {
socket.to(currentRoom).emit('socketHeaderTextClick', clickCount);
};
// When one user is hovering over Join Us, a second user clicks on "DONT" to change text to "I SAID DONT"
const onDontClick = (btnClicked) => {
socket.to(currentRoom).emit('SocketDontClick', btnClicked);
};
////////////////////////////////
// MAIN BODY MECHANICS
// mission section hover
const onMissionHover = (hover) => {
socket.to(currentRoom).emit('socket mission hover', hover);
};
// presentational event that indicates the ghost story has been flipped over by a client and should be flipped for the other clients
const onGhostStoryFlip = () => {
socket.to(currentRoom).emit('ghostStoryFlip');
};
const onGhostStoryPoint = (points) => {
socket.broadcast.to(currentRoom).emit('socketGhostStoryPoint', points);
};
//take in image gallery button data and emit to all clients
const onImageButtonTextChange = (imageButtonData) => {
socket.broadcast
.to(currentRoom)
.emit('button text change', imageButtonData);
};
const onImageHover = (imageHoverData) => {
socket.broadcast.to(currentRoom).emit('image hover', imageHoverData);
};
const onGlowingObjectClick = (glowingObjectData) => {
socket.broadcast
.to(currentRoom)
.emit('socket glowing object', glowingObjectData);
};
//click and points broadcast from ghost image
const onGhostClick = (newPosition) => {
socket.to(currentRoom).emit('socket ghost click', newPosition);
};
const onGhostPoints = () => {
socket.to(currentRoom).emit('socket ghost points');
};
////////////////////////////////
// FOOTER MECHANICS
// transparent footer click
const onTransparentClick = () => {
socket.to(currentRoom).emit('socket transparent click');
};
// transparent footer click points
const onTransparentPoints = () => {
socket.to(currentRoom).emit('socket transparent points');
};
// take in ghost icon data and emit to all clients
const onSocialIconChange = (iconData) => {
socket.to(currentRoom).emit('icon change', iconData);
};
//take in user click on footer to all users
const onFooterTitleClick = (titleData) => {
socket.to(currentRoom).emit('socketFooterTitleClick', titleData);
};
// attach functions to listeners
// GAME + CHAT MECHANICS
socket.on('movement', onMovement);
socket.on('game start', onGameStart);
socket.on('disconnect', onDisconnect);
socket.on('client message', onMessage);
// SEARCH + HEADER MECHANICS
socket.on('searchSubmit', onButtonClick);
socket.on('search input', onInputChange);
socket.on('duck', onDuckInput);
socket.on('link hover', onHover);
socket.on('headerTextClick', onHeaderClick);
socket.on('ClientDontClick', onDontClick);
// MAIN BODY MECHANICS
socket.on('missionHover', onMissionHover);
socket.on('ghostStoryFlip', onGhostStoryFlip);
socket.on('ghostStoryPoint', onGhostStoryPoint);
socket.on('button text change', onImageButtonTextChange);
socket.on('image hover', onImageHover);
socket.on('ghost click', onGhostClick);
socket.on('ghost points', onGhostPoints);
socket.on('glowing object click', onGlowingObjectClick);
// FOOTER MECHANICS
socket.on('transparent click', onTransparentClick);
socket.on('transparent points', onTransparentPoints);
socket.on('icon change', onSocialIconChange);
socket.on('footerTitleClick', onFooterTitleClick);
};
io.sockets.on('connection', onConnection);
server.listen(PORT, () => {
console.log(`Started on ${PORT}`);
});