Skip to content

Commit

Permalink
Iceberg stager (#523)
Browse files Browse the repository at this point in the history
* Add Iceberg stage tests

* Add Iceberg stager

* Import iceberg types from appropriate quest file
  • Loading branch information
hymccord authored Feb 4, 2024
1 parent 59461f2 commit a92e9fa
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,6 @@ import * as detailingFuncs from './modules/details/legacy';
"Foreword Farm": stagingFuncs.addForewordFarmStage,
"Furoma Rift": stagingFuncs.addFuromaRiftStage,
"Gnawnian Express Station": stagingFuncs.addTrainStage,
"Iceberg": stagingFuncs.addIcebergStage,
"Seasonal Garden": stagingFuncs.addSeasonalGardenStage,
"Sunken City": stagingFuncs.addSunkenCityStage,
"Toxic Spill": stagingFuncs.addToxicSpillStage,
Expand Down
41 changes: 41 additions & 0 deletions src/scripts/modules/stages/environments/iceberg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {type User} from '@scripts/types/hg';
import {type IntakeMessage} from '@scripts/types/mhct';
import {IcebergPhases, type IcebergPhase} from '@scripts/types/quests';
import {type IStager} from '../stages.types';

export class IcebergStager implements IStager {
readonly environment: string = 'Iceberg';

readonly phaseToStage: Record<IcebergPhase, string> = {
'Treacherous Tunnels': '0-300ft',
'Brutal Bulwark': '301-600ft',
'Bombing Run': '601-1600ft',
'The Mad Depths': '1601-1800ft',
'Icewing\'s Lair': '1800ft',
'Hidden Depths': '1801-2000ft',
'The Deep Lair': '2000ft',
'General': 'Generals',
};

/**
* Report the current distance / obstacle.
* TODO: Stage / hunt details for first & second icewing hunting?
*/
addStage(message: IntakeMessage, userPre: User, userPost: User, journal: unknown): void {
const quest = userPre.quests.QuestIceberg;

if (!quest) {
throw new Error('QuestIceberg is undefined');
}

if (!this.isIcebergPhase(quest.current_phase)) {
throw new Error('Skipping unknown Iceberg stage');
}

message.stage = this.phaseToStage[quest.current_phase];
}

private isIcebergPhase(value: string): value is IcebergPhase {
return IcebergPhases.includes(value as IcebergPhase);
}
}
2 changes: 2 additions & 0 deletions src/scripts/modules/stages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ForbiddenGroveStager} from './environments/forbiddenGrove';
import {FortRoxStager} from './environments/fortRox';
import {FungalCavernStager} from './environments/fungalCavern';
import {HarbourStager} from './environments/harbour';
import {IcebergStager} from './environments/iceberg';
import {IceFortressStager} from './environments/iceFortress';
import {LabyrinthStager} from './environments/labyrinth';
import {LivingGardenStager} from './environments/livingGarden';
Expand Down Expand Up @@ -37,6 +38,7 @@ const stageModules: IStager[] = [
new FortRoxStager(),
new FungalCavernStager(),
new HarbourStager(),
new IcebergStager(),
new IceFortressStager(),
new LabyrinthStager(),
new LivingGardenStager(),
Expand Down
26 changes: 0 additions & 26 deletions src/scripts/modules/stages/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,6 @@ export function addSeasonalGardenStage(message, user, user_post, hunt) {
}
}

/**
* Report the current distance / obstacle.
* TODO: Stage / hunt details for first & second icewing hunting?
* @param {import("@scripts/types/mhct").IntakeMessage} message The message to be sent.
* @param {import("@scripts/types/hg").User} user The user state object, when the hunt was invoked (pre-hunt).
* @param {import("@scripts/types/hg").User} user_post The user state object, after the hunt.
* @param {unknown} hunt The journal entry corresponding to the active hunt.
*/
export function addIcebergStage(message, user, user_post, hunt) {
const quest = user.quests.QuestIceberg;
message.stage = (({
"Treacherous Tunnels": "0-300ft",
"Brutal Bulwark": "301-600ft",
"Bombing Run": "601-1600ft",
"The Mad Depths": "1601-1800ft",
"Icewing's Lair": "1800ft",
"Hidden Depths": "1801-2000ft",
"The Deep Lair": "2000ft",
"General": "Generals",
})[quest.current_phase]);

if (!message.stage) {
message.location = null;
}
}

/**
* Report the zone and depth, if any.
* @param {Object <string, any>} message The message to be sent.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {IntakeRejectionEngine} from '@scripts/hunt-filter/engine';
import {addIcebergStage} from '@scripts/modules/stages/legacy';
import {IcebergStager} from '@scripts/modules/stages/environments/iceberg';
import {IStager} from '@scripts/modules/stages/stages.types';
import {User} from '@scripts/types/hg';
import {IntakeMessage} from '@scripts/types/mhct';
import {LoggerService} from '@scripts/util/logger';
import {getDefaultIntakeMessage, getDefaultUser} from '@tests/scripts/hunt-filter/common';

describe('Iceberg exemptions', () => {
let logger: LoggerService;
let stager: (message: IntakeMessage, pre: User, post: User, journal: unknown) => void;
let stager: IStager;
let target: IntakeRejectionEngine;

beforeEach(() => {
logger = {} as LoggerService;
stager = addIcebergStage;
stager = new IcebergStager();
target = new IntakeRejectionEngine(logger);

logger.debug = jest.fn();
Expand Down Expand Up @@ -93,8 +94,8 @@ describe('Iceberg exemptions', () => {

/** Sets the pre and post message stage based on current pre and post user */
function calculateStage() {
stager(preMessage, preUser, {} as User, {});
stager(postMessage, postUser, {} as User, {});
stager.addStage(preMessage, preUser, {} as User, {});
stager.addStage(postMessage, postUser, {} as User, {});
}
});

Expand Down
58 changes: 58 additions & 0 deletions tests/scripts/modules/stages/environments/iceberg.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {IcebergStager} from "@scripts/modules/stages/environments/iceberg";
import {User} from "@scripts/types/hg";
import {IntakeMessage} from "@scripts/types/mhct";

describe('Iceberg Stages', () => {
it('should be for the "Iceberg" environment', () => {
const stager = new IcebergStager();
expect(stager.environment).toBe('Iceberg');
});

it.each`
phase | expected
${'Treacherous Tunnels'} | ${'0-300ft'}
${'Brutal Bulwark'} | ${'301-600ft'}
${'Bombing Run'} | ${'601-1600ft'}
${'The Mad Depths'} | ${'1601-1800ft'}
${'Icewing\'s Lair'} | ${'1800ft'}
${'Hidden Depths'} | ${'1801-2000ft'}
${'The Deep Lair'} | ${'2000ft'}
${'General'} | ${'Generals'}
`('should set stage to $expected when in the $phase phase', ({expected, phase}) => {
const stager = new IcebergStager();
const message = {} as IntakeMessage;
const preUser = {quests: {QuestIceberg: {
current_phase: phase,
}}} as User;
const postUser = {} as User;
const journal = {};

stager.addStage(message, preUser, postUser, journal);

expect(message.stage).toBe(expected);
});

it('should should throw on unknown phase', () => {
const stager = new IcebergStager();
const message = {location: {}} as IntakeMessage;
const preUser = {quests: {QuestIceberg: {
current_phase: 'Aard\'s Lair',
}}} as unknown as User;
const postUser = {} as User;
const journal = {};

expect(() => stager.addStage(message, preUser, postUser, journal))
.toThrow('Skipping unknown Iceberg stage');
});

it.each([undefined, null])('should throw when QuestIceberg is %p', (state) => {
const stager = new IcebergStager();
const message = {location: {}} as IntakeMessage;
const preUser = {quests: {QuestIceberg: state}} as User;
const postUser = {} as User;
const journal = {};

expect(() => stager.addStage(message, preUser, postUser, journal))
.toThrow('QuestIceberg is undefined');
});
});

0 comments on commit a92e9fa

Please sign in to comment.