2021-02-03 11:33:01 -06:00
|
|
|
import axios from 'axios';
|
|
|
|
|
2021-02-25 07:02:08 -06:00
|
|
|
import Logger from 'core/logger';
|
|
|
|
|
2021-02-03 11:33:01 -06:00
|
|
|
import Layer from './layer.js';
|
|
|
|
|
|
|
|
class Layers {
|
|
|
|
constructor() {
|
2023-04-14 20:56:15 -05:00
|
|
|
this._layers = new Map();
|
2021-02-03 11:33:01 -06:00
|
|
|
|
|
|
|
this.pulled = false;
|
|
|
|
}
|
2023-04-26 06:08:14 -05:00
|
|
|
|
|
|
|
get layers() {
|
|
|
|
return [...this._layers.values()];
|
2023-04-14 20:56:15 -05:00
|
|
|
}
|
2021-02-03 11:33:01 -06:00
|
|
|
|
|
|
|
async pull(force = false) {
|
2021-02-25 07:02:08 -06:00
|
|
|
if (this.pulled && !force) {
|
2021-02-25 14:50:18 -06:00
|
|
|
Logger.verbose('Layers', 2, 'Already pulled layers.');
|
2023-04-14 20:56:15 -05:00
|
|
|
return this.layers;
|
2021-02-25 07:02:08 -06:00
|
|
|
}
|
2021-02-25 14:50:18 -06:00
|
|
|
if (force) Logger.verbose('Layers', 1, 'Forcing update to layer information...');
|
2021-02-03 12:41:35 -06:00
|
|
|
|
2023-04-14 20:56:15 -05:00
|
|
|
this._layers = new Map();
|
2021-02-03 11:33:01 -06:00
|
|
|
|
2021-03-04 16:36:57 -06:00
|
|
|
Logger.verbose('Layers', 1, 'Pulling layers...');
|
2023-04-26 06:08:14 -05:00
|
|
|
const response = await axios.post(
|
|
|
|
// Change get to post for mod support
|
|
|
|
'http://hub.afocommunity.com/api/layers.json',
|
|
|
|
[0, 2891780963, 1959152751, 2428425228]
|
2021-03-04 16:36:57 -06:00
|
|
|
);
|
2023-04-26 06:08:14 -05:00
|
|
|
|
|
|
|
// const response = await axios.get(
|
|
|
|
// 'https://raw.githubusercontent.com/Squad-Wiki/squad-wiki-pipeline-map-data/master/completed_output/_Current%20Version/finished.json'
|
|
|
|
// );
|
2021-02-25 11:37:20 -06:00
|
|
|
|
2021-02-25 14:50:18 -06:00
|
|
|
for (const layer of response.data.Maps) {
|
2023-04-14 20:56:15 -05:00
|
|
|
const newLayer = new Layer(layer);
|
|
|
|
this._layers.set(newLayer.layerid, newLayer);
|
2021-02-25 14:50:18 -06:00
|
|
|
}
|
2021-02-25 11:37:20 -06:00
|
|
|
|
2021-02-25 14:50:18 -06:00
|
|
|
Logger.verbose('Layers', 1, `Pulled ${this.layers.length} layers.`);
|
2021-02-25 11:37:20 -06:00
|
|
|
|
2021-02-25 14:50:18 -06:00
|
|
|
this.pulled = true;
|
2021-02-25 07:02:08 -06:00
|
|
|
|
2021-02-03 11:33:01 -06:00
|
|
|
return this.layers;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getLayerByCondition(condition) {
|
|
|
|
await this.pull();
|
|
|
|
|
|
|
|
const matches = this.layers.filter(condition);
|
2023-03-06 14:17:21 -06:00
|
|
|
if (matches.length >= 1) return matches[0];
|
2021-02-03 11:33:01 -06:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2023-04-26 06:08:14 -05:00
|
|
|
|
|
|
|
async getLayerById(layerId) {
|
2023-04-14 20:56:15 -05:00
|
|
|
await this.pull();
|
|
|
|
return this._layers.get(layerId) ?? null;
|
|
|
|
}
|
2021-02-03 11:33:01 -06:00
|
|
|
|
|
|
|
getLayerByName(name) {
|
|
|
|
return this.getLayerByCondition((layer) => layer.name === name);
|
|
|
|
}
|
|
|
|
|
|
|
|
getLayerByClassname(classname) {
|
|
|
|
return this.getLayerByCondition((layer) => layer.classname === classname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Layers();
|