-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
308 lines (275 loc) · 8.12 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const socketio = require("socket.io");
const http = require("http");
const cors = require("cors");
const bodyParser = require("body-parser");
const path = require("path");
const express = require("express");
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const {
addUser,
removeUser,
getUser,
getUsersInRoom,
stakeUser,
isEveryoneStaked,
getUserWithAddress,
} = require("./helpers/users");
const { addGame, getGame } = require("./helpers/games");
io.on("connection", (socket) => {
console.log("Client connected at ID: " + socket.id);
socket.on("join", async (payload, callback) => {
let numberOfUsersInRoom = getUsersInRoom(payload.room).length;
if (getUserWithAddress(payload.address) != undefined) {
return callback("Already in");
}
const { error, newUser } = await addUser({
id: socket.id,
name: numberOfUsersInRoom === 0 ? "Player 1" : "Player 2",
address: payload.address,
room: payload.room,
isStaked: false,
});
console.log(newUser);
if (error) return callback(error);
socket.join(newUser.room);
io.to(newUser.room).emit("roomData", {
room: newUser.room,
users: getUsersInRoom(newUser.room),
});
socket.emit("currentUserData", { name: newUser.name });
callback();
});
socket.on("stake", ({ address, room }) => {
stakeUser({ address, room });
if (isEveryoneStaked(room)) {
io.to(room).emit("startGame", {});
} else {
io.to(room).emit("stake", {
address,
});
}
});
socket.on("initGameState", async (gameState) => {
const user = await getUser(socket.id);
if (user) io.to(user.room).emit("initGameState", gameState);
});
socket.on("updateGameState", async (gameState) => {
const user = await getUser(socket.id);
if (user) io.to(user.room).emit("updateGameState", gameState);
});
socket.on("sendMessage", async (payload, callback) => {
const user = await getUser(socket.id);
io.to(user.room).emit("message", {
user: user.name,
text: payload.message,
});
callback();
});
socket.on("offer", (payload, callback) => {
const { creatorSocketId, name, bet, accepterSocketId } = payload;
console.log(payload);
console.log(socket.id);
io.to(creatorSocketId).emit("popup", {
from: accepterSocketId,
name,
bet,
});
});
socket.on("accept", async (payload, callback) => {
const { opponentSocketId, roomCode, bet, senderSocketId } = payload;
const { newGame } = await addGame({ room: roomCode, bet });
console.log(newGame);
console.log(payload);
io.to(opponentSocketId).emit("accept", {
roomCode,
bet,
senderSocketId,
});
});
socket.on("disconnect", async () => {
const user = await removeUser(socket.id);
console.log("DISCONNECTED!");
if (user)
io.to(user.room).emit("roomData", {
room: user.room,
users: await getUsersInRoom(user.room),
});
});
});
// MIDDLEWARES
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// API ENDPOINTS
// // Get Profile
// app.get("/profile/:address", async (req, res) => {
// try {
// const { address } = req.params;
// const profile = await pool.query(
// `SELECT * FROM profiles WHERE wallet_address = $1`,
// [address]
// );
// console.log("Fetching Profile!!!");
// console.log(profile.rows[0]);
// res.json(profile.rows[0]);
// } catch (err) {
// console.log("ERROR OCCUREED!!!");
// console.log(err);
// }
// });
// // Get followers
// app.get("/profile/followers/:address", async (req, res) => {
// try {
// const { address } = req.params;
// const profile = await pool.query(
// "SELECT * FROM follows WHERE user_walllet_address=$1",
// [address]
// );
// res.json(profile.rows[0]);
// } catch (err) {
// console.log(err);
// }
// });
// // Get Following
// app.get("/profile/following/:address", async (req, res) => {
// try {
// const { address } = req.params;
// const profile = await pool.query(
// "SELECT * FROM follows WHERE follower_wallet_address=$1",
// [address]
// );
// res.json(profile.rows[0]);
// } catch (err) {
// console.log(err);
// }
// });
// // Get bet for a game
// app.get("/bet/:address", async (req, res) => {
// try {
// const { address } = req.params;
// const bet = await pool.query("SELECT bet FROM games WHERE profile=$1", [
// address,
// ]);
// console.log(bet.rows[0]);
// res.json(bet.rows[0]);
// } catch (err) {
// console.log(err);
// }
// });
// // Get current Games
// app.get("/games", async (req, res) => {
// try {
// const games = await pool.query(
// "SELECT g.profile,g.socketid,g.bet,p.name,p.image from games g inner join profiles p on g.profile = p.wallet_address"
// );
// res.json(games.rows);
// } catch (err) {
// console.log(err);
// }
// });
// // Create a game
// app.post("/games", async (req, res) => {
// try {
// const { walletAddress, bet, socketid } = req.body;
// const newGame = await pool.query(
// "INSERT INTO games (profile, socketid, bet) VALUES ($1,$2,$3) RETURNING *",
// [walletAddress, socketid, bet]
// );
// // console.log(req.body);
// res.json(newGame.rows[0]);
// } catch (err) {
// console.log(err.message);
// }
// });
// // Delete a game
// app.post("/deleteGame", async (req, res) => {
// try {
// const { address } = req.body;
// const deletedGames = await pool.query(
// "DELETE FROM games WHERE profile=$(1)",
// [address]
// );
// res.json(deletedGames.rows);
// } catch (err) {
// console.log(err);
// }
// });
// // Create a profile
// app.post("/createProfile", async (req, res) => {
// try {
// const { address, name, image_url, description } = req.body;
// const profile = await pool.query(
// "INSERT INTO profiles (wallet_address,name,image,description) VALUES ($(1),$(2),$(3),$(4)) RETURNING *",
// [address, name, image_url, description]
// );
// res.json(profile.rows[0]);
// } catch (err) {
// console.log(err.message);
// }
// });
// // Update a profile
// app.post("/updateProfile", async (req, res) => {
// const query = "UPDATE profiles SET ";
// const { name, image_url, description } = req.body;
// let first = false;
// if (name) {
// query += "name=" + name;
// first = true;
// }
// if (image_url) {
// if (first) query += ", ";
// query += "image=" + image_url;
// first = true;
// }
// if (description) {
// if (first) query += ", ";
// query += "description=" + description;
// first = true;
// }
// try {
// const profile = await pool.query(query);
// res.json(profile.rows[0]);
// } catch (err) {
// console.log(err.message);
// }
// });
// // Follow a profile
// app.post("/followProfile", async (req, res) => {
// try {
// const { following_address, user_address } = req.body;
// const follow = await pool.query(
// "INSERT INTO follows (user_wallet_address,follower_wallet_address) VALUES ($(1),$(2))",
// [following_address, user_address]
// );
// res.json(follow.rows[0]);
// } catch (err) {
// console.log(err.message);
// }
// });
// // Unfollow a profile
// app.post("/unfollowProfile", async (req, res) => {
// try {
// const { following_address, user_address } = req.body;
// const unfollow = await pool.query(
// "DELETE FROM follows WHERE user_wallet_address=$(1) AND follower_wallet_address=$(2))",
// [following_address, user_address]
// );
// res.json(unfollow.rows[0]);
// } catch (err) {
// console.log(err.message);
// }
// });
// serve static assets in production
if (process.env.NODE_ENV === "production") {
//set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
io.listen(8080);
server.listen(process.env.PORT || 5000, () => {
console.log(`Server running on port ${process.env.PORT || 5000}`);
});