var socket = io.connect(window.location.hostname); var State = { WAITING: 0, DRAWING: 1, VOTING: 2, WIN: 3, RESET: -1, } var Game = { nick: null, state: State.WAITING, init: function(){ // Bind events from the server socket.on('event-welcome', Events.receive.welcome); socket.on('event-join', Events.receive.join); socket.on('event-part', Events.receive.part); socket.on('event-message', Events.receive.message); socket.on('event-state', Events.receive.state); socket.on('event-vote', Events.receive.vote); socket.on('event-image', Events.receive.image); Auth.init(); Chat.init(); } } var Events = { receive: { // Player connects to the game welcome: function(data){ for (var i in data.messages) { Chat.add(data.messages[i]); } }, // Another player joins the room join: function(data){ Events.receive.state(data); }, // Another player leaves the room part: function(data){ Events.receive.state(data); }, // Game state changes state: function(data){ if (Game.state == data.state) return; UI[ States.current ].unload(); UI[ data.state ].load(); Game.state = data.state; }, // Incoming chat message: function(msg) { Chat.add(msg); }, // Incoming image image: function(data) { console.log(data); if (data.nick == Game.nick) return; $("#image").attr("src", data.url).show(); }, // Another player votes on an image vote: function(data){ } }, send: { // Sending a message message: function(msg) { socket.emit('event-message', msg); }, // Sending a vote vote: function(id){ } }, }; // Explicit changes to the UI when moving from one state to another. // This is for things like moving divs off the screen, changing bindings, etc. // Anything a load method does should be undone by the unload method.. for now. var UI = {}; UI[ State.WAITING ] = { load: function(){ }, unload: function(){ } } UI[ State.DRAWING ] = { load: function(){ }, unload: function(){ } } UI[ State.VOTING ] = { load: function(){ }, unload: function(){ } } UI[ State.WIN ] = { load: function(){ }, unload: function(){ } } UI[ State.RESET ] = { load: function(){ }, unload: function(){ } } $(Game.init);