2022-01-25 13:42:41 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2022-01-26 10:40:13 +01:00
|
|
|
export interface ActionsMenuData {
|
|
|
|
playerName: string;
|
2022-02-02 13:30:49 +01:00
|
|
|
actions: { actionName: string; callback: Function }[];
|
2022-01-25 13:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function createActionsMenuStore() {
|
2022-01-26 10:40:13 +01:00
|
|
|
const { subscribe, update, set } = writable<ActionsMenuData | undefined>(undefined);
|
2022-01-25 13:42:41 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
2022-01-26 10:40:13 +01:00
|
|
|
initialize: (playerName: string) => {
|
|
|
|
set({
|
|
|
|
playerName,
|
|
|
|
actions: [],
|
2022-01-25 13:42:41 +01:00
|
|
|
});
|
|
|
|
},
|
2022-01-26 10:40:13 +01:00
|
|
|
addAction: (actionName: string, callback: Function) => {
|
|
|
|
update((data) => {
|
|
|
|
data?.actions.push({ actionName, callback });
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removeAction: (actionName: string) => {
|
|
|
|
update((data) => {
|
2022-02-02 13:30:49 +01:00
|
|
|
const actionIndex = data?.actions.findIndex((action) => action.actionName === actionName);
|
2022-01-26 10:40:13 +01:00
|
|
|
if (actionIndex !== undefined && actionIndex != -1) {
|
|
|
|
data?.actions.splice(actionIndex, 1);
|
|
|
|
}
|
|
|
|
return data;
|
2022-01-25 13:42:41 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Hides menu
|
|
|
|
*/
|
2022-01-26 10:40:13 +01:00
|
|
|
clear: () => {
|
|
|
|
set(undefined);
|
2022-02-02 13:30:49 +01:00
|
|
|
},
|
2022-01-25 13:42:41 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:50:25 +01:00
|
|
|
export const actionsMenuStore = createActionsMenuStore();
|