2020-12-16 16:54:41 +00:00
|
|
|
/*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Copyright 2021 James Lyne
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* 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
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* 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.
|
2020-12-16 16:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-23 16:16:26 +00:00
|
|
|
import {MutationTypes} from "@/store/mutation-types";
|
|
|
|
import {ActionContext, ActionTree} from "vuex";
|
|
|
|
import {State} from "@/store/state";
|
|
|
|
import {ActionTypes} from "@/store/action-types";
|
|
|
|
import {Mutations} from "@/store/mutations";
|
2020-12-11 15:28:51 +00:00
|
|
|
import {
|
2021-07-24 00:15:52 +00:00
|
|
|
DynmapAreaUpdate, DynmapCircleUpdate, DynmapLineUpdate,
|
2020-12-11 15:28:51 +00:00
|
|
|
DynmapMarkerUpdate,
|
2021-07-24 01:27:25 +00:00
|
|
|
DynmapTileUpdate,
|
2020-12-11 15:28:51 +00:00
|
|
|
} from "@/dynmap";
|
2021-07-24 03:06:19 +00:00
|
|
|
import {LiveAtlasMarkerSet, LiveAtlasPlayer, LiveAtlasWorldDefinition} from "@/index";
|
2020-11-23 16:16:26 +00:00
|
|
|
|
|
|
|
type AugmentedActionContext = {
|
|
|
|
commit<K extends keyof Mutations>(
|
|
|
|
key: K,
|
|
|
|
payload: Parameters<Mutations[K]>[1]
|
|
|
|
):ReturnType<Mutations[K]>;
|
|
|
|
} & Omit<ActionContext<State, State>, "commit">
|
|
|
|
|
|
|
|
export interface Actions {
|
|
|
|
[ActionTypes.LOAD_CONFIGURATION](
|
|
|
|
{commit}: AugmentedActionContext,
|
2021-07-24 00:15:52 +00:00
|
|
|
):Promise<void>
|
|
|
|
[ActionTypes.START_UPDATES](
|
2020-11-23 16:16:26 +00:00
|
|
|
{commit}: AugmentedActionContext,
|
2021-07-24 00:15:52 +00:00
|
|
|
):Promise<void>
|
|
|
|
[ActionTypes.STOP_UPDATES](
|
2020-12-01 23:20:38 +00:00
|
|
|
{commit}: AugmentedActionContext,
|
2021-07-24 00:15:52 +00:00
|
|
|
):Promise<void>
|
2020-12-01 23:20:38 +00:00
|
|
|
[ActionTypes.SET_PLAYERS](
|
|
|
|
{commit}: AugmentedActionContext,
|
2021-07-24 01:27:25 +00:00
|
|
|
payload: Set<LiveAtlasPlayer>
|
2021-07-24 03:06:19 +00:00
|
|
|
):Promise<Map<string, LiveAtlasMarkerSet>>
|
2020-12-11 15:28:51 +00:00
|
|
|
[ActionTypes.POP_MARKER_UPDATES](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: {markerSet: string, amount: number}
|
|
|
|
): Promise<DynmapMarkerUpdate[]>
|
|
|
|
[ActionTypes.POP_AREA_UPDATES](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: {markerSet: string, amount: number}
|
|
|
|
): Promise<DynmapAreaUpdate[]>
|
|
|
|
[ActionTypes.POP_CIRCLE_UPDATES](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: {markerSet: string, amount: number}
|
|
|
|
): Promise<DynmapCircleUpdate[]>
|
|
|
|
[ActionTypes.POP_LINE_UPDATES](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: {markerSet: string, amount: number}
|
|
|
|
): Promise<DynmapLineUpdate[]>
|
2020-12-11 18:51:23 +00:00
|
|
|
[ActionTypes.POP_TILE_UPDATES](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: number
|
|
|
|
): Promise<DynmapTileUpdate[]>
|
2021-01-07 22:40:05 +00:00
|
|
|
[ActionTypes.SEND_CHAT_MESSAGE](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: string
|
|
|
|
): Promise<void>
|
2021-08-30 21:27:41 +00:00
|
|
|
[ActionTypes.LOGIN](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: any
|
|
|
|
): Promise<void>
|
|
|
|
[ActionTypes.LOGOUT](
|
|
|
|
{commit}: AugmentedActionContext
|
|
|
|
): Promise<void>
|
|
|
|
[ActionTypes.REGISTER](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: any
|
|
|
|
): Promise<void>
|
2020-11-23 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const actions: ActionTree<State, State> & Actions = {
|
2021-07-24 00:15:52 +00:00
|
|
|
async [ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise<void> {
|
2021-05-17 23:19:51 +00:00
|
|
|
//Clear any existing has to avoid triggering a second config load, after this load changes the hash
|
2021-07-20 20:12:08 +00:00
|
|
|
commit(MutationTypes.CLEAR_SERVER_CONFIGURATION_HASH, undefined);
|
2021-05-17 02:39:25 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
if(!state.currentServer) {
|
|
|
|
console.warn('No current server');
|
|
|
|
return;
|
|
|
|
}
|
2020-11-23 16:16:26 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
await state.currentMapProvider!.loadServerConfiguration();
|
2021-05-17 23:19:51 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
//Skip default map/ui visibility logic if we already have a map selected (i.e config reload after hash change)
|
|
|
|
if(state.currentMap) {
|
2021-07-24 00:15:52 +00:00
|
|
|
return;
|
2021-05-18 17:22:34 +00:00
|
|
|
}
|
2021-01-27 00:47:59 +00:00
|
|
|
|
2021-05-19 14:44:26 +00:00
|
|
|
//Make UI visible if configured, there's enough space to do so, and this is the first config load
|
|
|
|
if(!state.ui.visibleElements.size && state.configuration.expandUI && !state.ui.smallScreen) {
|
2021-05-18 17:22:34 +00:00
|
|
|
commit(MutationTypes.SET_UI_ELEMENT_VISIBILITY, {element: 'players', state: true});
|
|
|
|
commit(MutationTypes.SET_UI_ELEMENT_VISIBILITY, {element: 'maps', state: true});
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
let worldName, mapName;
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Use config default world if it exists
|
2021-07-24 00:15:52 +00:00
|
|
|
if(state.configuration.defaultWorld && state.worlds.has(state.configuration.defaultWorld)) {
|
|
|
|
worldName = state.configuration.defaultWorld;
|
2021-05-18 17:22:34 +00:00
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Prefer world from parsed url if present and it exists
|
2021-07-20 02:25:42 +00:00
|
|
|
if(state.parsedUrl?.world && state.worlds.has(state.parsedUrl.world)) {
|
2021-05-18 17:22:34 +00:00
|
|
|
worldName = state.parsedUrl.world;
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Use first world, if any, if neither of the above exist
|
|
|
|
if(!worldName) {
|
|
|
|
worldName = state.worlds.size ? state.worlds.entries().next().value[1].name : undefined;
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
if(worldName) {
|
2021-07-23 19:32:15 +00:00
|
|
|
const world = state.worlds.get(worldName) as LiveAtlasWorldDefinition;
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Use config default map if it exists
|
2021-07-24 00:15:52 +00:00
|
|
|
if(state.configuration.defaultMap && world.maps.has(state.configuration.defaultMap)) {
|
|
|
|
mapName = state.configuration.defaultMap;
|
2021-05-18 17:22:34 +00:00
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Prefer map from parsed url if present and it exists
|
2021-07-20 02:25:42 +00:00
|
|
|
if(state.parsedUrl?.map && world.maps.has(state.parsedUrl.map)) {
|
2021-05-18 17:22:34 +00:00
|
|
|
mapName = state.parsedUrl.map;
|
2020-12-13 02:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
// Use first map, if any, if neither of the above exist
|
|
|
|
if(!mapName) {
|
|
|
|
mapName = world.maps.size ? world.maps.entries().next().value[1].name : undefined;
|
2020-11-23 16:16:26 +00:00
|
|
|
}
|
2021-05-18 17:22:34 +00:00
|
|
|
}
|
2020-11-23 16:16:26 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
if(worldName && mapName) {
|
|
|
|
commit(MutationTypes.SET_CURRENT_MAP, {
|
|
|
|
worldName, mapName
|
|
|
|
});
|
|
|
|
}
|
2020-11-23 16:16:26 +00:00
|
|
|
},
|
2021-05-18 17:22:34 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
async [ActionTypes.START_UPDATES]({state}) {
|
2020-11-23 16:16:26 +00:00
|
|
|
if(!state.currentWorld) {
|
|
|
|
return Promise.reject("No current world");
|
|
|
|
}
|
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
state.currentMapProvider!.startUpdates();
|
|
|
|
},
|
2021-05-18 17:22:34 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
async [ActionTypes.STOP_UPDATES]({state}) {
|
|
|
|
state.currentMapProvider!.stopUpdates();
|
2020-12-01 23:20:38 +00:00
|
|
|
},
|
2021-05-18 17:22:34 +00:00
|
|
|
|
2021-07-24 01:27:25 +00:00
|
|
|
[ActionTypes.SET_PLAYERS]({commit, state}, players: Set<LiveAtlasPlayer>) {
|
2020-12-01 23:20:38 +00:00
|
|
|
const keep: Set<string> = new Set();
|
|
|
|
|
|
|
|
for(const player of players) {
|
2021-07-24 01:27:25 +00:00
|
|
|
keep.add(player.name);
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Remove any players that aren't in the set
|
|
|
|
commit(MutationTypes.SYNC_PLAYERS, keep);
|
|
|
|
|
2021-07-24 01:27:25 +00:00
|
|
|
const processQueue = (players: Set<LiveAtlasPlayer>, resolve: Function) => {
|
2020-12-01 23:20:38 +00:00
|
|
|
commit(MutationTypes.SET_PLAYERS_ASYNC, players);
|
|
|
|
|
|
|
|
if(!players.size) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
requestAnimationFrame(() => processQueue(players, resolve));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set players every frame until done
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
requestAnimationFrame(() => processQueue(players, resolve));
|
|
|
|
});
|
|
|
|
},
|
2021-05-18 17:22:34 +00:00
|
|
|
|
|
|
|
async [ActionTypes.POP_MARKER_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapMarkerUpdate[]> {
|
2020-12-11 15:28:51 +00:00
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_MARKER_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2021-05-18 17:22:34 +00:00
|
|
|
return [];
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.markerUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_MARKER_UPDATES, {markerSet, amount});
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
return updates;
|
2020-12-11 15:28:51 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
async [ActionTypes.POP_AREA_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapAreaUpdate[]> {
|
2020-12-11 15:28:51 +00:00
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_AREA_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2021-05-18 17:22:34 +00:00
|
|
|
return [];
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.areaUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_AREA_UPDATES, {markerSet, amount});
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
return updates;
|
2020-12-11 15:28:51 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
async [ActionTypes.POP_CIRCLE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapCircleUpdate[]> {
|
2020-12-11 15:28:51 +00:00
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_CIRCLE_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2021-05-18 17:22:34 +00:00
|
|
|
return [];
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.circleUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_CIRCLE_UPDATES, {markerSet, amount});
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
return updates;
|
2020-12-11 15:28:51 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
async [ActionTypes.POP_LINE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapLineUpdate[]> {
|
2020-12-11 15:28:51 +00:00
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_LINE_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2021-05-18 17:22:34 +00:00
|
|
|
return [];
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.lineUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_LINE_UPDATES, {markerSet, amount});
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
return updates;
|
2020-12-11 15:28:51 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
async [ActionTypes.POP_TILE_UPDATES]({commit, state}, amount: number): Promise<Array<DynmapTileUpdate>> {
|
2020-12-11 18:51:23 +00:00
|
|
|
const updates = state.pendingTileUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_TILE_UPDATES, amount);
|
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
return updates;
|
2020-12-11 18:51:23 +00:00
|
|
|
},
|
2021-01-07 22:40:05 +00:00
|
|
|
|
2021-05-18 17:22:34 +00:00
|
|
|
async [ActionTypes.SEND_CHAT_MESSAGE]({commit, state}, message: string): Promise<void> {
|
2021-07-24 00:15:52 +00:00
|
|
|
await state.currentMapProvider!.sendChatMessage(message);
|
2021-01-07 22:40:05 +00:00
|
|
|
},
|
2021-08-30 21:27:41 +00:00
|
|
|
|
|
|
|
async [ActionTypes.LOGIN]({state, commit}, data: any): Promise<void> {
|
|
|
|
await state.currentMapProvider!.login(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
async [ActionTypes.LOGOUT]({state}): Promise<void> {
|
|
|
|
await state.currentMapProvider!.logout();
|
|
|
|
},
|
|
|
|
|
|
|
|
async [ActionTypes.REGISTER]({state}, data: any): Promise<void> {
|
|
|
|
await state.currentMapProvider!.register(data);
|
|
|
|
},
|
2021-06-23 17:19:09 +00:00
|
|
|
}
|