From 5386709b1a079de2abff223bc660a3a4286de562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 18:11:34 +0100 Subject: [PATCH 01/16] adding object to handle timeouts fixes #203 adding jsdoc refs #135 --- src/mods/core.js | 185 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 1 deletion(-) diff --git a/src/mods/core.js b/src/mods/core.js index 3569d01..5bb3f3a 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -1,4 +1,9 @@ idrinth.core = { + /** + * + * @param {string} str + * @returns {string} + */ escapeRegExp: function ( str ) { return str.replace ( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&" ); }, @@ -12,13 +17,41 @@ idrinth.core = { fieldIsSetting: function ( parent, field, allowObjects ) { return parent && typeof parent === 'object' && field && parent.hasOwnProperty ( field ) && ( parent[field] === null || typeof parent[field] !== 'object' || allowObjects ) && typeof parent[field] !== 'function'; }, + /** + * + * @type {object} + */ ajax: { + /** + * + * @type {object} + */ + active: { }, + /** + * + * @param {string} url + * @param {function} success + * @param {function} failure + * @param {function} timeout + * @param {string} additionalHeader + * @param {Boolean} [false] isStatic + * @returns {undefined} + */ runHome: function ( url, success, failure, timeout, additionalHeader, isStatic ) { var server = isStatic ? 'static' : ( idrinth.settings.get ( "isWorldServer" ) ? 'world-' : '' ) + idrinth.platform; var homeUrl = 'https://dotd.idrinth.de/' + server + ( '/' + url ).replace ( /\/\//, '/' ); idrinth.core.ajax.run ( homeUrl, success, failure, timeout, additionalHeader ); }, - active: { }, + /** + * + * @param {string} url + * @param {function} success + * @param {function} failure + * @param {function} timeout + * @param {string} additionalHeader + * @param {Boolean} [false] isStatic + * @returns {undefined} + */ run: function ( url, success, failure, timeout, additionalHeader ) { 'use strict'; var requestHandler = new XMLHttpRequest ( ); @@ -68,7 +101,16 @@ idrinth.core = { idrinth.core.ajax.active[url].send ( ); } }, + /** + * + * @type {object} + */ copyToClipboard: { + /** + * + * @param {string} text + * @returns {Boolean} + */ text: function ( text ) { var success; try { @@ -87,6 +129,11 @@ idrinth.core = { idrinth.ui.removeElement ( "idrinth-copy-helper" ); return success; }, + /** + * + * @param {HTMLElement} element + * @returns {string} + */ element: function ( element ) { if ( element.hasAttribute ( 'data-clipboard-text' ) ) { return idrinth.core.copyToClipboard.text ( element.getAttribute ( 'data-clipboard-text' ) ); @@ -97,6 +144,12 @@ idrinth.core = { return idrinth.core.copyToClipboard.text ( element.innerHTML ); } }, + /** + * + * @param {string} title + * @param {string|HTMLElement} content + * @returns {Boolean|window.Notification} + */ sendNotification: function ( title, content ) { if ( !( "Notification" in window ) ) { return false; @@ -112,18 +165,135 @@ idrinth.core = { body: content } ); }, + /** + * + * @type {object} + */ + timeouts: { + /** + * + * @type {object} + */ + next: null, + /** + * + * @type {object} + */ + list: { }, + /** + * + * @param {string} identifier + * @returns {undefined} + */ + remove: function ( identifier ) { + 'use strict'; + if ( idrinth.core.timeouts.list[identifier] !== undefined ) { + delete idrinth.core.timeouts.list[identifier]; + } + }, + /** + * + * @param {string} identifier + * @param {function} func + * @param {int} time in milliseconds + * @param {Boolean} [false] isInterval + * @returns {undefined} + */ + add: function ( identifier, func, time, isInterval ) { + 'use strict'; + var date = new Date (); + idrinth.core.timeouts.list[identifier] = { + func: func, + next: date.getTime () + date.getMilliseconds () + time / 1000, + duration: time, + interval: !!isInterval + }; + }, + /** + * activates all relevant timeouts and intervals + * @returns {undefined} + */ + process: function () { + 'use strict'; + var date = ( new Date () ).getTime () + ( new Date () ).getMilliseconds (); + var min = 10000; + /** + * + * @param {Number} durationLeft + * @param {Number} minDuration + * @returns {Number} + */ + var getVal = function ( durationLeft, minDuration ) { + if ( durationLeft < 0.1 ) { + return 0.1; + } + return durationLeft < minDuration ? durationLeft : minDuration; + }; + for (var property in idrinth.core.timeouts.list) { + if ( idrinth.core.timeouts.list.hasOwnProperty ( property ) ) { + if ( date >= idrinth.core.timeouts.list[property].next ) { + try { + idrinth.core.timeouts.list[property].func (); + if ( idrinth.core.timeouts.list[property].interval ) { + min = getVal ( idrinth.core.timeouts.list[property].duration, min ); + idrinth.core.timeouts.list[property].next = date + idrinth.core.timeouts.list[property].duration; + } else { + delete idrinth.core.timeouts.list[property]; + } + } catch ( e ) { + idrinth.core.log ( e.message ? e.message : e.getMessage () ); + } + } else { + min = getVal ( idrinth.core.timeouts.list[property].next - date, min ); + } + } + } + idrinth.core.timeouts.next = window.setTimeout ( idrinth.core.timeouts.process, min * 1000 ); + } + }, + /** + * + * @param {string} string + * @returns {undefined} + */ log: function ( string ) { 'use strict'; console.log ( '[IDotDS] ' + string ); }, + /** + * + * @param {string} text + * @returns {undefined} + */ alert: function ( text ) { idrinth.ui.buildModal ( 'Info', text ); }, + /** + * + * @param {string} text + * @param {function} callback + * @returns {undefined} + */ confirm: function ( text, callback ) { idrinth.ui.buildModal ( 'Do you?', text, callback ); }, + /** + * + * @type {object} + */ multibind: { + /** + * + * @type {object} + */ events: { }, + /** + * + * @param {string} event + * @param {string} selector + * @param {function} method + * @returns {undefined} + */ add: function ( event, selector, method ) { var bind = function ( event, selector, method ) { idrinth.core.multibind.events[event] = idrinth.core.multibind.events[event] ? idrinth.core.multibind.events[event] : { }; @@ -138,7 +308,20 @@ idrinth.core = { } bind ( event, selector, method ); }, + /** + * + * @param {HTMLElement} element + * @param {string} event + * @returns {undefined} + */ triggered: function ( element, event ) { + /** + * + * @param {HTMLElement} el + * @param {string} event + * @param {string} selector + * @returns {undefined} + */ var handleElement = function ( el, event, selector ) { if ( !el ) { return; From 6b1be3ac63c3fd05a0c26ba78b30ea94ee1b8b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 22:30:38 +0100 Subject: [PATCH 02/16] fixes #203 moving intervals and timeouts to new wrapper --- src/mods/chat.js | 16 +++++++-------- src/mods/names.js | 15 +++++--------- src/mods/raids.js | 25 +++++++---------------- src/mods/tier.js | 6 +++--- src/mods/ui.js | 5 ++--- src/mods/user.js | 46 ++++++++++++++++++++--------------------- src/mods/war.js | 13 +++--------- src/stable.js | 52 ++++++++++++++++------------------------------- 8 files changed, 68 insertions(+), 110 deletions(-) diff --git a/src/mods/chat.js b/src/mods/chat.js index 08bd3a1..da9fc3a 100644 --- a/src/mods/chat.js +++ b/src/mods/chat.js @@ -167,7 +167,7 @@ idrinth.chat = { processMessages ( data.messages ); } idrinth.chat.oldMessages = [ ]; - idrinth.chat.updateTimeout = window.setTimeout ( idrinth.chat.refreshChats, 999 ); + idrinth.core.timeouts.add ( 'chat', idrinth.chat.refreshChats, 999 ); }; var refreshMembers = function () { var applyMembers = function ( data ) { @@ -249,7 +249,7 @@ idrinth.chat = { idrinth.chat.messages.unshift ( idrinth.chat.oldMessages[count] ); } idrinth.chat.oldMessages = [ ]; - window.setTimeout ( idrinth.chat.refreshChats, 999 ); + idrinth.core.timeouts.add ( 'chat', idrinth.chat.refreshChats, 999 ); }, userclick: function ( element, user, chat ) { 'use strict'; @@ -707,14 +707,14 @@ idrinth.chat = { idrinth.chat.elements.chats = document.getElementById ( 'idrinth-chat' ).getElementsByTagName ( 'ul' )[1]; idrinth.chat.elements.menu = document.getElementById ( 'idrinth-chat' ).getElementsByTagName ( 'ul' )[0]; } - window.setTimeout ( function () { + idrinth.core.timeouts.add ( 'chat.login', function () { idrinth.core.ajax.runHome ( 'chat-service/login/', idrinth.chat.startLoginCallback, function ( reply ) { }, function ( reply ) { - window.setTimeout ( idrinth.chat.login, 1 ); + idrinth.core.timeouts.add ( 'chat.login', idrinth.chat.login, 1 ); }, JSON.stringify ( { user: idrinth.settings.get ( "chatuser" ), @@ -722,7 +722,7 @@ idrinth.chat = { } ) ); }, 2500 ); - window.setTimeout ( function () { + idrinth.core.timeouts.add ( 'chat.emoticons', function () { idrinth.core.ajax.runHome ( 'emoticons/data/', function ( reply ) { @@ -807,7 +807,7 @@ idrinth.chat = { if ( idrinth.chat.updateTimeout ) { return; } - idrinth.chat.updateTimeout = window.setTimeout ( idrinth.chat.refreshChats, 1500 ); + idrinth.core.timeouts.add ( 'chat', idrinth.chat.refreshChats, 1500 ); }, startLoginCallback: function ( data ) { if ( !data ) { @@ -939,7 +939,7 @@ idrinth.chat = { idrinth.core.alert ( idrinth.text.get ( "chat.error.login" ) ); }, timeout = function () { - window.setTimeout ( idrinth.chat.login, 1 ); + idrinth.core.timeouts.add ( 'chat.login', idrinth.chat.login, 1 ); }, headers = { user: '', @@ -952,7 +952,7 @@ idrinth.chat = { if ( key === 'relogin' ) { success = function () { - idrinth.chat.updateTimeout = window.setTimeout ( idrinth.chat.refreshChats, 1500 ); + idrinth.core.timeouts.add ( 'chat', idrinth.chat.refreshChats, 1500 ); }; headers.user = idrinth.settings.get ( "chatuser" ); headers.pass = idrinth.settings.get ( "chatpass" ); diff --git a/src/mods/names.js b/src/mods/names.js index 13cb34d..3b9e733 100644 --- a/src/mods/names.js +++ b/src/mods/names.js @@ -11,11 +11,6 @@ idrinth.names = { * @type {object} */ guilds: { }, - /** - * a timeout - * @type {object} - */ - ownTimeout: null, /** * @type {Number} */ @@ -106,7 +101,7 @@ idrinth.names = { } }; try { - if ( idrinth.names.counter % 300 === 0 || Object.keys ( idrinth.names.users ).length === 0 ) { + if ( idrinth.names.counter === 0 || Object.keys ( idrinth.names.users ).length === 0 ) { load ( Object.keys ( idrinth.names.classes ).length === 0 ? 'init/' : 'get/' ); } else if ( Object.keys ( idrinth.names.users ).length > 0 ) { add ( ); @@ -114,8 +109,8 @@ idrinth.names = { } catch ( e ) { idrinth.core.log ( e ); } - idrinth.names.counter = idrinth.names.counter + 1; - idrinth.names.ownTimeout = window.setTimeout ( idrinth.names.run, 6666 ); + idrinth.names.counter = ( idrinth.names.counter + 1 ) % 300; + idrinth.core.timeouts.add ( 'names', idrinth.names.run, 6666 ); }, /** * initialises the module @@ -258,7 +253,7 @@ idrinth.names = { idrinth.names.isHovering = false; var name = idrinth.names.parse ( element ).toLowerCase ( ); if ( idrinth.settings.get ( "names" ) && idrinth.ui.tooltip && idrinth.names.users[name] ) { - window.clearTimeout ( idrinth.ui.tooltipTO ); + idrinth.core.timeouts.remove ( 'names.tooltip' ); idrinth.ui.tooltip.setAttribute ( 'style', idrinth.ui.getElementPositioning ( element, -200, -100 ) ); tooltip ( idrinth.names.users[name].kongregate, idrinth.ui.tooltip.firstChild, false ); tooltip ( idrinth.names.users[name].world, idrinth.ui.tooltip.lastChild, true ); @@ -267,7 +262,7 @@ idrinth.names = { }; if ( idrinth.platform === 'kongregate' ) { idrinth.core.multibind.add ( 'mouseover', '.chat_message_window .username', showTooltip ); - idrinth.names.ownTimeout = window.setTimeout ( idrinth.names.run, 10000 ); + idrinth.core.timeouts.add ( 'names', idrinth.names.run, 10000 ); build (); } } diff --git a/src/mods/raids.js b/src/mods/raids.js index ff38b83..682b3e3 100644 --- a/src/mods/raids.js +++ b/src/mods/raids.js @@ -2,19 +2,7 @@ idrinth.raids = { script: null, list: { }, joined: { }, - interval: null, requested: 0, - clearInterval: function () { - try { - window.clearInterval ( idrinth.raids.interval ); - } catch ( e ) { - idrinth.raids.interval = null; - } - }, - restartInterval: function () { - this.clearInterval (); - idrinth.raids.interval = window.setInterval ( idrinth.raids.join.process, 1500 ); - }, import: function ( id ) { 'use strict'; if ( !idrinth.platform ) { @@ -59,13 +47,13 @@ idrinth.raids = { return ( ( Object.keys ( idrinth.raids.joined ) ).concat ( Object.keys ( idrinth.raids.list ) ) ).join (); }, clearAll: function () { - this.clearInterval ( ); + idrinth.core.timeouts.remove ( 'raids' ); while ( document.getElementById ( "idrinth-raid-link-list" ).firstChild ) { idrinth.ui.removeElement ( document.getElementById ( "idrinth-raid-link-list" ).firstChild.id ); } idrinth.raids.list = { }; idrinth.raids.joined = { }; - idrinth.raids.restartInterval (); + idrinth.raids.start (); }, join: { data: { @@ -172,7 +160,7 @@ idrinth.raids = { trying: function ( key ) { 'use strict'; ( ( function ( key ) { - window.setTimeout ( function () { + idrinth.core.timeouts.add ( 'raid.join.' + key, function () { var id = 'idrinth-raid-link-' + key; if ( document.getElementById ( id ) ) { idrinth.ui.removeElement ( id ); @@ -232,7 +220,7 @@ idrinth.raids = { }, { name: 'onload', - value: 'try{event.stopPropagation();}catch(e){}window.setTimeout(){function(){idrinth.ui.removeElement(\'' + key + '\');},1234);' + value: 'try{event.stopPropagation();}catch(e){}idrinth.core.timeouts.add(\'raids.join.cleanup.' + key + '\',{function(){idrinth.ui.removeElement(\'' + key + '\');},1234);' }, { name: 'onunload', @@ -241,7 +229,7 @@ idrinth.raids = { ] } ); ( ( function ( key ) { - return window.setTimeout ( function () { + return idrinth.core.timeouts.add ( 'raids.join.remove.' + key, function () { idrinth.ui.removeElement ( 'join-' + key ); }, 30000 ); } ) ( key ) ); @@ -317,6 +305,7 @@ idrinth.raids = { }, start: function ( ) { 'use strict'; - idrinth.raids.restartInterval (); + idrinth.core.timeouts.remove ( 'raids' ); + idrinth.core.timeouts.add ( 'raids', idrinth.raids.join.process, 1500, true ); } }; diff --git a/src/mods/tier.js b/src/mods/tier.js index e7f37ae..5662e1e 100644 --- a/src/mods/tier.js +++ b/src/mods/tier.js @@ -140,7 +140,7 @@ idrinth.tier = { } } } else { - window.setTimeout ( idrinth.tier.start, 1000 ); + idrinth.core.timeouts.add ( 'tier', idrinth.tier.start, 1000 ); } }; while ( 0 < window.innerWidth - 140 * ( pos + 1 ) ) { @@ -151,10 +151,10 @@ idrinth.tier = { 'tier-service/', importData, function ( ) { - window.setTimeout ( idrinth.tier.start, 10000 ); + idrinth.core.timeouts.add ( 'tier', idrinth.tier.start, 10000 ); }, function ( ) { - window.setTimeout ( idrinth.tier.start, 10000 ); + idrinth.core.timeouts.add ( 'tier', idrinth.tier.start, 10000 ); } ); }, diff --git a/src/mods/ui.js b/src/mods/ui.js index eb53028..4645170 100644 --- a/src/mods/ui.js +++ b/src/mods/ui.js @@ -14,7 +14,7 @@ idrinth.ui = { }, buildChat: function ( id, name, rank, pass ) { if ( !idrinth.chat.elements.chats ) { - window.setTimeout ( function () { + idrinth.core.timeouts.add ( 'chat-' + id, function () { idrinth.ui.buildChat ( id, name, rank, pass ); }, 500 ); } @@ -200,7 +200,6 @@ idrinth.ui = { return el; }, controls: null, - tooltipTO: null, buildModal: function ( title, content, altFunc ) { var mod = { children: [ ], @@ -265,7 +264,7 @@ idrinth.ui = { } }, setTooltipTimeout: function () { - idrinth.ui.tooltipTO = window.setTimeout ( idrinth.ui.hideTooltip, idrinth.settings.get ( "timeout" ) ? idrinth.settings.get ( "timeout" ) : 5000 ); + idrinth.core.timeouts.add ( 'names.tooltip', idrinth.ui.hideTooltip, idrinth.settings.get ( "timeout" ) ? idrinth.settings.get ( "timeout" ) : 5000 ); }, hideTooltip: function () { if ( idrinth.names.isHovering ) { diff --git a/src/mods/user.js b/src/mods/user.js index 8adc30d..1877617 100644 --- a/src/mods/user.js +++ b/src/mods/user.js @@ -27,32 +27,32 @@ idrinth.user = { idrinth.user.id = ag[1]; idrinth.user.token = ag[2]; } - window.setTimeout ( idrinth.user.sendAlive, 20000 ); - }, - sendAlive: function () { - var getIdentifier = function () { - var guid = function () { - //from http://stackoverflow.com/a/105074 - var s4 = function () { - return Math.floor ( ( 1 + Math.random () ) * 0x10000 ).toString ( 36 ); + var sendAlive = function () { + var getIdentifier = function () { + var guid = function () { + //from http://stackoverflow.com/a/105074 + var s4 = function () { + return Math.floor ( ( 1 + Math.random () ) * 0x10000 ).toString ( 36 ); + }; + return s4 () + '-' + + s4 () + s4 () + '-' + + s4 () + s4 () + s4 () + '-' + + s4 () + s4 () + s4 () + s4 () + '-' + + s4 () + s4 () + s4 () + s4 () + s4 () + '-' + + s4 () + s4 () + s4 () + s4 () + s4 () + s4 (); }; - return s4 () + '-' + - s4 () + s4 () + '-' + - s4 () + s4 () + s4 () + '-' + - s4 () + s4 () + s4 () + s4 () + '-' + - s4 () + s4 () + s4 () + s4 () + s4 () + '-' + - s4 () + s4 () + s4 () + s4 () + s4 () + s4 (); + idrinth.user.identifier = window.localStorage.getItem ( 'idrinth-dotd-uuid' ); + if ( !idrinth.user.identifier || idrinth.user.identifier === '' || idrinth.user.identifier === null || !idrinth.user.identifier.match ( /^[a-z0-9]{4}-[a-z0-9]{8}-[a-z0-9]{12}-[a-z0-9]{16}-[a-z0-9]{20}-[a-z0-9]{24}$/ ) ) { + idrinth.user.identifier = guid (); + } + window.localStorage.setItem ( 'idrinth-dotd-uuid', idrinth.user.identifier ); + return idrinth.user.identifier; }; - idrinth.user.identifier = window.localStorage.getItem ( 'idrinth-dotd-uuid' ); - if ( !idrinth.user.identifier || idrinth.user.identifier === '' || idrinth.user.identifier === null || !idrinth.user.identifier.match ( /^[a-z0-9]{4}-[a-z0-9]{8}-[a-z0-9]{12}-[a-z0-9]{16}-[a-z0-9]{20}-[a-z0-9]{24}$/ ) ) { - idrinth.user.identifier = guid (); + if ( !window.localStorage ) { + return; } - window.localStorage.setItem ( 'idrinth-dotd-uuid', idrinth.user.identifier ); - return idrinth.user.identifier; + idrinth.core.ajax.runHome ( 'i-am-alive/' + getIdentifier () + '/' ); }; - if ( !window.localStorage ) { - return; - } - idrinth.core.ajax.runHome ( 'i-am-alive/' + getIdentifier () + '/' ); + idrinth.core.timeouts.add ( 'user', sendAlive, 20000 ); } }; diff --git a/src/mods/war.js b/src/mods/war.js index 3e9c0cc..b171bb2 100644 --- a/src/mods/war.js +++ b/src/mods/war.js @@ -2,16 +2,9 @@ idrinth.war = { from: null, to: null, element: null, - warTO: null, setTO: function ( ) { - if ( !idrinth.war.warTO ) { - idrinth.war.warTO = window.setTimeout ( - idrinth.war.getData, - ( idrinth.war.element.getAttribute ( 'class' ) ).match ( /(^|\s)idrinth-hide($|\s)/ ) !== null ? - 30000 : - 120000 - ); - } + var active = ( idrinth.war.element.getAttribute ( 'class' ) ).match ( /(^|\s)idrinth-hide($|\s)/ ) !== null; + idrinth.core.timeouts.add ( 'war', idrinth.war.getData, active ? 30000 : 120000 ); }, getData: function () { var raids2Join = function () { @@ -246,7 +239,7 @@ idrinth.war = { ); idrinth.ui.body.appendChild ( idrinth.war.element ); }; - idrinth.war.warTO = window.setTimeout ( idrinth.war.getData, 5000 ); + idrinth.core.timeouts.add ( 'war', idrinth.war.getData, 5000 ); build (); } }; \ No newline at end of file diff --git a/src/stable.js b/src/stable.js index ee29345..b640233 100644 --- a/src/stable.js +++ b/src/stable.js @@ -3,10 +3,9 @@ var idrinth = { windowactive: true, facebook: { popup: null, - timeout: null, restart: function () { try { - window.clearTimeout ( idrinth.facebook.timeout ); + idrinth.core.timeouts.remove ( 'facebook' ); idrinth.facebook.popup.close (); idrinth.ui.reloadGame (); idrinth.raids.clearAll (); @@ -15,17 +14,12 @@ var idrinth = { } }, rejoin: function () { - try { - window.clearInterval ( idrinth.raids.interval ); - } catch ( e ) { - idrinth.core.log ( e ); - } + idrinth.core.timeouts.remove ( 'raids' ); idrinth.facebook.popup = window.open ( "https://apps.facebook.com/dawnofthedragons/" ); idrinth.facebook.popup.onload = function () { - window.clearTimeout ( idrinth.facebook.timeout ); - idrinth.facebook.timeout = window.setTimeout ( idrinth.facebook.restart, 3333 ); + idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 3333 ); }; - idrinth.facebook.timeout = window.setTimeout ( idrinth.facebook.restart, 11111 ); + idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 11111 ); } }, newgrounds: { @@ -42,9 +36,9 @@ var idrinth = { alarmCheck: function () { var now = new Date (); if ( idrinth.settings.get ( "alarmActive" ) && now.getHours () + ':' + now.getMinutes () === idrinth.settings.get ( "alarmTime" ) ) { - window.setTimeout ( idrinth.newgrounds.joinRaids, 1 ); + idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.joinRaids, 1 ); } - window.setTimeout ( idrinth.newgrounds.alarmCheck, 60000 ); + idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 60000 ); }, join: function () { if ( idrinth.newgrounds.raids.length === 0 ) { @@ -58,7 +52,7 @@ var idrinth = { frame.setAttribute ( 'src', link ); }, remove: function ( key ) { - window.setTimeout ( + idrinth.core.timeouts.add ( 'newgrounds.remove', function () { try { idrinth.raids.list[key].joined = true; @@ -88,19 +82,8 @@ var idrinth = { idrinth.settings.get ( "newgroundLoad" ) * 1000 ); } }, - clearTimeout: function ( timeout ) { - if ( timeout ) { - try { - window.clearTimeout ( timeout ); - } catch ( e ) { - idrinth.core.log ( typeof e.getMessage === 'function' ? e.getMessage : e ) - } - } - }, reload: function ( ) { - idrinth.clearTimeout ( idrinth.chat.updateTimeout ); - idrinth.clearTimeout ( idrinth.war.warTO ); - idrinth.raids.clearInterval (); + window.clearTimeout ( idrinth.core.timeouts.next ); idrinth.ui.removeElement ( 'idrinth-controls' ); idrinth.ui.removeElement ( 'idrinth-chat' ); idrinth.ui.removeElement ( 'idrinth-war' ); @@ -121,17 +104,18 @@ var idrinth = { frame.setAttribute ( 'src', ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ) + '&' + ( window.location.search ).replace ( /^\?/, '' ) ); } } catch ( e ) { - window.setTimeout ( idrinth.startInternal, 500 ); + return idrinth.core.timeouts.add ( 'start', idrinth.startInternal, 500 ); return; } - window.setTimeout ( idrinth.newgrounds.alarmCheck, 3333 ); + return idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 3333 ); } idrinth.settings.start ( ); idrinth.text.start ( ); - idrinth._tmp=window.setInterval(function() { - if(!idrinth.text.initialized) { + idrinth.core.timeouts.add ( 'start', function () { + if ( !idrinth.text.initialized ) { return; } + idrinth.core.timeouts.remove ( 'start' ); idrinth.ui.start ( ); idrinth.user.start ( ); idrinth.names.start ( ); @@ -139,9 +123,7 @@ var idrinth = { idrinth.tier.start ( ); idrinth.chat.start ( ); idrinth.war.start ( ); - window.clearInterval(idrinth._tmp); - delete idrinth['_tmp']; - window.setTimeout ( function () { + idrinth.core.timeouts.add ( 'core.multibind', function () { idrinth.core.multibind.add ( 'click', '.clipboard-copy', function ( element, event ) { idrinth.core.copyToClipboard.element ( element ); element.parentNode.parentNode.removeChild ( element.parentNode ); @@ -150,7 +132,8 @@ var idrinth = { }, 1000 ); delete idrinth['start']; delete idrinth['startInternal']; - },123); + }, 123, true ); + idrinth.core.timeouts.process (); }, start: function ( ) { 'use strict'; @@ -166,8 +149,7 @@ var idrinth = { idrinth.platform = 'facebook'; } if ( idrinth.platform === 'armorgames' ) { - window.setTimeout ( idrinth.startInternal, 2222 ); - return; + return idrinth.core.timeouts.add ( 'start', idrinth.startInternal, 2222 ); } idrinth.startInternal (); } From e5ce434063fab110eb545e4be784dcf457a17156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 22:32:21 +0100 Subject: [PATCH 03/16] fixes #203 removing overlooked timeout --- src/mods/chat.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mods/chat.js b/src/mods/chat.js index da9fc3a..0905c5e 100644 --- a/src/mods/chat.js +++ b/src/mods/chat.js @@ -774,7 +774,6 @@ idrinth.chat = { document.getElementById ( 'idrinth-make-chat' ).getElementsByTagName ( 'input' )[0].value = ''; }, users: { }, - updateTimeout: null, add: function () { idrinth.core.ajax.runHome ( 'chat-service/join/', @@ -804,9 +803,6 @@ idrinth.chat = { idrinth.ui.buildChat ( chatId, list[chatId].name, list[chatId].access, list[chatId].pass ); } } - if ( idrinth.chat.updateTimeout ) { - return; - } idrinth.core.timeouts.add ( 'chat', idrinth.chat.refreshChats, 1500 ); }, startLoginCallback: function ( data ) { From 3fc93198fd15c86d915477bc2ad80bfadcb60018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 22:41:30 +0100 Subject: [PATCH 04/16] refs #135 adding jsdoc headers --- src/mods/war.js | 381 +++++++++++++++++++++++++++++------------------- 1 file changed, 227 insertions(+), 154 deletions(-) diff --git a/src/mods/war.js b/src/mods/war.js index b171bb2..b62aa34 100644 --- a/src/mods/war.js +++ b/src/mods/war.js @@ -1,12 +1,230 @@ idrinth.war = { + /** + * + * @type {string} + */ from: null, + /** + * + * @type {string} + */ to: null, + /** + * + * @type {HTMLElement} + */ element: null, + /** + * sets the timeout + * @returns {undefined} + */ setTO: function ( ) { var active = ( idrinth.war.element.getAttribute ( 'class' ) ).match ( /(^|\s)idrinth-hide($|\s)/ ) !== null; idrinth.core.timeouts.add ( 'war', idrinth.war.getData, active ? 30000 : 120000 ); }, + /** + * requests data from the server + * @returns {undefined} + */ getData: function () { + /** + * + * @param {object} data + * @returns {undefined} + */ + var updateData = function ( data ) { + /** + * + * @param {object} data + * @returns {undefined} + */ + var process = function ( data ) { + /** + * + * @param {Boolean} onOff + * @returns {undefined} + */ + var toggleGUI = function ( onOff ) { + var toggle = onOff || false; + var addClasses = [ ]; + var removeClasses = [ ]; + if ( toggle === true ) { + removeClasses.push ( 'idrinth-hide' ); + addClasses.push ( "bottom" ); + if ( !idrinth.settings.get ( "warBottom" ) ) { + removeClasses.push ( "bottom" ); + } + } else { + addClasses.push ( "idrinth-hide" ); + while ( idrinth.war.element.childNodes[1].childNodes[1].firstChild ) { + idrinth.war.element.childNodes[1].childNodes[1].removeChild ( idrinth.war.element.childNodes[1].firstChild.firstChild ); + } + } + idrinth.ui.updateClassesList ( idrinth.war.element, addClasses, removeClasses ); + }; + /** + * + * @param {object} data + * @returns {undefined} + */ + var processJson = function ( data ) { + var magicIgmSrv = 'https://dotd.idrinth.de/static/magic-image-service/'; + /** + * + * @param {object} data + * @returns {Array} + */ + var getMagic = function ( data ) { + var magics = [ ]; + if ( !data || ( data.magics === null || data.magics === '' ) ) { + return [ ]; + } + var tmp = data.magics.split ( ',' ); + for (var key = 0; key < tmp.length; key++) { + var magic = tmp[key]; + var magicObj = { + type: 'img', + attributes: [ + { + name: 'src', + value: magicIgmSrv + magic + '/' + }, + { + name: 'width', + value: '20' + } ] + }; + magics.push ( magicObj ); + } + return magics; + }; + /** + * + * @param {object} raids + * @returns {undefined} + */ + function addRaids ( raids ) { + for (var key in raids) { + if ( idrinth.raids.joined[key] === undefined && idrinth.raids.list[key] === undefined ) { + idrinth.raids.list[key] = raids[key]; + } + } + } + /** + * + * @param {object} data + * @param {HTMLElement} element + * @returns {undefined} + */ + function updateBoss ( data, element ) { + //TODO: Dummy function, should be removed + function cleanUp () { + while ( element.getElementsByTagName ( 'td' )[3].firstChild ) { + element.getElementsByTagName ( 'td' )[3].removeChild ( element.getElementsByTagName ( 'td' )[3].firstChild ); + } + } + + cleanUp (); + var tmpMagics = getMagic ( data ); + for (var m = 0; m < tmpMagics.length; m++) { + element.getElementsByTagName ( 'td' )[3].appendChild ( idrinth.ui.buildElement ( tmpMagics[m] ) ); + } + element.getElementsByTagName ( 'td' )[0].setAttribute ( "class", 'traffic ' + ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ) ); + element.getElementsByTagName ( 'td' )[0].setAttribute ( "title", data.amount + '/100' ); + element.getElementsByTagName ( 'td' )[0].firstChild.innerHTML = ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ); + } + /** + * creates a new bpss + * @param {object} data + * @param {string} boss + * @returns {undefined} + */ + function newBoss ( data, boss ) { + idrinth.war.element.childNodes[1].appendChild ( idrinth.ui.buildElement ( + { + type: 'tr', + id: 'idrinth-war-' + boss, + children: [ + { + type: 'td', + css: 'traffic ' + ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ), + children: [ { + type: 'span', + content: ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ) + } ], + attributes: [ + { + name: 'title', + value: data.amount + '/100' + } + ] + }, + { + type: 'td', + content: data.name + }, + { + type: 'td', + children: [ + { + type: 'input', + attributes: [ + { + name: 'type', + value: 'checkbox' + }, + { + name: 'data-id', + value: boss + }, + { + name: 'title', + value: 'join ' + data.name + } + ] + } + ] + }, + { + type: 'td', + children: getMagic ( data ) + } + ] + } + ) ); + } + if ( data.raids !== undefined ) { + addRaids ( data.raids ); + } + for (var boss in data.stats) { + if ( document.getElementById ( 'idrinth-war-' + boss ) ) { + updateBoss ( data.stats[boss], document.getElementById ( 'idrinth-war-' + boss ) ); + } else { + newBoss ( data.stats[boss], boss ); + } + } + }; + if ( data === "" || data === "null" ) { + return; + } + if ( data === "{}" ) { + toggleGUI ( false ); + return; + } + toggleGUI ( true ); + try { + processJson ( JSON.parse ( data ) ); + } catch ( e ) { + idrinth.core.log ( e ); + } + }; + process ( data ); + idrinth.war.setTO (); + }; + /** + * + * @returns {String} + */ var raids2Join = function () { var list = [ ]; for (var input in idrinth.war.element.getElementsByTagName ( 'input' )) { @@ -21,166 +239,21 @@ idrinth.war = { }; idrinth.core.ajax.runHome ( "war-service/" + raids2Join () + "/" + Date.now () + "/", - idrinth.war.updateData, + updateData, idrinth.war.setTO, idrinth.war.setTO, idrinth.raids.knowRaids () ); }, - updateData: function ( data ) { - var process = function ( data ) { - var toggleGUI = function ( onOff ) { - var toggle = onOff || false; - var addClasses = [ ]; - var removeClasses = [ ]; - if ( toggle === true ) { - removeClasses.push ( 'idrinth-hide' ); - addClasses.push ( "bottom" ); - if ( !idrinth.settings.get ( "warBottom" ) ) { - removeClasses.push ( "bottom" ); - } - } else { - addClasses.push ( "idrinth-hide" ); - while ( idrinth.war.element.childNodes[1].childNodes[1].firstChild ) { - idrinth.war.element.childNodes[1].childNodes[1].removeChild ( idrinth.war.element.childNodes[1].firstChild.firstChild ); - } - } - idrinth.ui.updateClassesList ( idrinth.war.element, addClasses, removeClasses ); - }; - var processJson = function ( data ) { - var magicIgmSrv = 'https://dotd.idrinth.de/static/magic-image-service/'; - var getMagic = function ( data ) { - var magics = [ ], - tmp; - if ( !data || ( data.magics === null || data.magics === '' ) ) { - return [ ]; - } - tmp = data.magics.split ( ',' ); - for (var key = 0; key < tmp.length; key++) { - var magic = tmp[key]; - var magicObj = { - type: 'img', - attributes: [ - { - name: 'src', - value: magicIgmSrv + magic + '/' - }, - { - name: 'width', - value: '20' - } ] - }; - magics.push ( magicObj ); - } - return magics; - }; - function addRaids ( raids ) { - for (var key in raids) { - if ( idrinth.raids.joined[key] === undefined && idrinth.raids.list[key] === undefined ) { - idrinth.raids.list[key] = raids[key]; - } - } - } - function updateBoss ( data, element ) { - //TODO: Dummy function, should be removed - function cleanUp () { - while ( element.getElementsByTagName ( 'td' )[3].firstChild ) { - element.getElementsByTagName ( 'td' )[3].removeChild ( element.getElementsByTagName ( 'td' )[3].firstChild ); - } - } - - cleanUp (); - var tmpMagics = getMagic ( data ); - for (var m = 0; m < tmpMagics.length; m++) { - element.getElementsByTagName ( 'td' )[3].appendChild ( idrinth.ui.buildElement ( tmpMagics[m] ) ); - } - element.getElementsByTagName ( 'td' )[0].setAttribute ( "class", 'traffic ' + ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ) ); - element.getElementsByTagName ( 'td' )[0].setAttribute ( "title", data.amount + '/100' ); - element.getElementsByTagName ( 'td' )[0].firstChild.innerHTML = ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ); - } - function newBoss ( data, boss ) { - idrinth.war.element.childNodes[1].appendChild ( idrinth.ui.buildElement ( - { - type: 'tr', - id: 'idrinth-war-' + boss, - children: [ - { - type: 'td', - css: 'traffic ' + ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ), - children: [ { - type: 'span', - content: ( data.amount < 90 ? 'yes' : ( data.amount > 110 ? 'no' : 'maybe' ) ) - } ], - attributes: [ - { - name: 'title', - value: data.amount + '/100' - } - ] - }, - { - type: 'td', - content: data.name - }, - { - type: 'td', - children: [ - { - type: 'input', - attributes: [ - { - name: 'type', - value: 'checkbox' - }, - { - name: 'data-id', - value: boss - }, - { - name: 'title', - value: 'join ' + data.name - } - ] - } - ] - }, - { - type: 'td', - children: getMagic ( data ) - } - ] - } - ) ); - } - if ( data.raids !== undefined ) { - addRaids ( data.raids ); - } - for (var boss in data.stats) { - if ( document.getElementById ( 'idrinth-war-' + boss ) ) { - updateBoss ( data.stats[boss], document.getElementById ( 'idrinth-war-' + boss ) ); - } else { - newBoss ( data.stats[boss], boss ); - } - } - }; - if ( data === "" || data === "null" ) { - return; - } - if ( data === "{}" ) { - toggleGUI ( false ); - return; - } - toggleGUI ( true ); - try { - processJson ( JSON.parse ( data ) ); - } catch ( e ) { - idrinth.core.log ( e ); - } - }; - process ( data ); - idrinth.war.setTO (); - }, + /** + * initializes the module + * @returns {undefined} + */ start: function () { + /** + * build the gui part + * @returns {undefined} + */ var build = function ( ) { idrinth.war.element = idrinth.ui.buildElement ( { From 6724d04471fb4ff0316181707d46bd458ac699fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 22:45:29 +0100 Subject: [PATCH 05/16] refs #135 adding jsdoc headers --- src/mods/user.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/mods/user.js b/src/mods/user.js index 1877617..d097595 100644 --- a/src/mods/user.js +++ b/src/mods/user.js @@ -1,10 +1,35 @@ idrinth.user = { + /** + * + * @type String + */ token: '', + /** + * + * @type Number + */ id: 0, + /** + * + * @type String + */ name: '', + /** + * + * @type String + */ identifier: '', + /** + * initializes the module + * @returns {undefined} + */ start: function ( ) { 'use strict'; + /** + * + * @param {Sstring} name + * @returns {String} + */ var getCookie = function ( name ) { var ca = document.cookie.split ( ';' ); for (var i = 0; i < ca.length; i++) { @@ -27,10 +52,25 @@ idrinth.user = { idrinth.user.id = ag[1]; idrinth.user.token = ag[2]; } + /** + * sends an id to the server for statistic purposes + * @returns {undefined} + */ var sendAlive = function () { + /** + * + * @returns {String|idrinth.user.identifier} + */ var getIdentifier = function () { + /** + * from http://stackoverflow.com/a/105074 + * @returns {String} + */ var guid = function () { - //from http://stackoverflow.com/a/105074 + /** + * + * @returns {String} + */ var s4 = function () { return Math.floor ( ( 1 + Math.random () ) * 0x10000 ).toString ( 36 ); }; From 94eefccbd62c96767da6101b7e9c48baaa2b5bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 22:54:26 +0100 Subject: [PATCH 06/16] refs #135 adding jsdoc headers --- src/mods/settings.js | 201 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 200 insertions(+), 1 deletion(-) diff --git a/src/mods/settings.js b/src/mods/settings.js index 889460b..7df74dd 100644 --- a/src/mods/settings.js +++ b/src/mods/settings.js @@ -1,41 +1,189 @@ idrinth.settings = { + /** + * + * @type {object} + */ data: { + /** + * + * @type Boolean + */ raids: false, + /** + * + * @type String + */ favs: '', + /** + * + * @type Boolean + */ factor: true, + /** + * + * @type Boolean + */ moveLeft: false, + /** + * + * @type Boolean + */ minimalist: false, + /** + * + * @type Boolean + */ chatHiddenOnStart: true, + /** + * + * @type Boolean + */ names: true, + /** + * + * @type Number + */ timeout: 5000, + /** + * + * @type Number + */ loadtime: 5000, + /** + * + * @type Number + */ windows: 3, + /** + * + * @type Boolean + */ warBottom: false, + /** + * + * @type Boolean + */ landMax: true, + /** + * + * @type Boolean + */ chatting: true, + /** + * + * @type String + */ chatuser: '', + /** + * + * @type Number + */ newgroundLoad: 30, + /** + * + * @type String + */ chatpass: '', + /** + * + * @type Boolean + */ isWorldServer: false, + /** + * + * @type String + */ alarmTime: '8:0', + /** + * + * @type Boolean + */ alarmActive: false, + /** + * + * @type Object + */ bannedRaids: { }, - lang : null, + /** + * + * @type String + */ + lang: null, + /** + * + * @type {Object} + */ notification: { + /** + * + * @type Boolean + */ mention: true, + /** + * + * @type Boolean + */ message: true, + /** + * + * @type Boolean + */ raid: true }, + /** + * + * @type Object + */ land: { + /** + * + * @type Number + */ cornfield: 0, + /** + * + * @type Number + */ stable: 0, + /** + * + * @type Number + */ barn: 0, + /** + * + * @type Number + */ store: 0, + /** + * + * @type Number + */ pub: 0, + /** + * + * @type Number + */ inn: 0, + /** + * + * @type Number + */ tower: 0, + /** + * + * @type Number + */ fort: 0, + /** + * + * @type Number + */ castle: 0, + /** + * + * @type Number + */ gold: 0 } }, @@ -84,8 +232,21 @@ idrinth.settings = { remove ( 'idrinth-dotd-' + field[0] + '-' + field[1] ); return getValue ( idrinth.settings.data[field[0]], field[1], allowObject ); }, + /** + * + * @param {String} field + * @param {String|Booleab|Number} value + * @returns {undefined} + */ change: function ( field, value ) { 'use strict'; + /** + * + * @param {obect} parent + * @param {string} field + * @param {String|Booleab|Number} value + * @returns {Boolean} + */ var setValue = function ( parent, field, value ) { if ( idrinth.core.fieldIsSetting ( parent, field ) ) { parent[field] = value; @@ -93,6 +254,10 @@ idrinth.settings = { } return false; }; + /** + * saves the data to local storage + * @returns {undefined} + */ var store = function ( ) { window.localStorage.setItem ( 'idotd', JSON.stringify ( idrinth.settings.data ) ); }; @@ -112,10 +277,25 @@ idrinth.settings = { return; } }, + /** + * initializes the module + * @returns {undefined} + */ start: function ( ) { 'use strict'; + /** + * fills the data from json in idotd + * @returns {undefined} + */ var getCurrent = function () { var data = JSON.parse ( window.localStorage.getItem ( 'idotd' ) ); + /** + * + * @param {object} to + * @param {object} from + * @param {function} apply + * @returns {undefined} + */ var apply = function ( to, from, apply ) { for (var key in from) { if ( from.hasOwnProperty ( key ) ) { @@ -130,8 +310,27 @@ idrinth.settings = { }; apply ( idrinth.settings.data, data, apply ); }; + /** + * fills the data from seperate storages + * @returns {undefined} + */ var getOld = function () { + /** + * + * @param {object} object + * @param {String} prefix + * @param {function} objectIterator + * @returns {Boolean} + */ var objectIterator = function ( object, prefix, objectIterator ) { + /** + * + * @param {String} prefix + * @param {String} key + * @param {Number|String|Boolean} item + * @returns {Boolean} + * @todo remove this once old data is unlikely to exist + */ var itemHandler = function ( prefix, key, item ) { if ( typeof item !== 'function' ) { var tmp = window.localStorage.getItem ( 'idrinth-dotd-' + prefix + key ); From 92f8d6e5bc4932755c9c86e7d01b9218a9a8a18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 23:03:36 +0100 Subject: [PATCH 07/16] moving initialization to correct place --- src/stable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stable.js b/src/stable.js index b640233..0842cb4 100644 --- a/src/stable.js +++ b/src/stable.js @@ -132,8 +132,8 @@ var idrinth = { }, 1000 ); delete idrinth['start']; delete idrinth['startInternal']; + idrinth.core.timeouts.process (); }, 123, true ); - idrinth.core.timeouts.process (); }, start: function ( ) { 'use strict'; From dc5978b66765a23748bda0462058c1a8829e2fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 23:08:22 +0100 Subject: [PATCH 08/16] fixing setTimeout --- src/mods/core.js | 2 +- src/stable.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mods/core.js b/src/mods/core.js index 5bb3f3a..94ad5e2 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -248,7 +248,7 @@ idrinth.core = { } } } - idrinth.core.timeouts.next = window.setTimeout ( idrinth.core.timeouts.process, min * 1000 ); + idrinth.core.timeouts.next = window.setTimeout ( idrinth.core.timeouts.process, Math.ceil ( min * 1000 ) ); } }, /** diff --git a/src/stable.js b/src/stable.js index 0842cb4..b640233 100644 --- a/src/stable.js +++ b/src/stable.js @@ -132,8 +132,8 @@ var idrinth = { }, 1000 ); delete idrinth['start']; delete idrinth['startInternal']; - idrinth.core.timeouts.process (); }, 123, true ); + idrinth.core.timeouts.process (); }, start: function ( ) { 'use strict'; From 9a6577dc00752b89448a2554a6d32b4c8a509df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 23:10:20 +0100 Subject: [PATCH 09/16] adding factor for milliseconds --- src/mods/core.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mods/core.js b/src/mods/core.js index 94ad5e2..d830af2 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -204,7 +204,7 @@ idrinth.core = { var date = new Date (); idrinth.core.timeouts.list[identifier] = { func: func, - next: date.getTime () + date.getMilliseconds () + time / 1000, + next: date.getTime () + date.getMilliseconds () / 1000 + time / 1000, duration: time, interval: !!isInterval }; @@ -215,7 +215,7 @@ idrinth.core = { */ process: function () { 'use strict'; - var date = ( new Date () ).getTime () + ( new Date () ).getMilliseconds (); + var date = ( new Date () ).getTime () + ( new Date () ).getMilliseconds () / 1000; var min = 10000; /** * From d746a516768f17aaf4679d86df44869e937da523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 23:29:40 +0100 Subject: [PATCH 10/16] fixing remove timeout --- src/mods/core.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mods/core.js b/src/mods/core.js index d830af2..e994ed0 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -181,14 +181,16 @@ idrinth.core = { */ list: { }, /** - * + * makes sure the timeout is removed when called next * @param {string} identifier * @returns {undefined} */ remove: function ( identifier ) { 'use strict'; if ( idrinth.core.timeouts.list[identifier] !== undefined ) { - delete idrinth.core.timeouts.list[identifier]; + idrinth.core.timeouts.list[identifier].interval = false; + idrinth.core.timeouts.list[identifier].func = function () { + }; } }, /** @@ -216,7 +218,7 @@ idrinth.core = { process: function () { 'use strict'; var date = ( new Date () ).getTime () + ( new Date () ).getMilliseconds () / 1000; - var min = 10000; + var min = 10; /** * * @param {Number} durationLeft From 7c74d656f43b36e4d7cf2c617fa933b53cf18c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 11 Jan 2017 23:33:45 +0100 Subject: [PATCH 11/16] adding forgotten factor for milliseconds --- src/mods/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mods/core.js b/src/mods/core.js index e994ed0..9c31aad 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -238,7 +238,7 @@ idrinth.core = { idrinth.core.timeouts.list[property].func (); if ( idrinth.core.timeouts.list[property].interval ) { min = getVal ( idrinth.core.timeouts.list[property].duration, min ); - idrinth.core.timeouts.list[property].next = date + idrinth.core.timeouts.list[property].duration; + idrinth.core.timeouts.list[property].next = date + idrinth.core.timeouts.list[property].duration / 1000; } else { delete idrinth.core.timeouts.list[property]; } From f61ebfcc076d2a1974436e1d8e936d686e506f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 12 Jan 2017 00:01:46 +0100 Subject: [PATCH 12/16] binding everything to idrinth.ui.base to help refs #195 removing forgotten modules from stable --- src/mods/chat.js | 6 +- src/mods/core.js | 4 +- src/mods/facebook.js | 21 +++++ src/mods/names.js | 2 +- src/mods/newgrounds.js | 62 +++++++++++++ src/mods/raids.js | 2 +- src/mods/tier.js | 2 +- src/mods/ui.js | 8 +- src/mods/war.js | 2 +- src/stable.js | 200 +++++++++++++++-------------------------- 10 files changed, 167 insertions(+), 142 deletions(-) create mode 100644 src/mods/facebook.js create mode 100644 src/mods/newgrounds.js diff --git a/src/mods/chat.js b/src/mods/chat.js index 0905c5e..2ac5eb3 100644 --- a/src/mods/chat.js +++ b/src/mods/chat.js @@ -340,7 +340,7 @@ idrinth.chat = { value: 'this.parentNode.parentNode.removeChild(this.parentNode);' } ] } ); - idrinth.ui.body.appendChild ( idrinth.ui.buildElement ( { + idrinth.ui.base.appendChild ( idrinth.ui.buildElement ( { type: 'ul', children: popupContent, css: 'idrinth-userinfo-box', @@ -703,7 +703,7 @@ idrinth.chat = { return; } if ( !document.getElementById ( 'idrinth-chat' ) ) { - idrinth.ui.body.appendChild ( build () ); + idrinth.ui.base.appendChild ( build () ); idrinth.chat.elements.chats = document.getElementById ( 'idrinth-chat' ).getElementsByTagName ( 'ul' )[1]; idrinth.chat.elements.menu = document.getElementById ( 'idrinth-chat' ).getElementsByTagName ( 'ul' )[0]; } @@ -855,7 +855,7 @@ idrinth.chat = { }, showOptions: function ( event, element ) { event.preventDefault (); - idrinth.ui.body.appendChild ( idrinth.ui.buildElement ( { + idrinth.ui.base.appendChild ( idrinth.ui.buildElement ( { type: 'ul', css: 'idrinth-hovering-box', children: [ { diff --git a/src/mods/core.js b/src/mods/core.js index 9c31aad..3ba5a3b 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -119,7 +119,7 @@ idrinth.core = { id: "idrinth-copy-helper" } ); textAreaElement.value = text; - idrinth.ui.body.appendChild ( textAreaElement ); + idrinth.ui.base.appendChild ( textAreaElement ); textAreaElement.select ( ); success = document.execCommand ( 'copy' ); } catch ( exception ) { @@ -303,7 +303,7 @@ idrinth.core = { idrinth.core.multibind.events[event][selector].push ( method ); }; if ( !idrinth.core.multibind.events[event] ) { - idrinth.ui.body.addEventListener ( event, function ( e ) { + idrinth.ui.base.addEventListener ( event, function ( e ) { e = e || window.event; idrinth.core.multibind.triggered ( e.target, e.type ); } ); diff --git a/src/mods/facebook.js b/src/mods/facebook.js new file mode 100644 index 0000000..66606c6 --- /dev/null +++ b/src/mods/facebook.js @@ -0,0 +1,21 @@ +idrinth.facbook = { + popup: null, + restart: function () { + try { + idrinth.core.timeouts.remove ( 'facebook' ); + idrinth.facebook.popup.close (); + idrinth.ui.reloadGame (); + idrinth.raids.clearAll (); + } catch ( e ) { + idrinth.core.log ( e ); + } + }, + rejoin: function () { + idrinth.core.timeouts.remove ( 'raids' ); + idrinth.facebook.popup = window.open ( "https://apps.facebook.com/dawnofthedragons/" ); + idrinth.facebook.popup.onload = function () { + idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 3333 ); + }; + idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 11111 ); + } +}; \ No newline at end of file diff --git a/src/mods/names.js b/src/mods/names.js index 3b9e733..116b88d 100644 --- a/src/mods/names.js +++ b/src/mods/names.js @@ -218,7 +218,7 @@ idrinth.names = { } ] } ); - idrinth.ui.body.appendChild ( idrinth.ui.tooltip ); + idrinth.ui.base.appendChild ( idrinth.ui.tooltip ); }; /** * shows the tooltip if the element has a known name diff --git a/src/mods/newgrounds.js b/src/mods/newgrounds.js new file mode 100644 index 0000000..e0cdcf0 --- /dev/null +++ b/src/mods/newgrounds.js @@ -0,0 +1,62 @@ +idrinth.newgrounds = { + originalUrl: '', + raids: [ ], + joinRaids: function () { + for (var key in idrinth.raids.list) { + if ( idrinth.raids.list[key].hash && idrinth.raids.list[key].raidId ) { + idrinth.newgrounds.raids.push ( key ); + } + } + idrinth.newgrounds.join (); + }, + alarmCheck: function () { + var now = new Date (); + if ( idrinth.settings.get ( "alarmActive" ) && now.getHours () + ':' + now.getMinutes () === idrinth.settings.get ( "alarmTime" ) ) { + idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.joinRaids, 1 ); + } + idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 60000 ); + }, + join: function () { + if ( idrinth.newgrounds.raids.length === 0 ) { + idrinth.core.alert ( 'We\'re done! Have fun playing.' ); + return; + } + var frame = document.getElementById ( 'iframe_embed' ).getElementsByTagName ( 'iframe' )[0]; + var key = idrinth.newgrounds.raids.pop (); + var link = idrinth.newgrounds.originalUrl + '&' + ( idrinth.raids.join.getServerLink ( key ) ).replace ( /^.*?\?/, '' ); + frame.setAttribute ( 'onload', 'idrinth.newgrounds.remove(\'' + key + '\')' ); + frame.setAttribute ( 'src', link ); + }, + remove: function ( key ) { + idrinth.core.timeouts.add ( + 'newgrounds.remove', + function () { + try { + idrinth.raids.list[key].joined = true; + } catch ( e ) { + try { + idrinth.raids.joined[key].joined = true; + } catch ( f ) { + idrinth.core.log ( "We seem to have joined a dead raid" ); + } + } + if ( document.getElementById ( 'idrinth-raid-link-' + key ) ) { + idrinth.ui.removeElement ( 'idrinth-raid-link-' + key ); + } + try { + idrinth.raids.joined[key] = idrinth.raids.list[key]; + } catch ( e2 ) { + //lost? + } + try { + delete idrinth.raids.list[key]; + } catch ( e3 ) { + //already gone, nothing to do + } + idrinth.raids.join.messages.trying ( key ); + idrinth.newgrounds.join (); + }, + idrinth.settings.get ( "newgroundLoad" ) * 1000 + ); + } +}; \ No newline at end of file diff --git a/src/mods/raids.js b/src/mods/raids.js index 682b3e3..a283ec4 100644 --- a/src/mods/raids.js +++ b/src/mods/raids.js @@ -233,7 +233,7 @@ idrinth.raids = { idrinth.ui.removeElement ( 'join-' + key ); }, 30000 ); } ) ( key ) ); - idrinth.ui.body.appendChild ( frame ); + idrinth.ui.base.appendChild ( frame ); idrinth.raids.join.messages.trying ( key ); }; var postLink = function ( key ) { diff --git a/src/mods/tier.js b/src/mods/tier.js index 5662e1e..0527202 100644 --- a/src/mods/tier.js +++ b/src/mods/tier.js @@ -88,7 +88,7 @@ idrinth.tier = { ] } ); - idrinth.ui.body.appendChild ( idrinth.tier.taggedSlots[x] ); + idrinth.ui.base.appendChild ( idrinth.tier.taggedSlots[x] ); }; for (var key in this.taggedSlots) { if ( isFreeSlot ( key ) ) { diff --git a/src/mods/ui.js b/src/mods/ui.js index 4645170..c644e7c 100644 --- a/src/mods/ui.js +++ b/src/mods/ui.js @@ -253,7 +253,7 @@ idrinth.ui = { } else { mod.children[mod.children.length - 1].children = [ makeButton ( 'ok', '' ) ]; } - idrinth.ui.body.appendChild ( idrinth.ui.buildElement ( mod ) ); + idrinth.ui.base.appendChild ( idrinth.ui.buildElement ( mod ) ); }, matchesCss: function ( element, selector ) { while ( element && element !== document ) { @@ -289,7 +289,7 @@ idrinth.ui = { if ( element.className.match ( new RegExp ( '(^|\s)' + cssClass + '(\s|$)' ) ) ) { return true; } - if ( !element.parentNode || element === document.getElementsByTagName ( 'body' )[0] ) { + if ( !element.parentNode || element === idrinth.ui.base ) { return false; } element = element.parentNode; @@ -770,10 +770,10 @@ idrinth.ui = { id: 'idrinth-controls', children: children } ); - idrinth.ui.body.appendChild ( idrinth.ui.controls ); + idrinth.ui.base.appendChild ( idrinth.ui.controls ); document.getElementById ( 'idrinth-favs' ).setAttribute ( 'onkeyup', 'this.value=this.value.replace(/[^a-f0-9,]/g,\'\')' ); }; - idrinth.ui.body = document.getElementsByTagName ( 'body' )[0]; + idrinth.ui.base = document.getElementsByTagName ( 'body' )[0]; document.getElementsByTagName ( 'head' )[0].appendChild ( idrinth.ui.buildElement ( { type: 'link', attributes: [ { diff --git a/src/mods/war.js b/src/mods/war.js index b62aa34..6589589 100644 --- a/src/mods/war.js +++ b/src/mods/war.js @@ -310,7 +310,7 @@ idrinth.war = { ] } ); - idrinth.ui.body.appendChild ( idrinth.war.element ); + idrinth.ui.base.appendChild ( idrinth.war.element ); }; idrinth.core.timeouts.add ( 'war', idrinth.war.getData, 5000 ); build (); diff --git a/src/stable.js b/src/stable.js index b640233..c64c4ac 100644 --- a/src/stable.js +++ b/src/stable.js @@ -1,87 +1,21 @@ var idrinth = { + /** + * @type String + */ version: '###VERSION###', + /** + * @type Boolean + */ windowactive: true, - facebook: { - popup: null, - restart: function () { - try { - idrinth.core.timeouts.remove ( 'facebook' ); - idrinth.facebook.popup.close (); - idrinth.ui.reloadGame (); - idrinth.raids.clearAll (); - } catch ( e ) { - idrinth.core.log ( e ); - } - }, - rejoin: function () { - idrinth.core.timeouts.remove ( 'raids' ); - idrinth.facebook.popup = window.open ( "https://apps.facebook.com/dawnofthedragons/" ); - idrinth.facebook.popup.onload = function () { - idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 3333 ); - }; - idrinth.core.timeouts.add ( 'facebook', idrinth.facebook.restart, 11111 ); - } - }, - newgrounds: { - originalUrl: '', - raids: [ ], - joinRaids: function () { - for (var key in idrinth.raids.list) { - if ( idrinth.raids.list[key].hash && idrinth.raids.list[key].raidId ) { - idrinth.newgrounds.raids.push ( key ); - } - } - idrinth.newgrounds.join (); - }, - alarmCheck: function () { - var now = new Date (); - if ( idrinth.settings.get ( "alarmActive" ) && now.getHours () + ':' + now.getMinutes () === idrinth.settings.get ( "alarmTime" ) ) { - idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.joinRaids, 1 ); - } - idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 60000 ); - }, - join: function () { - if ( idrinth.newgrounds.raids.length === 0 ) { - idrinth.core.alert ( 'We\'re done! Have fun playing.' ); - return; - } - var frame = document.getElementById ( 'iframe_embed' ).getElementsByTagName ( 'iframe' )[0]; - var key = idrinth.newgrounds.raids.pop (); - var link = idrinth.newgrounds.originalUrl + '&' + ( idrinth.raids.join.getServerLink ( key ) ).replace ( /^.*?\?/, '' ); - frame.setAttribute ( 'onload', 'idrinth.newgrounds.remove(\'' + key + '\')' ); - frame.setAttribute ( 'src', link ); - }, - remove: function ( key ) { - idrinth.core.timeouts.add ( 'newgrounds.remove', - function () { - try { - idrinth.raids.list[key].joined = true; - } catch ( e ) { - try { - idrinth.raids.joined[key].joined = true; - } catch ( f ) { - idrinth.core.log ( "We seem to have joined a dead raid" ); - } - } - if ( document.getElementById ( 'idrinth-raid-link-' + key ) ) { - idrinth.ui.removeElement ( 'idrinth-raid-link-' + key ); - } - try { - idrinth.raids.joined[key] = idrinth.raids.list[key]; - } catch ( e2 ) { - //lost? - } - try { - delete idrinth.raids.list[key]; - } catch ( e3 ) { - //already gone, nothing to do - } - idrinth.raids.join.messages.trying ( key ); - idrinth.newgrounds.join (); - }, - idrinth.settings.get ( "newgroundLoad" ) * 1000 ); - } - }, + /** + * + * @type String + */ + platform: '', + /** + * + * @returns {undefined} + */ reload: function ( ) { window.clearTimeout ( idrinth.core.timeouts.next ); idrinth.ui.removeElement ( 'idrinth-controls' ); @@ -89,69 +23,77 @@ var idrinth = { idrinth.ui.removeElement ( 'idrinth-war' ); var sc = document.createElement ( 'script' ); sc.setAttribute ( 'src', 'https://dotd.idrinth.de/static/userscript/###RELOAD-VERSION###/' + Math.random () + '/' ); - document.getElementsByTagName ( 'body' )[0].appendChild ( sc ); + document.getElementsByTagName ( 'head' )[0].appendChild ( sc ); window.setTimeout ( function () { idrinth = { }; }, 1 ); }, - platform: '', - startInternal: function ( ) { - if ( idrinth.platform === 'newgrounds' ) { - try { - var frame = document.getElementById ( 'iframe_embed' ).getElementsByTagName ( 'iframe' )[0]; - idrinth.newgrounds.originalUrl = frame.getAttribute ( 'src' ); - if ( window.location.search ) { - frame.setAttribute ( 'src', ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ) + '&' + ( window.location.search ).replace ( /^\?/, '' ) ); - } - } catch ( e ) { - return idrinth.core.timeouts.add ( 'start', idrinth.startInternal, 500 ); - return; - } - return idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 3333 ); - } - idrinth.settings.start ( ); - idrinth.text.start ( ); - idrinth.core.timeouts.add ( 'start', function () { - if ( !idrinth.text.initialized ) { - return; - } - idrinth.core.timeouts.remove ( 'start' ); - idrinth.ui.start ( ); - idrinth.user.start ( ); - idrinth.names.start ( ); - idrinth.raids.start ( ); - idrinth.tier.start ( ); - idrinth.chat.start ( ); - idrinth.war.start ( ); - idrinth.core.timeouts.add ( 'core.multibind', function () { - idrinth.core.multibind.add ( 'click', '.clipboard-copy', function ( element, event ) { - idrinth.core.copyToClipboard.element ( element ); - element.parentNode.parentNode.removeChild ( element.parentNode ); - idrinth.core.log ( event + ' fired on ' + element ); - } ); - }, 1000 ); - delete idrinth['start']; - delete idrinth['startInternal']; - }, 123, true ); - idrinth.core.timeouts.process (); - }, + /** + * initializes the whole script + * @returns {undefined} + */ start: function ( ) { 'use strict'; - window.onblur = function () { - idrinth.windowactive = false; + /** + * + * @returns {undefined} + */ + var startInternal = function ( ) { + /** + * initializes all modules + * @returns {undefined} + */ + var init = function () { + if ( !idrinth.text.initialized ) { + return; + } + idrinth.core.timeouts.remove ( 'start' ); + idrinth.ui.start ( ); + idrinth.user.start ( ); + idrinth.names.start ( ); + idrinth.raids.start ( ); + idrinth.tier.start ( ); + idrinth.chat.start ( ); + idrinth.war.start ( ); + idrinth.core.timeouts.add ( 'core.multibind', function () { + idrinth.core.multibind.add ( 'click', '.clipboard-copy', function ( element, event ) { + idrinth.core.copyToClipboard.element ( element ); + element.parentNode.parentNode.removeChild ( element.parentNode ); + idrinth.core.log ( event + ' fired on ' + element ); + } ); + }, 1000 ); + delete idrinth['start']; + delete idrinth['startInternal']; + }; + if ( idrinth.platform === 'newgrounds' ) { + try { + var frame = document.getElementById ( 'iframe_embed' ).getElementsByTagName ( 'iframe' )[0]; + idrinth.newgrounds.originalUrl = frame.getAttribute ( 'src' ); + if ( window.location.search ) { + frame.setAttribute ( 'src', ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ) + '&' + ( window.location.search ).replace ( /^\?/, '' ) ); + } + } catch ( e ) { + return idrinth.core.timeouts.add ( 'start', idrinth.startInternal, 500 ); + } + return idrinth.core.timeouts.add ( 'newgrounds', idrinth.newgrounds.alarmCheck, 3333 ); + } + idrinth.settings.start ( ); + idrinth.text.start ( ); + idrinth.core.timeouts.add ( 'start', init, 123, true ); + idrinth.core.timeouts.process (); }; + idrinth.core.log ( 'Starting Idrinth\'s DotD Script' ); window.onblur = function () { - idrinth.windowactive = true; + idrinth.windowactive = !idrinth.windowactive; }; - idrinth.core.log ( 'Starting Idrinth\'s DotD Script' ); idrinth.platform = location.hostname.split ( '.' )[location.hostname.split ( '.' ).length - 2]; if ( idrinth.platform === 'dawnofthedragons' ) { idrinth.platform = 'facebook'; } if ( idrinth.platform === 'armorgames' ) { - return idrinth.core.timeouts.add ( 'start', idrinth.startInternal, 2222 ); + return idrinth.core.timeouts.add ( 'start', startInternal, 2222 ); } - idrinth.startInternal (); + startInternal (); } }; window.setTimeout ( idrinth.start, 6666 ); From f47e81ebefb19dd25f29b95b7a24087a74cc2100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 12 Jan 2017 00:20:00 +0100 Subject: [PATCH 13/16] adding jsdoc --- src/mods/ui.js | 231 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 2 deletions(-) diff --git a/src/mods/ui.js b/src/mods/ui.js index c644e7c..4290fa7 100644 --- a/src/mods/ui.js +++ b/src/mods/ui.js @@ -1,5 +1,24 @@ idrinth.ui = { + /** + * + * @type HTMLElement + */ tooltip: null, + /** + * + * @type HTMLElement + */ + base: null, + /** + * + * @type HTMLElement + */ + controls: null, + /** + * + * @param {Number} number + * @returns {String} + */ formatNumber: function ( number ) { if ( isNaN ( number ) ) { return ''; @@ -12,6 +31,14 @@ idrinth.ui = { } return number.toString () + post[count]; }, + /** + * + * @param {Number} id + * @param {string} name + * @param {string} rank + * @param {string} pass + * @returns {undefined} + */ buildChat: function ( id, name, rank, pass ) { if ( !idrinth.chat.elements.chats ) { idrinth.core.timeouts.add ( 'chat-' + id, function () { @@ -82,6 +109,13 @@ idrinth.ui = { ) ); }, + /** + * + * @param {HTMLElement} element + * @param {Number} offsetX + * @param {Number} offsetY + * @returns {String} + */ getElementPositioning: function ( element, offsetX, offsetY ) { var pos = { x: element.getBoundingClientRect ().left + ( offsetX ? offsetX : 0 ), @@ -89,8 +123,19 @@ idrinth.ui = { }; return 'position:fixed;left:' + pos.x + 'px;top:' + pos.y + 'px'; }, + /** + * + * @param {object} config + * @returns {HTMLElement} + */ buildElement: function ( config ) { 'use strict'; + /** + * + * @param {HTMLElement} el + * @param {object} config + * @returns {undefined} + */ var setBase = function ( el, config ) { if ( config.id ) { el.id = config.id; @@ -102,6 +147,12 @@ idrinth.ui = { el.appendChild ( document.createTextNode ( config.content ) ); } }; + /** + * + * @param {HTMLElement} el + * @param {object} config + * @returns {undefined} + */ var addChildren = function ( el, config ) { if ( !config.children || !config.children.length ) { return; @@ -110,7 +161,19 @@ idrinth.ui = { el.appendChild ( idrinth.ui.buildElement ( config.children[count] ) ); } }; + /** + * + * @param {HTMLElement} el + * @param {object} config + * @returns {undefined} + */ var addAttributes = function ( el, config ) { + /** + * + * @param {HTMLElement} el + * @param {object} set + * @returns {undefined} + */ var applyValue = function ( el, set ) { if ( !set || set.value === undefined ) { return; @@ -131,8 +194,19 @@ idrinth.ui = { applyValue ( el, config.attributes[count] ); } }; + /** + * + * @param {object} config + * @returns {HTMLElement} + */ var makeInputLabel = function ( config ) { 'use strict'; + /** + * + * @param {String|Number} value + * @param {Array} list + * @returns {Boolean} + */ var inArray = function ( value, list ) { 'use strict'; if ( !Array.isArray ( list ) ) { @@ -199,7 +273,13 @@ idrinth.ui = { addAttributes ( el, config ); return el; }, - controls: null, + /** + * + * @param {string} title + * @param {string|HTMLElement} content + * @param {string} altFunc + * @returns {undefined} + */ buildModal: function ( title, content, altFunc ) { var mod = { children: [ ], @@ -235,6 +315,12 @@ idrinth.ui = { mod.children.push ( { css: 'buttons' } ); + /** + * + * @param {String} text + * @param {String} func + * @returns {object} + */ var makeButton = function ( text, func ) { return { type: 'button', @@ -255,6 +341,12 @@ idrinth.ui = { } idrinth.ui.base.appendChild ( idrinth.ui.buildElement ( mod ) ); }, + /** + * + * @param {HTMLElement} element + * @param {string} selector + * @returns {HTMLElement} + */ matchesCss: function ( element, selector ) { while ( element && element !== document ) { if ( typeof element.matches === 'function' && element.matches ( selector ) ) { @@ -263,15 +355,27 @@ idrinth.ui = { element = element.parentNode } }, + /** + * + * @returns {undefined} + */ setTooltipTimeout: function () { idrinth.core.timeouts.add ( 'names.tooltip', idrinth.ui.hideTooltip, idrinth.settings.get ( "timeout" ) ? idrinth.settings.get ( "timeout" ) : 5000 ); }, + /** + * + * @returns {undefined} + */ hideTooltip: function () { if ( idrinth.names.isHovering ) { return idrinth.ui.setTooltipTimeout (); } idrinth.ui.updateClassesList ( idrinth.ui.tooltip, [ 'idrinth-hide' ], [ ] ); }, + /** + * + * @returns {undefined} + */ openCloseSettings: function ( ) { 'use strict'; var toRemove = [ ( idrinth.ui.controls.getAttribute ( 'class' ) ).match ( /(^|\s)inactive($|\s)/ ) ? 'inactive' : 'active' ]; @@ -283,6 +387,12 @@ idrinth.ui = { } idrinth.ui.updateClassesList ( idrinth.ui.controls, [ 'active', 'inactive', 'left-sided', 'small' ], toRemove ); }, + /** + * + * @param {HTMLElement} element + * @param {String} cssClass + * @returns {Boolean} + */ childOf: function ( element, cssClass ) { 'use strict'; do { @@ -296,6 +406,11 @@ idrinth.ui = { } while ( element ); return false; }, + /** + * + * @param {string} id + * @returns {undefined} + */ removeElement: function ( id ) { 'use strict'; var el = document.getElementById ( id ); @@ -303,8 +418,15 @@ idrinth.ui = { el.parentNode.removeChild ( el ); } }, + /** + * + * @returns {undefined} + */ reloadGame: function ( ) { 'use strict'; + /** + * @param {HTMLElement} parent + */ var handleFrame = function ( parent ) { var frame = parent.getElementsByTagName ( 'iframe' )[0]; var src = ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ); @@ -324,14 +446,40 @@ idrinth.ui = { idrinth.core.alert ( idrinth.text.get ( "ui.reloadGameFail" ) ); } }, + /** + * + * @param {HTMLElement} element + * @param {Array|String} add + * @param {Array|String} remove + * @returns {undefined} + */ updateClassesList: function ( element, add, remove ) { + /** + * + * @param {String} classString + * @param {Array|String} add + * @param {Array|String} remove + * @returns {unresolved} + */ var getClassesList = function ( classString, add, remove ) { + /** + * + * @param {String|Array} value + * @returns {Array} + */ var forceToArray = function ( value ) { return value && typeof value === 'object' && Array.isArray ( value ) && value !== null ? value : [ ]; }; var original = classString === null ? [ ] : classString.split ( ' ' ).concat ( forceToArray ( add ) ); var list = [ ]; remove = forceToArray ( remove ); + /** + * + * @param {Array} list + * @param {string} element + * @param {Array} forbidden + * @returns {unresolved} + */ var addUnique = function ( list, element, forbidden ) { if ( list.indexOf ( element ) === -1 && forbidden.indexOf ( element ) === -1 ) { list.push ( element ); @@ -345,9 +493,21 @@ idrinth.ui = { }; element.setAttribute ( 'class', getClassesList ( element.getAttribute ( 'class' ), add, remove ) ); }, + /** + * + * @param {String} name + * @returns {undefined} + */ activateTab: function ( name ) { var head = document.getElementById ( 'tab-activator-' + name ).parentNode.childNodes; var body = document.getElementById ( 'tab-element-' + name ).parentNode.childNodes; + /** + * + * @param {HTMLElement} head + * @param {HTMLElement} body + * @param {string} name + * @returns {undefined} + */ var setClasses = function ( head, body, name ) { if ( head === document.getElementById ( 'tab-activator-' + name ) ) { idrinth.ui.updateClassesList ( head, [ 'active' ], [ ] ); @@ -361,12 +521,34 @@ idrinth.ui = { setClasses ( head[count], body[count], name ); } }, + /** + * initializes the gui + * @returns {undefined} + */ start: function ( ) { 'use strict'; + /** + * builds most of the gui + * @returns {undefined} + */ var build = function () { - 'use strict'; + /** + * + * @returns {Array} + */ var wrapper = function ( ) { + /** + * creates the action tab + * @returns {Array} + */ var buildActions = function () { + /** + * + * @param {string} label + * @param {string} onclick + * @param {string} platform + * @returns {object} + */ var buttonMaker = function ( label, onclick, platform ) { return { css: 'idrinth-float-half' + ( platform && platform !== idrinth.platform ? " idrinth-hide" : "" ), @@ -406,7 +588,16 @@ idrinth.ui = { } ]; }; + /** + * + * @returns {Array} + */ var buildTiers = function () { + /** + * + * @param {string} label + * @returns {object} + */ var makeSearch = function ( label ) { return { type: 'input', @@ -444,6 +635,10 @@ idrinth.ui = { id: 'idrinth-tierlist' } ]; }; + /** + * + * @returns {Array} + */ var buildControls = function () { 'use strict'; return [ { @@ -564,7 +759,16 @@ idrinth.ui = { } ] } ]; }; + /** + * + * @returns {Array} + */ var buildLand = function () { + /** + * + * @param {string} label + * @returns {object} + */ var buildItem = function ( label ) { return { type: 'tr', @@ -664,10 +868,22 @@ idrinth.ui = { ] } ]; }; + /** + * + * @param {object} config + * @returns {Array} + */ var makeTabs = function ( config ) { var head = [ ]; var first = true; var body = [ ]; + /** + * + * @param {string} name + * @param {Number} width + * @param {Boolean} first + * @returns {object} + */ var buildHead = function ( name, width, first ) { return { type: 'li', @@ -686,6 +902,13 @@ idrinth.ui = { ] }; }; + /** + * + * @param {string} name + * @param {Array} children + * @param {Boolean} first + * @returns {object} + */ var buildBody = function ( name, children, first ) { return { type: 'li', @@ -719,6 +942,10 @@ idrinth.ui = { } ]; }; + /** + * + * @returns {Array} + */ var buildRaidJoinList = function () { return [ { content: idrinth.text.get ( "raids.clickCopy" ), From 7a8c382d5b6f2fd1cd1cccc88e27ec292a1a54f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 12 Jan 2017 10:46:34 +0100 Subject: [PATCH 14/16] adding option for limited repeats --- src/mods/core.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mods/core.js b/src/mods/core.js index 3ba5a3b..7dc23f0 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -199,16 +199,18 @@ idrinth.core = { * @param {function} func * @param {int} time in milliseconds * @param {Boolean} [false] isInterval + * @param {Number} [-1] isInterval -1 will repeat until the end of time * @returns {undefined} */ - add: function ( identifier, func, time, isInterval ) { + add: function ( identifier, func, time, isInterval, maxIntervals ) { 'use strict'; var date = new Date (); idrinth.core.timeouts.list[identifier] = { func: func, next: date.getTime () + date.getMilliseconds () / 1000 + time / 1000, duration: time, - interval: !!isInterval + interval: !!isInterval, + max: maxIntervals ? maxIntervals : -1 }; }, /** @@ -236,8 +238,9 @@ idrinth.core = { if ( date >= idrinth.core.timeouts.list[property].next ) { try { idrinth.core.timeouts.list[property].func (); - if ( idrinth.core.timeouts.list[property].interval ) { + if ( idrinth.core.timeouts.list[property].interval && idrinth.core.timeouts.list[property].max ) { min = getVal ( idrinth.core.timeouts.list[property].duration, min ); + idrinth.core.timeouts.list[property].max = Math.max ( -1, idrinth.core.timeouts.list[property].max - 1 ); idrinth.core.timeouts.list[property].next = date + idrinth.core.timeouts.list[property].duration / 1000; } else { delete idrinth.core.timeouts.list[property]; From 050620c1afb28d096dfd51b8155a9f9c72ee04dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 12 Jan 2017 10:59:13 +0100 Subject: [PATCH 15/16] removing useless property and adjusting uses --- src/mods/core.js | 12 +++++------- src/mods/raids.js | 2 +- src/stable.js | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/mods/core.js b/src/mods/core.js index 7dc23f0..118d365 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -198,19 +198,17 @@ idrinth.core = { * @param {string} identifier * @param {function} func * @param {int} time in milliseconds - * @param {Boolean} [false] isInterval - * @param {Number} [-1] isInterval -1 will repeat until the end of time + * @param {Number} [1] maxRepeats -1 will repeat until the end of time * @returns {undefined} */ - add: function ( identifier, func, time, isInterval, maxIntervals ) { + add: function ( identifier, func, time, maxRepeats ) { 'use strict'; var date = new Date (); idrinth.core.timeouts.list[identifier] = { func: func, next: date.getTime () + date.getMilliseconds () / 1000 + time / 1000, duration: time, - interval: !!isInterval, - max: maxIntervals ? maxIntervals : -1 + repeats: maxRepeats ? maxRepeats : 1 }; }, /** @@ -238,9 +236,9 @@ idrinth.core = { if ( date >= idrinth.core.timeouts.list[property].next ) { try { idrinth.core.timeouts.list[property].func (); - if ( idrinth.core.timeouts.list[property].interval && idrinth.core.timeouts.list[property].max ) { + idrinth.core.timeouts.list[property].repeats = Math.max ( -1, idrinth.core.timeouts.list[property].repeats - 1 ); + if ( idrinth.core.timeouts.list[property].repeats ) { min = getVal ( idrinth.core.timeouts.list[property].duration, min ); - idrinth.core.timeouts.list[property].max = Math.max ( -1, idrinth.core.timeouts.list[property].max - 1 ); idrinth.core.timeouts.list[property].next = date + idrinth.core.timeouts.list[property].duration / 1000; } else { delete idrinth.core.timeouts.list[property]; diff --git a/src/mods/raids.js b/src/mods/raids.js index a283ec4..c301f8d 100644 --- a/src/mods/raids.js +++ b/src/mods/raids.js @@ -306,6 +306,6 @@ idrinth.raids = { start: function ( ) { 'use strict'; idrinth.core.timeouts.remove ( 'raids' ); - idrinth.core.timeouts.add ( 'raids', idrinth.raids.join.process, 1500, true ); + idrinth.core.timeouts.add ( 'raids', idrinth.raids.join.process, 1500, -1 ); } }; diff --git a/src/stable.js b/src/stable.js index c64c4ac..39e21a3 100644 --- a/src/stable.js +++ b/src/stable.js @@ -79,7 +79,7 @@ var idrinth = { } idrinth.settings.start ( ); idrinth.text.start ( ); - idrinth.core.timeouts.add ( 'start', init, 123, true ); + idrinth.core.timeouts.add ( 'start', init, 123, -1 ); idrinth.core.timeouts.process (); }; idrinth.core.log ( 'Starting Idrinth\'s DotD Script' ); From 380d3d4af3b03784980e8819de0c7c85c7cc4834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 12 Jan 2017 11:01:02 +0100 Subject: [PATCH 16/16] adjusting removal --- src/mods/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mods/core.js b/src/mods/core.js index 118d365..1cad614 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -188,7 +188,7 @@ idrinth.core = { remove: function ( identifier ) { 'use strict'; if ( idrinth.core.timeouts.list[identifier] !== undefined ) { - idrinth.core.timeouts.list[identifier].interval = false; + idrinth.core.timeouts.list[identifier].repeats = 1; idrinth.core.timeouts.list[identifier].func = function () { }; }