var first = true; var uniqId = '0'; $(document).ready(function(){ //Ini Materialize M.AutoInit(); //Si message à afficher if($("#msg").val().trim() != ''){ var msg = ''; switch($("#msg").attr('data-type')){ case 'err': msg = message_erreur($("#msg").val()); break; case 'ok': msg = message_conf($("#msg").val()); break; default: msg = message_info($("#msg").val()); } float_message(msg); } //Envoyer formulaire $("#valid").on('click', function(){ //Verif form if($('#save-name').val() == null && $("#name").val().trim() == ''){ float_message(message_erreur('Lanceur vide')); return false; } //Envoi $.post('ajx/roll.php', {name: $("#name").val(), 'save-name': $('#save-name').val(), roll: $("#roll").val(), hide: $("#hide").prop('checked')}, function(data){ if(data.etat = 'ok'){ float_message(message_info('Resultat : ' + data.roll + ' / ' + data.dice)); reload(false); $("#name").val(''); } }, 'json'); return false; }); //Rechargement $(".reload").on('click', function(){ if(first){ reload(false); first = false; } else { reload(true); } }).click(); //Recharge toutes les X secondes perma_check(); //Reset $('.reset').on('click', function(){ reset_score(); }); //Quit $('.quit').on('click', function(){ location.href = './?quit'; }); $("body").on('click', '.float_message', function () { $(this).slideUp('slow', function () { $(this).html(''); }); }); }); function check(){ $.post("ajx/check.php", {'id': uniqId}, function(data){ if(data.etat == 'ok'){ if(data.update){ reload(false); } } }, 'json'); } function perma_check(time){ time = time || 15; setTimeout(function(){ check(); perma_check(time); }, time * 1000); } function reload(msg){ $.post("ajx/score.php", null, function(data){ if(data.etat == 'ok'){ uniqId = data.uniqId; $("#tbody").html(data.html); $("#save-name").html(data.savename); if(msg){ float_message(message_conf("Page rechargée")); } M.AutoInit(); } }, 'json'); } function reset_score(){ $.post("ajx/reset.php", null, function(data){ if(data.etat == 'ok'){ $("#tbody").html(''); $("#save-name").html(''); float_message(message_conf("Session reset")); M.AutoInit(); } }, 'json'); } function message_erreur(msg){ var html = ''; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' error' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' ' + msg + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + msg + '
 ' + "\n"; html += '
' + "\n"; html += '
' + "\n"; return html; } function message_info(msg){ var html = ''; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' info' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' ' + msg + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + msg + '
 ' + "\n"; html += '
' + "\n"; html += '
' + "\n"; return html; } function message_warn(msg){ var html = ''; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' warning' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' ' + msg + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + msg + '
 ' + "\n"; html += '
' + "\n"; html += '
' + "\n"; return html; } function message_conf(msg){ var html = ''; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' check_circle' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += ' ' + msg + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + "\n"; html += '
' + msg + '
 ' + "\n"; html += '
' + "\n"; html += '
' + "\n"; return html; } function float_message(text){ $("#float_msg").html(text).slideDown('slow', null); setTimeout(function(){ $("#float_msg").click(); }, '5000'); }