Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions src/app/workshop/computer.actions.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,79 @@
/**
* computer action file!
*
*
* all action definitions go in this file
*/
import { createAction, props } from "@ngrx/store";
import { NavigationData } from "../nav-db.service";
import { createAction, props } from "@ngrx/store";
import { IComputerDirective } from "../challenge.service";
import { NavigationData } from "../nav-db.service";

/**
* this is an example action, feel free to change it
*/
export const echo = createAction(
'[computer] echo',
props<{message: string}>()
);

// these three actions are for loading Navigation data
export const loadNavData = createAction('[computer] Load Navigation Data');
export const loadNavDataSuccess = createAction('[computer] Load Navigation Data Success', props<{navs: NavigationData[]}>());
export const loadNavDataError = createAction('[computer] Load Navigation Data Error');

//TODO: add a lot more action definitions!
// https://ngrx.io/guide/store/actions
/**
* this is an example action, feel free to change it
*/
export const MessageActions = {
echo: '[computer] echo',
shields: '[computer] shields',
engines: '[computer] engines',
laserPower: '[computer] laser',
tractorbeam: '[computer] tractor beam',
clamp: '[computer] docking clamp',
plotCourse: '[computer] plot course'
}

/** Displays Screen Messages */

export const echo = createAction(
MessageActions.echo,
props<{ message: string }>()
);


/** Create Actions based on the errors found */

/** Object Levels */

export const shields = createAction(
MessageActions.shields,
props<{ directive: IComputerDirective }>()
);

export const engines = createAction(
MessageActions.engines,
props<{ directive: IComputerDirective }>()
);

export const laserPower = createAction(
MessageActions.laserPower,
props<{ directive: IComputerDirective }>()
);



/** Active Objects */

export const tractorbeam = createAction(
MessageActions.tractorbeam,
props<{ directive: IComputerDirective }>()
);


export const dockingClamp = createAction(
MessageActions.clamp,
props<{ directive: IComputerDirective }>()
);

/** Location/View Screen */

export const plotCourse = createAction(
MessageActions.plotCourse,
props<{ directive: IComputerDirective }>()
);


// these three actions are for loading Navigation data
export const loadNavData = createAction('[computer] Load Navigation Data');
export const loadNavDataSuccess = createAction('[computer] Load Navigation Data Success', props<{ navs: NavigationData[] }>());
export const loadNavDataError = createAction('[computer] Load Navigation Data Error', props<{error: string}>());

//TODO: add a lot more action definitions!
// https://ngrx.io/guide/store/actions
217 changes: 170 additions & 47 deletions src/app/workshop/computer.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,177 @@
/**
* computer reducer file!
*
*
* all main computer logic should go in this file
*/
import { createReducer, on } from "@ngrx/store";
import { NavigationData } from "../nav-db.service";
import { echo, loadNavDataSuccess } from "./computer.actions";
import { createReducer, on } from "@ngrx/store";
import { IComputerDirective, IExpectations, SolarSystemLocation } from "../challenge.service";
import { NavigationData } from "../nav-db.service";
import { echo, engines, plotCourse, dockingClamp, laserPower, loadNavDataSuccess, shields, tractorbeam } from "./computer.actions";
import { DISENGAGE, ENGAGE, MAX_AMT_POWER, MIN_AMT_POWER, processComputerDirectives } from "./computer.service";
import { ViewscreenState } from "./viewscreen/viewscreen.component";

/**
* This is the "slice" that you need to fill out!
*
* Course data, location data, engine/shield/laser power level, all
* of these state properties must reside in this interface!
*
* All of the visual components will select their state from this central
* state location.
*/
export interface ComputerState{
/**
* these messages are displayed by the computer-messages component
*
* they are not required or useful, they are just example of state properties
*
* feel free to change or remove this
*/
echoMessages: string[]
//TODO: add a lot more state!
}

export const InitialComputerState: ComputerState = {
echoMessages: []
//TODO: add additional initial state!
}
/**
* This is the "slice" that you need to fill out!
*
* Course data, location data, engine/shield/laser power level, all
* of these state properties must reside in this interface!
*
* All of the visual components will select their state from this central
* state location.
*/
export interface ComputerState {
echoMessages: string[];
expectationState: IExpectations;
viewScreenState: ViewscreenState;
navigationData: NavigationData[];
//TODO: add a lot more state!
}

export const computerReducer = createReducer<ComputerState>(
InitialComputerState,
on(echo, (state, action) => {
return {
...state,
echoMessages: [
action.message,
...state.echoMessages
]
};
}),
//TODO: add an on() listener for loadNavDataSuccess that puts NavigationData[] in the state!
//TODO: use the NavigationData[] to set viewscreen state depending on location and/or course!
//TODO: add a lot more reducer action logic!
// https://ngrx.io/guide/store/reducers
// there should be a lot of logic in here!
);
export const InitialComputerState: ComputerState = {
echoMessages: [],
expectationState: {
tractorbeam: false,
satelliteView: false,
asteroidView: false,
course: 'LEO',
docking: false,
engine: 0,
laser: 0,
laserView: false,
location: 'LEO',
shield: 0,
tractorView: false,
},
navigationData: [],
viewScreenState: {
rightImage: undefined,
centerImage: undefined,
laser: false,
leftImage: undefined,
location: 'LEO',
tractor: false,
course: undefined
}
}


export const computerReducer = createReducer<ComputerState>(
InitialComputerState,
on(echo, (state, action) => {
return {
...state,
echoMessages: [
action.message,
...state.echoMessages
]
};
}),
on(engines, (state, action) => {
let power = action.directive.verb === ENGAGE ? processComputerDirectives(action.directive) : 0;
return {
...state,
expectationState: {
...state.expectationState,
laser: power >= MAX_AMT_POWER ? 0 : state.expectationState.laser,
shield: power >= MAX_AMT_POWER ? 0 : state.expectationState.shield,
asteroidView: power > MIN_AMT_POWER,
engine: power,
},
viewScreenState: {
...state.viewScreenState,
centerImage: action.directive.verb === DISENGAGE ? undefined : state.viewScreenState.centerImage
}
};
}),
on(plotCourse, (state, action) => {
let viewScreenState: ViewscreenState = InitialComputerState.viewScreenState;
const screenState = processComputerDirectives(action.directive);
const navData= state.navigationData.find(d => d.location === screenState);
if (navData != null) {
viewScreenState = {
centerImage: navData.centerImage,
laser: state.expectationState.laserView || false,
leftImage: navData.leftImage,
location: navData.location,
rightImage: navData.rightImage,
tractor: screenState === 'LunaOrbit',
course: screenState
};
}
return {
...state,
expectationState: {
...state.expectationState,
course: screenState,
},
viewScreenState
};
}),
on(shields, (state, action) => {
let power = action.directive.verb === ENGAGE ? processComputerDirectives(action.directive) : 0;
return {
...state,
expectationState: {
...state.expectationState,
shield: power,
engine: power >= MAX_AMT_POWER ? 0 : state.expectationState.engine,
laser: power >= MAX_AMT_POWER ? 0 : state.expectationState.laser,
},
viewScreenState: {
...state.viewScreenState,
leftImage: power >= MAX_AMT_POWER ? undefined : state.viewScreenState.leftImage,
laser: power >= MAX_AMT_POWER ? false : state.viewScreenState.laser,
centerImage: power >= MAX_AMT_POWER ? undefined : state.viewScreenState.centerImage,
}
};
}),
on(laserPower, (state, action) => {
let power = action.directive.verb === ENGAGE ? processComputerDirectives(action.directive) : 0;
return {
...state,
expectationState: {
...state.expectationState,
laser: power,
shield: power >= MAX_AMT_POWER ? 0 : state.expectationState.shield,
engine: power >= MAX_AMT_POWER ? 0 : state.expectationState.engine,
laserView: power > MIN_AMT_POWER
},
viewScreenState: {
...state.viewScreenState,
laser: power > MIN_AMT_POWER
}
};
}),
on(dockingClamp, (state, action) => {
let isDocking = (action.directive.verb === ENGAGE );
return {
...state,
expectationState: {
...state.expectationState,
docking: isDocking,
}
};
}),
on(tractorbeam, (state, action) => {
let isTractorbeam = (action.directive.verb === ENGAGE );
return {
...state,
expectationState: {
...state.expectationState,
tractorbeam: isTractorbeam,
satelliteView: action.directive.verb === DISENGAGE ? false : state.expectationState.satelliteView,
tractorView: isTractorbeam
},
viewScreenState: {
...state.viewScreenState,
tractor: isTractorbeam,
leftImage: action.directive.verb === DISENGAGE ? undefined : state.viewScreenState.leftImage
}
};
}),
on(loadNavDataSuccess, (state, action) => {
return {
...state,
navigationData: action.navs
};
})
);
Loading