Advertisement
Guest User

LMN

a guest
Aug 1st, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          [Leek Wars] Last Minute Notifier
  3. // @namespace     https://github.com/Ebatsin/Leek-Wars/
  4. // @version       0.9.3
  5. // @description   Permet d'être averti quelques temps avant minuit si il reste des combats
  6. // @author        Twilight
  7. // @projectPage   https://github.com/Ebatsin/Leek-Wars/
  8. // @updateURL     https://github.com/Ebatsin/Leek-Wars/raw/master/Last%20Minute%20Notifier/LastMinuteNotifier.user.js
  9. // @downloadURL   https://github.com/Ebatsin/Leek-Wars/raw/master/Last%20Minute%20Notifier/LastMinuteNotifier.user.js
  10. // @match         http://leekwars.com/*
  11. // @grant         none
  12. // ==/UserScript==
  13.  
  14. /*
  15. *   Disclaimer : Je passe une demie-heure à chaque changement d'heure pour savoir dans quel sens on doit tourner l'aiguille selon si on avance ou on recule d'heure
  16. *   Il est donc possible qu'il y ai des erreurs dues aux différentes timezone. Si le script ne fait pas ce qu'il doit faire, envoyez moi un MP, je corrigerai ça
  17. */
  18.  
  19. (function() {
  20.     var text = {
  21.                 name: 'Last Minute Notifier',
  22.                 message: 'Il est tard et il te reste des combats, pense à aller les faire !',
  23.                 failMessage: 'Suite à une erreur, il n\'y aurai pas d\'annonce de fin de journée pour vos combats',
  24.                 ok: 'OK', // toi aussi crée des variables utiles
  25.                 toGarden: 'Aller au potager',
  26.         };
  27.     var pop = {
  28.         drop: 0, content: 0, footer: 0, cancel: 0, toGarden: 0, title: 0
  29.     };
  30.     var server = {
  31.         offset: 0, time: 0
  32.     };
  33.     var currentTimeout;
  34.     var minutesBeforeMidnight = getPopTime(20);
  35.     var alreadyShown = localStorage.getItem('LMN.alreadyShown') == "true" || false;
  36.     var alreadyShownDate = localStorage.getItem('LMN.alreadyShownDate') || 0;
  37.    
  38.     function updateServerTime(callback) {
  39.                 $.getJSON('http://api.geonames.org/timezoneJSON?formatted=true&lat=43.577244&lng=7.055041&username=demo&style=full', function(data) {
  40.                         if(data.dstOffset === undefined) {
  41.                 var currentTime = new Date();
  42.                                 console.error('[LMN] Impossible de se connecter au serveur de temps');
  43.                 localStorage.setItem('LMN.log', '[' + currentTime.getDate() + '/' + (currentTime.getMonth() + 1) + ' ' + currentTime.getHours() + ':' + currentTime.getMinutes() + currentTime.getSeconds() + '] Impossible de se connecter au serveur de temps');
  44.                                 // on présuppose qu'on est en france
  45.                                 server.offset =  0;
  46.                                 server.time = new Date();
  47.                                 callback(server.time);
  48.                         }
  49.                         else {
  50.                                 server.offset = 3600000 * (((new Date()).getTimezoneOffset() / -60) - data.dstOffset);
  51.                                 server.time = new Date(Date.now() - server.offset);
  52.                                 callback(server.time);
  53.                         }
  54.                 });
  55.         }
  56.    
  57.     function getPopTime(defaultValue) {
  58.         return localStorage.getItem('LMN.nextPopTime') || defaultValue;
  59.     }
  60.    
  61.     function setPopTime(hours, minutes) {
  62.         if(typeof minutes !== 'number' || typeof hours !== 'number') {
  63.             return false;
  64.         }
  65.         minutes = Math.abs(minutes%60);
  66.         hours = Math.abs(hours%24);
  67.         minutesBeforeMidnight = 1440 - hours*60 - minutes;
  68.         localStorage.setItem('LMN.nextPopTime',  minutesBeforeMidnight);
  69.         clearTimeout(currentTimeout);
  70.         createNotifEvent();
  71.         return true;
  72.     }
  73.    
  74.     function getNextNotifDate() {
  75.                 var notifTime = new Date();
  76.                 notifTime.setDate(notifTime.getDate() + 1);
  77.                 notifTime.setHours(0, 0, 0, 0);
  78.         var serverMidnight = new Date(notifTime.getTime() - server.offset);
  79.                 notifTime = new Date(notifTime.getTime() - minutesBeforeMidnight * 60000 - server.offset);
  80.  
  81.         // notifTime : Heure du prochain affichage de la notif. Heure serveur
  82.         // server.time : heure actuelle du serveur
  83.         // serverMidnight : Minuit, heure serveur
  84.                 if (notifTime.getTime() > server.time.getTime()) {
  85.                         return notifTime; // l'heure d'affichage de la notif est pas encore passée
  86.                 }
  87.         else if(!alreadyShown && Date.now() < serverMidnight.getTime()) { // heure de la notif passée, mais minuit pas encore passé
  88.             localStorage.setItem('LMN.alreadyShown', true);
  89.             localStorage.setItem('LMN.alreadyShownDate', serverMidnight.getTime());
  90.             alreadyShown = true;
  91.             return new Date();
  92.         }
  93.                 else { // minuit passé, ou notif déja affichée, on reporte l'affichage au lendemain
  94.                         notifTime.setDate(notifTime.getDate() + 1);
  95.                         return notifTime;
  96.                 }
  97.         }
  98.    
  99.     function createNotifEvent() {
  100.                 updateServerTime(function(serverTime) {
  101.             var tmp = getNextNotifDate();
  102.                         currentTimeout = setTimeout(function() {
  103.                 if(alreadyShown && serverTime.getTime() > alreadyShownDate) { // on a passé minuit, on reset le fait que la opp-in a déja popée
  104.                     localStorage.setItem('LMN.alreadyShown', false);
  105.                 }
  106.                                 _.get('garden/get/$', function(data) {
  107.                                         if(data.success) {
  108.                                                 if(data.garden.solo_fights + data.garden.farmer_fights + data.garden.team_fights > 0) {
  109.                                                         popNotif(text.message);
  110.                                                 }
  111.                                         }
  112.                                         else {
  113.                                                 popNotif(text.failMessage);
  114.                                         }
  115.                                 });
  116.                                 setTimeout(createNotifEvent, 5000);
  117.                         }, Math.max(tmp.getTime() - server.time.getTime(), 0));
  118.                         window._LMN_next_pop = "[Last Minute Notifier] Le " + tmp.getDate() + ' à ' + tmp.getHours() + ':' + tmp.getMinutes();
  119.                 });
  120.     }
  121.    
  122.     function popNotif(message) {
  123.                 pop.content.text(message);
  124.                 pop.drop.css('top', 0);
  125.         }
  126.  
  127.         function hideNotif() {
  128.                 pop.drop.css('top', -pop.drop.outerHeight());
  129.         }
  130.    
  131.     function getHMFromM(minutes) {
  132.         return {
  133.             'hours': Math.floor((1440 - minutes)/60),
  134.             'minutes': (1440 - minutes)%60
  135.         };
  136.     }
  137.  
  138.     function initDropdown() {
  139.                 if(LW.farmer.id === undefined) {
  140.                         setTimeout(initDropdown, 1000);
  141.                         return;
  142.                 }
  143.        
  144.         LW.on('pageload', function() {
  145.             if(LW.currentPage === 'settings') {
  146.                  $('#settings-page .flex-container')
  147.                 .first()
  148.                 .append('<div class="column6"><div class="panel"><div class="header"><h2>[Userscript] Last Minute Notifier</h2></div><div class="content"><h4 style="text-align: left">Veuillez entrer l\'heure d\'affichage de la notification</h4><br><h4>Heures</h4><input type="number" id="LMN_hours"><br/><h4 style="margin-top: 0.6em">Minutes</h4><input type="number" id="LMN_minutes"><br></br></br><center><input type="submit" class="button green" value="Appliquer" id="LMN_apply"></center></div></div></div>');
  149.  
  150.                 $('#LMN_hours').val(getHMFromM(minutesBeforeMidnight).hours);
  151.                 $('#LMN_minutes').val(getHMFromM(minutesBeforeMidnight).minutes);
  152.                 $('#LMN_apply').click(function() {
  153.                     if(setPopTime(parseInt($('#LMN_hours').val()), parseInt($('#LMN_minutes').val()))) {
  154.                         _.toast('Heure de notification mise à jour');
  155.                     }
  156.                     else {
  157.                         _.toast('Heures non valide');
  158.                     }
  159.                     $('#LMN_hours').val(getHMFromM(minutesBeforeMidnight).hours);
  160.                     $('#LMN_minutes').val(getHMFromM(minutesBeforeMidnight).minutes);
  161.                 });
  162.             }
  163.         });
  164.                
  165.                 // création du dropdown
  166.                 pop.drop = $(document.createElement('div'))
  167.                                                 .css({width: '70%', position: 'fixed', left: '15%', top: '-1000px', 'background-image': 'url("http://leekwars.com/static/image/background.png")', 'z-index': 2000, 'transition': 'ease 0.6s top'})
  168.                                                 .appendTo(document.body);
  169.                 pop.title = $(document.createElement('div'))
  170.                                                 .text(text.name)
  171.                                                 .css({'text-align': 'center', height: '3em', 'line-height': '3em', 'font-size': '2em', color: 'white', background: 'hsla(0, 0%, 100%, 0.2)'})
  172.                                                 .appendTo(pop.drop);
  173.                 pop.content = $(document.createElement('div'))
  174.                                                 .css({padding: '1.5em', color: 'white', 'font-size': '1.5em', 'text-align': 'center'})
  175.                                                 .appendTo(pop.drop);
  176.                 pop.footer = $(document.createElement('div'))
  177.                                                 .appendTo(pop.drop);
  178.                 pop.cancel = $(document.createElement('button'))
  179.                                                 .text(text.ok)
  180.                                                 .css({width: '50%', background: '#555', padding: '0.6em', color: '#eee', 'text-align': 'center', border: 'none', 'font-size': '1.5em', cursor: 'pointer'})
  181.                                                 .click(hideNotif)
  182.                                                 .hover(function() {
  183.                                                         pop.cancel.css('background', '#777');
  184.                                                 }, function() {
  185.                                                         pop.cancel.css('background', '#555');
  186.                                                 })
  187.                                                 .appendTo(pop.footer);
  188.                 pop.toGarden = $(document.createElement('button'))
  189.                                                 .text(text.toGarden)
  190.                                                 .css({width: '50%', background: '#5FAD1B', padding: '0.6em', color: '#eee', 'text-align': 'center', border: 'none', 'font-size': '1.5em', cursor: 'pointer'})
  191.                                                 .click(function() {
  192.                                                         hideNotif();
  193.                                                         LW.page('/garden');
  194.                                                 })
  195.                                                 .hover(function() {
  196.                                                         pop.toGarden.css('background', '#73D120');
  197.                                                 }, function() {
  198.                                                         pop.toGarden.css('background', '#5FAD1B');
  199.                                                 })
  200.                                                 .appendTo(pop.footer);
  201.  
  202.                 hideNotif();
  203.                 createNotifEvent(); // on initialise l'event
  204.         }
  205.        
  206.         initDropdown();
  207.    
  208. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement