forked from bernatixer/BetYourDignity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
137 lines (117 loc) · 5.35 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
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var { login, getCurrentSessionAccount, getAccountMedia, like, getFollowers, follow } = require('./instagram-scraper');
require('dotenv').config();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/static'));
app.use(cookieParser());
var cookie = require('cookie');
var status = {
EMPTY: 0,
WAITING: 1,
FULL: 2
};
var hostBiene;
var visitorBiene;
var game = {};
function getRooms() {
return game;
}
io.on('connection', function (socket) {
var playing = false;
var sessionInfo;
var socketCookie = socket.handshake.headers.cookie;
var playerNum;
io.emit('setRooms', getRooms());
if (socketCookie != null) {
var cookieInfo = cookie.parse(socketCookie).sessionId;
playerNum = cookie.parse(socketCookie).playerNum;
if (cookieInfo != null) sessionInfo = JSON.parse(cookieInfo);
}
socket.on('move', function (data) {
if (data.action == 'horizontal') {
if (playerNum == 1) socket.broadcast.emit('move', { action: 'horizontal', player: playerNum, dir: data.dir });
else if (playerNum == 2) socket.broadcast.emit('move', { action: 'horizontal', player: playerNum, dir: data.dir });
} else if (data.action == 'vertical') {
if (playerNum == 1) socket.broadcast.emit('move', { action: 'vertical', player: playerNum, dir: data.dir });
else if (playerNum == 2) socket.broadcast.emit('move', { action: 'vertical', player: playerNum, dir: data.dir });
}
});
socket.on('fire', function () {
socket.broadcast.emit('sendBullet');
})
socket.on('loggin', function (data) {
login(data.username, data.password, function (sessionAccount) {
sessionInfo = sessionAccount;
socket.emit('loggin', { sessionId: sessionAccount, rooms: getRooms() });
})
});
socket.on('newRoom', function (roomName) {
game.name = roomName;
game.img = sessionInfo.account.profilePicture.url;
game.host = sessionInfo.account.username;
game.status = status.WAITING;
game[sessionInfo.sessionId] = 1;
game.visitor = "";
game.playerNum = 1;
game.sessionId1 = sessionInfo.sessionId;
playerNum = 1;
playing = true;
socket.emit('setPlayerNum', playerNum);
io.emit('setRooms', getRooms());
});
socket.on('joinRoom', function (roomName) {
if (game.status == status.WAITING && !playing) {
game.status = status.FULL;
game[sessionInfo.sessionId] = 2;
game.visitor = sessionInfo.account.username;
game.playerNum = 2;
game.sessionId2 = sessionInfo.sessionId;
playerNum = 2;
playing = true;
socket.emit('setPlayerNum', playerNum);
io.emit('setRooms', getRooms());
io.emit('selectBiene');
}
});
socket.on('selectBiene', function (biene) {
if (playerNum == 1) hostBiene = biene;
else if (playerNum == 2) visitorBiene = biene;
if (hostBiene != null && visitorBiene != null) io.emit('startGame');
});
socket.on('hit', function(mediaId) {
if (playerNum == 1) like(game.sessionId2, mediaId);
else like(game.sessionId1, mediaId);
});
});
app.get('/development', function (req, res) {
res.render('play', { currPlayer: "player1", hostBiene: 'andreu', visitorBiene: 'bernat', hostName: 'andreu', visitorName: 'bernat', hostMedia: 'https://scontent-frx5-1.cdninstagram.com/vp/888c05e40d49f6db2e5a7c7786358a22/5C6E29A4/t51.2885-15/e35/s240x240/43914840_259986014707285_7552744139318422425_n.jpg?ig_cache_key=MTkwNDc5NDQzNjkzNDQ4MjU1Mg%3D%3D.2', visitorMedia: 'https://scontent-frx5-1.cdninstagram.com/vp/888c05e40d49f6db2e5a7c7786358a22/5C6E29A4/t51.2885-15/e35/s240x240/43914840_259986014707285_7552744139318422425_n.jpg?ig_cache_key=MTkwNDc5NDQzNjkzNDQ4MjU1Mg%3D%3D.2' });
});
app.get('/play', function (req, res) {
var playerNum = req.cookies.playerNum;
if (playerNum != null) {
getAccountMedia(game.sessionId1, function (hostMedia) {
getAccountMedia(game.sessionId2, function (visitorMedia) {
hostMedia = JSON.stringify(hostMedia);
visitorMedia = JSON.stringify(visitorMedia);
if (playerNum == 1) res.render('play', { currPlayer: "player1", hostBiene: hostBiene, visitorBiene: visitorBiene, hostName: game.host, visitorName: game.visitor, hostMedia: hostMedia, visitorMedia: visitorMedia });
else if (playerNum == 2) res.render('play', { currPlayer: "player2", hostBiene: hostBiene, visitorBiene: visitorBiene, hostName: game.host, visitorName: game.visitor, hostMedia: hostMedia, visitorMedia: visitorMedia });
});
});
} else res.redirect('/');
});
app.get('/', function (req, res) {
res.render('start');
});
app.get('/winner', function (req, res) {
res.render('winner');
});
app.get('/loser', function (req, res) {
res.render('loser');
});
http.listen(process.env.PORT, function () {
console.log('BetYourDignity running on port ' + process.env.PORT);
});