cryptalk/public/js/cryptalk_modules/room.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-09-26 08:46:35 -04:00
/*
Accepts:
mediator.on('command:join', ...);
mediator.on('command:leave', ...);
mediator.on('command:key', ...);
Emits:
mediator.emit('room:changed',...);
mediator.emit('console:error',...);
mediator.emit('socket:emit',...);
*/
define(
{
compiles: ['$'],
requires: ['castrato','settings','templates']
}, function ($, requires, data) {
var
// Private properties
room=false,
// Require shortcuts
mediator = requires.castrato,
settings = requires.settings,
templates = requires.templates,
join = function(payload) {
if (room !== false) {
mediator.emit('console:error',$.template(templates.messages.already_in_room, { room: room }));
2014-09-26 12:21:27 -04:00
} else if (payload.length >= settings.room.maxLen) {
mediator.emit('console:error',$.template(templates.messages.room_name_too_long));
} else if (payload.length < settings.room.minLen) {
mediator.emit('console:error',$.template(templates.messages.room_name_too_short));
2014-09-26 08:46:35 -04:00
} else {
room = payload;
mediator.emit('room:changed', room );
mediator.emit('socket:emit',{ data: 'room:join' , payload: $.SHA1(room) } );
}
},
leave = function() {
if (room !== false) {
mediator.emit('socket:emit',{ data: 'room:leave' , payload: $.SHA1(room) } );
room = false;
} else {
mediator.emit('console:error',templates.messages.leave_from_nowhere);
}
},
count = function() {
if (room) {
mediator.emit('socket:emit','room:count');
} else {
mediator.emit('console:error', templates.messages.not_in_room);
}
};
// Connect events
mediator.on('command:join', join);
mediator.on('command:leave', leave);
mediator.on('command:count', count);
});