cryptalk/public/js/lib/room.js

67 lines
1.6 KiB
JavaScript
Raw Permalink 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',...);
*/
2017-02-07 15:19:23 -05:00
define(['$', 'castrato', 'settings', 'templates'], function ($, mediator, settings, templates) {
2014-09-27 08:44:41 -04:00
var // Private properties
room = false,
2014-09-26 08:46:35 -04:00
join = function(payload) {
if (room !== false) {
2014-09-27 08:44:41 -04:00
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) {
2014-09-27 08:44:41 -04:00
mediator.emit('console:error', $.template(templates.messages.room_name_too_long));
2014-09-26 12:21:27 -04:00
} else if (payload.length < settings.room.minLen) {
2014-09-27 08:44:41 -04:00
mediator.emit('console:error', $.template(templates.messages.room_name_too_short));
2014-09-26 08:46:35 -04:00
} else {
room = payload;
2014-09-27 08:44:41 -04:00
mediator
.emit('room:changed', room)
.emit('socket:emit', {
data: 'room:join',
payload: $.SHA1(room)
});
2014-09-26 08:46:35 -04:00
}
},
leave = function() {
if (room !== false) {
2014-09-27 08:44:41 -04:00
mediator.emit('socket:emit', {
data: 'room:leave',
payload: $.SHA1(room)
});
2014-09-26 08:46:35 -04:00
room = false;
} else {
2014-09-27 08:44:41 -04:00
mediator.emit('console:error', templates.messages.leave_from_nowhere);
2014-09-26 08:46:35 -04:00
}
},
count = function() {
if (room) {
2015-10-04 08:44:40 -04:00
mediator.emit('socket:emit', {data: 'room:count'});
2014-09-26 08:46:35 -04:00
} 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);
});