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
|
|
|
}
|