merged stress handler and added some functionalities to make it work with LPD

This commit is contained in:
Tom Odem 2024-05-15 20:32:05 -07:00
parent 7d82e53be1
commit b8691e2056
9 changed files with 102 additions and 27 deletions

View File

@ -11,6 +11,8 @@ import {
initializeState, initializeState,
addMessage, addMessage,
getAllElements, getAllElements,
getStressLevel,
setStressLevel,
} from 'src/redux/slices/minimapSlice'; } from 'src/redux/slices/minimapSlice';
// ~~~~~~~ Cusdom Hooks ~~~~~~~ // ~~~~~~~ Cusdom Hooks ~~~~~~~
import useWorldSim from 'src/hooks/useWorldSim'; import useWorldSim from 'src/hooks/useWorldSim';
@ -25,10 +27,12 @@ import { ownship, drones } from 'src/prototype/lpd/initialLPD';
import Home from 'src/components/Home'; import Home from 'src/components/Home';
import reactToMessage from 'src/prototype/reactToMessage'; import reactToMessage from 'src/prototype/reactToMessage';
import stressChangeHandler from 'src/prototype/stressChangeHandler'; import stressChangeHandler from 'src/prototype/stressChangeHandler';
import type { Widget } from 'src/types/widget';
const Prototype = () => { const Prototype = () => {
// ~~~~~ Custom Hooks ~~~~~~ // ~~~~~ Custom Hooks ~~~~~~
const { messages, stressLevel } = useWorldSim(); const { messages, stressLevel } = useWorldSim();
const currentStressLevel = useAppSelector(getStressLevel);
// ~~~~~ Selectors ~~~~~~ // ~~~~~ Selectors ~~~~~~
const sections = useAppSelector(getSections); const sections = useAppSelector(getSections);
@ -71,13 +75,23 @@ const Prototype = () => {
dispatch(addMessage(currentMessage)); dispatch(addMessage(currentMessage));
reactToMessage({dispatch, currentMessage}); reactToMessage({dispatch, currentMessage, stressLevel});
}, [messages]); }, [messages]);
useEffect(() => { useEffect(() => {
let allWidgetsInNewStressLPDIds: string[] = Object.keys(widgets); //this should be the actual new ones if(stressLevel != 0){ //don't run at start
stressChangeHandler({dispatch:dispatch, allWidgetIds:Object.keys(widgets), allMessages: messages, allWidgetsInNewStressLPDIds: allWidgetsInNewStressLPDIds}) console.log('stress level', stressLevel)
}, [stressLevel]) let allWidgetsInNewStressLPD: Widget[] = selector({stressLevel: stressLevel})
console.log('allwiidgets',allWidgetsInNewStressLPD)
let allWidgetsInNewStressLPDIds: string[] = allWidgetsInNewStressLPD.map(a => a.id); //get all widget ids from new stress level LPD and initial LPD
stressChangeHandler({dispatch:dispatch, allWidgetIds:Object.keys(widgets), allMessages: messages, allWidgetsInNewStressLPDIds: allWidgetsInNewStressLPDIds, stressLevel:stressLevel})
}
}, [currentStressLevel]);
useEffect(() => {
dispatch(setStressLevel(Math.floor(stressLevel * 3)));
}, [stressLevel]);
return <Home />; return <Home />;
}; };

View File

@ -7,6 +7,7 @@ import { v4 as uuid } from 'uuid';
import lpdHelper from 'src/utils/lpdHelper'; import lpdHelper from 'src/utils/lpdHelper';
import DANGER_ICON from 'src/assets/icons/danger.svg'; import DANGER_ICON from 'src/assets/icons/danger.svg';
import { elements } from './lowLPD'; import { elements } from './lowLPD';
import type { Widget } from 'src/types/widget';
// Functions to create widgets, elements, and sections for each message type // Functions to create widgets, elements, and sections for each message type
const requestApprovalToAttackMessageHigh = ( const requestApprovalToAttackMessageHigh = (
@ -217,8 +218,28 @@ const highLPDMessageFunctions: any = {
AcaHeadingToBase: acaHeadingToBaseMessageHigh, AcaHeadingToBase: acaHeadingToBaseMessageHigh,
}; };
const highLPD = (message: Message) => { const highLPD = (message?: Message) => {
return highLPDMessageFunctions[message.kind](message); if(message)
return highLPDMessageFunctions[message.kind](message);
//we can return all widgets in this LPD
const tempMessage = <RequestApprovalToAttack>({
priority: 2,
});
const messageKinds = [
'RequestApprovalToAttack',
'AcaFuelLow',
'AcaDefect',
'AcaHeadingToBase',
'MissileToOwnshipDetected'
];
let allPossibleWidgets: any = [];
messageKinds.forEach((kind) => {
highLPDMessageFunctions[kind](tempMessage).possibleWidgets.forEach((widget: Widget) => {
allPossibleWidgets.push(widget);
})
});
return allPossibleWidgets;
}; };
export default highLPD; export default highLPD;

View File

@ -3,6 +3,7 @@ import lpdHelper from 'src/utils/lpdHelper';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import type { Element } from 'src/types/element'; import type { Element } from 'src/types/element';
import DANGER_ICON from 'src/assets/icons/danger.svg'; import DANGER_ICON from 'src/assets/icons/danger.svg';
import type { Widget } from 'src/types/widget';
export const elements: Element[] = []; export const elements: Element[] = [];
@ -194,7 +195,29 @@ const lowLPDMessageFunctions: any = {
}; };
const lowLPD = (message: Message) => { const lowLPD = (message: Message) => {
return lowLPDMessageFunctions[message.kind](message); if(message.priority != -1){
return lowLPDMessageFunctions[message.kind](message);
}
//we can return all widgets in this LPD
const tempMessage = <RequestApprovalToAttack>({
priority: 2,
});
const messageKinds = [
'RequestApprovalToAttack',
'AcaFuelLow',
'AcaDefect',
'AcaHeadingToBase',
'MissileToOwnshipDetected'
];
let allPossibleWidgets: any = [];
messageKinds.forEach((kind) => {
lowLPDMessageFunctions[kind](tempMessage).possibleWidgets.forEach((widget: Widget) => {
allPossibleWidgets.push(widget);
})
});
return allPossibleWidgets;
}; };
export default lowLPD; export default lowLPD;

View File

@ -217,8 +217,9 @@ const mediumLPDMessageFunctions: any = {
MissileToOwnshipDetected: missileToOwnshipDetectedMessageMedium, MissileToOwnshipDetected: missileToOwnshipDetectedMessageMedium,
}; };
const mediumLPD = (message: Message) => { const mediumLPD = (message?: Message) => {
return mediumLPDMessageFunctions[message.kind](message); if(message)
return mediumLPDMessageFunctions[message.kind](message);
}; };
export default mediumLPD; export default mediumLPD;

View File

@ -9,6 +9,7 @@ type ReactToMessageProps = {
// define expected input here and it's type (number, string, etc.) // define expected input here and it's type (number, string, etc.)
dispatch: AppDispatch; dispatch: AppDispatch;
currentMessage: Message; currentMessage: Message;
stressLevel: number;
}; };
/** /**
@ -19,6 +20,7 @@ type ReactToMessageProps = {
const reactToMessage = ({ const reactToMessage = ({
dispatch, dispatch,
currentMessage, currentMessage,
stressLevel,
}: ReactToMessageProps) => { }: ReactToMessageProps) => {
const sections = store.getState().minimap.sections; const sections = store.getState().minimap.sections;
@ -27,6 +29,7 @@ const reactToMessage = ({
const { message, possibleWidgets } = selector({ const { message, possibleWidgets } = selector({
message: currentMessage, message: currentMessage,
stressLevel,
}); });
// possibleWidgets[0].id = uuid(); // possibleWidgets[0].id = uuid();

View File

@ -1,20 +1,9 @@
import type { Message } from 'src/types/schema-types'; import type { Message } from 'src/types/schema-types';
import { v4 as uuid } from 'uuid';
import type { Widget } from 'src/types/widget';
import type {
Element,
ButtonElement,
IconElement,
TextElement,
TableElement,
RequestApprovalElement,
MissileIncomingElement,
} from 'src/types/element';
import DANGER_ICON from 'src/icons/danger.svg';
import lowLPD from 'src/prototype/lpd/stress/lowLPD'; import lowLPD from 'src/prototype/lpd/stress/lowLPD';
import mediumLPD from 'src/prototype/lpd/stress/mediumLPD'; import mediumLPD from 'src/prototype/lpd/stress/mediumLPD';
import highLPD from 'src/prototype/lpd/stress/highLPD'; import highLPD from 'src/prototype/lpd/stress/highLPD';
import initialLPD from 'src/prototype/lpd/initialLPD'; import initialLPD from 'src/prototype/lpd/initialLPD';
import type { Widget } from 'src/types/widget';
const stressLevelLPDFunctions = [lowLPD, mediumLPD, highLPD]; const stressLevelLPDFunctions = [lowLPD, mediumLPD, highLPD];
@ -33,6 +22,17 @@ const selector = ({ message, stressLevel }: SelectorProps = {}) => {
if (!message && !stressLevel) { if (!message && !stressLevel) {
// If no message and no stress provided, return the initial LPD // If no message and no stress provided, return the initial LPD
return initialLPD; return initialLPD;
} else if(stressLevel && !message) { //return all widgets in current stressLevel
let widgetsInInitial: Widget[] = [];
for (const [key, widget] of Object.entries(initialLPD.widgets)) {
widgetsInInitial.push(widget);
}
stressLevel = Math.floor(stressLevel! * 3); //get stress level as int
const tempMessage = <Message>({ //dummy message to put into function
priority: -1,
})
return widgetsInInitial.concat(stressLevelLPDFunctions[stressLevel](tempMessage));
} else { } else {
// Transform range of stress levels from 0-1 to 0-2 only returning integers // Transform range of stress levels from 0-1 to 0-2 only returning integers
stressLevel = Math.floor(stressLevel! * 3); stressLevel = Math.floor(stressLevel! * 3);

View File

@ -1,9 +1,6 @@
import { ElementInGaze } from "src/redux/slices/gazeSlice";
import { removeWidget } from "src/redux/slices/minimapSlice"; import { removeWidget } from "src/redux/slices/minimapSlice";
import { AppDispatch } from "src/redux/store"; import type { AppDispatch } from "src/redux/store";
import { BaseElement } from "src/types/element"; import type { Message } from "src/types/schema-types";
import { Message } from "src/types/schema-types";
import { Widget } from "src/types/widget";
import reactToMessage from "./reactToMessage"; import reactToMessage from "./reactToMessage";
@ -14,6 +11,7 @@ type StressChangeHandlerProps = {
allWidgetIds: string[]; allWidgetIds: string[];
allMessages: Message[]; allMessages: Message[];
allWidgetsInNewStressLPDIds: string[]; allWidgetsInNewStressLPDIds: string[];
stressLevel: number;
}; };
/** /**
@ -26,6 +24,7 @@ const stressChangeHandler = ({
allWidgetIds, allWidgetIds,
allMessages, allMessages,
allWidgetsInNewStressLPDIds, allWidgetsInNewStressLPDIds,
stressLevel,
}: StressChangeHandlerProps) => { }: StressChangeHandlerProps) => {
allWidgetIds.forEach(function(widgetId, widgetIdIndex){ allWidgetIds.forEach(function(widgetId, widgetIdIndex){
if(!allWidgetsInNewStressLPDIds.includes(widgetId)){ if(!allWidgetsInNewStressLPDIds.includes(widgetId)){
@ -35,7 +34,7 @@ const stressChangeHandler = ({
allMessages.forEach(function(message, messageIndex){ allMessages.forEach(function(message, messageIndex){
if(!message.fulfilled){ if(!message.fulfilled){
reactToMessage({dispatch:dispatch, currentMessage:message}); reactToMessage({dispatch:dispatch, currentMessage:message, stressLevel:stressLevel});
} }
}); });

View File

@ -21,6 +21,8 @@ export type InitialMinimapState = {
widgets: WidgetMap; widgets: WidgetMap;
messages: Message[]; messages: Message[];
sections: Section[]; sections: Section[];
stressLevel: number;
}; };
const initialState: InitialMinimapState = { const initialState: InitialMinimapState = {
@ -31,6 +33,7 @@ const initialState: InitialMinimapState = {
messages: [], messages: [],
widgets: {}, widgets: {},
sections: [], sections: [],
stressLevel: 0,
}; };
export const minimapSlice = createSlice({ export const minimapSlice = createSlice({
@ -314,6 +317,10 @@ export const minimapSlice = createSlice({
addMessage: (state, action: PayloadAction<Message>) => { addMessage: (state, action: PayloadAction<Message>) => {
state.messages.push(action.payload); state.messages.push(action.payload);
}, },
setStressLevel: (state, action: PayloadAction<number>) => {
state.stressLevel = action.payload;
}
}, },
// selectors are used to access parts of the state within components // selectors are used to access parts of the state within components
selectors: { selectors: {
@ -360,6 +367,7 @@ export const minimapSlice = createSlice({
getVisualComplexity: (state) => state.visualComplexity, getVisualComplexity: (state) => state.visualComplexity,
getAudioComplexity: (state) => state.audioComplexity, getAudioComplexity: (state) => state.audioComplexity,
getMessages: (state) => state.messages, getMessages: (state) => state.messages,
getStressLevel: (state) => state.stressLevel,
// ~~~~~ selectors for ships ~~~~~ // ~~~~~ selectors for ships ~~~~~
getOwnship: (state) => { getOwnship: (state) => {
@ -399,6 +407,8 @@ export const {
deleteElementFromWidget, deleteElementFromWidget,
toggleElementInteraction, toggleElementInteraction,
setStressLevel,
} = minimapSlice.actions; } = minimapSlice.actions;
export const { export const {
@ -418,4 +428,6 @@ export const {
getVisualComplexity, getVisualComplexity,
getAudioComplexity, getAudioComplexity,
getStressLevel,
} = minimapSlice.selectors; } = minimapSlice.selectors;

View File

@ -22,6 +22,8 @@ export type BaseElement = {
expiration?: string; expiration?: string;
onExpiration?: 'delete' | 'escalate' | 'deescalate'; onExpiration?: 'delete' | 'escalate' | 'deescalate';
interacted?: boolean; interacted?: boolean;
canOverlap?: boolean;
style?: Properties;
}; };
// //