Skip to content

Commit 30d7b83

Browse files
authored
Play collection (#1765)
if a person requests to play a track in a playlist or album, continue playing from that track
1 parent 2f8f0b2 commit 30d7b83

File tree

2 files changed

+121
-26
lines changed

2 files changed

+121
-26
lines changed

ts/packages/agents/player/src/agent/playerGrammar.agr

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,69 @@
3737
| two -> 2
3838
| three -> 3
3939
| four -> 4
40+
| five -> 5
41+
| six -> 6
42+
| seven -> 7
43+
| eight -> 8
44+
| nine -> 9
45+
| ten -> 10
46+
| eleven -> 11
47+
| twelve -> 12
48+
| thirteen -> 13
49+
| fourteen -> 14
50+
| fifteen -> 15
51+
| sixteen -> 16
52+
| seventeen -> 17
53+
| eighteen -> 18
54+
| nineteen -> 19
55+
| twenty -> 20
56+
| twenty\-one -> 21
57+
| twenty\-two -> 22
58+
| twenty\-three -> 23
59+
| twenty\-four -> 24
60+
| twenty\-five -> 25
61+
| twenty\-six -> 26
62+
| twenty\-seven -> 27
63+
| twenty\-eight -> 28
64+
| twenty\-nine -> 29
65+
| thirty -> 30
4066

4167

4268
@ <Ordinal> =
4369
first -> 1
4470
| second -> 2
4571
| third -> 3
46-
| troisiemme -> 3
4772
| fourth -> 4
73+
| fifth -> 5
74+
| sixth -> 6
75+
| seventh -> 7
76+
| eighth -> 8
77+
| ninth -> 9
78+
| tenth -> 10
79+
| eleventh -> 11
80+
| twelfth -> 12
81+
| thirteenth -> 13
82+
| fourteenth -> 14
83+
| fifteenth -> 15
84+
| sixteenth -> 16
85+
| seventeenth -> 17
86+
| eighteenth -> 18
87+
| nineteenth -> 19
88+
| twentieth -> 20
89+
| twenty\-first -> 21
90+
| twenty\-second -> 22
91+
| twenty\-third -> 23
92+
| twenty\-fourth -> 24
93+
| twenty\-fifth -> 25
94+
| twenty\-sixth -> 26
95+
| twenty\-seventh -> 27
96+
| twenty\-eighth -> 28
97+
| twenty\-ninth -> 29
98+
| thirtieth -> 30
4899

49100
@ <TrackPhrase> = ((the)? <TrackTerm>)? $(trackName:<TrackName>)
50101
| $(trackName:<TrackName>) <TrackTerm>
51-
@ <PlaySpecificTrack> = play $(trackName:<TrackPhrase>) -> { actionName: "playTrack", parameters: { trackName: $(trackName) } }
52-
| play $(trackName:<TrackPhrase>) by $(artist:<ArtistName>) -> { actionName: "playTrack", parameters: { trackName: $(trackName), artists: [$(artist)] } }
102+
@ <PlaySpecificTrack> = play $(trackName:<TrackPhrase>) by $(artist:<ArtistName>) -> { actionName: "playTrack", parameters: { trackName: $(trackName), artists: [$(artist)] } }
53103
| play $(trackName:<TrackPhrase>) from (the)? album $(albumName:<AlbumName>) -> { actionName: "playTrack", parameters: { trackName: $(trackName), albumName: $(albumName) } }
54104
| play $(trackName:<TrackPhrase>) by $(artist:<ArtistName>) from (the)? album $(albumName:<AlbumName>) -> { actionName: "playTrack", parameters: { trackName: $(trackName), artists: [$(artist)], albumName: $(albumName) } }
55105

ts/packages/agents/player/src/client.ts

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,23 @@ function updatePlaylistList(
397397
context.currentPlaylistList = playlists;
398398
}
399399

400+
function updateTrackList(
401+
collection: ITrackCollection,
402+
context: IClientContext,
403+
) {
404+
context.currentTrackList = collection;
405+
context.lastTrackStartIndex = 0;
406+
context.lastTrackEndIndex = collection.getTrackCount();
407+
408+
context.currentTrackList = collection;
409+
}
410+
400411
async function updateTrackListAndPrint(
401412
collection: ITrackCollection,
402413
clientContext: IClientContext,
403414
) {
404415
await printTrackNames(collection, clientContext);
405-
clientContext.currentTrackList = collection;
406-
clientContext.lastTrackStartIndex = 0;
407-
clientContext.lastTrackEndIndex = collection.getTrackCount();
416+
updateTrackList(collection, clientContext);
408417
}
409418

410419
export async function getClientContext(
@@ -506,30 +515,62 @@ export async function searchForPlaylists(
506515
async function playTrackCollection(
507516
trackCollection: ITrackCollection,
508517
clientContext: IClientContext,
509-
trackIndex = 0,
518+
trackIndex: number | undefined = undefined,
510519
) {
511520
const deviceId = await ensureSelectedDeviceId(clientContext);
512521
const tracks = trackCollection.getTracks();
513522
const playContext = trackCollection.getContext();
514-
const singleTrackCollection = new TrackCollection([tracks[trackIndex]]);
515-
const actionResult = await htmlTrackNames(
516-
singleTrackCollection,
517-
"Now playing",
518-
);
519-
if (playContext === undefined) {
520-
const singleTracks = singleTrackCollection.getTracks();
521-
const uris = singleTracks.map((track) => track.uri);
522-
await play(clientContext.service, deviceId, uris);
523-
} else {
524-
await play(
525-
clientContext.service,
526-
deviceId,
527-
undefined,
528-
playContext,
529-
trackIndex,
523+
if (
524+
trackIndex !== undefined &&
525+
(trackIndex < 0 || trackIndex >= tracks.length)
526+
) {
527+
return createErrorActionResult(
528+
`Track index ${trackIndex} out of range for track collection of size ${tracks.length}`,
530529
);
530+
} else {
531+
if (trackIndex !== undefined) {
532+
if (playContext === undefined) {
533+
const singleTrackCollection = new TrackCollection([
534+
tracks[trackIndex],
535+
]);
536+
const actionResult = await htmlTrackNames(
537+
singleTrackCollection,
538+
"Now playing",
539+
);
540+
const singleTracks = singleTrackCollection.getTracks();
541+
const uris = singleTracks.map((track) => track.uri);
542+
await play(clientContext.service, deviceId, uris);
543+
return actionResult;
544+
} else {
545+
const trackCollectionFromTrackNumber = new TrackCollection(
546+
tracks.slice(trackIndex),
547+
playContext,
548+
);
549+
const actionResult = await htmlTrackNames(
550+
trackCollectionFromTrackNumber,
551+
"Now playing",
552+
);
553+
updateTrackList(trackCollectionFromTrackNumber, clientContext);
554+
await play(
555+
clientContext.service,
556+
deviceId,
557+
undefined,
558+
playContext,
559+
trackIndex,
560+
);
561+
return actionResult;
562+
}
563+
} else {
564+
const actionResult = await htmlTrackNames(
565+
trackCollection,
566+
"Now playing",
567+
);
568+
updateTrackList(trackCollection, clientContext);
569+
const uris = tracks.map((track) => track.uri);
570+
await play(clientContext.service, deviceId, uris, playContext);
571+
return actionResult;
572+
}
531573
}
532-
return actionResult;
533574
}
534575

535576
function randomShuffle<T>(array: T[]) {
@@ -551,8 +592,12 @@ async function playRandomAction(
551592
if (quantity > 0) {
552593
savedTracks.splice(quantity);
553594
}
554-
555-
const tracks = randomShuffle(savedTracks.map((track) => track.track));
595+
const rawTracks = savedTracks.map((track) => track.track);
596+
const uniqueTracks = new Map<string, SpotifyApi.TrackObjectFull>();
597+
for (const track of rawTracks) {
598+
uniqueTracks.set(track.id, track);
599+
}
600+
const tracks = randomShuffle(Array.from(uniqueTracks.values()));
556601
const collection = new TrackCollection(tracks);
557602
return playTrackCollection(collection, clientContext);
558603
}

0 commit comments

Comments
 (0)