-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (107 loc) · 3.93 KB
/
index.js
File metadata and controls
127 lines (107 loc) · 3.93 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
"use strict";
const express = require("express");
const socketIO = require("socket.io");
const PORT = process.env.PORT || 3000;
const INDEX = "/public/index.html";
const server = express()
.use(express.static("public"))
.listen(PORT, () => console.log(`Listensing on ${PORT}`));
const io = socketIO(server);
const lobbies = [];
const users = {};
const answers = {};
io.on("connection", (socket) => {
console.log("Client connected: " + socket.id);
users[socket.id] = "No name";
// When they ask for the lobbies, give the lobbies
socket.on("get-lobbies", () => {
console.log("current lobbies are:" + lobbies);
socket.emit("give-lobbies", lobbies);
});
// Creates a new lobby with the given lobby name and empty data, adds that lobby to lobbies (global)
socket.on("new-game", lname => {
let lobby = {"lname": lname, "src": socket.id, "players":[]};
lobbies.push(lobby);
console.log("emit new-lobby: " + lobby);
socket.emit("new-lobby", lobby);
});
// Player attempt to join, search through lobbies for matching name.
// If no name found send back null, otherwise add that player to that lobby,
// and send back the updated lobby
socket.on("join-game", object => {
let index = -1;
for (let i = 0; i < lobbies.length; i++) {
console.log(i + ": " + lobbies[i].lname);
if (object.lobby == lobbies[i].lname) {
console.log("found name!");
index = i;
}
}
if (index == -1) {
console.log("no game of name: " + object.lobby);
socket.emit("joined-game", null);
} else {
lobbies[index].players.push(object.player);
users[socket.id] = object.player;
console.log("joined game: " + object.player);
socket.emit("joined-game", lobbies[index]);
socket.broadcast.emit("joined-game", lobbies[index]);
}
});
socket.on("begin", arr => {
socket.broadcast.emit("begin-game", arr);
});
socket.on("image-displayed", index => {
socket.broadcast.emit("get-caption");
});
socket.on("answer", object => {
console.log(object);
if (answers[object.lname]) {
answers[object.lname].push({"player": object.player, "caption": object.caption});
} else {
answers[object.lname] = [{"player": object.player, "caption": object.caption}];
}
let index = -1;
for (let i = 0; i < lobbies.length; i++) {
if (object.lname == lobbies[i].lname) {
index = i;
}
}
if (index != -1) {
if (answers[object.lname].length >= lobbies[index].players.length) {
socket.broadcast.emit("all-captions", answers[object.lname]);
// Clear the answers
answers[object.lname] = null;
}
}
});
socket.on("create-voting", arr => {
socket.broadcast.emit("vote", arr);
});
// Sends a chat message and current users name to other sockets
socket.on("send-chat-message", message => {
socket.broadcast.emit("chat-message", {"message": message, "name": users[socket.id]});
});
// When current user disconnects
socket.on("disconnect", () => {
console.log("Client disconnected: " + socket.id);
// Get rid of lobby of disconnected host
for (let i = 0; i < lobbies.length; i++) {
if (lobbies[i].src == socket.id) { // If a host disconnected
let deletedlobby = lobbies.splice(i, 1);
// What do we do with the players in the deleted lobby??????????
socket.emit("give-lobbies", lobbies);
} else {
for (let j = 0; j < lobbies[i].players.length; j++) {
if (lobbies[i].players[j] == users[socket.id]) { // If a player disconnects
console.log("player disconnected: " + users[socket.id]);
lobbies[i].players.splice(j, 1);
socket.emit("player-disconnected", {"player": users[socket.id], "lobby": lobbies[i].lname});
}
}
}
}
//socket.broadcast.emit("user-disconnected", users[socket.id]);
});
});
setInterval(() => io.emit("time", new Date().toTimeString()), 1000);