/
MultiPlayer.js
362 lines (328 loc) · 10.2 KB
/
MultiPlayer.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**********************************
Setup variables for keeping in sync with opponent
*************************************/
var friend_user_id = ""; //stores the gtalk id of the other player
var isReady = false; //set when this user has finished unit setup
var isFriendReady = false; //set if the other player has finished unit setup
var hasSentUnits = false;// set if this player has successfully sent all units to the other
var numReceivedUnits = 0; //stores the number of units received by the other player in setup
/*************************************************************
Message Variables
**************************************************************/
// Talk messages
var msgStart = "start";
var msgCancel = "cancel";
var msgAccept = "accept";
var msgDecline = "decline";
var msgCant = "cant";
var msgForfeit = "forfeit";
var msgNoMoves = "noMoves";
var msgReady = "ready";
var msgWon = "won";
var msgReject = "reject";
googleTalk.onReceiveTalkData = OnReceiveTalkData; //function for handling receiving of data
/*
Called when receiving messages from other players
*/
function OnReceiveTalkData(friend, data) {
//alert("message received "+data);
//switch(data){
if (data == msgStart) {
if (isPlaying){
try{
googleTalk.SendTalkData(friend.user_id, msgCant);
} catch(e){
//if could not warn the user, user is disconnected so nothign can be done
}
}
else if (confirm(friend.name + " " + strWantsToPlay)) {
isPlayer1 = false;
isSinglePlay = false;
isPlaying = true;
//boardArea.contentFlags = gddContentFlagHaveDetails | gddContentFlagManualLayout;
drawSetupPage();
//setPlayerTurn(1);
try{
friend_user_id = friend.user_id;
googleTalk.SendTalkData(friend.user_id, msgAccept);
}
catch(e){
alert(strConnectionError);
DrawFriends();
}
} else {
try{
googleTalk.SendTalkData(friend.user_id, msgDecline);
} catch(e){
//user could have disconnected before rejection, but nothing can be done
}
}
}
else if (data == msgAccept) {
//alert(friend_user_id + " "+friend.user_id);
// don't start a game if this user cancelled the request, but the friend said yes
if (friend_user_id == friend.user_id) {
alert(friend.name+" " + strAccepted);
isPlayer1 = true;
isPlaying = true;
drawSetupPage();
}
else { //User was to late to accept, the offer was cancelled
try{
googleTalk.SendTalkData(friend.user_id, msgReject);
}
catch(e){
//I guess the user already moved on if this happens, so dont worry about it
}
}
}
else if (data == msgReject){
//This happens when the user accepted, but the other user cancelled while waiting
//The setup page is pulled out beforehand, then the user is disappointend :(
//However, the user may have already cancelled, so we check for that
if (friend_user_id == friend.user_id){
AlertReject(friend.name);
//alert(strReject);
// DisplayMainMenu();
}
//otherwise, do nothing
}
else if (data == msgDecline) {
//This only matters if still waiting for that user's response
if (friend_user_id == friend.user_id) {
AlertDecline(friend.name);
//alert(friend.name + " " + strDecline);
// friend_user_id = "";
//DrawFriends();
//DisplayMainMenu();
}
}
else if (data == msgCant){
//This only matters if still waiting for that user's response
if (friend_user_id == friend.user_id) {
AlertCant(friend.name);
//alert(friend.name + " " + strCant);
// friend_user_id = "";
//DrawFriends();
//DisplayMainMenu();
}
}
else if (data == msgCancel) {
// friend cancelled game
//alert("msgCancel received");
if (friend_user_id == friend.user_id) {
AlertCancel(friend.name);
//alert(friend.name + " " + strFriendCancelled);
//DisplayMainMenu();
}
}
else if (data == msgForfeit) {
//alert("msgForfeit received");
// friend cancelled game
if (friend_user_id == friend.user_id) {
AlertEndGame(friend.name,strFriendForfeited);
/*alert(friend.name + " " + strFriendForfeited);
setGameOver(1);
DisplayEndBoard();*/
}
}
else if (data == msgReady){
//alert("msgReady received");
if (friend_user_id == friend.user_id) {
if(isReady){
//alert("we are both ready, time to send units");
SendMyUnits();
}
else{
//alert("but you are not ready");
//This means that the other user finished setup, and is waiting
isFriendReady = true;
lblScore.innertext += strFriendWait;
}
}
}
else if (data == msgWon) {
if (friend_user_id == friend.user_id) {
AlertEndGame(friend.name,strFriendWon);
/* setGameOver(1); //The game is over, no more turns
alert(friend.name + " " + strFriendWon);
//DisplayMainMenu();
DisplayEndBoard();*/
}
}
else if (data == msgNoMoves){
if (friend_user_id == friend.user_id) {
AlertEndGame(friend.name,strFriendCantMove);
/*setGameOver(1); //The game is over, no more turns
alert(friend.name +" " + strFriendCantMove);
DisplayEndBoard();*/
}
}
else {
//alert("numerical data: "+data);
if (data > 0){
// must be a movement
HandleOpponentMove(data);
changeTurn(); //turn is now own's to make
}
if (data < 0){
//friend his sending the location of his initial board items
HandleOpponentSetup(data);
}
}
};
/*
Upon receiving a message of a move, this is called
Make move on own board, after opponent made one
*/
function HandleOpponentMove(data){
var item1 = data % 100;
var item2 = Math.floor(data / 100);
var xOld = item1 % 10;
var yOld = Math.floor(item1 / 10);
var xNew = item2 % 10;
var yNew = Math.floor(item2 / 10);
computerMove(xOld, yOld, xNew, yNew); //In AI right now, but performs the move on "hisUnits"
};
/*
The opponent sent the location of one unit.
This lets the player know where to place that unit at setup
*/
function HandleOpponentSetup(data){
var unitNum = Math.floor((-data) / 100);
var x = ((-data) % 100) % 10;
var y = (Math.floor((-data) / 10)) % 10;
//alert(data+"new opponentunit:" +unitNum+" "+x+" "+y+" "+numReceivedUnits+" "+hasSentUnits);
board[x][y] = -unitNum - 1;
//alert("new board value set to "+ (-unitNum-1));
Units[1][unitNum].setSoldierLocation(x,y);
//alert("new board value set to "+ (-unitNum-1));
numReceivedUnits++;
CheckCanStart();
};
/*
Send messages to your friend, regarding the setup of your units
*/
function SendMyUnits(){
DisplaySendingUnits();
var errorCount = 0;
for (var i = 0; i < 40; i++){
try {
//message must include unit number, x location, and y location. must be negative to indicate the type of message
googleTalk.SendTalkData(friend_user_id, -(i*100 + Units[0][i].y*10 + Units[0][i].x));
} catch (e) {
if (errorCount > 10){
alert(strConnectionError);
DisplayMainMenu();
return;
}
errorCount++;
i--;
}
}
//all units have been sent. once all units are received, the game can start
hasSentUnits = true;
//checks if all units are sent and received, in which the game can start
CheckCanStart();
};
/*
Determines if all units have been sent to the other player,
and the other player has sent all units
if so, print the board to play the game
*/
function CheckCanStart(){
//alert("ccheck start: "+numReceivedUnits+" "+hasSentUnits);
if ((numReceivedUnits == 40)&&(hasSentUnits)){
//alert("time to draw");
boardArea.contentFlags = gddContentFlagHaveDetails | gddContentFlagManualLayout;
DrawBoard();
printScore();
TestStartPosition();
/*if (!TestCanMove()){
alert(strMineBlock);
}*/
}
};
function DisplaySendingUnits(){
boardArea.removeAllContentItems();
//boardArea.contentFlags = gddContentFlagHaveDetails | gddContentFlagManualLayout;
boardArea.contentFlags = gddContentFlagHaveDetails;
AddButtonAuto(strCancel, OnCancelToFriends);
AddLabelAuto(strSendingUnits);
};
function DisplayWaitForAccept(){
// add cancel button
boardArea.removeAllContentItems();
boardArea.contentFlags = gddContentFlagHaveDetails;
AddButtonAuto(strCancel, OnCancelToFriends);
AddLabelAuto(strWaitingForResponse);
};
/*************************************************************
Utilities
************************************************************/
/*
go through my units, and determine if a unit can move
returns true if at least one unit can move somewhere
returns false
*/
function CanMove(){
// go through each unit
for (var i = 0; i < 33; i++){
//if one unit can move, then the player can still move
if (CanMoveUnit(i)) return true;
}
//otherwise player cannot move
return false;
};
/*
takes a unit num, determines if that unit is able to move somewhere
*/
function CanMoveUnit(unitVal){
if (Units[0][unitVal].isAlive == 0) return false;
//check all 4 adjacent spots for this unit
if ((isValidSpot(Units[0][unitVal].x, Units[0][unitVal].y+1))&&(board[Units[0][unitVal].x][Units[0][unitVal].y+1]==0)) return true;
if ((isValidSpot(Units[0][unitVal].x, Units[0][unitVal].y-1))&&(board[Units[0][unitVal].x][Units[0][unitVal].y-1]==0)) return true;
if ((isValidSpot(Units[0][unitVal].x+1, Units[0][unitVal].y))&&(board[Units[0][unitVal].x+1][Units[0][unitVal].y]==0)) return true;
if ((isValidSpot(Units[0][unitVal].x-1, Units[0][unitVal].y))&&(board[Units[0][unitVal].x-1][Units[0][unitVal].y]==0)) return true;
//if it gets here, this unit cannot move
return false;
};
/*********************************************************************
Button Events
************************************************************************/
function OnMainClick(item){
DisplayMainMenu();
};
function OnStartMultiPlayer(item){
//alert("sending start data "+item.snippet);
friend_user_id = item.snippet;
try {
googleTalk.SendTalkData(friend_user_id, msgStart);
} catch (e) {
alert(strCantConnect);
//DisplayMainMenu();
}
DisplayWaitForAccept(); //Set the user to wait for an accept
//drawSetupPage();
};
/*
Called when a user has been selected
tries to connect to that user
*/
function OnStartMulti(item) {
// Start a multi-player game
//state = stateMultiPlayerWaiting;
friend_user_id = item.snippet;
isPlaying = true;
try {
googleTalk.SendTalkData(friend_user_id, msgStart);
} catch (e) {
alert(strCantConnect);
OnMainMenu();
}
};
function OnCancelToFriends(){
friend_user_id = "";
isPlaying = false;
DrawFriends();
}