- (()=>{
- const _g = window._g;
- const parseInput = window.parseInput;
- const lastHeroCords = {
- x: window.hero.x,
- y: window.hero.y
- };
- let heroId = parseInt(window.getCookie("mchar_id"));
- let logOut = false;
- let isInitPending = false;
- const init = initLevel => new Promise(resolve => {
- let successDataOld = window.successData;
- window.successData = function(t) {
- window.successData = successDataOld;
- resolve(JSON.parse(t))
- };
- let msg = "t=init&initlvl=" + initLevel + "&clientTs=" + Date.now() / 1E3 + "&mucka=" + Math.random() + (initLevel > 1 ? "&browser_token=" + g.browser_token : "");
- window.webSocketSendData(msg, "")
- });
- const getObjectToDelete = object => {
- const newObj = {};
- Object.keys(object).forEach(id => {
- newObj[id] = {
- del: true
- }
- });
- return newObj
- };
- const itemsToDelete = (bool = false) => {
- const newObj = {};
- Object.entries(window.g.item).forEach(([id, item]) => {
- if (item.loc !==
- "g" || bool) newObj[id] = {
- del: true
- }
- });
- return newObj
- };
- const clearChat = () => {
- document.querySelector("#chattxt").innerHTML = "";
- Object.assign(window.g.chat, {
- txt: ["", "", "", ""],
- tabs: [
- [],
- [],
- [],
- []
- ],
- ts: 0
- })
- };
- const clearGameElementsBefore = () => {
- const elementsToRemove = {
- item: itemsToDelete(),
- other: getObjectToDelete(window.g.other)
- };
- window.newNpc(getObjectToDelete(window.g.npc));
- parseInput(elementsToRemove);
- window.g.gw = {};
- window.map.water.length = 0;
- window.g.agressiveNpc = [];
- document.querySelectorAll(".gw, .rip").forEach(element =>
- element.remove());
- document.querySelector("#ground").style.background = "#6E9C9B";
- document.querySelector("#bground").style.background = "#6E9C9B";
- const weather = document.querySelector(".weatherwrapper");
- if (weather !== null) weather.remove()
- };
- const reInit = async () => {
- if (isInitPending === true) return;
- isInitPending = true;
- let deleteItems = false;
- window.g.gameLoader = new window.GameLoader;
- clearGameElementsBefore();
- for (let i = 1; i <= 4; i++) {
- window.g.ats = Date.now();
- let data = await init(i);
- if (data.c) clearChat();
- if (data.h && data.h.id) {
- deleteItems =
- data.h.id === heroId;
- if (!deleteItems) {
- heroId = data.h.id;
- parseInput({
- item: itemsToDelete(true)
- })
- }
- }
- if (data.h && data.h.x && data.h.y) Object.assign(lastHeroCords, {
- x: data.h.x,
- y: data.h.y
- });
- if (deleteItems === true && data.item) {
- Object.entries(data.item).forEach(([id, item]) => {
- if (item.loc === "g") delete data.item[id]
- });
- deleteItems = false
- }
- window.successData(JSON.stringify(data));
- if (i === 2) window.map.center(window.hero.x * 32, window.hero.y * 32 - 16)
- }
- heroOnGtwAfterInit = true;
- isInitPending = false
- };
- const checkHeroCoords = (hx, hy) => {
- const {
- x,
- y
- } = lastHeroCords;
- if (x === hx && y === hy) return false;
- Object.assign(lastHeroCords, {
- x: hx,
- y: hy
- });
- return true
- };
- const checkHeroMls = ({
- x,
- y,
- ml
- }) => {
- const lastMl = ml.slice(-1)[0];
- if (lastMl === undefined) return false;
- const [hx, hy] = lastMl.split(",");
- return parseInt(hx) === x && parseInt(hy) === y
- };
- const issetGatewayAtPosition = (x, y) => {
- return Object.keys(window.g.gw).some(gateway => {
- const [gx, gy] = gateway.split(".");
- return parseInt(gx) === x && parseInt(gy) === y
- })
- };
- window._g = (...args) => {
- if (isInitPending) return;
- return _g(...args)
- };
- window.parseInput = (data, ...args) => {
- if (data.t === "reload" && !logOut) {
- delete data.t;
- delete window.g.browser_token;
- reInit()
- }
- if (data.logoff_time_left) logOut = data.logoff_time_left === 5;
- if (isInitPending && window.g.ats === 0) window.g.ats = Date.now();
- return parseInput(data, ...args)
- };
- window.hero = new Proxy(window.hero, {
- set(object, property, ...args) {
- const ret = Reflect.set(object, property, ...args);
- if (["x", "y"].includes(property))
- if (checkHeroCoords(object.x, object.y) && issetGatewayAtPosition(object.x, object.y))
- if (window.road.length ===
- 0 && checkHeroMls(object)) window._g("walk");
- return ret
- }
- })
- })();