SquadJS/squad-server/layers/layers.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

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() {
this.layers = [];
this.pulled = false;
}
async pull(force = false) {
2021-02-25 07:02:08 -06:00
if (this.pulled && !force) {
Logger.verbose('Layers', 2, 'Already pulled layers.');
2021-02-25 07:02:08 -06:00
return;
}
if (force) Logger.verbose('Layers', 1, 'Forcing update to layer information...');
2021-02-25 07:02:08 -06:00
this.layers = [];
2021-02-03 11:33:01 -06:00
2021-03-04 16:36:57 -06:00
Logger.verbose('Layers', 1, 'Pulling layers...');
2023-03-02 01:21:25 -06:00
const response = await axios.post( // Change get to post for mod support
'http://hub.afocommunity.com/api/layers.json', [0, 1959152751]
2021-03-04 16:36:57 -06:00
);
for (const layer of response.data.Maps) {
this.layers.push(new Layer(layer));
}
Logger.verbose('Layers', 1, `Pulled ${this.layers.length} layers.`);
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;
}
getLayerByName(name) {
return this.getLayerByCondition((layer) => layer.name === name);
}
getLayerByClassname(classname) {
return this.getLayerByCondition((layer) => layer.classname === classname);
}
}
export default new Layers();