-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolar-program.ts
154 lines (137 loc) · 6.17 KB
/
polar-program.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { IGPSState } from './gps';
import { withStateAsync } from './state';
import { Player } from './player';
import { debugLog } from './debug';
import * as dateFns from 'date-fns';
async function waitForMoving(getState: () => Promise<IGPSState>, moving: boolean) {
return await withStateAsync({ waitForMoving: moving }, async () => {
while (true) {
const state = await getState();
if (state.moving == moving) return state;
}
});
}
async function waitForTrigger(getState: () => Promise<IGPSState>, trigger: string) {
return await withStateAsync({ waitForTrigger: trigger }, async () => {
while (true) {
const state = await getState();
if (state.triggerDistances[trigger] <= 0) return state;
}
});
}
async function waitForTime(getState: () => Promise<IGPSState>, target: Date) {
debugLog('waitForTime', target);
return await withStateAsync({ waitForTime: target }, async () => {
let state = await getState();
while (dateFns.isBefore(state.time, target)) {
state = await getState();
}
return state;
});
}
const boardingAnnouncements: [number, string][] = [
[20, 'music/b29 - 29,59 (20m) boarding countdown.wav'],
[15, 'music/b34 - 34,04 (15m) boarding countdown.wav'],
[10, 'music/b39 - 39,09 (10m) boarding countdown.wav'],
[5, 'music/b44 - 44,14 (5m) boarding countdown.wav'],
[3, 'music/b46 - 46,16 (3m) boarding countdown.wav'],
[2, 'music/b47 - 47,17 (2m) boarding countdown.wav'],
[1, 'music/b48 - 48,18 (1m) boarding countdown.wav'],
];
const boardingSongs: [number, string][] = [
[253, 'music/c01 - When Christmas Comes to Town.wav'],
[159, 'music/c02 - Spirit of the Season.wav'],
[232, 'music/c03 - Seeing is Believing.wav'],
];
export async function polarProgram(
startTime: Date,
getState: () => Promise<IGPSState>,
play: (fileNames: string[], volume?: number) => Player,
studio: string,
) {
await withStateAsync({ startTime, studio }, async () => {
debugLog('polarProgram', startTime, studio);
let state = await getState();
debugLog('post getstate', state);
const startDate = startTime;
const boardingStartDate = dateFns.sub(startDate, { minutes: 11 });
debugLog('boardingStartDate', boardingStartDate);
for (let [minutesBefore, file] of boardingAnnouncements) {
const announcementDate = dateFns.sub(boardingStartDate, {
minutes: minutesBefore,
});
if (dateFns.isAfter(state.time, announcementDate)) {
continue;
}
debugLog('announcementDate', announcementDate);
state = await waitForTime(getState, announcementDate);
play([file]);
}
let adjustedStartDate = boardingStartDate;
let boardingSongsToPlay = [];
for (let [duration, file] of boardingSongs) {
if (dateFns.isAfter(state.time, adjustedStartDate)) {
adjustedStartDate = dateFns.add(adjustedStartDate, { seconds: duration });
} else {
boardingSongsToPlay.push(file);
}
}
debugLog(startDate, adjustedStartDate);
if (dateFns.isAfter(state.time, dateFns.add(startDate, { minutes: 5 }))) {
// Too late, go to next show
return;
}
await waitForTime(getState, adjustedStartDate);
let playing = play([
...boardingSongsToPlay,
'music/c04 - The Polar Express.wav',
'music/c04a - Good evening and welcome aboard.wav',
'music/c05 - Hot Chocolate (1).wav',
'music/c05a - I dont think everybody has had hot chocolate yet.wav',
'music/c05b - 5s pause.wav',
'music/c06 - Hot Chocolate (2).wav',
'music/c06a - And now lets hear the story of the Polar Express.wav',
'music/c07 - (Book Reading Short).wav',
]);
await waitForTrigger(getState, 'wolvesTriggerFrontCars');
play(['music/a07Fa - Hungry wolves.wav', 'music/a07Fb - Glacier Gulch.wav'], 100);
await waitForTrigger(getState, 'wolvesTriggerRearCars');
play(['music/a07Ra - Hungry wolves.wav', 'music/a07Rb - Glacier Gulch.wav'], 100);
await playing.wait();
await waitForTrigger(getState, 'santaMusicStart');
playing = play(['music/c08 - Santa Claus is Coming to Town.wav']);
await waitForTrigger(getState, 'npTriggerFrontCars');
play(['music/a08Fa - As we round the bend (North Pole).wav'], 100);
await waitForTrigger(getState, 'santaTriggerFrontCars');
play(['music/a08Fb - The elves are out this evening.wav'], 100);
await waitForTrigger(getState, 'npTriggerRearCars');
play(['music/a08Ra - As we round the bend (North Pole).wav'], 100);
await waitForTrigger(getState, 'santaTriggerRearCars');
play(['music/a08Rb - The elves are out this evening.wav'], 100);
await playing.wait();
await waitForMoving(getState, false);
await play(['music/c08a - Brief station stop.wav']).wait();
await waitForMoving(getState, true);
await waitForTrigger(getState, 'npMusicStart');
await play([
'music/c09 - Believe (1).wav',
'music/c10 - We Wish You a Merry Christmas (1).wav',
]).wait();
await waitForMoving(getState, false);
await play(['music/c10a - Thousands of caribou.wav']).wait();
await waitForMoving(getState, true);
await play([
'music/c11 - Rudolph the Red-Nosed Reindeer.wav',
'music/c12 - Frosty the Snowman.wav',
'music/c13 - Believe (2).wav',
'music/c14 - We Wish You a Merry Christmas (2).wav',
'music/c14a - Please join us in singing.wav',
'music/c15 - Twelve Days of Christmas.wav',
'music/c16 - Holly Jolly Christmas.wav',
'music/c16a - And now for our final number.wav',
"music/c17 - Rockin' Around the Christmas Tree.wav",
`music/c17a - (${studio}) Closing comments.wav`,
'music/c18 - Suite from The Polar Express.wav',
]).wait();
});
}