Created
April 6, 2013 15:58
-
-
Save zoranf/5326576 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
url = "mongodb://nodejitsu_zoranf:[email protected]:51947/nodejitsu_zoranf_nodejitsudb8036041315", | |
coll = ['users', 'games'], | |
db = require('mongojs').connect(url, coll) | |
http = require('http'), | |
path = require('path'); | |
app = express(), | |
server = http.createServer(app), | |
io = require('socket.io').listen(server); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.favicon(__dirname + '/public/images/favicon.ico')); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
}); | |
app.configure('development', function() { | |
app.use(express.errorHandler()); | |
}); | |
app.configure('production', function() { | |
app.use(express.errorHandler()); | |
}); | |
/////////////////////////// | |
// routes // | |
/////////////////////////// | |
// GET | |
// Index | |
app.get('/', function(req, res) { | |
db.games.find(function(err, docs) { | |
res.render('index', { | |
title: 'Home', | |
games: docs | |
}); | |
console.log(docs); | |
}); | |
}); | |
// Lobby | |
app.get('/lobby/:id', function(req, res) { | |
db.games.find( { name: req.params.id }, function(err, game) { | |
if (err || game == '') { | |
return res.render('404', { | |
title: 'Game not found', | |
message: 'Game ' + req.params.id + 'not found' | |
}); | |
} | |
return res.render('lobby', { title: 'Lobby', name: req.params.id }); | |
}); | |
}); | |
// Room | |
app.get('/room/:id', function(req, res) { | |
res.render('room', { title: 'Room'}); | |
var room_id = req.params.id; | |
}); | |
// POST | |
// Sign in | |
app.post('/sign_in', function(req, res) { | |
res.redirect(); | |
}); | |
// Sing up | |
app.post('/sign_up', function() { | |
res.redirect(); | |
}); | |
// Create room | |
app.post('/room', function(req, res) { | |
res.redirect('/room/1'); // tu daj ovi counter ka maš | |
}); | |
/////////////////////////// | |
// error // | |
/////////////////////////// | |
// 404 File not found | |
app.use(function(req, res, next) { | |
res.status(404); | |
res.render('404', { | |
title: '404 File not found', | |
message: '404 File not found' | |
}); | |
}); | |
// 500 Internal server error | |
app.use(function(err, req, res, next) { | |
res.status(500); | |
res.render('500', { | |
title: '500 Internal server error', | |
message: '500 Internal server error' | |
}); | |
}); | |
server.listen(app.get('port'), function() { console.log("Express server listening on port " + app.get('port')); }); | |
var clients = []; | |
var rooms1 = []; | |
var rooms2 = []; | |
var rooms3 = []; | |
var rooms1_id = []; | |
var counter1 = 0; | |
io.sockets.on('connection', function(socket) { | |
socket.on('connect', function(data){ | |
console.log("Connected " + socket.id); | |
}); | |
socket.on('disconnect', function(){ | |
console.log("Disconnected " + socket.id); | |
}); | |
socket.on('create_room', function(data){ | |
if(data.game == "Schnapssen"){ | |
rooms1[counter1] = data; | |
rooms1_id.push(counter1); | |
counter1++; | |
socket.emit('update_rooms', {rooms: rooms1, rooms_id: rooms1_id}); | |
} | |
}); | |
socket.on('send_rooms', function(data){ | |
if(data.game == "Schnapssen"){ | |
socket.emit('update_rooms', rooms1); | |
} | |
}); | |
socket.on('join_room', function(data){ | |
socket.join(data.room_id); | |
}); | |
socket.on('send_message', function(data) { | |
msg = data.username + ': ' + data.message; | |
io.sockets.in(data.room_id).emit('get_message', msg); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block content | |
h1 Game lobby #{name} | |
p Feel free to play with others | |
#lobby | |
ul#nav | |
li#id # | |
li#name Name | |
li#host Host | |
li#type Type | |
li#free Free | |
li#password Password | |
#filter | |
form(method='POST', action='/room') | |
p Search | |
.input_bg | |
input(type="text", name="search", value="Search", id="search_input") | |
p Create new game | |
.input_bg | |
input(type="text", name="search", value="Name", id="name_input") | |
.input_bg | |
input(type="text", name="search", value="Password", id="password_input") | |
p Game type | |
#game_type | |
input(type="submit", name="create_game", value="", id="create_game") | |
#rooms.scroll-me |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment