Facebook
From Ivory Sloth, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 712
  1. (()=>{
  2.     const _g = window._g;
  3.     const parseInput = window.parseInput;
  4.     const lastHeroCords = {
  5.         x: window.hero.x,
  6.         y: window.hero.y
  7.     };
  8.     let heroId = parseInt(window.getCookie("mchar_id"));
  9.     let logOut = false;
  10.     let isInitPending = false;
  11.     const init = initLevel => new Promise(resolve => {
  12.         let successDataOld = window.successData;
  13.         window.successData = function(t) {
  14.             window.successData = successDataOld;
  15.             resolve(JSON.parse(t))
  16.         };
  17.         let msg = "t=init&initlvl=" + initLevel + "&clientTs=" + Date.now() / 1E3 + "&mucka=" + Math.random() + (initLevel > 1 ? "&browser_token=" + g.browser_token : "");
  18.         window.webSocketSendData(msg, "")
  19.     });
  20.     const getObjectToDelete = object => {
  21.         const newObj = {};
  22.         Object.keys(object).forEach(id => {
  23.             newObj[id] = {
  24.                 del: true
  25.             }
  26.         });
  27.         return newObj
  28.     };
  29.     const itemsToDelete = (bool = false) => {
  30.         const newObj = {};
  31.         Object.entries(window.g.item).forEach(([id, item]) => {
  32.             if (item.loc !==
  33.                 "g" || bool) newObj[id] = {
  34.                 del: true
  35.             }
  36.         });
  37.         return newObj
  38.     };
  39.     const clearChat = () => {
  40.         document.querySelector("#chattxt").innerHTML = "";
  41.         Object.assign(window.g.chat, {
  42.             txt: ["", "", "", ""],
  43.             tabs: [
  44.                 [],
  45.                 [],
  46.                 [],
  47.                 []
  48.             ],
  49.             ts: 0
  50.         })
  51.     };
  52.     const clearGameElementsBefore = () => {
  53.         const elementsToRemove = {
  54.             item: itemsToDelete(),
  55.             other: getObjectToDelete(window.g.other)
  56.         };
  57.         window.newNpc(getObjectToDelete(window.g.npc));
  58.         parseInput(elementsToRemove);
  59.         window.g.gw = {};
  60.         window.map.water.length = 0;
  61.         window.g.agressiveNpc = [];
  62.         document.querySelectorAll(".gw, .rip").forEach(element =>
  63.             element.remove());
  64.         document.querySelector("#ground").style.background = "#6E9C9B";
  65.         document.querySelector("#bground").style.background = "#6E9C9B";
  66.         const weather = document.querySelector(".weatherwrapper");
  67.         if (weather !== null) weather.remove()
  68.     };
  69.     const reInit = async () => {
  70.         if (isInitPending === true) return;
  71.         isInitPending = true;
  72.         let deleteItems = false;
  73.         window.g.gameLoader = new window.GameLoader;
  74.         clearGameElementsBefore();
  75.         for (let i = 1; i <= 4; i++) {
  76.             window.g.ats = Date.now();
  77.             let data = await init(i);
  78.             if (data.c) clearChat();
  79.             if (data.h && data.h.id) {
  80.                 deleteItems =
  81.                     data.h.id === heroId;
  82.                 if (!deleteItems) {
  83.                     heroId = data.h.id;
  84.                     parseInput({
  85.                         item: itemsToDelete(true)
  86.                     })
  87.                 }
  88.             }
  89.             if (data.h && data.h.x && data.h.y) Object.assign(lastHeroCords, {
  90.                 x: data.h.x,
  91.                 y: data.h.y
  92.             });
  93.             if (deleteItems === true && data.item) {
  94.                 Object.entries(data.item).forEach(([id, item]) => {
  95.                     if (item.loc === "g") delete data.item[id]
  96.                 });
  97.                 deleteItems = false
  98.             }
  99.             window.successData(JSON.stringify(data));
  100.             if (i === 2) window.map.center(window.hero.x * 32, window.hero.y * 32 - 16)
  101.         }
  102.         heroOnGtwAfterInit = true;
  103.         isInitPending = false
  104.     };
  105.     const checkHeroCoords = (hx, hy) => {
  106.         const {
  107.             x,
  108.             y
  109.         } = lastHeroCords;
  110.         if (x === hx && y === hy) return false;
  111.         Object.assign(lastHeroCords, {
  112.             x: hx,
  113.             y: hy
  114.         });
  115.         return true
  116.     };
  117.     const checkHeroMls = ({
  118.         x,
  119.         y,
  120.         ml
  121.     }) => {
  122.         const lastMl = ml.slice(-1)[0];
  123.         if (lastMl === undefined) return false;
  124.         const [hx, hy] = lastMl.split(",");
  125.         return parseInt(hx) === x && parseInt(hy) === y
  126.     };
  127.     const issetGatewayAtPosition = (x, y) => {
  128.         return Object.keys(window.g.gw).some(gateway => {
  129.             const [gx, gy] = gateway.split(".");
  130.             return parseInt(gx) === x && parseInt(gy) === y
  131.         })
  132.     };
  133.     window._g = (...args) => {
  134.         if (isInitPending) return;
  135.         return _g(...args)
  136.     };
  137.     window.parseInput = (data, ...args) => {
  138.         if (data.t === "reload" && !logOut) {
  139.             delete data.t;
  140.             delete window.g.browser_token;
  141.             reInit()
  142.         }
  143.         if (data.logoff_time_left) logOut = data.logoff_time_left === 5;
  144.         if (isInitPending && window.g.ats === 0) window.g.ats = Date.now();
  145.         return parseInput(data, ...args)
  146.     };
  147.     window.hero = new Proxy(window.hero, {
  148.         set(object, property, ...args) {
  149.             const ret = Reflect.set(object, property, ...args);
  150.             if (["x", "y"].includes(property))
  151.                 if (checkHeroCoords(object.x, object.y) && issetGatewayAtPosition(object.x, object.y))
  152.                     if (window.road.length ===
  153.                         0 && checkHeroMls(object)) window._g("walk");
  154.             return ret
  155.         }
  156.     })
  157. })();