-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.js
332 lines (254 loc) · 9.55 KB
/
sender.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/** @const */
var MSG_NAMESPACE = 'urn:x-cast:com.google.cast.demo.battleship';
var APP_ID = 'BDF10103';
window.castSender=null;
window.playerId="None";
window.DEBUG=false;
update = function () {
window.castSender.sendMessage($("#inputText").val());
};
CastSender = function() {
this.session=null;
};
setMessage = function( message ){
$("#textarea").append( message + "\n" );
// set textarea auto scroll to down
var textArea = $("#textarea");
textArea.scrollTop( textArea[0].scrollHeight - textArea.height() );
};
CastSender.prototype.sendMessageOnSuccess = function (message){
if(window.DEBUG) setMessage("Success Message sent: " + JSON.stringify(message));
if (message['command']== "join"){
setMessage("Game tip: Send the join message, wait another player!!");
}else if(message['command']== "setShipComplete"){
}else if(message['command']== "moveReply"){
}else{
}
};
CastSender.prototype.sendMessageOnError = function (message){
if(window.DEBUG) setMessage("Error Message sent: " + JSON.stringify(message));
if (message['command']== "join"){
setMessage("Game tip: Send the join message, but error on sending.");
}else if(message['command']== "setShipComplete"){
}else if(message['command']== "moveReply"){
}else{
}
};
CastSender.prototype.sendMessage = function(message){
if (this.session!=null) {
this.session.sendMessage(MSG_NAMESPACE, message, this.sendMessageOnSuccess(message), this.sendMessageOnError.bind(this));
}
else {
/*chrome.cast.requestSession(function(e) {
window.session_ = e;
window.session_.sendMessage(MSG_NAMESPACE, message, sendMessageOnSuccess, sendMessageOnError);
}, onError);*/
setMessage("Error mEssage sent: Session is null, please connect to reciever first.");
}
};
CastSender.prototype.sessionUpdateListener = function(isAlive) {
var message = isAlive ? 'Session Updated' : 'Session Removed';
message += ': ' + this.session.sessionId;
setMessage(message);
if (!isAlive) {
this.session = null;
}
};
CastSender.prototype.onReceiverMessage = function(namespace, messageString) {
if(window.DEBUG) setMessage("Got message: " + namespace + " " + messageString);
message=JSON.parse(messageString);
if (message.command == "joinReply"){
handleJoinReply(message);
}else if(message.command == "startSetShip"){
startSetShip();
}else if(message.command == "gameStart"){
prepareToStart();
}else if(message.command == "move"){
prepareToMove(message);
}else if(message.command == "moveResult" ){
showMoveResult(message);
}else if(message.command == "gameOver"){
handleGameOver(message);
}else{
setMessage("Unknow command: "+message);
}
};
CastSender.prototype.sessionListener = function(e) {
setMessage('New session ID: ' + e.sessionId);
// save it as a global variable
this.session = e;
e.addUpdateListener(this.sessionUpdateListener.bind(this));
e.addMessageListener(MSG_NAMESPACE, this.onReceiverMessage.bind(this));
};
CastSender.prototype.receiverListener = function(e) {
setMessage('receiver listener: ' + e);
};
CastSender.prototype.onInitSuccess = function() {
setMessage("onInitSuccess");
};
CastSender.prototype.onError = function(message) {
setMessage("onError: "+JSON.stringify(message));
};
CastSender.prototype.initializeCastApi = function() {
//if chrome.cast is not loaded yet, retry after 1 sec..
if (!chrome.cast || !chrome.cast.isAvailable) {
setTimeout(this.initializeCastApi.bind(this), 1000);
return;
}
// init the connection
var sessionRequest = new chrome.cast.SessionRequest(APP_ID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this),
this.receiverListener.bind(this));
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.onError.bind(this));
};
join = function(castSender){
command={'command': 'join',
'playerId': 'None'};
castSender.sendMessage(command);
refreshBoardAndUI();
};
handleJoinReply = function(replyMessage) {
// if (replyMessage['playerId']== '1'){
if (replyMessage.playerId == "1"){
window.playerId='1';
window.boardui.board1.setPlayerId("1");
window.boardui.board1.setIsOwner(true);
window.boardui.board1.setPlayerId("2");
window.boardui.board1.setIsOwner(false);
setMessage("Game tip: Your the the player 1");
//}else if(replyMessage['playerId']== '2'){
}else if (replyMessage.playerId == "2"){
window.playerId='2';
window.boardui.board2.setPlayerId("1");
window.boardui.board2.setIsOwner(false);
window.boardui.board2.setPlayerId("2");
window.boardui.board2.setIsOwner(true);
setMessage("Game tip: Your the the player 2");
}else{
setMessage("Game tip: unknow reply on JoinReply!!");
}
// after get join reply, sender wait receiver broadcast to ship setting phase
};
testAndSetShip= function(i,j){
var playerBoard=null;
if( window.playerId == '1'){
playerBoard=window.boardui.board1;
}else if(window.playerId == '2'){
playerBoard=window.boardui.board2;
}else{
setMessage("Unknow player id: " + window.playerId);
return false;
}
// check if ship is out of bound.
var currentShipAlreadySet=playerBoard.getCurrentShipCount();
if (currentShipAlreadySet >= window.MAXSHIPCOUNT){
setMessage("Ships already full. please wait!!");
return false;
}
//check select position is a valid position.
currentStatus=playerBoard.getBoardStatus(i,j);
if(currentStatus != BoardStatus.SEA){
setMessage("Not a valid position at ("+i+","+j+")");
return false;
}
// set position as ship, and change the icon.
playerBoard.placeShipOnBoard(i,j);
currentShipAlreadySet++;
playerBoard.setCurrentShipCount(currentShipAlreadySet);
window.boardui.changeIcon(window.playerId, i, j);
// if ships already reach the MAX, notify the receiver.
if (currentShipAlreadySet == window.MAXSHIPCOUNT){
command={'command':"setShipComplete",
'playerId': window.playerId,
'boardStatus': playerBoard.getBoardEntireStatus()};
window.castSender.sendMessage(command);
}
return true;
};
testThrowBomb = function(i, j){
// get competitor board
var competitorBoard=null;
var competitorId=null;
if (window.playerId == "1"){
competitorBoard=window.boardui.board2;
competitorId="2";
}else if(window.playerId == "2"){
competitorBoard=window.boardui.board1;
competitorId="1";
}
// test position is not bombed yet.
var currentStatus=competitorBoard.getBoardStatus(i, j);
if ( currentStatus != BoardStatus.SEA){
// In the player's vision, they don't know where the ship is.
// if this position status is unknown, that will be SEA.
alert("Not a valid position, please choose another!");
return false;
}
command=JSON.stringify({
'command': "moveReply",
'playerId': window.playerId,
'posI': i.toString(),
'posJ': j.toString()});
window.castSender.sendMessage(command);
// disable all click event for waiting another.
window.boardui.disableClickEvent(competitorId);
}
startSetShip = function(){
setMessage("Game tip: please set "+window.MAXSHIPCOUNT+" ships!");
window.boardui.combineUIAndSetShip(window.playerId);
};
prepareToStart = function(){
setMessage("Game tip: prepare to start the game.");
window.boardui.disableClickEvent(window.playerId);
};
prepareToMove = function(message){
if (message.playerId == window.playerId){
setMessage("Game tip: Your turn.");
var competitorPlayerId=null;
if (window.playerId == "1"){
competitorPlayerId="2";
}else if (window.playerId == "2"){
competitorPlayerId="1";
}
// you can throw bombs at competitor's board.
window.boardui.combineUIAndThrowBomb(competitorPlayerId);
}else{
setMessage("Game tip: player"+message.playerId+"'s turn, please wait.");
}
};
showMoveResult = function(message){
// get target board
var targetBoard = null;
if (message.targetBoard == "1"){
targetBoard=window.boardui.board1;
}else if(message.targetBoard == "2"){
targetBoard=window.boardui.board2;
}else{
setMessage("Unknow target board in showMoveResult.");
return false;
}
// update position status
targetBoard.setBoardStatus(message.posI, message.posJ, message.posResult);
window.boardui.changeIcon(message.targetBoard, message.posI, message.posJ);
};
handleGameOver = function(message){
// import final state of two board, because some status may not know by players.
window.boardui.board1.importBoard(message.player1);
window.boardui.board2.importBoard(message.player2);
window.boardui.showAllIcon();
setMessage("Game tip: Game End!!!");
if(message.win == window.playerId){
alert("You win!");
}else{
alert("You lose.");
}
};
refreshBoardAndUI= function(){
window.boardui.destroyUI();
// init game board UI
window.b1=new Board();
window.b2=new Board();
window.boardui=new BoardUI(b1, b2);
window.boardui.drawUI();
};