Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. function cmp (a, b) {
  2.  
  3. var timeA = a.title.split(" ");
  4. var timeB = b.title.split(" ");
  5.  
  6. let A = parseInt(timeA[1]) + 31* parseInt( months[timeA[2]] ) ;
  7. let B = parseInt(timeB[1]) + 31* parseInt( months[timeB[2]] ) ;
  8.  
  9. let comparison = 0;
  10. if (A > B ) {
  11. comparison = 1;
  12. } else if ( A < B ) {
  13. comparison = -1;
  14. }
  15. return comparison;
  16. }
  17.  
  18. var months = { "Sep" : 8 , "Oct" : 9 , "Nov" : 10 , "Dec" : 11 };
  19. var days = [ "Sat" , "Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" ];
  20. var offset = { "Sep" : 0 , "Oct" : 2 , "Nov" : 5 , "Dec" : 0 };
  21.  
  22. function convertTime ( time ) {
  23. let str = time.split(":");
  24. let h = parseInt(str[0]);
  25. if ( h >= 12 ) {
  26. h %=12 ;
  27. if ( h <= 9 ) return "0"+h+":"+str[1]+"PM";
  28. return h+":"+str[1]+"PM";
  29. }
  30. return time +"AM" ;
  31. }
  32.  
  33. var func = ( ListOfMovies ) => {
  34. let ret = [];
  35. let usedTitles = new Object();
  36. let newListOfMovies = [] ;
  37. let currentDate = new Date() ;
  38. ListOfMovies.forEach ( function ( movie ){
  39. let currentMovie = [] ;
  40. currentMovie.name = movie.name ;
  41. currentMovie.sessions = [] ;
  42. let used = [] ;
  43. JSON.parse(movie.list_timing).forEach( function ( detail ) {
  44. let date = detail.showdate.split(" ");
  45.  
  46. if ( months[date[1]] < currentDate.getMonth() ) return ;
  47. if ( months[date[1]] == currentDate.getMonth() && parseInt(date[0]) < currentDate.getDate() ) return ;
  48.  
  49. let cur = days[ ((parseInt(date[0])%7) + offset[date[1]] ) % 7 ] +" "+ date[0] + " " + date[1] ;
  50. if ( usedTitles[cur] == null ) {
  51. usedTitles[cur] = 1 ;
  52. ret.push ( { "title": cur , "movies": [] } );
  53. }
  54. if ( used[cur] == null ) {
  55. used[cur] = currentMovie.sessions.length ;
  56. currentMovie.sessions[ used[cur] ] = {"date":cur , "screens" : [] } ;
  57. }
  58. let idx = used[cur] ;
  59.  
  60. let nw = true ;
  61. let currentType = detail.cinemaOperatorCode;
  62. if ( currentType.length > 0 ) currentType = currentType.substr(4);
  63. if ( currentType.length == 0 ) currentType = "NotDefine" ;
  64.  
  65. for ( i =0 ; i < currentMovie.sessions[ idx ].screens.length ; i++ ){
  66. if ( currentMovie.sessions[ idx ].screens[i].type == currentType ) {
  67. let toAddTime = new Object();
  68. toAddTime.time = convertTime( detail.timingShow );
  69. currentMovie.sessions[ idx ].screens[i].sessions.push(toAddTime);
  70. nw = false ;
  71. break;
  72. }
  73. }
  74. if ( nw === true ){
  75. let nwScreen = { "type" : currentType , "sessions":[] };
  76. let toAddTime = new Object();
  77. toAddTime.time = convertTime( detail.timingShow );
  78. nwScreen.sessions.push(toAddTime);
  79. currentMovie.sessions[ idx ].screens.push(nwScreen);
  80. }
  81.  
  82. })
  83.  
  84. if ( currentMovie.sessions.length > 1 ) {
  85. currentMovie.sessions[0].date = "Today" ;
  86. currentMovie.sessions[1].date = "Tomorrow" ;
  87. }else if ( currentMovie.sessions.length == 1 ) {
  88. currentMovie.sessions[0].date = "Today" ;
  89. }
  90.  
  91. newListOfMovies.push(currentMovie);
  92.  
  93. })
  94.  
  95. ret.sort( cmp ) ;
  96.  
  97. if ( ret.length > 1 ) {
  98. ret[0].title = "Today" ;
  99. ret[1].title = "Tomorrow" ;
  100. }else if ( ret.length == 1 ) {
  101. ret[0].title = "Today" ;
  102. }
  103.  
  104. let nwIdx = [];
  105. for ( i = 0 ; i < ret.length ; i++ ) {
  106. nwIdx[ ret[i].title ] = i ;
  107. }
  108. newListOfMovies.forEach ( function ( movie ) {
  109. for ( i = 0; i < movie.sessions.length ; i++ ) {
  110. ret[ nwIdx[movie.sessions[i].date] ].movies.push(movie);
  111. }
  112. })
  113. return ret;
  114. }
  115.  
  116. var temp = [
  117. {
  118. "id": 661,
  119. "name": "BOOKING/EVENT/PREMIERE",
  120. "list_timing": "[{\"showdate\": \"04 Oct 2019\", \"sessionId\": \"264151\", \"timingShow\": \"10:00\", \"cinemaOperatorCode\": \"VOX STANDARD\"}]"
  121. },
  122. {
  123. "id": 702,
  124. "name": "The Lion King",
  125. "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262767\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262882\", \"timingShow\": \"22:40\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262846\", \"timingShow\": \"13:20\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262771\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262887\", \"timingShow\": \"22:25\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262851\", \"timingShow\": \"13:20\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262775\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262892\", \"timingShow\": \"22:25\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"264010\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"263974\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"263976\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"04 Oct 2019\", \"sessionId\": \"264016\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263946\", \"timingShow\": \"10:50\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"264022\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263986\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263988\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"264028\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"263991\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"263993\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"264034\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"263996\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"263998\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264040\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264001\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264003\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"09 Oct 2019\", \"sessionId\": \"264046\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"09 Oct 2019\", \"sessionId\": \"264006\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}]"
  126. },
  127. {
  128. "id": 698,
  129. "name": "The Big Trip",
  130. "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262880\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262885\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262890\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}]"
  131. },
  132. {
  133. "id": 701,
  134. "name": "Once Upon A Time in Hollywood (Distraction Free)",
  135. "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262700\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262705\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262710\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"04 Oct 2019\", \"sessionId\": \"263807\", \"timingShow\": \"11:00\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263812\", \"timingShow\": \"11:00\", \"cinemaOperatorCode\": \"\"}]"
  136. }];
  137.  
  138. var result = func( temp );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement