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 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;
|
|
|
|
|
2021-08-30 21:14:39 +00:00
|
|
|
sendChatMessage(message: string) {
|
|
|
|
throw new Error('Provider does not support chat');
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:27:41 +00:00
|
|
|
async login(data: any) {
|
|
|
|
throw new Error('Provider does not support logging in');
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout() {
|
|
|
|
throw new Error('Provider does not support logging out');
|
|
|
|
}
|
|
|
|
|
|
|
|
async register(data: any) {
|
|
|
|
throw new Error('Provider does not support registration');
|
|
|
|
}
|
|
|
|
|
2021-07-25 00:57:59 +00:00
|
|
|
destroy() {
|
|
|
|
this.currentWorldUnwatch();
|
|
|
|
}
|
2021-07-24 19:13:19 +00:00
|
|
|
|
2021-08-30 21:17:52 +00:00
|
|
|
protected static async fetchJSON(url: string, options: any) {
|
2021-07-24 19:13:19 +00:00
|
|
|
let response, json;
|
|
|
|
|
|
|
|
try {
|
2021-08-30 21:17:52 +00:00
|
|
|
response = await fetch(url, options);
|
2021-07-24 19:13:19 +00:00
|
|
|
} 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-08-30 21:17:52 +00:00
|
|
|
|
|
|
|
protected static async getJSON(url: string, signal: AbortSignal) {
|
|
|
|
return MapProvider.fetchJSON(url, {signal, credentials: 'include'});
|
|
|
|
}
|
2021-07-24 00:15:52 +00:00
|
|
|
}
|