social_stream-presence 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -223,7 +223,7 @@
223
223
  .hover(function() {uiChatboxTitlebarGames.addClass('ui-state-hover');},
224
224
  function() {uiChatboxTitlebarGames.removeClass('ui-state-hover');})
225
225
  .click(function(event) {
226
- pickGamesButton(self)
226
+ PRESENCE.GAME.INTERFACE.pickGamesButton(self)
227
227
  return false;
228
228
  })
229
229
  .appendTo(uiChatboxTitlebar),
@@ -1,4 +1,19 @@
1
1
  var PRESENCE = PRESENCE || {};
2
2
 
3
3
  PRESENCE.VERSION = '<%=SocialStream::Presence::VERSION%>';
4
- PRESENCE.AUTHORS = 'Aldo Gordillo';
4
+ PRESENCE.AUTHORS = 'Aldo Gordillo';
5
+
6
+
7
+ PRESENCE.CORE = (function(P,$,undefined){
8
+
9
+ var init = function(){
10
+ initFocusListeners();
11
+ checkVideocallFeature();
12
+ PRESENCE.GAME.init(strophe_connection);
13
+ initAudio();
14
+ };
15
+
16
+ return {
17
+ init: init
18
+ };
19
+ }) (PRESENCE, jQuery);
@@ -53,6 +53,12 @@ function chatContact(domain,resource,client,version) {
53
53
  this.guest_token=null;
54
54
  this.session=null;
55
55
  this.publisher=null;
56
+
57
+ //Game params
58
+ //Sender: disconnected, waiting, playing
59
+ //Receiver: disconnected , pending , playing
60
+ this.gameStatus="disconnected";
61
+ this.game=null;
56
62
  }
57
63
 
58
64
  //Rooms information
@@ -387,6 +393,7 @@ function processUnavailablePresenceStanza(presence){
387
393
  if (slug != user_slug) {
388
394
  if (getConnectionBoxFromSlug(slug)!=null){
389
395
  updateInterfaceAfterPresenceStanza(slug,false)
396
+ PRESENCE.GAME.userDisconnected(slug)
390
397
  }
391
398
  }
392
399
  }
@@ -426,7 +433,7 @@ function onIQStanza(iq){
426
433
  var type = iq.getAttribute("type");
427
434
  var from = iq.getAttribute("from");
428
435
  var slug = from.split("@")[0];
429
-
436
+
430
437
  if (slug == user_slug) {
431
438
  return true;
432
439
  }
@@ -577,12 +584,11 @@ function handleIQResultFromVideochatRequestCancel(iq,slug){
577
584
  var response = queryElement.getElementsByTagName('response');
578
585
 
579
586
  if(response.length == 1){
580
- receiveVideoChatCancelationFromUser(slug,$(response).text());
587
+ receiveVideoChatCancelationFromUser(slug);
581
588
  return true;
582
589
  }
583
590
  }
584
591
 
585
- receiveVideoChatCancelationFromUser(slug,"Bad response");
586
592
  return true;
587
593
  }
588
594
 
@@ -764,6 +770,7 @@ function handleIQResultFromMucOccupantsRequest(iq){
764
770
  var nick = $(value).attr("name");
765
771
  if(roomsInfo[room_slug].occupants.indexOf(nick)==-1){
766
772
  roomsInfo[room_slug].occupants.push(nick)
773
+ addNickToNotificationInGroup(room_slug,nick)
767
774
  }
768
775
  });
769
776
  }
@@ -3,74 +3,309 @@
3
3
  ////////////////////
4
4
 
5
5
  PRESENCE.GAME = (function(P,$,undefined){
6
-
7
- //Gamecore variables
8
- var connection = null;
9
- iqStanzaID['gameRequest'] = "gameRequestID";
10
6
 
11
- var init = function(myConnection){
12
- connection = myconnection;
13
- };
7
+ /////////////////////////////////
8
+ // GAME CORE VARS //
9
+ /////////////////////////////////
10
+
11
+ //Manage Game information
12
+ //gameID == Game to play
13
+ //Players == List of players
14
+ //Options == Game options
15
+ function game(gameId,gameName,players,options){
16
+ this.id = gameId;
17
+ this.name = gameName;
18
+ this.players = players;
19
+ this.options = options;
20
+ this.handler = null;
21
+ }
14
22
 
23
+ function player(id,name){
24
+ this.id = id;
25
+ this.name = name;
26
+ }
27
+
28
+ function validationResult(pass,reason){
29
+ this.pass = pass;
30
+ this.reason = reason;
31
+ }
32
+
33
+ var createValidationResult = function (pass,reason){
34
+ return new validationResult(pass,reason);
35
+ }
15
36
 
16
- var sendIQStanzaToRequestGame = function (slug,players,options){
17
-
18
- if (slug in contactsInfo) {
19
- var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
20
-
21
- var iq = $iq({to: jid, type: "get", id: iqStanzaID['gameRequest']})
22
- .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
23
- .c("game", {name: "gameName"})
24
- .c("players", {})
25
- .c("player1").t("player1value").up()
26
- .c("player2").t("player2value").up()
27
- .c("options", {})
28
- .c("options1").t("options1value").up()
29
- .c("options2").t("options2value").up()
30
- strophe_connection.sendIQ(iq);
31
- }
32
- };
33
-
34
- //Getters
35
- var getDummy = function(){
36
- return param;
37
- };
38
-
39
37
 
40
- return {
41
- init: init,
42
- sendIQStanzaToRequestGame: sendIQStanzaToRequestGame,
43
- getDummy: getDummy
38
+ /////////////////////////////////
39
+ // CORE METHODS //
40
+ /////////////////////////////////
41
+
42
+ var init = function(myConnection){
43
+ P.GAME.COMUNICATION.init(myConnection);
44
44
  };
45
+
45
46
 
46
- }) (PRESENCE, jQuery);
47
-
48
-
47
+ var requestUserToPlay = function (slug,game){
48
+ if (slug in contactsInfo) {
49
+ var gameStatus = contactsInfo[slug].gameStatus;
50
+ } else {
51
+ var gameStatus = null;
52
+ }
53
+
54
+ if(! isUserConnected()){
55
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug, I18n.t("chat.game.offline"));
56
+ if(gameStatus!=null){
57
+ contactsInfo[slug].gameStatus = "disconnected";
58
+ }
59
+ return;
60
+ }
49
61
 
62
+ if(! isSlugChatConnected(slug)){
63
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug, I18n.t("chat.game.guestOffline", {name: getNameFromSlug(slug)}));
64
+ if(gameStatus!=null){
65
+ contactsInfo[slug].gameStatus = "disconnected";
66
+ }
67
+ return;
68
+ }
69
+
70
+ if(gameStatus==null){
71
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,I18n.t('chat.game.unable'));
72
+ return;
73
+ }
74
+
75
+ if(gameStatus!="disconnected"){
76
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,"Esta ocupado en otro estado!=desconectado...")
77
+ return;
78
+ }
50
79
 
80
+ //gameStatus=="disconnected"
81
+ //Start connection
82
+ contactsInfo[slug].gameStatus="waiting";
83
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,I18n.t('chat.game.waiting'));
51
84
 
85
+ contactsInfo[slug].game = game
86
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
87
+ return P.GAME.COMUNICATION.sendIQStanzaToRequestGame(jid,game);
88
+ }
89
+
90
+ var responseUserToPlay = function (slug,result){
91
+ if (! slug in contactsInfo) {
92
+ return;
93
+ }
94
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
95
+ P.GAME.COMUNICATION.sendIQStanzaToResponseGameRequest(jid,result)
96
+ if(result=="yes"){
97
+ contactsInfo[slug].gameStatus="playing";
98
+ PRESENCE.GAME.INTERFACE.updateInterfaceBeforeStartGame(slug);
99
+ } else {
100
+ contactsInfo[slug].gameStatus="disconnected";
101
+ PRESENCE.GAME.INTERFACE.updateInterfaceAfterFinishGame(slug);
102
+ }
103
+ }
52
104
 
105
+ var sendAction = function(gameId,players,action){
106
+ $.each(players, function(index, value) {
107
+ var slug = value.id;
108
+ if(slug!=user_slug){
109
+ if(slug in contactsInfo){
110
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
111
+ P.GAME.COMUNICATION.sendIQStanzaWithAction(jid,gameId,action);
112
+ }
113
+ }
114
+ });
115
+ }
53
116
 
54
117
 
118
+ var updateLogicAfterReceivedGameRequest = function(slug,gameId,gameName,players,options){
119
+ if(! slug in contactsInfo){
120
+ return;
121
+ }
122
+ contactsInfo[slug].game = new game(gameId,gameName,players,options)
123
+ if (contactsInfo[slug].gameStatus!="disconnected"){
124
+ P.GAME.COMUNICATION.sendIQStanzaToResponseGameRequest(slug,"busy");
125
+ return true;
126
+ } else {
127
+ contactsInfo[slug].gameStatus="pending";
128
+ }
129
+ P.GAME.INTERFACE.updateInterfaceAfterGameRequestReceived(slug)
130
+ }
55
131
 
132
+ var updateLogicAfterReceivedGameRequestResponse = function(slug,response){
133
+ if(contactsInfo[slug].gameStatus!="waiting"){
134
+ return true;
135
+ }
136
+ if(response=="yes"){
137
+ contactsInfo[slug].gameStatus = "playing";
138
+ P.GAME.INTERFACE.updateInterfaceBeforeStartGame(slug)
139
+ } else if (response=="no"){
140
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,I18n.t("chat.game.rejected", {name: getNameFromSlug(slug)}));
141
+ contactsInfo[slug].gameStatus="disconnected";
142
+ } else if (response=="busy"){
143
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,I18n.t("chat.game.rejectedBusy", {name: getNameFromSlug(slug)}));
144
+ contactsInfo[slug].gameStatus="disconnected";
145
+ } else {
146
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,I18n.t("chat.game.unknown", {name: getNameFromSlug(slug)}));
147
+ contactsInfo[slug].gameStatus="disconnected";
148
+ }
149
+ }
150
+
151
+ var updateLogicAfterReceivedGameRequestCancelation = function(slug,status){
152
+ if(slug in contactsInfo){
153
+ if (status == "finish") {
154
+ if (contactsInfo[slug].gameStatus == "pending") {
155
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug, I18n.t("chat.game.cancel", {name: getNameFromSlug(slug)}));
156
+ contactsInfo[slug].gameStatus = "disconnected";
157
+ } else if (contactsInfo[slug].gameStatus == "playing") {
158
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug, I18n.t("chat.game.finish", {name: getNameFromSlug(slug)}));
159
+ contactsInfo[slug].gameStatus = "disconnected";
160
+ }
161
+ }
162
+ } else {
163
+ finishGame(slug);
164
+ }
165
+ }
56
166
 
167
+ var getPlayerNameWithId = function(slug){
168
+ if(! slug in contactsInfo){
169
+ var game = contactsInfo[slug].game
170
+ if((game!=null)&&(game.players!=null)){
171
+ $.each(game.players, function(index, value) {
172
+ if(value[0]==slug){
173
+ return value[1]
174
+ }
175
+ });
176
+ }
177
+ }
178
+ return null
179
+ }
57
180
 
181
+ //Validation functions
182
+ var validateParams = function(myPlayer,game,myDivID){
183
+ var result;
184
+
185
+ result = checkGameObject(game);
186
+ if(! result[0] ){
187
+ return result;
188
+ }
189
+
190
+ if ((typeof myPlayer=="undefined")||(typeof myDivID=="undefined")){
191
+ return new validationResult(false,"myPlayer or myDivID undefined");
192
+ }
58
193
 
194
+ //Check if myPlayers is an Array
195
+ if (game.players.constructor.toString().indexOf("Array") == -1){
196
+ return new validationResult(false,"Game.Players is not an array");
197
+ }
59
198
 
199
+ //Check if myOptions is an Array
200
+ if (game.options.constructor.toString().indexOf("Array") == -1){
201
+ return new validationResult(false,"Game.options is not an array");
202
+ }
203
+
204
+ //Check that myPlayer is contained on myPlayers
205
+ if(game.players.indexOf(myPlayer)==-1){
206
+ return new validationResult(false,"myPlayer is not contained in players array");
207
+ }
60
208
 
209
+ return new validationResult(true,"Ok");
210
+ }
211
+
61
212
 
62
- ////////////////////
63
- //Interface Events
64
- ////////////////////
65
- var global;
213
+ var checkGameObject = function(game){
214
+ if((typeof game=="undefined")||(typeof game.id=="undefined")||(typeof game.name=="undefined")
215
+ ||(typeof game.players=="undefined")||(typeof game.options=="undefined")){
216
+ return new validationResult(false,"Some game field (game/game.id/game.name/game.players/game.options) is undefined");
217
+ }
218
+
219
+ if((game.id=="")||(game.name=="")){
220
+ return new validationResult(false,"game.id or game.name is empty");
221
+ }
222
+
223
+ if(game.id.split(" ").length > 2){
224
+ return new validationResult(false,"Invalid game.id. No whitespaces allowd in this field");
225
+ }
226
+
227
+ return new validationResult(true,"Ok");
228
+ }
229
+
230
+ var startGame = function (slug,divID){
231
+ var game = contactsInfo[slug].game;
232
+ var myplayer = new player(user_slug,getPlayerNameWithId(user_slug))
233
+ var validationResult = validateParams(myplayer,game,divID)
234
+
235
+ if(! validationResult.pass){
236
+ var msg = "StartPlay error with reason: " + validationResult.reason;
237
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,msg);
238
+ return;
239
+ }
240
+
241
+ if(typeof PRESENCE.GAME[game.id].validateParams == "function"){
242
+ validationResult = PRESENCE.GAME[game.id].validateParams(myplayer,game,divID)
243
+ if(! validationResult.pass){
244
+ var msg = "StartPlay error with reason: " + validationResult.reason;
245
+ P.GAME.INTERFACE.updateInterfaceOnInformationMessage(slug,msg);
246
+ return;
247
+ }
248
+ } else {
249
+ log("Warning: Missed " + game.name + " validateParams method")
250
+ }
251
+
252
+ if(typeof PRESENCE.GAME[game.id].minimumRequirements == "function"){
253
+ //Check requirements
254
+ } else {
255
+ log("Warning: Missed " + game.name + " minRequirements method")
256
+ }
257
+
258
+ //Add handler and remove previous if exits
259
+ if(game.handler!=null){
260
+ PRESENCE.GAME.COMUNICATION.removeHandler(game.handler)
261
+ }
262
+ game.handler = PRESENCE.GAME.COMUNICATION.addHandler(game.id)
263
+
264
+ //Call PRESENCE.GAME.gameId
265
+ var myplayer = new player(user_slug,user_name)
266
+ PRESENCE.GAME[game.id].init(myplayer,game,divID)
267
+ }
268
+
269
+ var finishGame = function (slug){
270
+ if(slug in contactsInfo){
271
+ var gameStatus = contactsInfo[slug].gameStatus;
272
+ if(gameStatus=="playing"){
273
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
274
+ P.GAME.COMUNICATION.sendIQStanzaToFinishGame(jid);
275
+ if(contactsInfo[slug].game.handler!=null){
276
+ P.GAME.COMUNICATION.removeHandler(contactsInfo[slug].game.handler);
277
+ contactsInfo[slug].game.handler==null;
278
+ }
279
+ } else if(gameStatus=="waiting"){
280
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
281
+ P.GAME.COMUNICATION.sendIQStanzaToFinishGame(jid);
282
+ } else if(gameStatus=="pending"){
283
+ P.GAME.COMUNICATION.sendIQStanzaToResponseGameRequest(slug,"no");
284
+ }
285
+ contactsInfo[slug].gameStatus="disconnected";
286
+ }
287
+
288
+ PRESENCE.GAME.INTERFACE.updateInterfaceAfterFinishGame(slug)
289
+ }
66
290
 
67
- function play(){
68
- $(".chat-gamesthick").css("display","block");
69
- }
291
+ var userDisconnected = function(slug){
292
+ finishGame(slug)
293
+ }
70
294
 
71
- function pickGamesButton(uiElement){
72
- var slug = $(uiElement.element).attr("id");
73
- //clickGamesButton(slug);
74
- log("clickGamesButton(slug)" + slug)
75
- }
295
+ return {
296
+ init: init,
297
+ game: game,
298
+ player: player,
299
+ startGame: startGame,
300
+ finishGame: finishGame,
301
+ createValidationResult: createValidationResult,
302
+ requestUserToPlay: requestUserToPlay,
303
+ responseUserToPlay: responseUserToPlay,
304
+ updateLogicAfterReceivedGameRequest: updateLogicAfterReceivedGameRequest,
305
+ updateLogicAfterReceivedGameRequestCancelation: updateLogicAfterReceivedGameRequestCancelation,
306
+ updateLogicAfterReceivedGameRequestResponse: updateLogicAfterReceivedGameRequestResponse,
307
+ sendAction: sendAction,
308
+ userDisconnected: userDisconnected
309
+ };
76
310
 
311
+ }) (PRESENCE, jQuery);
@@ -0,0 +1,313 @@
1
+ ////////////////////
2
+ //Multiplayer Games Core based on XMPP
3
+ //Comunication Module
4
+ ////////////////////
5
+
6
+ PRESENCE.GAME.COMUNICATION = (function(P,$,undefined){
7
+
8
+ //////////////////////////////////////////////
9
+ // GAME COMUNICATION VARIABLES //
10
+ //////////////////////////////////////////////
11
+
12
+ var connection = null;
13
+ iqStanzaID['gameRequest'] = "gameRequestID";
14
+ iqStanzaID['gameRequestFinish'] = "gameRequestFinishID";
15
+ iqStanzaID['gameAction'] = "gameActionID";
16
+
17
+
18
+ /////////////////////////////////
19
+ // COMUNICATION METHODS //
20
+ /////////////////////////////////
21
+
22
+ var init = function(myConnection){
23
+ connection = myConnection;
24
+ //addHandler:(callback, namespace to match, stanza name, stanza type, stanza id , stanza from, options)
25
+ connection.addHandler(handleRequestGameIQStanza,null, "iq", "get",iqStanzaID['gameRequest'], null);
26
+ connection.addHandler(handleIQResultFromGameRequest,null, "iq", "result",iqStanzaID['gameRequest'], null);
27
+ connection.addHandler(handleIQResultFromGameRequestFinish,null, "iq", "result", iqStanzaID['gameRequestFinish'], null);
28
+ };
29
+
30
+
31
+
32
+ /////////////////////
33
+ // MANAGE GAME REQUESTS
34
+ /////////////////////
35
+
36
+ //SEND GAME REQUEST
37
+ //Jid == Game Room or User to send the request game stanza
38
+ //Game == Game to play with configuration
39
+ var sendIQStanzaToRequestGame = function (jid,game){
40
+ var optionIndex=0;
41
+ var optionsSize=Object.keys(game.options).length;
42
+
43
+ var iq = $iq({to: jid, type: "get", id: iqStanzaID['gameRequest']})
44
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
45
+ .c("game", {id: game.id, name: game.name})
46
+ .c("players", {})
47
+
48
+ $.each(game.players, function(index, value) {
49
+ if(index==game.players.length-1){
50
+ iq.c("player", {id: value.id}).t(value.name).up().up()
51
+ } else {
52
+ iq.c("player", {id: value.id}).t(value.name).up()
53
+ }
54
+ });
55
+
56
+ iq.c("options", {})
57
+
58
+ for (var key in game.options) {
59
+ if(optionIndex == optionsSize-1){
60
+ iq.c("option", {name: key}).t(game.options[key]).up().up()
61
+ } else {
62
+ iq.c("option", {name: key}).t(game.options[key]).up()
63
+ }
64
+ optionIndex=optionIndex+1;
65
+ }
66
+
67
+ connection.sendIQ(iq);
68
+ };
69
+
70
+
71
+ //RECEIVE GAME REQUEST
72
+ var handleRequestGameIQStanza = function(iq){
73
+ var from = iq.getAttribute("from");
74
+ var slug = from.split("@")[0];
75
+ var gameId = "";
76
+ var gameName = "";
77
+ var players = []
78
+ var options = new Array();
79
+
80
+ //Build game object
81
+ var gameTag = iq.getElementsByTagName('game');
82
+
83
+ if (gameTag.length == 1) {
84
+ var gameElement = gameTag[0];
85
+ gameName = $(gameElement).attr("name")
86
+ gameId = $(gameElement).attr("id")
87
+
88
+ var playersTag = iq.getElementsByTagName('players');
89
+ if (playersTag.length > 0) {
90
+ $.each($(playersTag).children(), function(index, value) {
91
+ players.push(new P.GAME.player($(value).attr("id"),$(value).text()))
92
+ });
93
+ }
94
+
95
+ var optionsTag = iq.getElementsByTagName('options');
96
+ if (optionsTag.length > 0) {
97
+ $.each($(optionsTag).children(), function(index, value) {
98
+ options[$(value).attr("name")]=$(value).text();
99
+ });
100
+ }
101
+ }
102
+
103
+ P.GAME.updateLogicAfterReceivedGameRequest(slug,gameId,gameName,players,options)
104
+ return true;
105
+ }
106
+
107
+
108
+
109
+ /////////////////////
110
+ // MANAGE GAME REQUESTS RESPONSES
111
+ /////////////////////
112
+
113
+ //SEND GAME REQUEST RESPONSE
114
+ var sendIQStanzaToResponseGameRequest = function(jid,result){
115
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['gameRequest']})
116
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
117
+ .c("response").t(result);
118
+ connection.sendIQ(iq);
119
+ }
120
+
121
+ //RECEIVE GAME REQUEST RESPONSE
122
+ var handleIQResultFromGameRequest = function(iq){
123
+ var from = iq.getAttribute("from");
124
+ var slug = from.split("@")[0];
125
+ var queryTag = iq.getElementsByTagName('query');
126
+
127
+ if (queryTag.length > 0) {
128
+ var queryElement = queryTag[0];
129
+ var response = queryElement.getElementsByTagName('response');
130
+
131
+ if(response.length == 1){
132
+ P.GAME.updateLogicAfterReceivedGameRequestResponse(slug,$(response).text())
133
+ return true;
134
+ }
135
+ }
136
+ return true;
137
+ }
138
+
139
+
140
+
141
+ /////////////////////
142
+ // MANAGE GAME REQUESTS CANCELATIONS
143
+ /////////////////////
144
+
145
+ //SEND GAME REQUEST CANCELATION
146
+ var sendIQStanzaToFinishGame = function(jid){
147
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['gameRequestFinish']})
148
+ .c("game", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
149
+ .c("status").t("finish");
150
+ connection.sendIQ(iq);
151
+ }
152
+
153
+ //RECEIVE GAME REQUEST CANCELATION
154
+ var handleIQResultFromGameRequestFinish = function(iq){
155
+ var from = iq.getAttribute("from");
156
+ var slug = from.split("@")[0];
157
+ var gameTag = iq.getElementsByTagName('game');
158
+
159
+ if (gameTag.length > 0) {
160
+ var gameTagElement = gameTag[0];
161
+ var response = gameTagElement.getElementsByTagName('status');
162
+
163
+ if(response.length == 1){
164
+ var gameStatus = $(response).text();
165
+ P.GAME.updateLogicAfterReceivedGameRequestCancelation(slug,gameStatus)
166
+ return true;
167
+ }
168
+ }
169
+ return true;
170
+ }
171
+
172
+
173
+
174
+ /////////////////////
175
+ // MANAGE ACTION EXCHANGE
176
+ /////////////////////
177
+
178
+ function action(){}
179
+
180
+
181
+ //SEND ACTIONS
182
+
183
+ var sendIQStanzaWithAction = function (jid,gameId,action){
184
+ var iqId = getIdForIqAction(gameId);
185
+ var iq = $iq({to: jid, type: "result", id: iqId})
186
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
187
+ .c("game", {id: gameId})
188
+ .c("action", {type: action.type})
189
+ action.type=null;
190
+ iq = genetareIQStanzaWithObject(iq,action)
191
+ return connection.sendIQ(iq);
192
+ };
193
+
194
+ var getIdForIqAction = function (gameId){
195
+ return iqStanzaID['gameAction'] + "_" + gameId;
196
+ }
197
+
198
+ var genetareIQStanzaWithObject = function(iq,object){
199
+ if(typeof object != "object"){
200
+ return iq;
201
+ }
202
+ var childs=Object.keys(object).length;
203
+ var childsCounter = 0;
204
+
205
+ $.each(object, function(index, value) {
206
+ if(value==null){
207
+ return;
208
+ }
209
+ if((typeof value == "object")){
210
+ //Complex object
211
+ iq.c(index, {})
212
+ iq = genetareIQStanzaWithObject(iq,value)
213
+ } else {
214
+ //Simple object
215
+ if(typeof value == "number"){
216
+ value = value + "";
217
+ }
218
+ if(childsCounter==childs-1){
219
+ iq.c(index, {}).t(value).up().up()
220
+ } else {
221
+ iq.c(index, {}).t(value).up()
222
+ }
223
+ }
224
+ childsCounter++;
225
+ });
226
+ return iq;
227
+ }
228
+
229
+
230
+ //RECEIVE ACTIONS
231
+
232
+ var handleActionIQStanza = function(iq){
233
+ var from = iq.getAttribute("from");
234
+ var slug = from.split("@")[0];
235
+ var queryTags = iq.getElementsByTagName('query');
236
+
237
+ if (queryTags.length == 1) {
238
+ var queryElement = queryTags[0];
239
+ var gameTags = queryElement.getElementsByTagName('game');
240
+
241
+ if(gameTags.length == 1){
242
+ var game = gameTags[0];
243
+ var gameId = $(game).attr("id")
244
+ var actionElements = game.getElementsByTagName('action');
245
+ $.each(actionElements, function(index, value) {
246
+ var myaction = buildActionObject(value)
247
+ PRESENCE.GAME[gameId].onActionReceived(myaction)
248
+ });
249
+ }
250
+ }
251
+ return true;
252
+ }
253
+
254
+ var buildActionObject = function (actionTag){
255
+ var myaction = new action();
256
+ var type = $(actionTag).attr("type")
257
+ myaction.type = type;
258
+ myaction = buildComplexObject(myaction,actionTag)
259
+ return myaction
260
+ }
261
+
262
+ var buildComplexObject = function(parent,childs){
263
+ $.each($(childs).children(), function(index, value) {
264
+ if ($(value).children().length>0) {
265
+ //Complex object
266
+ parent[value.tagName]= new Object();
267
+ parent[value.tagName]= buildComplexObject(parent[value.tagName],$(value))
268
+ } else {
269
+ //Simple object
270
+
271
+ //Check dataformat
272
+ if(! isNaN(+($(value).text()))){
273
+ //$(value).text() is a string which contains a number
274
+ var myvalue = +($(value).text());
275
+ } else {
276
+ var myvalue = $(value).text();
277
+ }
278
+ parent[value.tagName]=myvalue
279
+ }
280
+ });
281
+ return parent
282
+ }
283
+
284
+
285
+
286
+ /////////////////////////////
287
+ //REMOVE CONNECTION HANDLERS
288
+ /////////////////////////////
289
+
290
+ var removeHandler = function(handler){
291
+ connection.deleteHandler(handler);
292
+ }
293
+
294
+ var addHandler = function(gameId){
295
+ return connection.addHandler(handleActionIQStanza, null, "iq", "result", getIdForIqAction(gameId) , null);
296
+ }
297
+
298
+
299
+ return {
300
+ init: init,
301
+ sendIQStanzaToRequestGame: sendIQStanzaToRequestGame,
302
+ handleRequestGameIQStanza: handleRequestGameIQStanza,
303
+ sendIQStanzaToResponseGameRequest: sendIQStanzaToResponseGameRequest,
304
+ handleIQResultFromGameRequest: handleIQResultFromGameRequest,
305
+ sendIQStanzaToFinishGame: sendIQStanzaToFinishGame,
306
+ handleIQResultFromGameRequestFinish: handleIQResultFromGameRequestFinish,
307
+ sendIQStanzaWithAction: sendIQStanzaWithAction,
308
+ handleActionIQStanza: handleActionIQStanza,
309
+ addHandler : addHandler,
310
+ removeHandler: removeHandler
311
+ };
312
+
313
+ }) (PRESENCE, jQuery);