-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastController.js
125 lines (109 loc) · 3.69 KB
/
CastController.js
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
/*
Chromecast Receiver app for Achtung die Kurve! for Chromecast
CastController.js takes care of the communication with the sender app
Copyright (C) 2018 Hans Harmannij
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const context = cast.framework.CastReceiverContext.getInstance();
const playerManager = context.getPlayerManager();
const CUSTOM_CHANNEL = 'urn:x-cast:nl.harmannij.achtungchromecast';
var currentMedia;
var loopMedia = true;
function sendMessage(message, senderId) {
console.log("Sending message '" + message + "' to sender " + senderId);
context.sendCustomMessage(CUSTOM_CHANNEL, senderId, message);
}
function loadMedia(media) {
if(media == currentMedia && playerManager.getPlayerState() == cast.framework.messages.PlayerState.PLAYING)
return;
currentMedia = media;
const loadRequestData = new cast.framework.messages.LoadRequestData();
loadRequestData.media = new cast.framework.messages.MediaInformation();
loadRequestData.media.contentId = media;
loadRequestData.autoplay = true;
playerManager.load(loadRequestData).then(
function() { console.log('Load succeed'); },
function(errorCode) { console.log('Error code: ' + errorCode); }
);
}
function reloadMedia() {
if(currentMedia)
loadMedia(currentMedia);
}
function runController(players) {
context.addCustomMessageListener(CUSTOM_CHANNEL, function(customEvent) {
if(customEvent.data == "Ready") {
for(var i in waitingRoom) {
if(waitingRoom[i].senderId == customEvent.senderId) {
playerReady(waitingRoom[i]);
}
}
}
else if(customEvent.data == "Left") {
for(var i in players){
if(players[i].senderId == customEvent.senderId){
players[i].steering = -1;
}
}
}
else if(customEvent.data == "Right") {
for(var i in players){
if(players[i].senderId == customEvent.senderId){
players[i].steering = 1;
}
} }
else if(customEvent.data == "Released") {
for(var i in players){
if(players[i].senderId == customEvent.senderId){
players[i].steering = 0;
}
}
}
else {
console.log(customEvent);
}
});
context.addEventListener(cast.framework.system.EventType.SENDER_CONNECTED, function(event){
console.log(event);
var player = registerPlayer();
if(player) {
player.senderId = event.senderId;
setTimeout(function(){
sendMessage(gameRunning ? "GameStarted" : "WaitingRoom", event.senderId);
sendMessage({"name": player.name, "color": player.color}, event.senderId);
}, 500);
}
});
context.addEventListener(cast.framework.system.EventType.SENDER_DISCONNECTED, function(event){
console.log(event);
for(var i in waitingRoom) {
if(waitingRoom[i].senderId == event.senderId) {
unregisterPlayer(waitingRoom[i]);
}
}
});
context.start();
playerManager.addEventListener(cast.framework.events.EventType.MEDIA_FINISHED, function(event){
console.log(event);
if(event.endedReason == cast.framework.events.EndedReason.END_OF_STREAM //Don't endlessly retry on error
&& gameRunning && loopMedia)
reloadMedia();
})
loadMedia("dance.mp3");
}
function onGameStart() {
loadMedia("dance.mp3");
sendMessage("GameStarted");
}
function onGameEnd() {
sendMessage("WaitingRoom");
}