social_stream 0.23.2 → 0.23.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/base/app/assets/javascripts/layouts.js +0 -1
  2. data/base/app/assets/javascripts/social_stream.comments.js +10 -3
  3. data/base/app/assets/javascripts/social_stream.search.js.erb +3 -1
  4. data/base/app/assets/javascripts/social_stream.wall.js.erb +8 -1
  5. data/base/app/assets/stylesheets/{activities.css.scss → activities.css.scss.erb} +1 -1
  6. data/base/app/models/relation.rb +4 -0
  7. data/base/app/models/tie.rb +5 -0
  8. data/base/app/views/cheesecake/_index.html.erb +2 -2
  9. data/base/app/views/layouts/_search.html.erb +2 -2
  10. data/base/app/views/posts/_new_activity_fields.erb +0 -6
  11. data/base/lib/generators/social_stream/base/templates/relations.yml +4 -4
  12. data/base/lib/social_stream/base/version.rb +1 -1
  13. data/base/lib/tasks/db/populate.rake +2 -2
  14. data/base/spec/dummy/config/relations.yml +4 -4
  15. data/base/vendor/assets/javascripts/jquery.watermark.js +563 -0
  16. data/documents/app/assets/javascripts/social_stream.audio.js.erb +25 -0
  17. data/documents/app/assets/javascripts/social_stream.repository.js.erb +27 -0
  18. data/documents/app/assets/javascripts/social_stream.video.js.erb +38 -0
  19. data/documents/app/assets/stylesheets/documents.css.scss +11 -14
  20. data/documents/app/views/audios/_audio.html.erb +0 -18
  21. data/documents/app/views/audios/_audio_processed.html.erb +35 -39
  22. data/documents/app/views/common_documents/_document_info.html.erb +2 -2
  23. data/documents/app/views/common_documents/_edit_form.html.erb +1 -1
  24. data/documents/app/views/common_documents/_headers.html.erb +1 -1
  25. data/documents/app/views/common_documents/_index.html.erb +3 -0
  26. data/documents/app/views/pictures/_picture.html.erb +3 -1
  27. data/documents/app/views/videos/_video.html.erb +0 -20
  28. data/documents/app/views/videos/_video_processed.html.erb +56 -43
  29. data/documents/config/locales/en.yml +1 -1
  30. data/documents/config/locales/es.yml +1 -1
  31. data/documents/lib/social_stream/documents/version.rb +1 -1
  32. data/documents/social_stream-documents.gemspec +1 -1
  33. data/documents/vendor/assets/javascripts/jquery.jplayer.js +87 -68
  34. data/documents/vendor/assets/swf/Jplayer.swf +0 -0
  35. data/events/app/assets/javascripts/social_stream-events.js +2 -0
  36. data/events/app/assets/javascripts/social_stream.calendar.js.erb +98 -0
  37. data/events/app/assets/javascripts/social_stream.event.js +17 -0
  38. data/events/app/assets/javascripts/social_stream.events.poster.js +1 -0
  39. data/events/app/assets/javascripts/{events.js.coffee → social_stream.events.tools.js.coffee} +0 -16
  40. data/events/app/assets/stylesheets/events.css.scss +33 -0
  41. data/events/app/controllers/events_controller.rb +19 -9
  42. data/events/app/views/events/_event.html.erb +5 -0
  43. data/events/app/views/events/_sidebar_calendar.html.erb +6 -28
  44. data/events/app/views/events/index.js.erb +3 -0
  45. data/events/app/views/rooms/_form.html.erb +1 -1
  46. data/events/app/views/rooms/create.js.erb +1 -1
  47. data/events/lib/social_stream/events/version.rb +1 -1
  48. data/events/social_stream-events.gemspec +1 -1
  49. data/lib/social_stream/version.rb +1 -1
  50. data/presence/app/assets/javascripts/jquery.ui.chatbox.sstreampresence.js +1 -1
  51. data/presence/app/assets/javascripts/presence.js.erb +16 -1
  52. data/presence/app/assets/javascripts/presence_XmppClient.js.erb +10 -3
  53. data/presence/app/assets/javascripts/presence_game.js.erb +284 -49
  54. data/presence/app/assets/javascripts/presence_game_comunication.js.erb +313 -0
  55. data/presence/app/assets/javascripts/presence_game_interface.js.erb +134 -0
  56. data/presence/app/assets/javascripts/presence_game_ter.js.erb +237 -188
  57. data/presence/app/assets/javascripts/presence_persistence.js +1 -1
  58. data/presence/app/assets/javascripts/presence_uiManager.js.erb +4 -2
  59. data/presence/app/assets/javascripts/presence_utilities.js +1 -1
  60. data/presence/app/assets/javascripts/presence_videochat.js.erb +2 -3
  61. data/presence/app/assets/javascripts/presence_windowManager.js +8 -1
  62. data/presence/app/assets/stylesheets/TresEnRaya.css +57 -0
  63. data/presence/app/assets/stylesheets/chat.css.scss +22 -1
  64. data/presence/app/views/chat/_index.html.erb +15 -18
  65. data/presence/config/locales/en.yml +14 -1
  66. data/presence/config/locales/es.yml +14 -1
  67. data/presence/lib/social_stream/presence/version.rb +1 -1
  68. data/presence/lib/tasks/presence/installer.rake +2 -2
  69. data/presence/social_stream-presence.gemspec +1 -1
  70. data/presence/vendor/assets/javascripts/jquery.gamequery-0.5.1.js +1164 -0
  71. data/social_stream.gemspec +4 -4
  72. data/spec/dummy/config/relations.yml +4 -4
  73. metadata +44 -34
  74. data/base/vendor/assets/javascripts/jquery.watermarkinput.js +0 -81
  75. data/documents/app/assets/javascripts/documents.js.erb +0 -71
@@ -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);
@@ -0,0 +1,134 @@
1
+ ////////////////////
2
+ //Multiplayer Games Interface Manager
3
+ ////////////////////
4
+
5
+ PRESENCE.GAME.INTERFACE = (function(P,$,undefined){
6
+
7
+ //////////////////////////
8
+ // TRIGGERS //
9
+ //////////////////////////
10
+
11
+ var pickGamesButton = function (uiElement){
12
+ var slug = $(uiElement.element).attr("id");
13
+ var videoBoxVisibility = toggleVideoBoxForSlug(slug);
14
+
15
+ if (videoBoxVisibility) {
16
+ hideVideoChatButton(slug);
17
+ var mygame = generateDefaultGame(slug);
18
+ PRESENCE.GAME.requestUserToPlay(slug,mygame);
19
+ } else {
20
+ showVideoChatButton(slug);
21
+ PRESENCE.GAME.finishGame(slug);
22
+ }
23
+ }
24
+
25
+
26
+ ////////////////////////////
27
+ // CALLBACKS //
28
+ ////////////////////////////
29
+
30
+ updateInterfaceAfterGameRequestReceived = function(slug){
31
+ //gameStatus="pending";
32
+
33
+ //Show or create chatbox
34
+ createBuddyChatBox(slug);
35
+ var chatBox = getChatBoxForSlug(slug);
36
+
37
+ //Show invitation message
38
+ showGameInvitation(slug);
39
+
40
+ //Hide hideVideoChatButton
41
+ hideVideoChatButton(slug);
42
+
43
+ //Show gamebox
44
+ toggleVideoBoxForSlug(slug,true);
45
+ }
46
+
47
+ var updateInterfaceBeforeStartGame = function(slug){
48
+ var parentDiv = document.createElement('div');
49
+ var divID = 'game_' + slug + contactsInfo[slug].game.id;
50
+ parentDiv.setAttribute('id', divID);
51
+ $(parentDiv).css('width', "100%");
52
+ $(parentDiv).css('height', "100%");
53
+ setVideoBoxContent(slug,parentDiv);
54
+
55
+ PRESENCE.GAME.startGame(slug,divID)
56
+ }
57
+
58
+ var updateInterfaceAfterFinishGame = function(slug){
59
+ toggleVideoBoxForSlug(slug,false);
60
+ showVideoChatButton(slug);
61
+ }
62
+
63
+ var updateInterfaceOnInformationMessage = function(slug,msg){
64
+ setVideoBoxContent(slug,"<p class=\"game-info\"> " + msg +" </p>");
65
+ }
66
+
67
+
68
+
69
+ ///////////////////////////////
70
+ // INTERFACE HELP METHODS //
71
+ ///////////////////////////////
72
+
73
+ var showGameInvitation = function(slug){
74
+ var title = "<p class=\"game-info\">" + I18n.t("chat.game.call", {name: getNameFromSlug(slug)}) + " </p>";
75
+ var msg = title + "<p class=\"game-request\"> <a class=\"gameButton\" slug=\""+slug+"\" value=\"yes\">" + I18n.t("chat.game.accept") + "</a> -"
76
+ + " <a class=\"gameButton\" slug=\""+slug+"\" value=\"no\">" + I18n.t("chat.game.deny") + "</a> </p>";
77
+ setVideoBoxContent(slug,msg);
78
+ setGameRequestButtonsFunction();
79
+ }
80
+
81
+ var setGameRequestButtonsFunction = function(){
82
+ $(".gameButton").click(function(event) {
83
+ event.preventDefault();
84
+ var result = $(this).attr("value");
85
+ var slug = $(this).attr("slug");
86
+ PRESENCE.GAME.responseUserToPlay(slug,result);
87
+ });
88
+ }
89
+
90
+ var hideVideoChatButton = function(slug){
91
+ $(getChatBoxButtonForSlug(slug,"video")).hide();
92
+ }
93
+
94
+ var showVideoChatButton = function(slug){
95
+ $(getChatBoxButtonForSlug(slug,"video")).show();
96
+ }
97
+
98
+
99
+
100
+ //////////////////////
101
+ // DEBUG METHODS //
102
+ //////////////////////
103
+
104
+ var generateDefaultGame = function(guest_slug){
105
+ var player1 = new PRESENCE.GAME.player(user_slug,user_name)
106
+ var player2 = new PRESENCE.GAME.player(guest_slug,getNameFromSlug(guest_slug))
107
+ var localPlayers = [player1,player2]
108
+ var localOptions = new Array();
109
+ localOptions['theme']="Modern";
110
+ var mygame = new PRESENCE.GAME.game("TER","Tres en Raya",localPlayers,localOptions)
111
+ return mygame;
112
+ }
113
+
114
+
115
+ return {
116
+ pickGamesButton: pickGamesButton,
117
+ updateInterfaceAfterGameRequestReceived: updateInterfaceAfterGameRequestReceived,
118
+ updateInterfaceBeforeStartGame: updateInterfaceBeforeStartGame,
119
+ updateInterfaceAfterFinishGame: updateInterfaceAfterFinishGame,
120
+ updateInterfaceOnInformationMessage: updateInterfaceOnInformationMessage,
121
+ generateDefaultGame: generateDefaultGame
122
+ };
123
+
124
+ }) (PRESENCE, jQuery);
125
+
126
+
127
+
128
+ ////////////////////
129
+ //Interface Events
130
+ ////////////////////
131
+
132
+ function play(){
133
+ $(".chat-gamesthick").css("display","block");
134
+ }
@@ -1,96 +1,218 @@
1
1
  ////////////////////
2
- //Tres en Raya
2
+ // Tres en Raya //
3
3
  ////////////////////
4
4
 
5
5
  PRESENCE.GAME.TER = (function(P,$,undefined){
6
6
 
7
- //Game core
8
- var core = P.GAME
9
-
10
- //Game variables
11
- var players = null;
7
+ //Gamecore
8
+ var CORE = P.GAME
9
+
10
+ //Games variables
11
+ var game; //Game and player are objects defined in the gamecore.
12
12
  var player = null;
13
13
  var currentPlayer = null;
14
- var options = null;
14
+ var playing=true;
15
+
16
+ //We must define our ACTION object.
17
+ //The information exchanged between players is performed through the exchange of action type objects.
18
+ //We can define our own types of actions, with different fields
15
19
  function action(player,square) {
16
20
  this.player = player;
21
+ this.type = "general";
17
22
  this.square = square;
18
23
  }
19
- var playing=true;
20
-
24
+
21
25
  //Html Divs
22
26
  var divID = null;
23
27
  var containerDivId = 'tresEnRayaContainerDivID';
24
28
  var messageDivId = 'tresEnRayaMessageDivID';
25
29
 
26
- //Boardgame
30
+ //Boardgame variables
27
31
  var board = [];
28
32
  var boardDimension = null;
29
33
  var squareDimension = null;
30
34
  var middleSquareDimension = null;
31
35
 
32
- var init = function(myPlayers,myPlayer,myOptions,myDivID){
33
- if (!validateParams(myPlayers,myPlayer,myOptions,myDivID)){
34
- showMessage("Invalid parameters");
35
- return;
36
- }
37
- players = myPlayers;
38
- player = myPlayer;
39
- options = settingOptions(myOptions);
40
- divID = myDivID;
36
+
37
+
38
+ ////////////////
39
+ // STARTER //
40
+ ////////////////
41
+ //The core will invoke this method automatically to start the game.
42
+ //*myPlayer = myself
43
+ //*myGame = Game object (is the same for all players)
44
+ //*myDivID = Div to render the game
45
+ var init = function(myPlayer,myGame,myDivID){
46
+ player = myPlayer;
47
+ game = myGame;
48
+ divID = myDivID;
49
+
50
+ //Setting options
51
+ game.options = settingOptions(myGame.options);
52
+
53
+ //Init Graphics
41
54
  drawBoard();
55
+
56
+ //Init Logic
42
57
  settingBoard();
58
+
59
+ //Init Events
43
60
  setBoardEvents();
44
- currentPlayer = players[0];
45
- if(player==currentPlayer){
61
+
62
+ //Init game variables
63
+ currentPlayer = game.players[0];
64
+ playing=true;
65
+
66
+ //Start Play!
67
+ if(player.id==currentPlayer.id){
46
68
  showMessage("You start!")
47
69
  } else {
48
- showMessage(currentPlayer + " starts!")
70
+ showMessage(currentPlayer.name + " starts!")
49
71
  }
50
72
  };
51
73
 
52
- var validateParams = function(myPlayers,myPlayer,myOptions,myDivID){
53
-
54
- if ((typeof myPlayers=="undefined")||(typeof myPlayer=="undefined")||(typeof myOptions=="undefined")||(typeof myDivID=="undefined")){
55
- return false;
74
+
75
+ //Set defaults for unspecified options
76
+ var settingOptions = function(myOptions){
77
+
78
+ if (!('theme' in myOptions)){
79
+ myOptions['theme'] = "Modern"
80
+ }
81
+
82
+ if (!('rounds' in myOptions)){
83
+ myOptions['rounds'] = "1"
56
84
  }
57
85
 
58
- //Check if myPlayers is an Array
59
- if (myPlayers.constructor.toString().indexOf("Array") == -1){
60
- return false;
86
+ //Apply default options for non specified opts.
87
+ return myOptions;
88
+ };
89
+
90
+
91
+
92
+ ////////////////////////////////
93
+ // CORE CONFIG CALLBACKS //
94
+ ////////////////////////////////
95
+
96
+ //Optional methods that can be invoked by the core
97
+
98
+ //The Core provides basic validation
99
+ //We can include our own validation conditions here!
100
+ //The core will invoke this method automatically in the validation process
101
+ var validateParams = function(myPlayer,myGame,myDivID){
102
+ if(myGame.players.lenght < 2){
103
+ return new CORE.createValidationResult(false,"This game needs two players at least");
61
104
  }
105
+ return new CORE.createValidationResult(true,"Ok");
106
+ }
107
+
108
+
109
+ //MinimumRequirements to execute this game
110
+ var minimumRequirements = function(){
111
+ var requirements = new Array();
112
+ return requirements;
113
+ }
62
114
 
63
- //2 players at least
64
- if(myPlayers.lenght < 2){
65
- return false;
115
+
116
+
117
+ /////////////////////////////////
118
+ // GAMECORE CALLS & CALLBACKS // * To send our actions to other players we will use the << sendAction >> method.
119
+ ///////////////////////////////// * When some player send us a action, the core will invoke the << onActionReceived >> method.
120
+
121
+ var sendAction = function(action){
122
+ CORE.sendAction(game.id,game.players,action)
123
+ return;
124
+ }
125
+
126
+ var onActionReceived = function(action){
127
+ processAction(action);
128
+ return;
129
+ }
130
+
131
+
132
+
133
+ /////////////////////////
134
+ // ACTION MANAGEMENT // * These methods are not mandatory, but are highly recommended.
135
+ ////////////////////////
136
+
137
+
138
+ //Processes an action of any player
139
+ var processAction = function(action){
140
+
141
+ if(playing==false){
142
+ return
143
+ }
144
+
145
+ //Check if the action is legal.
146
+ if(!checkAction(action)){
147
+ showMessage("Ilegal action")
148
+ return false
149
+ }
150
+
151
+ if(player.id==action.player.id){
152
+ //Notify my actions!
153
+ sendAction(action)
66
154
  }
67
155
 
68
- //Check if myOptions is an Array
69
- if (myOptions.constructor.toString().indexOf("Array") == -1){
156
+ //Update logic
157
+ updateLogic(action)
158
+
159
+ //Update Graphics
160
+ updateInterface(action)
161
+
162
+ //Check for winner
163
+ //If there is a winner, checkWinner() will return the winner's name.
164
+ //If not, it will return "finish" if the game is over without a winner, or null if the game is not over.
165
+ var winner = checkWinner()
166
+ if(winner!=null){
167
+ processWinner(winner)
168
+ finishGame()
169
+ return
170
+ }
171
+
172
+ //We continue with the next turn if the game is not over.
173
+ nextTurn();
174
+ }
175
+
176
+
177
+ //Check if the action is legal.
178
+ var checkAction = function(action){
179
+
180
+ if(currentPlayer.id!=action.player.id){
181
+ //The player has made an action out of his turn.
70
182
  return false;
71
183
  }
72
184
 
73
- //Check that myPlayer is contained on myPlayers
74
- if(myPlayers.indexOf(myPlayer)==-1){
185
+ if(board[action.square][2]!=0){
186
+ //Square is already occupied by another figure.
75
187
  return false;
76
188
  }
77
189
 
78
- return true;
190
+ return true;
191
+ }
192
+
193
+
194
+ //If we have a turn-based game we probably need a nextTurn method.
195
+ //This is an example
196
+ var nextTurn = function(){
197
+ var playersQuantity = game.players.length;
198
+ var indexActualPlayer = game.players.indexOf(currentPlayer);
199
+ if(indexActualPlayer==(playersQuantity-1)){
200
+ currentPlayer=game.players[0];
201
+ } else {
202
+ currentPlayer=game.players[indexActualPlayer+1];
203
+ }
204
+ if(player.id==currentPlayer.id){
205
+ showMessage("Is your turn!")
206
+ } else {
207
+ showMessage(currentPlayer.name + " turn")
208
+ }
79
209
  }
80
210
 
81
- var settingOptions = function(myOptions){
82
211
 
83
- if (!('theme' in myOptions)){
84
- myOptions['theme'] = "Modern"
85
- }
86
212
 
87
- if (!('rounds' in myOptions)){
88
- myOptions['rounds'] = "1"
89
- }
90
-
91
- //Apply default options for non specified opts.
92
- return myOptions;
93
- };
213
+ ///////////////////////
214
+ // TER GAME METHODS //
215
+ ///////////////////////
94
216
 
95
217
  var drawBoard = function(){
96
218
  var containerDiv = document.createElement('div');
@@ -101,14 +223,15 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
101
223
  var messageDiv = document.createElement('div');
102
224
  messageDiv.setAttribute('id', messageDivId);
103
225
  $(messageDiv).addClass('tresEnRayaMessageDivClass')
104
- $(messageDiv).addClass('tresEnRayaMessageTheme' + options['theme'])
226
+ $(messageDiv).addClass('tresEnRayaMessageTheme' + game.options['theme'])
105
227
  $("#" + divID).append(messageDiv);
106
228
 
107
229
  var p = document.createElement('p');
108
230
  p.setAttribute('id', messageDivId + "p");
109
- $(p).addClass('tresEnRayaMessagePTheme' + options['theme'])
231
+ $(p).addClass('tresEnRayaMessagePTheme' + game.options['theme'])
110
232
  $("#" + messageDivId).append(p);
111
233
 
234
+ var messageDivHeight = 12;
112
235
 
113
236
  //Adjust dimensions
114
237
  var parentWidth = $("#" + divID).width();
@@ -116,16 +239,18 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
116
239
  var maxDimension = 300;
117
240
  var minDimension = 100;
118
241
 
119
- var dimension = Math.min(parentWidth,parentHeight,maxDimension)
120
- boardDimension = Math.max(dimension,minDimension)
242
+ var dimension = Math.max(Math.min(parentWidth,parentHeight,maxDimension),minDimension)
243
+ boardDimension = dimension - messageDivHeight;
121
244
 
122
245
  $("#" + containerDivId).height(boardDimension)
123
246
  $("#" + containerDivId).width(boardDimension);
247
+ $("#" + messageDivId).width(boardDimension);
124
248
 
125
249
  //Apply Theme
126
- $("#" + containerDivId).addClass('tresEnRayaContainerTheme' + options['theme'])
250
+ $("#" + containerDivId).addClass('tresEnRayaContainerTheme' + game.options['theme'])
127
251
  };
128
252
 
253
+
129
254
  var settingBoard = function(){
130
255
  squareDimension = boardDimension/3;
131
256
  middleSquareDimension = squareDimension/2;
@@ -141,9 +266,9 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
141
266
  board[8] = [boardDimension-middleSquareDimension,boardDimension-middleSquareDimension,0]
142
267
  };
143
268
 
269
+
144
270
  var setBoardEvents = function(){
145
271
  $("#" + containerDivId).click(function (event) {
146
-
147
272
  var offsetX = event.pageX - $("#" + containerDivId).offset().left
148
273
  var offsetY = event.pageY - $("#" + containerDivId).offset().top
149
274
 
@@ -151,15 +276,17 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
151
276
  return;
152
277
  }
153
278
 
154
- if(currentPlayer!=player){
155
- //return;
279
+ if(currentPlayer.id!=player.id){
280
+ return;
156
281
  }
157
282
  square = getSquare(offsetX,offsetY)
283
+
158
284
  var actionNew = new action(currentPlayer,square);
159
285
  processAction(actionNew)
160
286
  });
161
287
  };
162
288
 
289
+
163
290
  var getSquare = function(x,y){
164
291
  for(var i=0; i<board.length; i++){
165
292
  if((Math.abs(x-board[i][0])<middleSquareDimension)&&(Math.abs(y-board[i][1])<middleSquareDimension)){
@@ -169,51 +296,6 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
169
296
  return 0;
170
297
  }
171
298
 
172
-
173
- var processAction = function(action){
174
-
175
- if(playing==false){
176
- return;
177
- }
178
-
179
- //Check if the action is legal
180
- if(!checkAction(action)){
181
- showMessage("Ilegal action");
182
- return false;
183
- }
184
-
185
- if(player==currentPlayer){
186
- //Notify action
187
- sendAction(action);
188
- }
189
-
190
-
191
- //Update logic
192
- updateLogic(action);
193
-
194
- //Update interface
195
- updateInterface(action);
196
-
197
- //Check for winner
198
- var winner = checkWinner()
199
- if(winner!=null){
200
- if(winner!="finish"){
201
- if(player==winner){
202
- showMessage("You have won the game")
203
- } else {
204
- showMessage(winner + " has won the game")
205
- }
206
- } else {
207
- showMessage("Finished game");
208
- }
209
- finishGame();
210
- return
211
- }
212
-
213
- nextTurn();
214
- }
215
-
216
-
217
299
  var showMessage = function(msg){
218
300
  $("#" + messageDivId + "p").html(msg)
219
301
  }
@@ -222,52 +304,43 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
222
304
  playing = false;
223
305
  }
224
306
 
225
- var checkAction = function(action){
226
- if(board[action.square][2]!=0){
227
- return false;
228
- }
229
-
230
- return true;
231
- }
232
-
233
307
  var updateLogic = function(action){
234
- board[action.square][2]=players.indexOf(action.player)+1;
308
+ if(action.player.id == game.players[0].id){
309
+ var r = 1;
310
+ } else {
311
+ var r = 2;
312
+ }
313
+ board[action.square][2]=r;
235
314
  }
236
315
 
237
316
  var updateInterface = function(action){
238
317
  var img = document.createElement('img');
239
318
 
240
- if(action.player==players[0]){
319
+ if(action.player.id==game.players[0].id){
241
320
  var figure = "circle";
242
321
  } else {
243
322
  var figure = "aspa";
244
323
  }
245
324
 
246
- $(img).attr("src","themes/" + options['theme'].toLowerCase() + "_" + figure + ".png");
247
-
325
+ var path = "/assets/games/ter/modern_circle.png"
326
+ $(img).attr("src","/assets/games/ter/" + game.options['theme'].toLowerCase() + "_" + figure + ".png");
248
327
  $("#" + containerDivId).append(img);
249
- $(img).addClass('tresEnRayaImgTheme' + options['theme'])
250
- $(img).width(squareDimension*0.8)
251
- $(img).height(squareDimension*0.8)
252
- var x = $("#" + containerDivId).offset().left
253
- var y = $("#" + containerDivId).offset().top
254
- $(img).css("top",y+board[action.square][1]-middleSquareDimension*0.8)
255
- $(img).css("left",x+board[action.square][0]-middleSquareDimension*0.8)
328
+ $(img).addClass('tresEnRayaImgTheme' + game.options['theme'])
329
+
330
+ //Calculate displacement
331
+ var figureSizePercent = 0.8;
332
+ var xdisplacement = -squareDimension*figureSizePercent/2*0.9;
333
+ var ydisplacement = 10;
334
+
335
+ $(img).width(squareDimension*figureSizePercent)
336
+ $(img).height(squareDimension*figureSizePercent)
337
+ $(img).css("top",board[action.square][1]+ydisplacement)
338
+ $(img).css("left",board[action.square][0]+xdisplacement)
256
339
  return;
257
340
  }
258
341
 
259
- var sendAction = function(action){
260
- //Notify action to other players
261
- //[...]
262
- return;
263
- }
264
342
 
265
- var onActionReceived = function(action){
266
- processAction(action);
267
- return;
268
- }
269
-
270
- //Return winner's name if exists and null if dont exits.
343
+ //Return winner's name if exists and null if don't exits.
271
344
  //Return "finish" when the game is blocked.
272
345
  var checkWinner = function(){
273
346
 
@@ -288,97 +361,73 @@ PRESENCE.GAME.TER = (function(P,$,undefined){
288
361
 
289
362
  //Check horizontal
290
363
  if((board[0][2]==board[1][2])&&(board[1][2]==board[2][2])&&(board[2][2]!=0)){
291
- return players[board[0][2]-1];
364
+ return game.players[board[0][2]-1];
292
365
  }
293
366
  if((board[3][2]==board[4][2])&&(board[4][2]==board[5][2])&&(board[5][2]!=0)){
294
- return players[board[3][2]-1];
367
+ return game.players[board[3][2]-1];
295
368
  }
296
369
  if((board[6][2]==board[7][2])&&(board[7][2]==board[8][2])&&(board[8][2]!=0)){
297
- return players[board[6][2]-1];
370
+ return game.players[board[6][2]-1];
298
371
  }
299
372
 
300
373
  //Check vertical
301
374
  if((board[0][2]==board[3][2])&&(board[3][2]==board[6][2])&&(board[6][2]!=0)){
302
- return players[board[0][2]-1];
375
+ return game.players[board[0][2]-1];
303
376
  }
304
377
  if((board[1][2]==board[4][2])&&(board[4][2]==board[7][2])&&(board[7][2]!=0)){
305
- return players[board[1][2]-1];
378
+ return game.players[board[1][2]-1];
306
379
  }
307
380
  if((board[2][2]==board[5][2])&&(board[5][2]==board[8][2])&&(board[8][2]!=0)){
308
- return players[board[2][2]-1];
381
+ return game.players[board[2][2]-1];
309
382
  }
310
383
 
311
384
  //Check diagonals
312
385
  if((board[0][2]==board[4][2])&&(board[4][2]==board[8][2])&&(board[8][2]!=0)){
313
- return players[board[0][2]-1];
386
+ return game.players[board[0][2]-1];
314
387
  }
315
388
  if((board[6][2]==board[4][2])&&(board[4][2]==board[2][2])&&(board[2][2]!=0)){
316
- return players[board[6][2]-1];
389
+ return game.players[board[6][2]-1];
317
390
  }
318
391
 
319
392
  return null;
320
393
  }
321
394
 
322
- var nextTurn = function(){
323
- var playersQuantity = players.length;
324
- var indexActualPlayer = players.indexOf(currentPlayer);
325
- if(indexActualPlayer==(playersQuantity-1)){
326
- currentPlayer=players[0];
395
+
396
+ var processWinner = function(winner){
397
+ if(winner!="finish"){
398
+ if(player.id==winner.id){
399
+ showMessage("You have won the game")
400
+ } else {
401
+ showMessage(winner.name + " has won the game")
402
+ }
327
403
  } else {
328
- currentPlayer=players[indexActualPlayer+1];
329
- }
330
- if(player==currentPlayer){
331
- showMessage("Is your turn!")
332
- } else {
333
- showMessage(currentPlayer + " turn")
404
+ showMessage("Finished game");
334
405
  }
335
406
  }
336
407
 
337
- var getRandomPlayer = function(){
338
- var playersQuantity = players.length;
339
- var randomIndex = Math.round(Math.random()*(players.length-1))
340
- return players[randomIndex]
341
- }
342
-
343
408
  //Getters
344
- var getPlayers = function(){
345
- return players;
409
+ var getGame = function(){
410
+ return game;
346
411
  }
347
-
348
- var getOptions = function(){
349
- return options;
412
+
413
+ var getBoard = function(){
414
+ return board;
350
415
  }
351
416
 
352
- var getDummy = function(){
353
- return core;
354
- }
417
+ var getMyplayer = function(){
418
+ return player;
419
+ }
420
+
421
+ var getCurrentPlayer = function(){
422
+ return currentPlayer;
423
+ }
355
424
 
356
425
 
357
426
  return {
358
427
  init: init,
359
- getDummy: getDummy,
360
- onActionReceived: onActionReceived
428
+ validateParams: validateParams,
429
+ minimumRequirements: minimumRequirements,
430
+ onActionReceived: onActionReceived
361
431
  };
362
432
 
363
- }) (PRESENCE, jQuery);
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
- ////////////////////
373
- //Interface Events
374
- ////////////////////
375
- function play(){
376
- $(".chat-gamesthick").css("display","block");
377
- }
378
-
379
- function pickGamesButton(uiElement){
380
- var slug = $(uiElement.element).attr("id");
381
- //clickGamesButton(slug);
382
- log("clickGamesButton(slug)" + slug)
383
- }
384
-
433
+ }) (PRESENCE, jQuery);