2021-07-25 14:12:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 James Lyne
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-07-25 00:57:59 +00:00
|
|
|
import {
|
|
|
|
HeadQueueEntry,
|
|
|
|
LiveAtlasMapProvider,
|
|
|
|
LiveAtlasServerDefinition,
|
|
|
|
LiveAtlasWorldDefinition
|
|
|
|
} from "@/index";
|
2021-07-24 00:15:52 +00:00
|
|
|
import {useStore} from "@/store";
|
2021-07-25 00:57:59 +00:00
|
|
|
import {computed, watch} from "@vue/runtime-core";
|
|
|
|
import {WatchStopHandle} from "vue";
|
2021-07-24 00:15:52 +00:00
|
|
|
|
|
|
|
export default abstract class MapProvider implements LiveAtlasMapProvider {
|
|
|
|
protected readonly store = useStore();
|
2021-07-25 00:57:59 +00:00
|
|
|
protected readonly config: LiveAtlasServerDefinition;
|
|
|
|
private readonly currentWorldUnwatch: WatchStopHandle;
|
2021-07-24 00:15:52 +00:00
|
|
|
|
|
|
|
protected constructor(config: LiveAtlasServerDefinition) {
|
2021-07-25 00:57:59 +00:00
|
|
|
this.config = config;
|
2021-07-24 00:15:52 +00:00
|
|
|
const currentWorld = computed(() => this.store.state.currentWorld);
|
|
|
|
|
2021-07-25 00:57:59 +00:00
|
|
|
this.currentWorldUnwatch = watch(currentWorld, (newValue) => {
|
|
|
|
if (newValue) {
|
|
|
|
this.populateWorld(newValue);
|
2021-07-24 00:15:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract loadServerConfiguration(): Promise<void>;
|
2021-07-25 00:57:59 +00:00
|
|
|
abstract populateWorld(world: LiveAtlasWorldDefinition): Promise<void>;
|
2021-07-24 00:15:52 +00:00
|
|
|
abstract sendChatMessage(message: string): void;
|
2021-07-25 00:57:59 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
abstract startUpdates(): void;
|
|
|
|
abstract stopUpdates(): void;
|
2021-07-25 00:57:59 +00:00
|
|
|
|
|
|
|
abstract getPlayerHeadUrl(head: HeadQueueEntry): string;
|
|
|
|
abstract getTilesUrl(): string;
|
|
|
|
abstract getMarkerIconUrl(icon: string): string;
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.currentWorldUnwatch();
|
|
|
|
}
|
2021-07-24 19:13:19 +00:00
|
|
|
|
|
|
|
protected static async fetchJSON(url: string, signal: AbortSignal) {
|
|
|
|
let response, json;
|
|
|
|
|
|
|
|
try {
|
|
|
|
response = await fetch(url, {signal});
|
|
|
|
} catch(e) {
|
|
|
|
if(e instanceof DOMException && e.name === 'AbortError') {
|
|
|
|
console.warn(`Request aborted (${url}`);
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Network request failed`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network request failed (${response.statusText || 'Unknown'})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = await response.json();
|
|
|
|
} catch(e) {
|
|
|
|
if(e instanceof DOMException && e.name === 'AbortError') {
|
|
|
|
console.warn(`Request aborted (${url}`);
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
throw new Error('Request returned invalid json');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
2021-07-24 00:15:52 +00:00
|
|
|
}
|