-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (147 loc) · 5.89 KB
/
app.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
let express = require("express");
const { Socket } = require("socket.io");
let app = express();
let http = require("http").createServer(app);
var socketIO = require("socket.io")(http);
app.use(express.static("static"))
app.get("/:code", (req, res) => {
res.sendFile(__dirname + "/static/index.html");
});
http.listen(process.env.PORT || 3000, () => {
console.log("listening on *:3000");
});
socketIO.on("connection", (socket) => {
socket.on("create room", () => {
socketIO.to(socket.id).emit("created room",
createRoom(socket.id));
console.log(storytellers);
});
socket.on("join room", (code) => {
if(Object.keys(storytellers).includes(code)) {
storytellers[socket.id] = storytellers[code];
socketIO.to(socket.id).emit("joined room");
} else
socketIO.to(socket.id).emit("failed to join room");
});
socket.on("update card", (info) => {
socketIO.to(storytellers[socket.id]).emit("update card", info, socket.id);
});
socket.on("update card back", (info, address) => {
socketIO.to(address).emit("update card back", info);
});
socket.on("personality2", ()=> {
for(personality of keysByValue(storytellers, socket.id)) {
socketIO.to(personality).emit("personality2");
}
});
socket.on("score request", (name, score, goal, reason)=>{
socketIO.to(storytellers[socket.id]).emit("score request", name, score, goal, reason, socket.id);//, name, score, goal, reason, socket.id);
});
socket.on("score accepted", (points, goal, reason, address)=>{
socketIO.to(address).emit("score accepted", points, goal, reason);
});
socket.on("score denied", (score, goal, reason, denial, address)=>{
socketIO.to(address).emit("score denied", score, goal, reason, denial);
});
socket.on("vote modal", (d20, d10, domiAddress, perCount) => {
for(personality of keysByValue(storytellers, socket.id)) {
socketIO.to(personality).emit("vote modal", d10, d20, personality === domiAddress);
}
votes[socket.id] = 0;
allvotes[socket.id] = perCount - 1;
// console.log(`votes: ${votes[sock]}`);
});
socket.on("voted", (inFavor)=>{
allvotes[storytellers[socket.id]] -= 1;
if(!inFavor) {
votes[storytellers[socket.id]] += 1;
let v = votes[storytellers[socket.id]];
for(personality of keysByValue(storytellers, storytellers[socket.id])) {
console.log(`sent new score to ${personality}`);
socketIO.to(personality).emit("vote updated", v);
}
socketIO.to(storytellers[socket.id]).emit("vote updated", v);
}
if(allvotes[storytellers[socket.id]] == 0) {
console.log("-------all voted-------");
socketIO.to(storytellers[socket.id]).emit("vote over", votes[storytellers[socket.id]]);
}
});
socket.on("time over", ()=>{
socketIO.to(socket.id).emit("vote over", votes[socket.id]);
});
socket.on("dice roll", (d20, d10, domiAddress)=>{
for(personality of keysByValue(storytellers, socket.id)) {
socketIO.to(personality).emit("dice roll", d20, d10, personality === domiAddress);
}
});
socket.on("roll button clicked", ()=>{
console.log("func called");
let d20arr = new Array(20);
d20arr[0] = Math.ceil(Math.random() * 20);
for(let i = 1; i < d20arr.length; i++) {
do {
d20arr[i] = Math.ceil(Math.random() * 20);
} while(d20arr[i] == d20arr[i - 1]);
}
let d10arr = new Array(23);
d10arr[0] = Math.ceil(Math.random() * 10);
for(let i = 1; i < d10arr.length; i++) {
do {
d10arr[i] = Math.ceil(Math.random() * 10);
} while(d10arr[i] == d10arr[i - 1]);
}
console.log("arrs set");
for(personality of keysByValue(storytellers, storytellers[socket.id])) {
socketIO.to(personality).emit("roll button clicked", d20arr, d10arr);
}
socketIO.to(storytellers[socket.id]).emit("roll button clicked", d20arr, d10arr);
// //DELETE IMPORTENT TO DELETE
// socketIO.to(socket.id).emit("roll button clicked", d20arr, d10arr);
// console.log("response emitted");
});
socket.on("continue button clicked", ()=>{
for(personality of keysByValue(storytellers, socket.id)) {
socketIO.to(personality).emit("continue button clicked");
}
});
socket.on("summary", (cardsInfo, winnerKeys)=>{
for(personality of keysByValue(storytellers, socket.id)) {
socketIO.to(personality).emit("summary", cardsInfo, winnerKeys);
}
});
socket.on("disconnect", ()=>{
if (Object.keys(storytellers).includes(socket.id)) {
socketIO.to(storytellers[socket.id]).emit("player disconnected", socket.id);
delete storytellers[socket.id];
} else {
// let code = Object.keys(storytellers).find((key)=> storytellers[key] === socket.id);
let code = keyByValue(storytellers, socket.id);
console.log(code);
delete storytellers[code];
}
});
});
function keyByValue(object, value) {
return Object.keys(object).find((key)=> storytellers[key] === value);
}
function keysByValue(object, value) {
return Object.keys(object).filter((key)=> storytellers[key] === value);
}
let allvotes = {};
let votes = {};
let storytellers = {};
function createRoom(storyteller) {
let code = "";
do {
for (let i = 0; i < 4; i++) {
code += String.fromCharCode(Math.round(
65 + Math.random() * 25));
}
} while (Object.keys(storytellers)
.includes(code))
storytellers[code] = storyteller;
votes[storyteller] = 0;
allvotes[storyteller] = 0;
return code;
}