11import { Hallway } from "./Hallway" ;
2- import { Room } from "./Room" ;
3- import { getGraph , getShortestPath , isConnectedGraph } from "./graph" ;
2+ import { ForkableRoom } from "./ForkableRoom" ;
3+ import {
4+ getShortestPath ,
5+ isConnectedGraph ,
6+ isConnectionStairs ,
7+ getGraph ,
8+ } from "./graph" ;
49import { isLeftOrRight } from "./Direction" ;
10+ import { ForkNode } from "./ForkNode" ;
11+ import { StairNode } from "./StairNode" ;
512
613/**
714 * @ignore
@@ -51,7 +58,10 @@ function format(str: string, { capitalize = true, periods = false }) {
5158 * Continue, then turn left into room C
5259 * ```
5360 */
54- export class Building {
61+ export class Building <
62+ ForkName extends string = string ,
63+ StairName extends string = string
64+ > {
5565 /**
5666 * The graph that is generated from the nodes in the [[hallways]] and the
5767 * [[hallwayConnections]] and [[stairConnections]] between them
@@ -72,18 +82,14 @@ export class Building {
7282 * in the building. Each connection is an array of node IDs that starts at the
7383 * bottom floor and goes to the top floor.
7484 */
75- constructor (
76- readonly hallways : Hallway [ ] ,
77- readonly hallwayConnections : [ string , string ] [ ] = [ ] ,
78- readonly stairConnections : string [ ] [ ] = [ ]
79- ) {
85+ constructor ( readonly hallways : Hallway < ForkName , StairName > [ ] ) {
8086 const hallwayNodes = this . hallways . map ( h => {
8187 return h . nodes ;
8288 } ) ;
83- this . graph = getGraph ( hallwayNodes , stairConnections , hallwayConnections ) ;
89+ this . graph = getGraph ( hallwayNodes ) ;
8490 this . roomsList = hallways
8591 . flatMap ( h => h . partList )
86- . filter ( ( a ) : a is Room => "name" in a && a . name != null )
92+ . filter ( ( a ) : a is ForkableRoom < ForkName > => "name" in a && a . name != null )
8793 . flatMap ( r => r . aliases . concat ( r . name ! ) )
8894 . sort ( ) ;
8995 }
@@ -107,39 +113,25 @@ export class Building {
107113 * is the index of the hallway where the node is located, and
108114 * the second element is the index of the node in the hallway
109115 */
110- protected getHallwayIndexAndIndexFromNode ( nodeId : string ) : [ number , number ] {
116+ protected getHallwayIndexAndIndexFromNode (
117+ nodeId : ForkNode < ForkName > | StairNode < StairName >
118+ ) : [ number , number ] {
111119 const inds = this . hallways . map ( h =>
112- h . partList . findIndex ( r => "nodeId" in r && r . nodeId === nodeId )
120+ h . partList . findIndex (
121+ r =>
122+ "nodeId" in r && JSON . stringify ( r . nodeId ) === JSON . stringify ( nodeId )
123+ )
113124 ) ;
114125 const hallwayInd = inds . findIndex ( a => a !== - 1 ) ;
115126 return [ hallwayInd , inds [ hallwayInd ] ] ;
116127 }
117128
118- /**
119- * @param id1
120- * @param id2
121- * @return Is the connection between these two nodes
122- * a Stairs connection? (as opposed to a Fork)
123- */
124- protected isConnectionStairs ( id1 : string , id2 : string ) : boolean {
125- return (
126- this . stairConnections . findIndex (
127- arr => arr . includes ( id1 ) && arr . includes ( id2 )
128- ) != - 1
129- ) ;
130- }
131-
132129 protected getStairConnectionInstruction (
133- id1 : string ,
134- id2 : string ,
130+ id1 : StairNode < StairName > ,
131+ id2 : StairNode < StairName > ,
135132 numFlights : number
136133 ) : string {
137- const goingUp = this . stairConnections . find (
138- arr =>
139- arr . includes ( id1 ) &&
140- arr . includes ( id2 ) &&
141- arr . indexOf ( id2 ) > arr . indexOf ( id1 )
142- ) ;
134+ const goingUp = id2 . floor > id1 . floor ;
143135 const maybeS = numFlights > 1 ? "s" : "" ;
144136 return `go ${
145137 goingUp ? "up" : "down"
@@ -208,12 +200,7 @@ export class Building {
208200 const [ prevHallwayInd , prevInd ] = this . getHallwayIndexAndIndexFromNode (
209201 shortest [ i - 1 ]
210202 ) ;
211- if (
212- this . isConnectionStairs (
213- shortest [ i - 1 ] ,
214- shortest [ i ]
215- ) /* going up or down stairs */
216- ) {
203+ if ( isConnectionStairs ( shortest [ i ] ) /* going up or down stairs */ ) {
217204 directions += this . hallways [ currentHallwayInd ] . getDirectionsFromIndices (
218205 currentInd ,
219206 prevInd ,
@@ -223,12 +210,13 @@ export class Building {
223210 entranceWasStraight,
224211 }
225212 ) ;
226- const numStairFlights = Math . ceil (
227- this . graph [ shortest [ i - 1 ] ] [ shortest [ i ] ]
213+ const numStairFlights = Math . abs (
214+ ( shortest [ i - 1 ] as StairNode < StairName > ) . floor -
215+ ( shortest [ i ] as StairNode < StairName > ) . floor
228216 ) ;
229217 directions += this . getStairConnectionInstruction (
230- shortest [ i - 1 ] ,
231- shortest [ i ] ,
218+ shortest [ i - 1 ] as StairNode < StairName > ,
219+ shortest [ i ] as StairNode < StairName > ,
232220 numStairFlights
233221 ) ;
234222 [ currentHallwayInd , currentInd ] = this . getHallwayIndexAndIndexFromNode (
@@ -246,7 +234,9 @@ export class Building {
246234 }
247235 ) ;
248236 entranceWasStraight = ! isLeftOrRight (
249- ( < Room > this . hallways [ currentHallwayInd ] . partList [ prevInd ] ) . side
237+ ( this . hallways [ currentHallwayInd ] . partList [ prevInd ] as ForkableRoom <
238+ ForkName
239+ > ) . side
250240 ) ;
251241 [ currentHallwayInd , currentInd ] = [ hallwayInd , ind ] ;
252242 }
0 commit comments