2020-12-16 16:54:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
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 API from '@/api';
|
|
|
|
import {Mutations} from "@/store/mutations";
|
2020-12-11 15:28:51 +00:00
|
|
|
import {
|
|
|
|
DynmapAreaUpdate, DynmapCircleUpdate,
|
|
|
|
DynmapConfigurationResponse, DynmapLineUpdate,
|
|
|
|
DynmapMarkerSet,
|
|
|
|
DynmapMarkerUpdate,
|
2020-12-13 02:50:17 +00:00
|
|
|
DynmapPlayer, DynmapTileUpdate,
|
|
|
|
DynmapUpdateResponse, DynmapWorld
|
2020-12-11 15:28:51 +00:00
|
|
|
} from "@/dynmap";
|
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,
|
|
|
|
):Promise<DynmapConfigurationResponse>
|
|
|
|
[ActionTypes.GET_UPDATE](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
):Promise<DynmapUpdateResponse>
|
2020-12-01 23:20:38 +00:00
|
|
|
[ActionTypes.GET_MARKER_SETS](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
):Promise<Map<string, DynmapMarkerSet>>
|
|
|
|
[ActionTypes.SET_PLAYERS](
|
|
|
|
{commit}: AugmentedActionContext,
|
|
|
|
payload: Set<DynmapPlayer>
|
|
|
|
):Promise<Map<string, DynmapMarkerSet>>
|
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>
|
2020-11-23 16:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const actions: ActionTree<State, State> & Actions = {
|
2020-12-13 02:50:17 +00:00
|
|
|
[ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise<DynmapConfigurationResponse> {
|
2020-11-23 16:16:26 +00:00
|
|
|
return API.getConfiguration().then(config => {
|
|
|
|
commit(MutationTypes.SET_CONFIGURATION, config.config);
|
|
|
|
commit(MutationTypes.SET_MESSAGES, config.messages);
|
|
|
|
commit(MutationTypes.SET_WORLDS, config.worlds);
|
2020-11-24 01:56:03 +00:00
|
|
|
commit(MutationTypes.SET_COMPONENTS, config.components);
|
2020-11-23 16:16:26 +00:00
|
|
|
|
2020-12-13 02:50:17 +00:00
|
|
|
let worldName, mapName;
|
|
|
|
|
|
|
|
// Use config default world if it exists
|
|
|
|
if(config.config.defaultWorld && state.worlds.has(config.config.defaultWorld)) {
|
|
|
|
worldName = config.config.defaultWorld;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer world from parsed url if present and it exists
|
|
|
|
if(state.parsedUrl.world && state.worlds.has(state.parsedUrl.world)) {
|
|
|
|
worldName = state.parsedUrl.world;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use first world, if any, if neither of the above exist
|
|
|
|
if(!worldName) {
|
2020-12-22 01:50:45 +00:00
|
|
|
worldName = state.worlds.size ? state.worlds.entries().next().value[1].name : undefined;
|
2020-12-13 02:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(worldName) {
|
|
|
|
const world = state.worlds.get(worldName) as DynmapWorld;
|
|
|
|
|
|
|
|
// Use config default map if it exists
|
|
|
|
if(config.config.defaultMap && world.maps.has(config.config.defaultMap)) {
|
|
|
|
mapName = config.config.defaultMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer map from parsed url if present and it exists
|
|
|
|
if(state.parsedUrl.map && world.maps.has(state.parsedUrl.map)) {
|
|
|
|
mapName = state.parsedUrl.map;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use first map, if any, if neither of the above exist
|
|
|
|
if(!mapName) {
|
2020-12-22 01:39:19 +00:00
|
|
|
mapName = world.maps.size ? world.maps.entries().next().value[1].name : undefined;
|
2020-12-13 02:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(worldName && mapName) {
|
2020-11-23 16:16:26 +00:00
|
|
|
commit(MutationTypes.SET_CURRENT_MAP, {
|
2020-12-13 02:50:17 +00:00
|
|
|
worldName, mapName
|
2020-11-23 16:16:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
});
|
|
|
|
},
|
2020-12-01 23:20:38 +00:00
|
|
|
[ActionTypes.GET_UPDATE]({commit, dispatch, state}) {
|
2020-11-23 16:16:26 +00:00
|
|
|
if(!state.currentWorld) {
|
|
|
|
return Promise.reject("No current world");
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
return API.getUpdate(state.updateRequestId, state.currentWorld.name, state.updateTimestamp.valueOf()).then(update => {
|
2020-12-01 23:20:38 +00:00
|
|
|
commit(MutationTypes.SET_WORLD_STATE, update.worldState);
|
2020-11-23 16:16:26 +00:00
|
|
|
commit(MutationTypes.SET_UPDATE_TIMESTAMP, new Date(update.timestamp));
|
|
|
|
commit(MutationTypes.INCREMENT_REQUEST_ID, undefined);
|
2020-12-11 15:28:51 +00:00
|
|
|
commit(MutationTypes.ADD_MARKER_SET_UPDATES, update.updates.markerSets);
|
2020-12-11 18:51:23 +00:00
|
|
|
commit(MutationTypes.ADD_TILE_UPDATES, update.updates.tiles);
|
2020-12-17 14:50:12 +00:00
|
|
|
commit(MutationTypes.ADD_CHAT, update.updates.chat);
|
2020-11-23 16:16:26 +00:00
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
return dispatch(ActionTypes.SET_PLAYERS, update.players).then(() => {
|
|
|
|
return update;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[ActionTypes.SET_PLAYERS]({commit, state}, players: Set<DynmapPlayer>) {
|
|
|
|
const keep: Set<string> = new Set();
|
|
|
|
|
|
|
|
for(const player of players) {
|
|
|
|
keep.add(player.account);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove any players that aren't in the set
|
|
|
|
commit(MutationTypes.SYNC_PLAYERS, keep);
|
|
|
|
|
|
|
|
const processQueue = (players: Set<DynmapPlayer>, resolve: Function) => {
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[ActionTypes.GET_MARKER_SETS]({commit, state}) {
|
|
|
|
if(!state.currentWorld) {
|
|
|
|
return Promise.reject("No current world");
|
|
|
|
}
|
|
|
|
|
|
|
|
return API.getMarkerSets(state.currentWorld.name).then(markerSets => {
|
|
|
|
commit(MutationTypes.SET_MARKER_SETS, markerSets);
|
|
|
|
|
|
|
|
return markerSets;
|
2020-11-23 16:16:26 +00:00
|
|
|
});
|
2020-12-11 15:28:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[ActionTypes.POP_MARKER_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapMarkerUpdate[]> {
|
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_MARKER_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2020-12-11 15:28:51 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.markerUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_MARKER_UPDATES, {markerSet, amount});
|
|
|
|
|
|
|
|
return Promise.resolve(updates);
|
|
|
|
},
|
|
|
|
|
|
|
|
[ActionTypes.POP_AREA_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapAreaUpdate[]> {
|
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_AREA_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2020-12-11 15:28:51 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.areaUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_AREA_UPDATES, {markerSet, amount});
|
|
|
|
|
|
|
|
return Promise.resolve(updates);
|
|
|
|
},
|
|
|
|
|
|
|
|
[ActionTypes.POP_CIRCLE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapCircleUpdate[]> {
|
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_CIRCLE_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2020-12-11 15:28:51 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.circleUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_CIRCLE_UPDATES, {markerSet, amount});
|
|
|
|
|
|
|
|
return Promise.resolve(updates);
|
|
|
|
},
|
|
|
|
|
|
|
|
[ActionTypes.POP_LINE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapLineUpdate[]> {
|
|
|
|
if(!state.markerSets.has(markerSet)) {
|
2020-12-18 16:12:14 +00:00
|
|
|
console.warn(`POP_LINE_UPDATES: Marker set ${markerSet} doesn't exist`);
|
2020-12-11 15:28:51 +00:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = state.pendingSetUpdates.get(markerSet)!.lineUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_LINE_UPDATES, {markerSet, amount});
|
|
|
|
|
|
|
|
return Promise.resolve(updates);
|
|
|
|
},
|
|
|
|
|
2020-12-11 18:51:23 +00:00
|
|
|
[ActionTypes.POP_TILE_UPDATES]({commit, state}, amount: number): Promise<Array<DynmapTileUpdate>> {
|
|
|
|
const updates = state.pendingTileUpdates.slice(0, amount);
|
|
|
|
|
|
|
|
commit(MutationTypes.POP_TILE_UPDATES, amount);
|
|
|
|
|
|
|
|
return Promise.resolve(updates);
|
|
|
|
},
|
2021-01-07 22:40:05 +00:00
|
|
|
|
|
|
|
[ActionTypes.SEND_CHAT_MESSAGE]({commit, state}, message: string): Promise<void> {
|
|
|
|
return API.sendChatMessage(message);
|
|
|
|
},
|
2020-11-23 16:16:26 +00:00
|
|
|
}
|