Advertisement
Guest User

c.js - Diep.io - Uncompressed

a guest
Feb 9th, 2019
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This layer feeds input into the game
  2. (function(){
  3.  
  4. var scale = window.localStorage['no_retina'] ? 1.0 : window.devicePixelRatio;
  5. var canvas = document.getElementById('canvas');
  6. var typing = false;
  7.  
  8. function preventDefault(e){
  9.     if(e.preventDefault) e.preventDefault();
  10. }
  11.  
  12. function onMouseWheel(e){
  13.     e = e || window.event;
  14.    
  15.     preventDefault(e);
  16.    
  17.     if(!window['input']) return;
  18.     window['input']['wheel'](e.wheelDelta / -120 || e.detail || 0);
  19. }
  20.  
  21. function flushInputHooks(){
  22.     if(window["input"] && window["input"]["flushInputHooks"]) window["input"]["flushInputHooks"]();
  23. }
  24.  
  25. if(/firefox/i.test(navigator.userAgent)){
  26.     document.addEventListener("DOMMouseScroll", onMouseWheel, false);
  27. }else{
  28.     document.body['onmousewheel'] = onMouseWheel;
  29. }
  30.  
  31. canvas.onmousemove = function(e){
  32.     e = e || window.event;
  33.    
  34.     if(!window['input']) return;
  35.     window['input']['mouse'](e.clientX * scale, e.clientY * scale);
  36. }
  37.  
  38. canvas.onmousedown = function(e) {
  39.     flushInputHooks();
  40.    
  41.     e = e || window.event;
  42.    
  43.     if(!window['input']) return;
  44.    
  45.     preventDefault(e);
  46.    
  47.     if(!e.which && e.button !== undefined ) {
  48.         e.which = (e.button & 1 ? 1 : (e.button & 2 ? 3 : (e.button & 4 ? 2 : 0)));
  49.     }
  50.    
  51.     if(e.which >= 1 && e.which <= 3) window['input']['keyDown'](e.which);
  52. }
  53.  
  54. canvas.onmouseup = function(e) {
  55.     flushInputHooks();
  56.    
  57.     e = e || window.event;
  58.    
  59.     if(!window['input']) return;
  60.    
  61.     preventDefault(e);
  62.    
  63.     if(!e.which && e.button !== undefined ) {
  64.         e.which = (e.button & 1 ? 1 : (e.button & 2 ? 3 : (e.button & 4 ? 2 : 0)));
  65.     }
  66.    
  67.     if(e.which >= 1 && e.which <= 3) window['input']['keyUp'](e.which);
  68. }
  69.  
  70. window.onkeydown = function(e){
  71.     flushInputHooks();
  72.    
  73.     e = e || window.event;
  74.    
  75.     if(!window['input']) return;
  76.     if(e.keyCode >= 112 && e.keyCode <= 130 && e.keyCode != 113) return; // F2- keys, don't prevent
  77.     window['input']['keyDown'](e.keyCode);
  78.    
  79.     if(e.keyCode == 9) preventDefault(e);
  80.     if(!typing && !e.ctrlKey && !e.metaKey) preventDefault(e);
  81. }
  82.  
  83. window.onkeyup = function(e){
  84.     flushInputHooks();
  85.    
  86.     e = e || window.event;
  87.    
  88.     if(!window['input']) return;
  89.     if(e.keyCode >= 112 && e.keyCode <= 130 && e.keyCode != 113) return; // F2- keys, don't prevent
  90.     window['input']['keyUp'](e.keyCode);
  91.    
  92.     if(e.keyCode == 9) preventDefault(e);
  93.     if(!typing && !e.ctrlKey && !e.metaKey) preventDefault(e);
  94. }
  95.  
  96. canvas.onclick = window.onclick = function(e){
  97.     flushInputHooks();
  98. }
  99.  
  100. canvas.ondragstart = function(e){
  101.     e = e || window.event;
  102.    
  103.     preventDefault(e);
  104. }
  105.  
  106. window.onblur = function(e){
  107.     e = e || window.event;
  108.    
  109.     if(!window['input']) return;
  110.    
  111.     window['input'].blur();
  112. }
  113.  
  114. canvas.oncontextmenu = function(e){
  115.     e = e || window.event;
  116.    
  117.     if(!window['input'] || !window['input']['prevent_right_click'] || window['input']['prevent_right_click']()) preventDefault(e);
  118. }
  119.  
  120. window['setLoadingStatus'] = function(str){
  121.     document.getElementById('loading').innerText = str;
  122. }
  123.  
  124. window['setTyping'] = function(v){ typing = v; }
  125. window['unscale'] = function(v){ return v / scale; }
  126.  
  127. function onResize(){
  128.     canvas.width = window.innerWidth * scale;
  129.     canvas.height = window.innerHeight * scale;
  130. }
  131.  
  132. window.onerror = function(message, source, lineno, colno, error){
  133.     window.onerror = null;
  134.    
  135.     if(error) error = error.toString();
  136.    
  137.     // Don't log errors for outdated browsers
  138.     if(typeof Uint8Array == 'undefined') return;
  139.     if(typeof WebSocket == 'undefined') return;
  140.     if(typeof CanvasRenderingContext2D == 'undefined') return;
  141.     if(!document.createElement('canvas').getContext('2d')) return;
  142.    
  143.     // Or for sources we don't care about
  144.     if(source != null && source.indexOf("//diep.io") == -1) return;
  145.     if(source != null && source.indexOf(".js") == -1) return;
  146.    
  147.     if(source != null){
  148.         setTimeout(function(){
  149.             alert("The game crashed, refreshing page to recover from error");
  150.             window.onbeforeunload = null;
  151.             window.location.reload(true);
  152.         }, 1000);
  153.     }
  154.    
  155.     // Or weird errors in general
  156.     if(error == null && lineno == 0 && colno == 0) return;
  157.     if(error != null){
  158.         if(error.indexOf("renoTransGloRef") != -1) return;
  159.     }
  160.    
  161.     var e = JSON.stringify({
  162.         message: message,
  163.         source: source,
  164.         lineno: lineno,
  165.         colno: colno,
  166.         error: error
  167.     });
  168.     console.log("Uncaught error: " + e);
  169.     var xhr = new XMLHttpRequest();
  170.     xhr.open("POST", "//lb.diep.io/v1/error", true);
  171.     xhr.send(e);
  172.    
  173. }
  174.  
  175.  
  176. window.onbeforeunload = function(){
  177.     if(window["input"] && window["input"]["should_prevent_unload"] && window["input"]["should_prevent_unload"]()){
  178.         return "Are you sure you wanna quit?";
  179.     }
  180. }
  181.  
  182. window.addEventListener("gamepadconnected", function(e){
  183.     console.log("Gamepad connected: " + e.id);
  184. });
  185.  
  186. window.onresize = onResize;
  187.  
  188. onResize();
  189.  
  190. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement