Browse code

Externalisation du systeme de menu commun aux jeux

schardon authored on 27/04/2020 23:08:28
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,195 @@
1
+(function($) {
2
+$(document).ready(function() {
3
+
4
+  var menuList = [];
5
+  var scPartieId = scInput_PartieId;
6
+  var scJoueurId = scInput_JoueurId;
7
+  var menuMode = '';
8
+
9
+  function refreshScreen(data) {
10
+    if(data['mode'] == 'g') {
11
+      if(scJoueurId > 0)
12
+        window.location.href = 'index.php?j=' + scJoueurId;
13
+      else
14
+        window.location.href = 'index.php';
15
+      return;
16
+    }
17
+
18
+    if(data['mode'] != menuMode) {
19
+      menuMode = data['mode'];
20
+      if(menuMode == 'p') {
21
+        $('#creerTitre').html(scInput_JeuNom.toUpperCase() + ' - Choisir une partie');
22
+        $('#creerTitre').removeClass('creer-titre-attente');
23
+        $('#creerTitre').removeClass('creer-titre-joueur');
24
+        $('#creerTitre').addClass('creer-titre-partie');
25
+        $('#creerForm').show();
26
+        $('#creerGo').hide();
27
+        scPartieId = 0;
28
+        scJoueurId = 0;
29
+      }
30
+      if(menuMode == 'j') {
31
+        $('#creerTitre').html('<div class="creer-titre-retour creer-btn">&lt;&lt;</div>' + scInput_JeuNom.toUpperCase() + ' [ Partie : ' + data['partie'] + ' ] Choisir un joueur');
32
+        $('#creerTitre').removeClass('creer-titre-attente');
33
+        $('#creerTitre').removeClass('creer-titre-partie');
34
+        $('#creerTitre').addClass('creer-titre-joueur');
35
+        $('#creerForm').show();
36
+        $('#creerGo').hide();
37
+        scJoueurId = 0;
38
+      }
39
+      if(menuMode == 's') {
40
+        $('#creerTitre').html('<div class="creer-titre-retour creer-btn">&lt;&lt;</div>' + scInput_JeuNom.toUpperCase() + ' [ Partie : ' + data['partie'] + ' ] [ Joueur : ' + data['joueur'] + " ] Salle d'attente");
41
+        $('#creerTitre').removeClass('creer-titre-partie');
42
+        $('#creerTitre').removeClass('creer-titre-joueur');
43
+        $('#creerTitre').addClass('creer-titre-attente');
44
+        $('#creerForm').hide();
45
+      }
46
+      $('#creerListeTab').empty();
47
+      menuList = [];
48
+    }
49
+
50
+    // Parties/Joueurs ajoutes ou modifies ?
51
+    var dataListeTaille = 0;
52
+    for(var key in data['liste']) {
53
+      dataListeTaille++;
54
+      var val = data['liste'][key];
55
+      if( menuList[key] ) {
56
+        if( menuList[key] != val )
57
+          $('#creerListeItem' + key + ' .creer-liste-nom').html( val );
58
+      }
59
+      else {
60
+        var tr = $('<tr id="creerListeItem' + key + '"><td class="creer-liste-nom">' + val + '</td>' + (scJoueurId > 0 ? '' : '<td class="creer-liste-btn"><div class="creer-liste-btn-entrer creer-btn">Entrer</div><div class="creer-liste-btn-suppr creer-btn">Supprimer</div></td>') + '</tr>');
61
+        $('#creerListeTab').prepend(tr);
62
+        tr.hide().fadeIn();
63
+        menuList[key] = val;
64
+      }
65
+    }
66
+
67
+    // Parties/Joueurs supprimes ?
68
+    for(var key in menuList) {
69
+      if( !data['liste'][key] ) {
70
+        $('#creerListeItem' + key).fadeOut();
71
+        menuList[key] = false;
72
+      }
73
+    }
74
+
75
+    if(data['mode'] == 's') {
76
+      if( dataListeTaille > 1 )
77
+        $('#creerGo').show();
78
+      else
79
+        $('#creerGo').hide();
80
+    }
81
+  }
82
+
83
+  function refresh() {
84
+    $.get('commun/creer.php?p=' + scPartieId + '&j=' + scJoueurId)
85
+      .done(function(data, text, jqxhr) {
86
+        refreshScreen(  JSON.parse(jqxhr.responseText) );
87
+      })
88
+      .fail(function(jqxhr){
89
+//        alert(jqxhr.responseText);
90
+      })
91
+      .always(function(){
92
+        refreshTimer = setTimeout(refresh, 1000);
93
+      });
94
+  }
95
+
96
+  refresh();
97
+  var refreshTimer;
98
+
99
+
100
+  function creerSwitchTo(id) {
101
+    if(scPartieId > 0)
102
+      scJoueurId = id;
103
+    else
104
+      scPartieId = id;
105
+
106
+    clearTimeout(refreshTimer);
107
+    refresh();
108
+  }
109
+
110
+  $('#creerTitre').on('click', '.creer-titre-retour', function(e) {
111
+    if(scJoueurId > 0) {
112
+      var tempJ = scJoueurId;
113
+      scJoueurId = 0;
114
+      $.get('commun/creer.php?p=' + scPartieId + '&j=' + tempJ + '&out=1')
115
+        .done(function(data, text, jqxhr) {
116
+
117
+        })
118
+        .fail(function(jqxhr){
119
+  //        alert(jqxhr.responseText);
120
+        })
121
+        .always(function(){
122
+          clearTimeout(refreshTimer);
123
+          refresh();
124
+        });
125
+    }
126
+    else {
127
+      scPartieId = 0;
128
+      creerSwitchTo(0);
129
+    }
130
+  });
131
+
132
+  $('#creerListeTab').on('click', '.creer-liste-btn-entrer', function(e) {
133
+    e.preventDefault();
134
+    var id = $(this).parents('tr').attr('id').substring('creerListeItem'.length);
135
+    creerSwitchTo(id);
136
+  });
137
+
138
+  $('#creerListeTab').on('click', '.creer-liste-btn-suppr', function(e) {
139
+    e.preventDefault();
140
+    var id = $(this).parents('tr').attr('id').substring('creerListeItem'.length);
141
+    $(this).parents('td').empty();
142
+    $(this).parents('tr').fadeOut();
143
+    menuList[id] = false;
144
+    $.get('commun/creer.php?p=' + scPartieId + '&suppr=' + id)
145
+      .done(function(data, text, jqxhr) {
146
+
147
+      })
148
+      .fail(function(jqxhr){
149
+//        alert(jqxhr.responseText);
150
+      })
151
+      .always(function(){
152
+
153
+      });
154
+  });
155
+
156
+  $('#creerGo .creer-go-btn').on('click', function(e) {
157
+    e.preventDefault();
158
+    $('#creerGo').fadeOut();
159
+
160
+    $.get('commun/creer.php?p=' + scPartieId + '&j=' + scJoueurId + '&go=1')
161
+      .done(function(data, text, jqxhr) {
162
+
163
+      })
164
+      .fail(function(jqxhr){
165
+//        alert(jqxhr.responseText);
166
+      })
167
+      .always(function(){
168
+        clearTimeout(refreshTimer);
169
+        refresh();
170
+      });
171
+  });
172
+
173
+  $('#creerForm').on('submit', function(e) {
174
+    e.preventDefault();
175
+    if($('#creerForm .creer-form-input').val().length < 2) return;
176
+    var $form = $(this);
177
+
178
+    $form.hide();
179
+
180
+    $.post('commun/creer.php?p=' + scPartieId, $form.serializeArray())
181
+      .done(function(data, text, jqxhr) {
182
+        creerSwitchTo( jqxhr.responseText );
183
+        $('#creerForm .creer-form-input').val('');
184
+      })
185
+      .fail(function(jqxhr){
186
+//        alert(jqxhr.responseText);
187
+      })
188
+      .always(function(){
189
+        $form.show();
190
+      });
191
+  });
192
+
193
+
194
+});
195
+})(jQuery);