Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = {
  2.    
  3.     startedSocket: false,
  4.     debugMode: false,
  5.    
  6.     webSocket: null,
  7.     reconnectionInterval: null,
  8.     pingInterval: null,
  9.    
  10.     host: null,
  11.     port: null,
  12.    
  13.     healthCycler: null,
  14.    
  15.     walking: false,
  16.     walkingInterval: null,
  17.    
  18.        
  19.     CaptchaBox: $('#captcha-box'),
  20.     CaptchaBoxNotif: $('.captcha-box-notif'),
  21.     InputBox: $('.captcha-box-input'),
  22.     ErrorBox: $('.captcha-box-status'),
  23.     SuccessTick: $('.captcha-box-success'),
  24.     processCaptcha: null,
  25.     checkSeconds: 2,
  26.     curSeconds: 0,
  27.    
  28.     /**
  29.      *
  30.      * Initializes all components within the application.
  31.      *
  32.      * @property {function} initialize.
  33.      */  
  34.     initialize: function(myID, myUsername, myFigure) {
  35.        
  36.         user = new User(myID, myUsername, myFigure);   
  37.        
  38.         user.id = myID;
  39.         user.name = myUsername;
  40.         user.figure = myFigure;
  41.    
  42.         app['host'] = flashvars['connection.socket.host'];
  43.         app['port'] = flashvars['connection.socket.port'];
  44.        
  45.         app['bindEvents']();
  46.         app['initSockets']();
  47.     },
  48.      /**
  49.      *
  50.      * Binds the WebSocket Events (clicks, and such)
  51.      *
  52.      * @property {function} bindEvents.
  53.      */
  54.     bindEvents: function() {
  55.  
  56.    
  57.         // INITALIZE UR JQUERY EVENTS HERE LIKE .CLICK AND STUFF.
  58.        
  59.         app['bindATM']();
  60.         app['bindCaptchaBox']();
  61.    
  62.     },
  63.    
  64.     /**
  65.      *
  66.      * Sends data to the users WebSocket
  67.      *
  68.      * @property {function} sendData.
  69.      */
  70.     sendData: function(event, data, bypass, json){
  71.         if(typeof app['webSocket'] == undefined)
  72.             return;
  73.        
  74.         if(app['startedSocket'] == false || app['webSocket'] == null)
  75.         {
  76.             console.log('[WEBSOCKET] Failed to send data as socket is not running!');
  77.             return;
  78.         }
  79.        
  80.         if(app['debugMode'])
  81.         {
  82.             console.log('sent data:::\n\n');
  83.             var ap = "---------------------------------------------\n";
  84.             ap += "Data Type: " + ( (app['isJSONData'](data)) ? "json" : "split_text") + "\n";
  85.             ap += "Data contents: " + ( (app['isJSONData'](data)) ? JSON.stringify(JSON.parse(data)) : data) + "\n";
  86.             ap += "---------------------------------------------\n\n"
  87.             console.log(ap);
  88.         }
  89.  
  90.         bypass = typeof bypass === 'undefined' ? false : bypass;
  91.        
  92.         app['webSocket'].send(JSON.stringify({     
  93.             UserId: user.id,
  94.             EventName: event,
  95.             Bypass: bypass,
  96.             ExtraData: data,   
  97.             JSON: json,
  98.         }));   
  99.     },
  100.    
  101.     isJSONData: function(data){
  102.         try {
  103.             JSON.parse(data);
  104.         } catch (e) {
  105.             return false;
  106.         }
  107.         return true;
  108.     },
  109.    
  110.     /**
  111.      *
  112.      * Tests the WebSocket component within the application.
  113.      *
  114.      * @property {function} testSockets.
  115.      */
  116.     testSockets: function() {
  117.        
  118.     },
  119.    
  120.     /**
  121.      *
  122.      * Attempts to reconnect to websocket
  123.      *
  124.      * @property {function} tryReconnect.
  125.      */
  126.     tryReconnect: function () {
  127.        
  128.         console.log('[WEBSOCKET] Attempting to reconnect to WEBSOCKET..');
  129.         app['webSocket'].close();
  130.         app['webSocket'] = null;
  131.         app['initSockets']();
  132.        
  133.     },
  134.    
  135.     /**
  136.      *
  137.      * Initializes the WebSocket component within the application.
  138.      *
  139.      * @property {function} initSockets.
  140.      */
  141.     initSockets: function() {
  142.        
  143.         clearInterval(app['reconnectionInterval']);
  144.         clearInterval(app['pingInterval']);
  145.      
  146.         var path = 'ws://' + app['host'] + ':' + app['port'] + '/' + user.id;
  147.        
  148.         if(typeof(WebSocket) == undefined)
  149.             $('#sstatus').css('color', 'yellow').html('Please use a newer browser!');  
  150.         else
  151.             app['webSocket'] = new WebSocket(path);
  152.        
  153.         app['webSocket'].onopen = function() { 
  154.             $('#sstatus').css('color', 'green').html('Connected!');    
  155.             app['startedSocket'] = true;
  156.             app['fetchStatistics']();
  157.            
  158.             app['pingInterval'] = setInterval(function() {
  159.                 app['sendData']('event_pong', '', true, false);
  160.             }, 30000);
  161.  
  162.             console.log("[WEBSOCKET] Successfully established WEBSOCKET connection");  
  163.         };
  164.        
  165.         app['webSocket'].onclose = function () {
  166.             $('#sstatus').css('color', 'red').html('Disconnected!');                   
  167.              clearInterval(app['pingInterval']);
  168.              console.log('[WEBSOCKET] Disconnected from WEBSOCKET..');
  169.              app['startedSocket'] = false;
  170.              app['webSocket'].close();
  171.              
  172.              app['reconnectionInterval'] = setInterval(app['tryReconnect'], 2500); 
  173.              return;
  174.         }
  175.        
  176.         app['webSocket'].onerror = function(event) {       
  177.             console.log("[WEBSOCKET] Websocket error " + JSON.stringify(event));
  178.         };
  179.  
  180.         app['webSocket'].onmessage = function(event) {
  181.            
  182.            
  183.             if(app['debugMode'])
  184.             {
  185.                 console.log('received data:::\n\n');
  186.                 var ap = "---------------------------------------------\n";
  187.                 ap += "Data Type: " + ( (app['isJSONData'](event.data)) ? "json" : "split_text") + "\n";
  188.                 ap += "Data contents: " + ( (app['isJSONData'](event.data)) ? JSON.stringify(JSON.parse(event.data)) : event.data) + "\n";
  189.                 ap += "---------------------------------------------\n\n"
  190.                 console.log(ap);
  191.             }
  192.        
  193.             if(app['isJSONData'](event.data))
  194.             {          
  195.                 var jsonObj = JSON.parse(event.data);
  196.        
  197.                 switch(jsonObj.event){
  198.                     case "chatManager":
  199.                     if(user.chatMgr != null)
  200.                         user.chatMgr.handleData(jsonObj);
  201.                     break;
  202.                     default:
  203.                         return;
  204.                     break;
  205.                 }
  206.                 return;
  207.             }
  208.            
  209.            
  210.            
  211.             var eventData = event.data.split(':');
  212.             var eventName = jQuery.trim(eventData[0]);
  213.             var extraData = eventData[1];
  214.                
  215.             switch (eventName) {
  216.  
  217.                 case 'compose_jsalert': {
  218.                     alert(extraData);
  219.                     break;
  220.                 }
  221.                
  222.                 case 'compose_newonlinecount': {
  223.                    
  224.                     var count = eventData[1];
  225.                    
  226.                     $('#userson').fadeOut(500, function(){
  227.                         $('#userson').html(count).fadeIn(50);
  228.                     });
  229.                    
  230.                     break;
  231.                 }
  232.                
  233.                 case 'compose_arrowmovement': {
  234.                    
  235.                     var enable = eventData[1];
  236.                    
  237.                     if(enable == 'yes')
  238.                     {
  239.                         app['bindWalking']();
  240.                     }
  241.                     else
  242.                     {
  243.                         app['unbindWalking']();
  244.                     }
  245.                    
  246.                     break;
  247.                 }
  248.                
  249.                 case 'compose_ping': {
  250.                    
  251.                     console.log('[WEBSOCKET] Awaiting websocket data...');
  252.                     break;
  253.                 }
  254.                
  255.                 case 'compose_characterbar': {
  256.                     app['loadStatistics'](extraData);
  257.                     break;
  258.                 }
  259.                
  260.                 case 'compose_clear_characterbar': {
  261.                     app['loadStatistics'](extraData, true);
  262.                 }
  263.                
  264.                 case "compose_atm": {
  265.                    
  266.                     var Action = jQuery.trim(extraData);
  267.                     var UserData = (jQuery.trim(eventData[2])).split(',');
  268.                     var HasBank = (UserData[0] == "1" ? true : false);
  269.                     var ChequingsBalance = UserData[1];
  270.                     var SavingsBalance = UserData[2];                  
  271.        
  272.        
  273.                     switch(Action)
  274.                     {
  275.                         case "open":
  276.                             $('#ActivityOverlay').show();
  277.                             $('#AtmMachine').addClass('oAtmMachine').show();
  278.                             $('.c_amt').html(ChequingsBalance);
  279.                             $('.s_amt').html(SavingsBalance);                      
  280.                         break;
  281.                         case "error":
  282.                             var Error = eventData[2];
  283.                             $('#AtmCloseBtn').html('<div class="AtmError" style="position:absolute;top: -1028%;left: -222%;font-size: 10px;color: white;width: 300px;background: #aa7200;height: 10px;font-weight: bold;text-align: center;border: 2px solid #ffdd00;padding: 5px;line-height: 10px;border-radius: 2px;">' + Error + '</div>');
  284.                         break; 
  285.                         case "change_balance_1":
  286.                             var Amount = eventData[2];
  287.                             $('.c_amt').html(Amount);
  288.                         break;
  289.                         case "change_balance_2":
  290.                             var Amount = eventData[2];
  291.                             $('.s_amt').html(Amount);
  292.                         break;
  293.                         default:
  294.                             //alert(Action);
  295.                         break;
  296.                     }              
  297.                     break;
  298.                 }
  299.                    
  300.                 case "compose_htmlpage": {
  301.                
  302.                     var page = (event.data.split(',')[0]).split(':')[1];
  303.                     var action = (event.data.split(',')[1]).split(':')[1];
  304.                    
  305.                     $.ajax({
  306.                         type: "POST",
  307.                         url: "/resources-ajax/settings/" + page + ".php",
  308.                         cache: false,
  309.                         data: {
  310.                            
  311.                         }  
  312.                     }).done(function(data) {
  313.                         $('.PageElement').html(data);
  314.                     });            
  315.                     break;
  316.                 }
  317.                
  318.                 case "compose_timer": {
  319.                    
  320.                     var Timer = (event.data.split(',')[0]).split(':')[1];
  321.                     var Action = (event.data.split(',')[1]).split(':')[1];
  322.                     var Value = (event.data.split(',')[2]).split(':')[1];
  323.                    
  324.                     var DisplayName = Timer.split('-')[0] + " " + Timer.split('-')[1];
  325.                     var TimerDialogue = $('.' + Timer.split('-')[0].toLowerCase() + "" + Timer.split('-')[1].toLowerCase());
  326.                    
  327.                     switch(Action)
  328.                     {
  329.                         case "add":
  330.                        
  331.                             TimerDialogue.html(DisplayName + ': ' + Value).fadeIn();
  332.                            
  333.                         break;
  334.                        
  335.                         case "remove":
  336.                            
  337.                             TimerDialogue.fadeOut();
  338.                        
  339.                         break;
  340.                        
  341.                         case "decrement":
  342.                            
  343.                             TimerDialogue.html(DisplayName + ': ' + Value);
  344.                        
  345.                         break;
  346.                     }
  347.                    
  348.                     break;
  349.                 }      
  350.                
  351.                 case "compose_captchabox": {
  352.                    
  353.                     var captchaData = extraData;
  354.                     var captchaParts = captchaData.split(',');
  355.                    
  356.                     var Title = captchaParts[1];
  357.                     var GeneratedString = captchaParts[2];
  358.                    
  359.                                        
  360.                    
  361.                     app['CaptchaBox'].fadeIn();
  362.                     $('.captcha-box-information').fadeOut().html(Title).fadeIn();
  363.                     $('.captcha-box-generatedtxt').fadeOut().text(GeneratedString).fadeIn();
  364.                    
  365.                 }
  366.                
  367.                 case "compose_chat": {
  368.  
  369.                     user.chatMgr.handleData(event.data);
  370.                    
  371.                     break;
  372.                 }
  373.                
  374.                 default: {
  375.                     console.log('Unable to get event using the specified name: ' + JSON.stringify(event));
  376.                 break;
  377.                 }
  378.  
  379.             }
  380.         };
  381.      
  382.        
  383.    },
  384.    
  385.     unbindWalking: function() {
  386.         $(document).unbind('keydown');
  387.         $(document).unbind('keyup');
  388.     },
  389.    
  390.     bindWalking: function() {
  391.        
  392.        
  393.         $(document).keydown(function(e) {
  394.            
  395.             app['walking'] = true;
  396.             clearTimeout(app['walkingInterval']);
  397.            
  398.             var key = "";
  399.            
  400.             switch(e.which) {
  401.                 case 37:
  402.                     key = "Left";
  403.                 break;
  404.  
  405.                 case 38:
  406.                     key = "Up";
  407.                 break;
  408.  
  409.                 case 39:
  410.                     key = "Right";
  411.                 break;
  412.  
  413.                 case 40:
  414.                     key = "Down";
  415.                 break;
  416.  
  417.                 default: return;
  418.             }
  419.            
  420.             if(key == "")
  421.                 return;
  422.            
  423.            
  424.             app['sendData']('event_walk', key, false, false);
  425.             app['walking'] = true;
  426.            
  427.            
  428.         }).keyup(function(){
  429.            
  430.             app['walking'] = false;
  431.            
  432.             app['walkingInterval'] = setTimeout(function(){
  433.                
  434.                 if(app['walking'] == true)
  435.                     return;
  436.                
  437.                 app['sendData']('event_walk', "stop", false, false);
  438.                
  439.             }, 500);   
  440.         });
  441.        
  442.     },
  443.  
  444.     bindATM: function () {
  445.                
  446.         $('#AtmCloseBtn').on('click', function(){
  447.             $('#ActivityOverlay').hide();
  448.             $('#AtmMachine').hide();
  449.         });
  450.  
  451.         $('.deposit').on('click', function(){
  452.             $('.AtmHomeScreen').fadeOut(function(){
  453.                 $('.AtmDepositScreen').fadeIn();
  454.             });
  455.         });
  456.    
  457.         $('.withdraw').on('click', function(){
  458.             $('.AtmHomeScreen').fadeOut(function(){
  459.                 $('.AtmWithdrawScreen').fadeIn();
  460.             });
  461.         });
  462.        
  463.         $('.atmback').on('click', function(){
  464.             $('.AtmWithdrawScreen, .AtmDepositScreen').fadeOut(function(){
  465.                 $('.AtmHomeScreen').fadeIn();
  466.             });
  467.         });
  468.        
  469.         $('.deposit_submit').on('click', function(){
  470.             var depositAmount = parseInt($('input[class=deposit_amount]').val());
  471.             var account_type = $('select[class=deposit_acc]').val();           
  472.             var data = 'deposit,' + depositAmount + "," + account_type;
  473.            
  474.             app['sendData']('event_atm', data, false, false);
  475.            
  476.         });
  477.        
  478.         $('.withdraw_submit').on('click', function(){          
  479.             var withdrawAmount = parseInt($('input[class=withdraw_amount]').val());
  480.             var account_type = $('select[class=withdraw_acc]').val();
  481.             var data = 'withdraw,' + withdrawAmount + "," + account_type;
  482.            
  483.             app['sendData']('event_atm', data, false, false);
  484.            
  485.         });
  486.    
  487.     },
  488.    
  489.     bindCaptchaBox: function () {  
  490.        
  491.     function composeCaptchaError(error){
  492.         app['ErrorBox'].html(error).slideDown(500);
  493.        
  494.         app['InputBox'].animate({borderColor:'#fb6d6d !important',},1000);
  495.         app['CaptchaBox'].animate({borderColor:'#fb6d6d !important',},1000);
  496.         app['CaptchaBoxNotif'].animate({borderColor:'#fb6d6d !important',},1000);
  497.     }
  498.    
  499.     function composeCaptchaSuccess(){
  500.         app['ErrorBox'].slideUp(500);
  501.        
  502.         app['SuccessTick'].fadeIn(200);
  503.        
  504.         app['InputBox'].animate({borderColor:'#87bd83 !important',},1000);
  505.         app['CaptchaBox'].animate({borderColor:'#87bd83 !important',},1000);
  506.         app['CaptchaBoxNotif'].animate({borderColor:'#87bd83 !important',},1000);
  507.     }
  508.    
  509.     function resetCaptchaBox()
  510.     {
  511.         setTimeout(function(){ 
  512.             app['CaptchaBox'].fadeOut();       
  513.             app['ErrorBox'].slideUp(); 
  514.             app['SuccessTick'].fadeOut();
  515.             app['InputBox'].animate({borderColor:'#ffb91d !important',},1000);
  516.             app['CaptchaBox'].animate({borderColor:'rgba(174, 174, 174, 0.4) !important',},1000);
  517.             app['CaptchaBoxNotif'].animate({borderColor:'rgba(174, 174, 174, 0.4) !important',},1000);
  518.             app['SuccessTick'].fadeOut();  
  519.             app['InputBox'].val("");
  520.         },2000);       
  521.     }  
  522.         app['InputBox'].on('keydown', function(){  
  523.        
  524.             if( app['SuccessTick'].is(':visible') || !(app['CaptchaBox'].is(':visible')) )
  525.                 return;
  526.                
  527.             if(app['processCaptcha'] == null)
  528.             {
  529.                
  530.                 app['processCaptcha']  = setInterval(function(){
  531.  
  532.                     if(app['curSeconds'] < app['checkSeconds'])
  533.                     {
  534.                         app['curSeconds']++;
  535.                     }
  536.                     else
  537.                     {  
  538.                         var Title = $('.captcha-box-information').text();
  539.                        
  540.                         if( ( jQuery.trim($('.captcha-box-generatedtxt').text()).toLowerCase() != jQuery.trim($('.captcha-box-input').val()).toLowerCase() ) )
  541.                         {
  542.                             clearInterval(app['processCaptcha']);
  543.                             app['processCaptcha'] = null;
  544.                            
  545.                             app['sendData']('event_captcha', 'regenerate,' + Title, false, false);
  546.                             composeCaptchaError("Incorrect. Try again!");
  547.                            
  548.                             app['curSeconds'] = 0;
  549.                            
  550.                    
  551.                         }
  552.                         else
  553.                         {
  554.                            
  555.                             clearInterval(app['processCaptcha']);  
  556.                             app['processCaptcha'] = null;
  557.                            
  558.                             composeCaptchaSuccess();
  559.                             app['sendData']('event_captcha', 'complete,' + Title, false, false);
  560.                            
  561.                             resetCaptchaBox();                                                                 
  562.                             app['curSeconds'] = 0;
  563.                            
  564.                            
  565.                             return;
  566.                         }
  567.                     }
  568.                    
  569.                 },1000);
  570.             }
  571.            
  572.             app['curSeconds'] = 0;
  573.            
  574.         });
  575.            
  576.     },
  577.    
  578.     fetchStatistics: function(){
  579.        
  580.         app['sendData']('event_retrieveconnectingstatistics', '', true, false);
  581.        
  582.     },
  583.  
  584.     loadStatistics: function(usersdata, clear){
  585.        
  586.         clear = typeof clear === 'undefined' ? false : clear;
  587.        
  588.         if(!clear)
  589.         {
  590.             var DataParts = usersdata.split(',');
  591.            
  592.             var UserID = parseInt(DataParts[0]);
  593.             var UsersFigure = DataParts[1];
  594.             var CurHealth = parseInt(DataParts[2]);
  595.             var MaxHealth = parseInt(DataParts[3]);
  596.             var CurEnergy = parseInt(DataParts[4]);
  597.             var MaxEnergy = parseInt(DataParts[5]);
  598.             var CurXP = parseInt(DataParts[6]);
  599.             var NeedXP = parseInt(DataParts[7]);
  600.             var Level = parseInt(DataParts[8]);
  601.            
  602.             if(CurHealth > MaxHealth)
  603.                 CurHealth = MaxHealth;
  604.            
  605.             if(CurEnergy > MaxEnergy)
  606.                 CurEnergy = MaxEnergy;
  607.            
  608.             if(CurXP > NeedXP)
  609.                 CurXP = NeedXP;
  610.    
  611.             var calculatedHP = Math.ceil((((CurHealth / MaxHealth)) ) * 108);
  612.             var calculatedEnergy = Math.ceil((((CurEnergy / MaxEnergy)) ) * 108);
  613.             var calculatedXP = Math.ceil((((CurXP / NeedXP)) ) * 56);
  614.            
  615.             var figure = "https://www.habbo.com/habbo-imaging/avatarimage?figure=" + UsersFigure + "&gesture=srp&head_direction=3&headonly=1";
  616.            
  617.            
  618.             if(UserID == user.id)
  619.             {
  620.                 // THATS ME
  621.                
  622.                 // Health bar SETTER
  623.                 var HealthBar = $('.1').find('.HealthBar');
  624.                 var HealthValue = HealthBar.css('width');
  625.                
  626.                 // Energy Bar SETTER
  627.                 var EnergyBar = $('.1').find('.EnergyBar');
  628.                 var EnergyValue = EnergyBar.css('width');
  629.                
  630.                 // XP Bar SETTER
  631.                 var XPBar = $('.1').find('.XPBar');
  632.                 var XPValue = XPBar.css('width');
  633.                
  634.                 // Character Figure
  635.                 var CharacterDiv = $('.1').find('.Character');
  636.                 var CharacterValue = CharacterDiv.css('background');
  637.                 var NewCharacterValue = null;
  638.                
  639.                 if(NewCharacterValue == null)
  640.                 {
  641.                     CharacterDiv.css({background: 'url(' + figure + ')'});
  642.                     NewCharacterValue = figure;
  643.                 }
  644.                
  645.                 if(NewCharacterValue != figure)
  646.                 {
  647.                     $('.1').fadeOut();
  648.                     CharacterDiv.css({background: 'url(' + figure + ')'});
  649.                     NewCharacterValue = figure;
  650.                 }
  651.                
  652.                 // Update Health Bar Width
  653.                 if((calculatedHP + 'px') != HealthValue)
  654.                 HealthBar.stop().animate({queue: false, width: calculatedHP + 'px'});
  655.                
  656.                 // Update Energy Bar Width
  657.                 if((calculatedEnergy + 'px') != EnergyValue)
  658.                 EnergyBar.stop().animate({queue: false, width: calculatedEnergy + 'px'});
  659.            
  660.                 // Update XP Bar Width
  661.                 if((calculatedXP + 'px') != XPValue)
  662.                 XPBar.stop().animate({queue: false, width: calculatedXP + 'px'});
  663.            
  664.            
  665.                 $('.1').find('.LevelIndicator').html("" + Level);
  666.                
  667.                 // Fade in stats bar if not present
  668.                 $('.1').fadeIn();
  669.             }
  670.             else
  671.             {
  672.                 var HealthBar = $('.2').find('.HealthBar');
  673.                 var HealthValue = HealthBar.css('width');
  674.                
  675.                 var EnergyBar = $('.2').find('.EnergyBar');
  676.                 var EnergyValue = EnergyBar.css('width');
  677.                
  678.                 var CharacterDiv = $('.2').find('.Character');
  679.                 var CharacterValue = CharacterDiv.css('background');
  680.                
  681.                
  682.                 if(jQuery.trim(CharacterValue.toLowerCase()).indexOf(figure) <= -1)
  683.                 {
  684.                     $('.2').fadeOut(200,function(){
  685.                         CharacterDiv.css({background: 'url(' + figure + ')'});
  686.                         NewCharacterValue = figure;
  687.                     });
  688.                 }
  689.                
  690.                 if((calculatedHP + 'px') != HealthValue)
  691.                 HealthBar.animate({width: calculatedHP + 'px'});
  692.                
  693.                 if((calculatedEnergy + 'px') != EnergyValue)
  694.                 EnergyBar.animate({width: calculatedEnergy + 'px'});
  695.                
  696.                 $('.2').find('.LevelIndicator').html("" + Level).fadeIn();
  697.                
  698.                 $('.2').fadeIn();
  699.                
  700.             }
  701.         }
  702.         else
  703.         {
  704.             $('.2').fadeOut();
  705.         }
  706.     },
  707.      
  708. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement