Advertisement
Guest User

Untitled

a guest
Jun 12th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. var express = require('express')
  2. , djApp = express()
  3. , server = djApp.listen(3001)
  4. , io = require('socket.io')(server)
  5. , bars = require('handlebars')
  6. , mailer = require('nodemailer')
  7. , yt = require('./yt-audio-extractor')
  8. , fs = require('fs')
  9. , child = require('child_process');
  10.  
  11. var boothList = {};
  12. function Booth(creator, openOrInvite, pool, cue) {
  13. this.creator = creator;
  14. this.openOrInvite = openOrInvite;
  15. this.pool = pool;
  16. this.cue = cue;
  17. }
  18. function Pool(creator) {
  19. return {'nextUser': creator, 'users': [creator]};
  20. }
  21. function Cue() {
  22. return {'list':[], 'index':0};
  23. }
  24.  
  25. io.on('connection', function(socket) {
  26. // Handler for validating a new booth creator's name
  27. socket.on('checkCreator', function(obj) {
  28. if (obj.creator in boothList) {
  29. socket.emit('nameTaken', obj);
  30. } else {
  31. socket.emit('nameValid', obj);
  32. }
  33. });
  34.  
  35. // Handler for creating a new booth after its creator has been validated
  36. socket.on('createEvent', function(obj) {
  37. var cue = new Cue();
  38. var creator = obj.creator;
  39. var pool = new Pool(creator);
  40. var booth = new Booth(creator, obj.openOrInvite, pool, cue);
  41. boothList[creator] = booth;
  42. socket.emit('boothCreated', {'booth':booth, 'openOrInvite':obj.openOrInvite});
  43. });
  44.  
  45. // Handler for generating a list of booths when user is finding a booth
  46. socket.on('findEvent', function(obj) {
  47. var booths = {};
  48. for (booth in boothList) {
  49. if (boothList[booth].openOrInvite) {
  50. booths[booth] = {'currentSong': boothList[booth].cue.list[boothList[booth].cue.index].song,
  51. 'booth': boothList[booth]};
  52. }
  53. }
  54. socket.emit('generateList', {'booths': booths});
  55. });
  56.  
  57. socket.on('triggerUpdateBoothListing', function () {
  58. socket.broadcast.emit('updateBoothListing', {})
  59. });
  60.  
  61. socket.on('emailEvent', inviteDjs);
  62.  
  63. socket.on('deleteUser', function (obj) {
  64. var index = obj.booth.pool.users.indexOf(obj.user);
  65. if (index > -1) {
  66. boothList[obj.booth.creator].pool.users.splice(index, 1);
  67. } else {
  68. console.log("That user does not exit in this pool.");
  69. }
  70. if (obj.user == obj.booth.pool.nextUser) {
  71. var nextUser = nextDj(obj.booth.pool.users, obj.user);
  72. boothList[obj.booth.creator].pool.nextUser = nextUser;
  73. }
  74. socket.broadcast.emit('userDeleted', {'booth':boothList[obj.booth.creator]});
  75. });
  76.  
  77. socket.on('poolUpdate', function (obj) {
  78. var lowerCase = [];
  79. for(var i=0; i<obj.booth.pool.users.length; i++) {
  80. lowerCase.push(obj.booth.pool.users[i].toLowerCase());
  81. }
  82. if (lowerCase.indexOf(obj.newUser.toLowerCase()) >= 0) {
  83. socket.emit('userJoinError', {});
  84. } else {
  85. boothList[obj.booth.creator].pool.users.push(obj.newUser);
  86. socket.broadcast.emit('userJoined', {'booth': boothList[obj.booth.creator], 'firstTime': false, 'newUser': obj.newUser, 'buildPlayer': obj.buildPlayer});
  87. socket.emit('userJoined', {'booth': boothList[obj.booth.creator], 'firstTime': true, 'newUser': obj.newUser, 'buildPlayer': obj.buildPlayer});
  88. }
  89. });
  90.  
  91. socket.on('cueEvent', function (obj) {
  92. if (obj.ytLink) {
  93. var link = obj.ytLink.split('&index')[0].split('&list')[0]; // Maybe not necessary
  94. var id = link.split('=')[1];
  95. yt.downloader(link, cleanUp);
  96. } else {
  97. cleanUp("No song choosen yet...", true);
  98. }
  99.  
  100.  
  101. function cleanUp(songName, valid) {
  102. if (valid) {
  103. var songObj = {'user': obj.user, 'song': songName, 'id':id};
  104. var nextUser = nextDj(boothList[obj.booth.creator].pool.users, obj.user);
  105. boothList[obj.booth.creator].pool.nextUser = nextUser;
  106. if (boothList[obj.booth.creator].cue.list[0] && boothList[obj.booth.creator].cue.list[0].song == "No song choosen yet...") {
  107. boothList[obj.booth.creator].cue.list.pop();
  108. boothList[obj.booth.creator].cue.list.push(songObj);
  109. io.emit('songCued', {'booth':boothList[obj.booth.creator], 'replace':true, 'nextUser':boothList[obj.booth.creator].pool.nextUser});
  110. } else {
  111. boothList[obj.booth.creator].cue.list.push(songObj);
  112. io.emit('songCued', {'booth':boothList[obj.booth.creator], 'replace':false, 'nextUser':boothList[obj.booth.creator].pool.nextUser});
  113. }
  114. } else {
  115. socket.emit('songError', obj.booth);
  116. }
  117. }
  118. });
  119. });
  120.  
  121. function nextDj (pool, currentDj) {
  122. for (var i=0; i<pool.length; i++) {
  123. if (pool[i] == currentDj && i+1 < pool.length) {
  124. return pool[i+1];
  125. } else if (pool[i] == currentDj && i+1 >= pool.length) {
  126. return pool[0];
  127. }
  128. }
  129. }
  130.  
  131. function inviteDjs(obj) {
  132. var transporter = mailer.createTransport({
  133. service: 'Gmail',
  134. auth: {
  135. user: 'cueloopinvite@gmail.com',
  136. pass: 'e0X{sXIe)eeVfs,'
  137. }
  138. });
  139.  
  140. var mailOptions = {
  141. from: 'no-reply@localhost:3001',
  142. to: obj.emails,
  143. subject: obj.creator+' invited you to DJ in their CueLoop booth!',
  144. text: 'Pretty uh, prettttty cool.'
  145. };
  146.  
  147. transporter.sendMail(mailOptions, function(error, info){
  148. if(error){
  149. console.log(error);
  150. }else{
  151. console.log('Message sent: ' + info.response);
  152. };
  153. });
  154. }
  155.  
  156. djApp.use('/', express.static(__dirname+'/public'));
  157. djApp.get('/*', function (req, res) {
  158. for(booth in boothList) {
  159. var path = req.path.split('/')[1].toLowerCase();
  160. var boothID = boothList[booth].creator.toLowerCase();
  161. if (path == boothID) {
  162. res.sendFile(__dirname+'/public/index.html');
  163. io.on('connection', function(socket) {
  164. socket.emit('renderBooth', {'booth': boothList[booth]});
  165. });
  166. }
  167. }
  168. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement