Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
7.5 kB
4
Indexable
Never
javascript:
// sempaaj 09.02.2021
// troche kodu zapozyczone ze skryptu: Modyfikacja mapy - niestandardowe granice
// autor: grabie2

var hours = 12;

var villagesURL = '/map/village.txt';
var conquerURL = '/map/conquer.txt';
var villages = [];
var data = [];
var newdata = [];

var FinalText = "";

var req = new XMLHttpRequest();
req.open('GET', villagesURL, true);
req.onreadystatechange = function (e) {
    if (req.readyState == 4 && req.status == 200 && processConquers)
        processConquers(req.responseText);
    else
        console.log("Błąd podczas ładowania strony...\n");

};
req.send(null);

function processConquers(dataFromReq) {
    villages = dataFromReq.split('\n');

    for (var i = 0; i < villages.length - 1; i++) {
        villages[i] = villages[i].split(',');
    }

    var req = new XMLHttpRequest();
    req.open('GET', conquerURL, true);
    req.onreadystatechange = function (e) {
        if (req.readyState == 4 && req.status == 200 && callback)
            callback(req.responseText);
        else
            console.log("Błąd podczas ładowania strony...\n");

    };
    req.send(null);
}

function callback(dataFromReq) {
    data = dataFromReq.split('\n');

    for (var i = 0; i < data.length - 1; i++) {
        data[i] = data[i].split(',');
        if (Math.floor(Date.now() / 1000) - data[i][1] < hours * 60 * 60) {

            for (var j = 0; j < villages.length - 1; j++) {
                if (villages[j][0] == data[i][0])
                    var temp = [villages[j][2], villages[j][3]];
            }

            FinalText += temp[0] + "|" + temp[1] + " ";
            newdata.push(data[i]);
        }
    }
    test(FinalText);
}

function test(villages) {
    if (typeof TWMap !== 'object') {
        UI.InfoMessage('Skrypt działa tylko na ekranach na których widoczna jest mapa', 5000, 'error');
        return;
    }

    if (!window.gbOriginalSpawnSector)
        window.gbOriginalSpawnSector = TWMap.mapHandler.spawnSector;

    if (!window.gbOriginalMinimapLoadSector)
        window.gbOriginalMinimapLoadSector = TWMap.minimapHandler.loadSector;

    const originalSpawnSector = TWMap.mapHandler.spawnSector;
    const originalMinimapLoadSector = TWMap.minimapHandler.loadSector;

    villages = villages.split(" ");
    villages.pop();
    for (let i = 0; i < villages.length; i++) {
        villages[i] = villages[i].split("|");
        villages[i][0] = parseInt(villages[i][0]);
        villages[i][1] = parseInt(villages[i][1]);
    }

    TWMap.minimapHandler.loadSector = function (a) {
        originalMinimapLoadSector.apply(TWMap.minimapHandler, [a]);

        const map = a._map;
        const canvas = document.createElement("canvas");

        canvas.width = map.scale[0] * map.sectorSize;
        canvas.height = map.scale[1] * map.sectorSize;
        canvas.style.position = "absolute";
        canvas.style.zIndex = "10";

        a.appendElement(canvas, 0, 0);

        const context = canvas.getContext('2d');
        if (context) {
            const sector = {
                sx: a.x,
                ex: a.x + map.sectorSize,
                sy: a.y,
                ey: a.y + map.sectorSize,
            };

            context.strokeStyle = "white";
            context.lineWidth = 3;
            context.lineCap = "square";

            for (let i = 0; i < villages.length; i++) {
                context.beginPath();
                context.moveTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);

                context.lineTo((villages[i][0] + 1 - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] + 1 - sector.sx) * map.scale[0], (villages[i][1] + 1 - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] + 1 - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);
                context.stroke();
            }
        }
    }

    TWMap.mapHandler.spawnSector = function (e, a) {
        originalSpawnSector.apply(TWMap.mapHandler, [e, a]);

        const find_canvas = (a) => {
            let canvas = a._elements ? a._elements.find(e => e instanceof HTMLCanvasElement && (e.id || '').startsWith('map_canvas')) : null;
            if (!canvas) {
                const cavas_id = "map_canvas_" + a.x + "_" + a.y;
                canvas = document.getElementById(cavas_id);
            }
            if (!canvas) {
                a.spawn();
                const cavas_id = "map_canvas_" + a.x + "_" + a.y;
                canvas = document.getElementById(cavas_id);
            }
            return canvas;
        }

        let canvas = find_canvas(a);
        if (!canvas) {
            MapCanvas.createCanvas(a, e);
            canvas = find_canvas(a);
        }

        if (!canvas) {
            console.log("Cannot find canvas...");
            return;
        }

        const context = canvas.getContext('2d');
        const map = a._map;
        if (context) {
            const sectorBorderLines = [{
                    sx: a.x,
                    sy: a.y,
                    ex: a.x + map.sectorSize,
                    ey: a.y
                }, {
                    sx: a.x + map.sectorSize,
                    sy: a.y,
                    ex: a.x + map.sectorSize,
                    ey: a.y + map.sectorSize
                }, {
                    sx: a.x + map.sectorSize,
                    sy: a.y + map.sectorSize,
                    ex: a.x,
                    ey: a.x + map.sectorSize
                }, {
                    sx: a.x,
                    sy: a.x + map.sectorSize,
                    ex: a.x,
                    ey: a.y
                },
            ];
            const sector = {
                sx: a.x,
                ex: a.x + map.sectorSize,
                sy: a.y,
                ey: a.y + map.sectorSize,
            };

            context.strokeStyle = "white";
            context.lineWidth = 5;
            context.lineCap = "square";

            for (let i = 0; i < villages.length; i++) {
                context.beginPath();
                context.moveTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);

                context.lineTo((villages[i][0] + 1 - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] + 1 - sector.sx) * map.scale[0], (villages[i][1] + 1 - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] + 1 - sector.sy) * map.scale[1]);
                context.stroke();
                context.lineTo((villages[i][0] - sector.sx) * map.scale[0], (villages[i][1] - sector.sy) * map.scale[1]);
                context.stroke();
            }

        } else {
            console.log('Cannot create context!');
        }
    }
    TWMap.reload();
}