-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (57 loc) · 1.76 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
var express = require('express'),
_ = require('underscore');
var app = express.createServer(express.logger());
var publicDirname = __dirname + "/public";
app.use(express['static'](publicDirname)); // annoying JSLint error.
app.set('view engine', 'jade');
app.set('view options', {
layout : false
});
var io = require('socket.io').listen(app);
var Room = require('./room').Room;
io.sockets.on('connection', function(socket) {
var room;
socket.on('room-observer', function(roomName) {
room = Room.findOrCreateByName(roomName);
room.addObserver(socket);
});
socket.on('room-connect', function(info) {
var roomName = info.roomName,
userName = info.userName,
playerNumber = info.playerNumber;
room = Room.findOrCreateByName(roomName);
room.join(socket, playerNumber, userName);
});
socket.on('player-ready', function() {
room.registerPlayerAsReady(socket);
});
socket.on('game-countdown', function(currentCount) {
room.emitCountdown(currentCount);
});
socket.on('game-start', function(gameState) {
room.emitGameStart(gameState);
});
socket.on('game-sync', function(gameState) {
room.emitGameSync(gameState);
});
socket.on('game-move', function(direction) {
room.beginMove(socket, direction);
});
socket.on('game-endmove', function(direction) {
room.endMove(socket, direction);
});
socket.on('disconnect', function() {
if (room) { room.socketDisconnected(socket); }
});
});
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log("Listening on " + port);
});
app.get("/games/:roomName", function(req, res) {
var roomName = req.params.roomName;
var room = Room.findOrCreateByName(roomName);
res.render('games/show.html.jade', {
room : room
});
});