Skip to content

Instantly share code, notes, and snippets.

@dougpfeffer
Created May 24, 2012 21:57
Show Gist options
  • Save dougpfeffer/2784455 to your computer and use it in GitHub Desktop.
Save dougpfeffer/2784455 to your computer and use it in GitHub Desktop.
Hangouts/Backbone.js integration bit
// Fired off by the gadget manager, starts up our app by hooking it into the hangout loading process.
function init() {
gapi.hangout.onApiReady.add(
function (eventObj) {
if (eventObj.isApiReady) {
// Are we the admin?
var currentParticipants = gapi.hangout.getParticipants();
if (currentParticipants.length == 1) {
isAdmin = true;
}
// If we're the admin, clear the state going forward.
if (isAdmin) {
gapi.hangout.data.clearValue('status');
gapi.hangout.data.clearValue('buzzedByUserId');
gapi.hangout.data.clearValue('currentVideoIndex');
gapi.hangout.data.clearValue('participantData');
}
gapi.hangout.data.onStateChanged.add(onHangoutsStatusChanged);
gapi.hangout.onParticipantsChanged.add(onParticipantsChanged);
setupApp(); // Fire off the Backbone.js app here.
// When a user first joins build out the local participant structures.
App.initializeLocalParticipants(currentParticipants);
}
});
}
// Here's where we catch the updated data from Hangouts and hand it off to the Backbone app to process.
// These various App.* methods break apart the received data and perform the usual Backbone model object.set(key, value)
// bits, so everyone's in line with updates that get sent over from other people.
function onHangoutsStatusChanged(addedKeys, metadata, removedKeys, state) {
App.updateGameStateStatus(addedKeys.state.status);
App.updateGameStateBuzzedByUserId(addedKeys.state.buzzedByUserId);
App.updateGameStateCurrentVideoIndex(addedKeys.state.currentVideoIndex);
if (!isAdmin) {
if (!_.isUndefined(addedKeys.state.participantData)) {
var newParticipants = JSON.parse(addedKeys.state.participantData);
App.updateScoresForParticipants(newParticipants); // Find each in the local participants and tweak their scores.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment