Add a personal, configurable quest

This commit is contained in:
Pieter Vander Vennet 2020-07-31 16:17:16 +02:00
parent 9c42839f01
commit 7ec00a3301
21 changed files with 376 additions and 237 deletions

View file

@ -7,9 +7,8 @@ import {Basemap} from "./Leaflet/Basemap";
import {State} from "../State";
export class LayerUpdater {
private _layers: FilteredLayer[];
private widenFactor: number;
public readonly sufficentlyZoomed: UIEventSource<boolean> = new UIEventSource<boolean>(false);
public readonly runningQuery: UIEventSource<boolean> = new UIEventSource<boolean>(false);
public readonly retries: UIEventSource<number> = new UIEventSource<number>(0);
/**
@ -17,8 +16,6 @@ export class LayerUpdater {
*/
private previousBounds: Bounds;
private _overpass: Overpass;
private _minzoom: number;
/**
* The most important layer should go first, as that one gets first pick for the questions
@ -26,28 +23,39 @@ export class LayerUpdater {
* @param minzoom
* @param layers
*/
constructor(minzoom: number,
widenFactor: number,
layers: FilteredLayer[]) {
this.widenFactor = widenFactor;
this._layers = layers;
this._minzoom = minzoom;
var filters: TagsFilter[] = [];
for (const layer of layers) {
filters.push(layer.filters);
}
this._overpass = new Overpass(new Or(filters));
constructor(state: State) {
const self = this;
State.state.locationControl.addCallback(function () {
self.update();
state.locationControl.addCallback(() => {
self.update(state)
});
self.update();
state.layoutToUse.addCallback(() => {
self.update(state)
});
self.update(state);
}
private GetFilter(state: State) {
var filters: TagsFilter[] = [];
state = state ?? State.state;
for (const layer of state.layoutToUse.data.layers) {
if (state.locationControl.data.zoom < layer.minzoom) {
return undefined;
}
filters.push(layer.overpassFilter);
}
if (filters.length === 0) {
return undefined;
}
return new Or(filters);
}
private handleData(geojson: any) {
const self = this;
self.retries.setData(0);
function renderLayers(layers: FilteredLayer[]) {
if (layers.length === 0) {
self.runningQuery.setData(false);
@ -65,69 +73,79 @@ export class LayerUpdater {
renderLayers(rest);
}, 50)
}
renderLayers(this._layers);
renderLayers(State.state.filteredLayers.data);
}
private handleFail(reason: any) {
private handleFail(state: State, reason: any) {
this.retries.data++;
this.ForceRefresh();
console.log(`QUERY FAILED (retrying in ${5 * this.retries.data} sec)`, reason);
this.previousBounds = undefined;
this.retries.data ++;
this.retries.ping();
const self = this;
window?.setTimeout(
function(){self.update()}, this.retries.data * 5000
function () {
self.update(state)
}, this.retries.data * 5000
)
}
private update(): void {
if (this.IsInBounds()) {
private update(state: State): void {
if (this.IsInBounds(state)) {
return;
}
console.log("Zoom level: ",State.state.bm.map.getZoom(), "Least needed zoom:", this._minzoom)
if (State.state.bm.map.getZoom() < this._minzoom || State.state.bm.Location.data.zoom < this._minzoom) {
const filter = this.GetFilter(state);
this.sufficentlyZoomed.setData(filter !== undefined);
if (filter === undefined) {
console.log("Zoom insufficient to run query")
return;
}
if (this.runningQuery.data) {
console.log("Still running a query, skip");
return;
}
const bounds = State.state.bm.map.getBounds();
const diff = this.widenFactor;
const bounds = state.bm.map.getBounds();
const diff = state.layoutToUse.data.widenFactor;
const n = bounds.getNorth() + diff;
const e = bounds.getEast() + diff;
const e = bounds.getEast() + diff;
const s = bounds.getSouth() - diff;
const w = bounds.getWest() - diff;
const w = bounds.getWest() - diff;
this.previousBounds = {north: n, east: e, south: s, west: w};
this.runningQuery.setData(true);
const self = this;
this._overpass.queryGeoJson(this.previousBounds,
const overpass = new Overpass(filter);
overpass.queryGeoJson(this.previousBounds,
function (data) {
self.handleData(data)
},
function (reason) {
self.handleFail(reason)
self.handleFail(state, reason)
}
);
}
private IsInBounds(): boolean {
private IsInBounds(state: State): boolean {
if (this.previousBounds === undefined) {
return false;
}
const b = State.state.bm.map.getBounds();
const b = state.bm.map.getBounds();
if (b.getSouth() < this.previousBounds.south) {
return false;
}
@ -145,5 +163,9 @@ export class LayerUpdater {
return true;
}
public ForceRefresh(){
this.previousBounds = undefined;
}
}