Created
May 3, 2012 04:05
-
-
Save ctriolo/2583061 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
| diff --git a/controllers/game.io.js b/controllers/game.io.js | |
| index cd905e8..2c011da 100644 | |
| --- a/controllers/game.io.js | |
| +++ b/controllers/game.io.js | |
| @@ -2,12 +2,18 @@ | |
| * Game Server Side Socket IO | |
| */ | |
| +// Constants | |
| + | |
| +var OPENTOK_API_KEY = '13250542'; | |
| +var OPENTOK_API_SECRET = '0263b77a74734ba0fdf356047286ee2af88cfab1'; | |
| + | |
| // Dependecies | |
| -var GameProvider = require('../models/GameProvider'); | |
| +var GameProvider = require('../models/GameProvider') | |
| + , OpenTok = require('opentok') | |
| + , ot = new OpenTok.OpenTokSDK(OPENTOK_API_KEY, OPENTOK_API_SECRET); | |
| var uid_to_gid = {}; // user to game | |
| - | |
| /** | |
| * updatePlayerInfo | |
| * | |
| @@ -49,6 +55,26 @@ module.exports = function(sockets) { | |
| sockets.to(user_id).emit('start', game.getPlayers(), user_id); | |
| if (!game.isPlayer(user_id)) sockets.to(user_id).send('You cannot join a game that\'s already started.'); | |
| } else if (game.canStart()) sockets.to(game_id).emit('canStart'); | |
| + | |
| + if (!('sessionId' in game) && game.isPlayer(user_id)) { | |
| + ot.createSession('localhost', {}, function(session) { | |
| + game.sessionId = session.sessionId; | |
| + var token = ot.generateToken({ | |
| + sessionId: game.sessionId, | |
| + role: OpenTok.Roles.SUBSCRIBER | |
| + }); | |
| + game.players[game._translate(user_id)].token = token; | |
| + socket.emit('joined', OPENTOK_API_KEY, game.sessionId, token); | |
| + }); | |
| + } else if (game.isPlayer(user_id)) { | |
| + var token = ot.generateToken({ | |
| + sessionId: game.sessionId, | |
| + role: OpenTok.Roles.SUBSCRIBER | |
| + }); | |
| + game.players[game._translate(user_id)].token = token; | |
| + socket.emit('joined', OPENTOK_API_KEY, game.sessionId, token); | |
| + } | |
| + | |
| }); | |
| diff --git a/controllers/game.js b/controllers/game.js | |
| index 3b8209e..fd564c6 100644 | |
| --- a/controllers/game.js | |
| +++ b/controllers/game.js | |
| @@ -9,7 +9,6 @@ var Game = require('../models/Game') | |
| , GameProvider = require('../models/GameProvider') | |
| , UIBoard = require('../models/Board/UIBoard'); | |
| - | |
| /** | |
| * view | |
| * | |
| @@ -25,8 +24,9 @@ module.exports.view = function(req, res) { | |
| game.id = id; | |
| } | |
| + var player_id = -1; | |
| if (!game.isPlayer(req.sessionID) && !game.isStarted()) { | |
| - game.addPlayer(req.sessionID); | |
| + player_id = game.addPlayer(req.sessionID); | |
| } | |
| gp.save(game); | |
| diff --git a/models/Game.js b/models/Game.js | |
| index d80793a..1e4f479 100644 | |
| --- a/models/Game.js | |
| +++ b/models/Game.js | |
| @@ -537,7 +537,10 @@ Game.prototype.canBuildDevelopment = function(user_id) { | |
| Game.prototype.addPlayer = function(user_id) { | |
| this._validatePhase(PHASE.START); | |
| + | |
| this.players.push(new Player(this.players.length, user_id)); | |
| + | |
| + | |
| return this.players.length - 1; | |
| }; | |
| diff --git a/package.json b/package.json | |
| index 84a7174..12f4be9 100644 | |
| --- a/package.json | |
| +++ b/package.json | |
| @@ -9,5 +9,6 @@ | |
| , "socket.io": ">= 0.9.2" | |
| , "connect": ">= 0.0.1" | |
| , "everyauth": ">= 0.0.1" | |
| + , "opentok": ">= 0.0.1" | |
| } | |
| } | |
| diff --git a/public/javascripts/game.js b/public/javascripts/game.js | |
| index 20fe041..f56872f 100644 | |
| --- a/public/javascripts/game.js | |
| +++ b/public/javascripts/game.js | |
| @@ -8,6 +8,10 @@ var me; | |
| var roll_frequency = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
| var total_rolls = 0.0; | |
| +// open tok | |
| +var session; | |
| +var publisher; | |
| + | |
| function makeSVG(tag, attrs) { | |
| var el= document.createElementNS('http://www.w3.org/2000/svg', tag); | |
| for (var k in attrs) | |
| @@ -148,6 +152,45 @@ window.onload = function() { | |
| * Game Socket Listeners | |
| */ | |
| + /** | |
| + * join | |
| + */ | |
| + socket.on('joined', function(apiKey, sessionId, token) { | |
| + session = TB.initSession(sessionId); | |
| + session.addEventListener('sessionConnected', sessionConnectedHandler); | |
| + session.addEventListener('streamCreated', streamCreatedHandler); | |
| + session.connect(apiKey, token); | |
| + | |
| + console.log(apiKey, sessionId, token); | |
| + | |
| + function sessionConnectedHandler(event) { | |
| + publisher = session.publish('myPublisherDiv'); | |
| + // Subscribe to streams that were in the session when we connected | |
| + subscribeToStreams(event.streams); | |
| + } | |
| + | |
| + function streamCreatedHandler(event) { | |
| + // Subscribe to any new streams that are created | |
| + subscribeToStreams(event.streams); | |
| + } | |
| + | |
| + function subscribeToStreams(streams) { | |
| + for (var i = 0; i < streams.length; i++) { | |
| + // Make sure we don't subscribe to ourself | |
| + if (streams[i].connection.connectionId == session.connection.connectionId) { | |
| + return; | |
| + } | |
| + | |
| + // Create the div to put the subscriber element in to | |
| + var div = document.createElement('div'); | |
| + div.setAttribute('id', 'stream' + streams[i].streamId); | |
| + document.body.appendChild(div); | |
| + | |
| + // Subscribe to the stream | |
| + session.subscribe(streams[i], div.id); | |
| + } | |
| + } | |
| + }); | |
| // Messages | |
| diff --git a/views/game.jade b/views/game.jade | |
| index efb21c2..323ff9b 100644 | |
| --- a/views/game.jade | |
| +++ b/views/game.jade | |
| @@ -6,6 +6,7 @@ block title | |
| block head | |
| link(rel='stylesheet', href='/stylesheets/board.css') | |
| link(rel='stylesheet', href='/stylesheets/game.css') | |
| + script(src="http://staging.tokbox.com/v0.91/js/TB.min.js") | |
| script(src="/javascripts/game.js") | |
| block board | |
| @@ -31,6 +32,7 @@ block labels | |
| block player | |
| #player0.player.well | |
| .left | |
| + #myPublisherDiv | |
| img(src='http://i.imgur.com/TFOqB.jpg').pic | |
| h3.name Empty | |
| .right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment