script.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var first = true;
  2. var uniqId = '0';
  3. $(document).ready(function(){
  4. //Ini Materialize
  5. M.AutoInit();
  6. //Si message à afficher
  7. if($("#msg").val().trim() != ''){
  8. var msg = '';
  9. switch($("#msg").attr('data-type')){
  10. case 'err':
  11. msg = message_erreur($("#msg").val());
  12. break;
  13. case 'ok':
  14. msg = message_conf($("#msg").val());
  15. break;
  16. default:
  17. msg = message_info($("#msg").val());
  18. }
  19. float_message(msg);
  20. }
  21. //Envoyer formulaire
  22. $("#valid").on('click', function(){
  23. //Verif form
  24. if($('#save-name').val() == null && $("#name").val().trim() == ''){
  25. float_message(message_erreur('Lanceur vide'));
  26. return false;
  27. }
  28. //Envoi
  29. $.post('ajx/roll.php', {name: $("#name").val(), 'save-name': $('#save-name').val(), roll: $("#roll").val(), hide: $("#hide").prop('checked')}, function(data){
  30. if(data.etat = 'ok'){
  31. float_message(message_info('Resultat : ' + data.roll + ' / ' + data.dice));
  32. reload(false);
  33. $("#name").val('');
  34. }
  35. }, 'json');
  36. return false;
  37. });
  38. //Rechargement
  39. $(".reload").on('click', function(){
  40. if(first){
  41. reload(false);
  42. first = false;
  43. } else {
  44. reload(true);
  45. }
  46. }).click();
  47. //Recharge toutes les X secondes
  48. perma_check();
  49. //Reset
  50. $('.reset').on('click', function(){
  51. reset_score();
  52. });
  53. //Quit
  54. $('.quit').on('click', function(){
  55. location.href = './?quit';
  56. });
  57. $("body").on('click', '.float_message', function () {
  58. $(this).slideUp('slow', function () {
  59. $(this).html('');
  60. });
  61. });
  62. });
  63. function check(){
  64. $.post("ajx/check.php", {'id': uniqId}, function(data){
  65. if(data.etat == 'ok'){
  66. if(data.update){
  67. reload(false);
  68. }
  69. }
  70. }, 'json');
  71. }
  72. function perma_check(time){
  73. time = time || 15;
  74. setTimeout(function(){
  75. check();
  76. perma_check(time);
  77. }, time * 1000);
  78. }
  79. function reload(msg){
  80. $.post("ajx/score.php", null, function(data){
  81. if(data.etat == 'ok'){
  82. uniqId = data.uniqId;
  83. $("#tbody").html(data.html);
  84. $("#save-name").html(data.savename);
  85. if(msg){
  86. float_message(message_conf("Page rechargée"));
  87. }
  88. M.AutoInit();
  89. }
  90. }, 'json');
  91. }
  92. function reset_score(){
  93. $.post("ajx/reset.php", null, function(data){
  94. if(data.etat == 'ok'){
  95. $("#tbody").html('');
  96. $("#save-name").html('<option value="" disabled selected>Selectionnez un lanceur</option>');
  97. float_message(message_conf("Session reset"));
  98. M.AutoInit();
  99. }
  100. }, 'json');
  101. }
  102. function message_erreur(msg){
  103. var html = '';
  104. html += '<div class="hide-on-small-only">' + "\n";
  105. html += ' <div class="row red-text red lighten-4" style="height: 60px; border: dashed;">' + "\n";
  106. html += ' <div class="col s2">' + "\n";
  107. html += ' <i class="material-icons" style="font-size: 3em; line-height: 125%">error</i>' + "\n";
  108. html += ' </div>' + "\n";
  109. html += ' <div class="col s10 truncate " style="font-size: 1.5em; line-height: 250%">' + "\n";
  110. html += ' ' + msg + "\n";
  111. html += ' </div>' + "\n";
  112. html += ' </div>' + "\n";
  113. html += '</div>' + "\n";
  114. html += '<div class="hide-on-med-and-up">' + "\n";
  115. html += ' <div class="red lighten-4 red-text center" style="border: dashed">' + "\n";
  116. html += ' <br>' + msg + '<br>&nbsp;' + "\n";
  117. html += ' </div>' + "\n";
  118. html += '</div>' + "\n";
  119. return html;
  120. }
  121. function message_info(msg){
  122. var html = '';
  123. html += '<div class="hide-on-small-only">' + "\n";
  124. html += ' <div class="row blue-text blue lighten-4" style="height: 60px; border: dashed;">' + "\n";
  125. html += ' <div class="col s2">' + "\n";
  126. html += ' <i class="material-icons" style="font-size: 3em; line-height: 125%">info</i>' + "\n";
  127. html += ' </div>' + "\n";
  128. html += ' <div class="col s10 truncate " style="font-size: 1.5em; line-height: 250%">' + "\n";
  129. html += ' ' + msg + "\n";
  130. html += ' </div>' + "\n";
  131. html += ' </div>' + "\n";
  132. html += '</div>' + "\n";
  133. html += '<div class="hide-on-med-and-up">' + "\n";
  134. html += ' <div class="blue lighten-4 blue-text center" style="border: dashed">' + "\n";
  135. html += ' <br>' + msg + '<br>&nbsp;' + "\n";
  136. html += ' </div>' + "\n";
  137. html += '</div>' + "\n";
  138. return html;
  139. }
  140. function message_warn(msg){
  141. var html = '';
  142. html += '<div class="hide-on-small-only">' + "\n";
  143. html += ' <div class="row amber-text yellow lighten-4" style="height: 60px; border: dashed;">' + "\n";
  144. html += ' <div class="col s2">' + "\n";
  145. html += ' <i class="material-icons" style="font-size: 3em; line-height: 125%">warning</i>' + "\n";
  146. html += ' </div>' + "\n";
  147. html += ' <div class="col s10 truncate " style="font-size: 1.5em; line-height: 250%">' + "\n";
  148. html += ' ' + msg + "\n";
  149. html += ' </div>' + "\n";
  150. html += ' </div>' + "\n";
  151. html += '</div>' + "\n";
  152. html += '<div class="hide-on-med-and-up">' + "\n";
  153. html += ' <div class="yellow lighten-4 amber-text center" style="border: dashed">' + "\n";
  154. html += ' <br>' + msg + '<br>&nbsp;' + "\n";
  155. html += ' </div>' + "\n";
  156. html += '</div>' + "\n";
  157. return html;
  158. }
  159. function message_conf(msg){
  160. var html = '';
  161. html += '<div class="hide-on-small-only">' + "\n";
  162. html += ' <div class="row green-text green lighten-4" style="height: 60px; border: dashed;">' + "\n";
  163. html += ' <div class="col s2">' + "\n";
  164. html += ' <i class="material-icons" style="font-size: 3em; line-height: 125%">check_circle</i>' + "\n";
  165. html += ' </div>' + "\n";
  166. html += ' <div class="col s10 truncate " style="font-size: 1.5em; line-height: 250%">' + "\n";
  167. html += ' ' + msg + "\n";
  168. html += ' </div>' + "\n";
  169. html += ' </div>' + "\n";
  170. html += '</div>' + "\n";
  171. html += '<div class="hide-on-med-and-up">' + "\n";
  172. html += ' <div class="green lighten-4 green-text center" style="border: dashed">' + "\n";
  173. html += ' <br>' + msg + '<br>&nbsp;' + "\n";
  174. html += ' </div>' + "\n";
  175. html += '</div>' + "\n";
  176. return html;
  177. }
  178. function float_message(text){
  179. $("#float_msg").html(text).slideDown('slow', null);
  180. setTimeout(function(){
  181. $("#float_msg").click();
  182. }, '5000');
  183. }