bpunter / Réservation Crossfit

// ==UserScript==
// @name         Réservation Crossfit
// @description  Réservation automatique ou manuelle de créneau de Crossfit
// @version      2.0
// @updateURL    https://openuserjs.org/meta/bpunter/Réservation_Crossfit.meta.js
// @author       Bpunter <blakpunter@gmail.com>
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require      https://raw.githubusercontent.com/HubSpot/vex/master/js/vex.min.js
// @match        http://www.heitzsystem.com/mobile/*

// ==/UserScript==
!function(){"use strict";var e={notifier:function(e,t){if("Notification"in window)if("granted"===Notification.permission){new Notification(e,{body:t})}else"denied"!==Notification.permission&&Notification.requestPermission(function(i){if("granted"===i){new Notification(e,{body:t})}});else alert("This browser does not support desktop notification")}},t={idSite:"ud2Q/yVu18FTPtYFtICXhIPY7PwtrUYH",codeWOD:100128449,codeOPEN:100309422,codeSPEC:100290586,planning:null,availables:null,selections:null,getPlanning:function(){return this.planning},setPlanning:function(e){this.planning=e},load:function(){var e=this;n.getPlanning(function(t){e.setPlanning(t),console.info("Plannings chargés avec succès."),e.availableCourses()},function(){},"",this.makeDate(new Date),14)},makeDate:function(e){var t=e.getMonth()+1;10>t&&(t="0"+t);var i=e.getDate();return 10>i&&(i="0"+i),i+"-"+t+"-"+e.getFullYear()},availableCourses:function(){for(var e=[],t=0,i=this.planning.length;i>t;t++){var n=this.planning[t];n.occupe<n.place&&e.push(n)}this.availables=e,this.selection([2,4],"12")},makefavoritesDaysAndHours:function(e,t){for(var i=new Date,n=[],a=0;15>a;a++)-1!==e.indexOf(i.getDay())&&n.push(this.makeDate(i)+" "+t),i.setDate(i.getDate()+1);return console.info("Vos jours favoris sont "+n.join("h, ")+"h."),n},selection:function(e,t){var n=[];this.selections=[];for(var a=this.makefavoritesDaysAndHours(e,t),o=0,s=this.availables.length;s>o;o++)for(var r=this.availables[o],l=0,u=a.length;u>l;l++){var c=a[l];-1!==r.debut.indexOf(c)&&n.push(r)}this.selections=n,i.compare(this.selections)}},i={clientId:0,reservations:[],email:"",password:"",setReservation:function(e){this.reservations=e},getReservation:function(){return this.reservations},setClientId:function(e){this.clientId=e},updateReservation:function(){var e=this;n.reservationList(function(t){e.reservations=[],e.setReservation(t),o.fillResaList(t)},function(){})},connectAndUpdate:function(e,i){var a=this;this.email=e,this.password=i,n.connect(function(){a.updateReservation(),t.load()},function(){},e,i)},isAlreadyReserved:function(e){for(var t=!1,i=0,n=this.reservations.length;n>i;i++)this.reservations[i].debut===e.debut&&(t=!0);return t},compare:function(e){var t=!1;if(e.length>0){var i=[];$.each(e,function(e,t){i.push(t.debut)}),console.info("Voici les créneaux correspondants à votre demande: "+i.join(", ")+".");for(var o=0,s=e.length;s>o;o++){var r=e[o];this.isAlreadyReserved(r)?console.info("Vous avez déjà réservé le WOD du "+r.debut+"."):(t=!0,a.automatic?n.reserver(r.id):confirm("Réserver le WOD du "+r.debut+" ?")&&n.reserver(r.id))}}else console.info("Aucun créneaux ne correpond.")}},n={url:"http://193.248.43.185:8080/json",sessionId:0,connect:function(e,t,n,a){var o={email:n,code:a,clientId:0,sessionId:0},s=this.constructData(1,o),r=this;this.call(function(n){"ok"===n.status?(i.setClientId(n.idClient),r.idSession=n.idSession,e(!0)):t(n)},function(e){},s)},reservationList:function(e,t){var n={},o=this.constructData(5,n);this.call(function(t){"ok"===t.status?e(t.reservation):a.start(i.email,i.password)},function(e){},o)},getPlanning:function(e,n,o,s,r){var l={nombreJourPlanning:u,datePlanning:s},u=r||1;switch(o){case"spec":l.idRequete=t.codeSPEC;break;case"open":l.idRequete=t.codeOPEN;break;default:l.idRequete=t.codeWOD}var c=this.constructData(4,l);this.call(function(t){"ok"===t.status?e(t.planning):a.start(i.email,i.password)},function(e){},c)},reserver:function(t){var i={};i.idRequete=t;var n=this.constructData(301,i);this.call(function(t){"ok"===t.status&&e.notifier("Réservation","Votre réservation a bien été effectuée.")},function(e){},n)},constructData:function(e,n){for(var a={idSession:this.idSession,idClient:i.clientId,idSite:t.idSite,status:0,idErreur:0,type:e},o=0,s=Object.keys(n).length;s>o;o++){var r=Object.keys(n)[o];a[r]=n[r]}return a},call:function(e,t,i){var n=this;$.ajax({type:"POST",url:n.url,data:i,dataType:"json"}).done(function(t){e(t)})}},a={started:!0,automatic:!1,startTime:null,elapsedTime:0,start:function(e,t,n){this.automatic=n,i.connectAndUpdate(e,t),this.started&&(this.started=!1,this.run(this.getInterval()))},run:function(e){var t=this;setInterval(function(){console.clear(),t.start(i.email,i.password,i.automatic)},e)},end:function(){this.elapsedTime=(new Date).getTime()-this.startTime,this.elapsedTime=Math.round(this.elapsedTime/1e3),console.info("Script effectué en "+this.elapsedTime+"s.")},getInterval:function(){return Math.floor(20001*Math.random())+3e4}},o={loadCss:function(){var e="<style>";e+="#config_window {background-color:#eee;color:#333;height:100vh;width:25vw;display:flex;flex-direction:column;justify-content:center;position:absolute;top:0;left:0;padding:10px;min-width:270px}",e+="#config_window h2 {border-bottom:1px #333 solid;margin-bottom:20px;padding:5px;}#config_window label {text-transform: uppercase;margin-top:5px;}",e+="#jours{display:flex;justify-content:space-between;flex-flow:row wrap;margin-bottom:15px}#jours label{display:block;width:33.33%;min-width:88px;text-transform:capitalize}",e+="#config_window button{width:100%;padding:10px;text-align:center}#spinner{max-width:80px}",e+="</style><link href='https://raw.githubusercontent.com/HubSpot/vex/master/css/vex.css'><link href='https://raw.githubusercontent.com/HubSpot/vex/master/css/vex-theme-os.css'>",$("head").append(e)},getForm:function(){var e='<div id="config_window"><div id="config_content">';e+="<h2>Configuration</h2><h3>Version 2.0</h3><form>",e+="<label>Email</label>",e+='<div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-a"><input type="email" name="email" placeholder="email" id="log_email" class="metro-txt ui-input-text ui-body-a"></div>',e+="<br/><label>Mot de passe</label>",e+='<div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-a"><input type="password" name="password" placeholder="Mot de passe" id="log_password" class="metro-txt ui-input-text ui-body-a"></div>',e+='<br/><label>Jours</label><div id="jours"><label><input type="checkbox" name="jours[]" value="1">&nbsp;Lundi</label><label><input type="checkbox" name="jours[]" value="2">&nbsp;Mardi</label><label><input type="checkbox" name="jours[]" value="3">&nbsp;Mercredi</label>',e+='<label><input type="checkbox" name="jours[]" value="4">&nbsp;Jeudi</label><label><input type="checkbox" name="jours[]" value="5">&nbsp;Vendredi</label><label><input type="checkbox" name="jours[]" value="6">&nbsp;Samedi</label></div>',e+='<label>Heures</label><div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-a"><input type="number" id="fav_hours" name="heure" step="1" min="7" max="20" class="metro-txt ui-input-text ui-body-a"></div>',e+='<br/><label><input type="checkbox" id="auto" name="Automatique">&nbsp;Automatique</label><br/>',e+='<button id="config" class="ui-btn ui-shadow ui-btn-corner-all ui-btn-up-a" data-disabled="false">Enregistrer</button>',e+='</form><div style="text-align:center"><img id="spinner" src="http://www.ramtrucks.com/shared/htmlcolorizer/images/colorizer/spinner_animation02.gif" style="display: inline;">',e+="</div></div></div>",$("#login").append(e)},fillResaList:function(e){var t='<div id="resa"><h3>Vos r&eacute;servation</h3><ul>';$.each(e,function(e,i){t+="<li>WOD le "+i.debut+"</li>"}),t+="</ul></div>",$("#resa").remove(),$("#config_content").append(t),$("#config_content form").hide()},fill:function(){$("#log_email").val(i.email),$("#log_password").val(i.password),i.automatic&&$("#auto").prop("checked"),$("#fav_hours").val(i.favHours),$("#jours input").each(function(e,t){-1!==i.favDays.indexOf($(t).val())&&$(t).prop("checked")})},display:function(){this.loadCss(),this.getForm()}};o.display(),$("#spinner").hide(),$("#config").on("click",function(e){e.preventDefault(),i.email=$("#log_email").val(),i.password=$("#log_password").val(),i.automatic=$("#auto:checked").length>0,i.favHours=$("#fav_hours").val();var t=[];$("#jours input:checked").each(function(e,i){t.push(parseInt($(i).val()))}),i.favDays=t,a.start(i.email,i.password,i.automatic),$("#config").fadeOut(),$("#spinner").show()})}();