forked from jeremyqzt/offensiveHearts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (117 loc) · 4.53 KB
/
index.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
var express = require('express');
var bodyParser = require("body-parser");
var session = require('express-session');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var deckClass = require('./deck');
var gameClass = require('./game');
var adaptor = require('./gameServerAdaptor');
var serverAdaptor = new adaptor.gameServerAdaptor()
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({secret: 'secret', resave: true, saveUninitialized: true}));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render("start");
});
app.get('/room/:rid/player/:pid/', function (req, res) {
okayName = serverAdaptor.getOkayName(req.params.rid, req.params.pid);
if (okayName == req.params.pid) {
var roomInfo = {roomName: req.params.rid, player: req.params.pid};
res.render("game", {info: roomInfo});
} else {
res.redirect(`/room/${req.params.rid}/player/${okayName}`); //Avoid name collision
}
})
app.get('/room/:rid/', function (req, res) {
var room = req.params.rid;
var name = "NoName";
name = serverAdaptor.getOkayName(room, name);
res.redirect(`/room/${room}/player/${name}`);
})
app.post("/createRoom",function(req, res){
var name = req.body.name;
var room = serverAdaptor.createRoom();
res.redirect(`/room/${room}/player/${name}`)
});
app.get('/gameOver',function(req, res){
res.sendFile(__dirname + '/public/winner.html');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
var sockRooms = {};
io.on('connection', (socket) => {
socket.on('disconnect', () => {
var room = serverAdaptor.getRoom(socket);
var player = serverAdaptor.getPlayer(socket);
try{
console.log( `${player} has left the ${room}!`)
io.to(room).emit('chat', `${player} has left the room!`);
serverAdaptor.leaveRoom(socket);
} catch(e) {
console.log(e);
}
});
socket.on('join', (room, player) => {
//Join the room
console.log( `${player} has joined the ${room}!`)
serverAdaptor.joinRoom(room, socket, player);
//Notify everyone
io.to(room).emit('chat', player + " has joined the room!");
updateScore(socket, room);
//Flip over already flipped cards
var alreadyUsed = serverAdaptor.getGame(room).getRemovedCards();
for (var i = 0; i < alreadyUsed.length; i++){
var toFlip = "#R" + alreadyUsed[i].row + "C" + alreadyUsed[i].column;
io.to(room).emit('disappear', toFlip);
}
});
socket.on('flipReq', (row, col, name) => {
var room = serverAdaptor.getRoom(socket);
let toFlip = `#R${row}C${col}`;
var actions = serverAdaptor.getGame(room).flipCard(row, col, name);
if (actions.toFlip != null){
io.to(room).emit('serverFlip', toFlip, "/img/" + actions.toFlip.imageName, true, name);
postFlipActions(actions, socket, name, room);
}
});
socket.on('chat', (player, msg) => {
var room = serverAdaptor.getRoom(socket);
io.to(room).emit('chat', player + ": " + msg);
});
async function postFlipActions(actions, sock, name, room){
//return;
var game = serverAdaptor.getGame(room);
if (actions.toFlipDisappear.length > 0){
updateScore(sock, room);
}
await new Promise(r => setTimeout(r, 500));
var toFlip = "";
for (var i = 0; i< actions.toFlipDelay.length; i++){
toFlip = "#R" + actions.toFlipDelay[i].row + "C" + actions.toFlipDelay[i].column;
io.to(room).emit('serverFlip', toFlip, "/img/back.png", false, name);
}
await new Promise(r => setTimeout(r, 200));
for (var i = 0; i< actions.toFlipDisappear.length; i++){
toFlip = "#R" + actions.toFlipDisappear[i].row + "C" + actions.toFlipDisappear[i].column;
io.to(room).emit('disappear', toFlip);
io.to(room).emit('serverFlip', toFlip, "/img/back.png", false, name); //Also have to flip them back
}
if (game.isGameOver()){
var scoreUpdate = game.getScores();
var qString="?";
var idxToPlace =["first", "second", "third"];
for (var i = 0; i< scoreUpdate.length; i++){
qString += idxToPlace[i] + "=" + scoreUpdate[i].player + "," + scoreUpdate[i].score + "&"
}
io.to(room).emit('gameOver', qString);
}
}
function updateScore(socket, room){
var scoreUpdate = serverAdaptor.getGame(room).getScores();
io.to(room).emit('scoreUpdate', scoreUpdate);
}
});