diff --git a/MAINTAINERS b/MAINTAINERS index 32f7e22..f05f8cb 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1,2 +1,2 @@ -idrinth +Idrinth w20k \ No newline at end of file diff --git a/src/libs/matches-selector-polyfill.js b/src/libs/matches-selector-polyfill.js new file mode 100644 index 0000000..bac5b8b --- /dev/null +++ b/src/libs/matches-selector-polyfill.js @@ -0,0 +1,20 @@ +/** + * from https://developer.mozilla.org/de/docs/Web/API/Element/matches + */ +Element.prototype.matches = + Element.prototype.matches || + Element.prototype.matchesSelector || + Element.prototype.mozMatchesSelector || + Element.prototype.msMatchesSelector || + Element.prototype.oMatchesSelector || + Element.prototype.webkitMatchesSelector || + function ( s ) { + var matches = ( this.document || this.ownerDocument ).querySelectorAll ( s ); + var i = matches.length; + while ( --i >= 0 ) { + if ( matches.item ( i ) === this ) { + return true; + } + } + return false; + }; \ No newline at end of file diff --git a/src/mods/core.js b/src/mods/core.js index 166596f..ea50034 100644 --- a/src/mods/core.js +++ b/src/mods/core.js @@ -99,5 +99,48 @@ idrinth.core = { }, confirm: function ( text, callback ) { idrinth.ui.buildModal ( 'Do you?', text, callback ); + }, + multibind: { + events: { }, + 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] : { }; + idrinth.core.multibind.events[event][selector] = idrinth.core.multibind.events[event][selector] ? idrinth.core.multibind.events[event][selector] : [ ]; + idrinth.core.multibind.events[event][selector].push ( method ); + }; + if ( idrinth.core.multibind.events[event] ) { + var attribute = 'idrinth.core.triggered(this,\'' + event + '\');'; + //trying not to break all old code there + if ( idrinth.ui.body.getAttribute ( 'on' + event ) ) { + attribute += idrinth.ui.body.getAttribute ( 'on' + event ); + } + if ( idrinth.ui.body['on' + event] && typeof idrinth.ui.body['on' + event] === 'function' ) { + bind ( event, 'body', idrinth.ui.body['on' + event] ); + } + idrinth.ui.body.setAttribute ( 'on' + event, attribute ); + } + bind ( event, selector, method ); + }, + triggered: function ( element, event ) { + var handleElement = function ( el, event, selector ) { + if ( !el ) { + return; + } + for (var pos = 0; pos < idrinth.core.multibind.events[event][selector].length; pos++) { + try { + idrinth.core.multibind.events[event][selector][pos].bind ( el, event ); + } catch ( exception ) { + idrinth.core.log ( exception.getMessage () ); + } + } + }; + if ( idrinth.core.multibind.events[event] ) { + for (var selector in idrinth.core.multibind.events[event]) { + if ( idrinth.core.multibind.events[event].hasOwnProperty ( selector ) ) { + handleElement ( idrinth.ui.matchesCss ( element, selector ), event, selector ); + } + } + } + } } }; \ No newline at end of file diff --git a/src/mods/land.js b/src/mods/land.js index 5fbfb40..08cc75b 100644 --- a/src/mods/land.js +++ b/src/mods/land.js @@ -6,37 +6,47 @@ idrinth.land = { return ( 10 + idrinth.settings.land[building] ) * idrinth.land.data[building].base; }; var results = { }; - var check = function ( checkElementFunc, building, factor, res, nextPrice ) { - for (var count = 0; count < checkElementFunc.length; count++) { - if ( !checkElementFunc[count] ( building, factor, res, nextPrice ) ) { - return res; + var applyResult = function ( res, factor, nextPrice ) { + idrinth.settings.land.gold = idrinth.settings.land.gold - nextPrice () * factor / 10; + results[res.key] = ( results[res.key] === undefined ? 0 : results[res.key] ) + factor; + idrinth.settings.land[res.key] = idrinth.settings.land[res.key] + factor; + }; + var processBuildings = function ( checkElementFunc, factor, nextPrice ) { + var check = function ( checkElementFunc, building, factor, res, nextPrice ) { + for (var count = 0; count < checkElementFunc.length; count++) { + if ( !checkElementFunc[count] ( building, factor, res, nextPrice ) ) { + return res; + } } - } - return { - min: nextPrice ( building ) / idrinth.land.data[building].perHour, - key: building + return { + min: nextPrice ( building ) / idrinth.land.data[building].perHour, + key: building + }; }; - }; - while ( idrinth.settings.land.gold >= 0 ) { var res = { key: null, min: null }; for (var building in idrinth.land.data) { - res = check ( checkElementFunc, building, factor, res, nextPrice ); + if ( building && idrinth.land.data[building] && idrinth.land.data.hasOwnProperty ( building ) ) { + res = check ( checkElementFunc, building, factor, res, nextPrice ); + } } + return res; + }; + while ( idrinth.settings.land.gold >= 0 ) { + var res = processBuildings ( checkElementFunc, factor, nextPrice ); if ( res.key === null ) { return results; } - idrinth.settings.land.gold = idrinth.settings.land.gold - ( 10 + idrinth.settings.land[res.key] ) * factor * idrinth.land.data[res.key].base / 10; - results[res.key] = ( results[res.key] === undefined ? 0 : results[res.key] ) + factor; - idrinth.settings.land[res.key] = idrinth.settings.land[res.key] + factor; + applyResult ( res, factor, nextPrice ); } + idrinth.settings.save (); return results; }; var getRequirements = function () { var bestPrice = function ( building, factor, res, nextPrice ) { - return res.min === null || nextPrice () / idrinth.land.data[building].perHour < res.min; + return res.min === null || nextPrice ( building ) / idrinth.land.data[building].perHour < res.min; }; var useUp = function ( building, factor, res, nextPrice ) { return nextPrice ( building ) * factor / 10 <= idrinth.settings.land.gold; @@ -56,7 +66,7 @@ idrinth.land = { idrinth.settings.save (); }; for (var key in idrinth.settings.land) { - idrinth.settings.land[key] = parseInt ( document.getElementById ( 'idrinth-land-' + key ).value, 10 ); + idrinth.settings.change ( 'land-' + key, parseInt ( document.getElementById ( 'idrinth-land-' + key ).value, 10 ) ); } var results = baseCalculator ( getRequirements () ); if ( Object.keys ( results ).length === 0 ) { diff --git a/src/mods/settings.js b/src/mods/settings.js index 7c8f8a1..8e99120 100644 --- a/src/mods/settings.js +++ b/src/mods/settings.js @@ -40,9 +40,9 @@ idrinth.settings = { var store = function ( prefix, list, store ) { for (var key in list) { if ( list.hasOwnProperty ( key ) && typeof list[key] !== 'object' && typeof list[key] !== 'function' ) { - window.localStorage.setItem ( prefix + key, idrinth.settings[key] ); + window.localStorage.setItem ( prefix + key, list[key] ); } else if ( list.hasOwnProperty ( key ) && typeof list[key] === 'object' ) { - save ( prefix + key + '-', list[key], store ); + store ( prefix + key + '-', list[key], store ); } } }; diff --git a/src/mods/ui.js b/src/mods/ui.js index c4b7195..ad3dadf 100644 --- a/src/mods/ui.js +++ b/src/mods/ui.js @@ -311,6 +311,14 @@ idrinth.ui = { idrinth.ui.setTooltipTimeout (); } }, + matchesCss: function ( element, selector ) { + while ( element && element !== document ) { + if ( typeof element.matches === 'function' && element.matches ( selector ) ) { + return element; + } + element = element.parentNode + } + }, setTooltipTimeout: function () { idrinth.ui.tooltipTO = window.setTimeout ( idrinth.ui.hideTooltip, idrinth.settings.timeout ? idrinth.settings.timeout : 5000 ); }, @@ -355,12 +363,13 @@ idrinth.ui = { 'use strict'; var handleFrame = function ( parent ) { var frame = parent.getElementsByTagName ( 'iframe' )[0]; - frame.setAttribute ( 'src', ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ) + '&ir=' + Math.random () ); + var src = ( frame.getAttribute ( 'src' ) ).replace ( /&ir=.*/, '' ); + frame.setAttribute ( 'src', src + ( src.indexOf ( '?' ) > -1 ? '&' : '?' ) + 'ir=' + Math.random () ); }; try { if ( idrinth.platform === 'kongregate' ) { window.activateGame ( ); - } else if ( idrinth.platform === 'dawnofthedragons' ) { + } else if ( idrinth.platform === 'facebook'/*'dawnofthedragons'*/ ) { handleFrame ( document ); } else if ( idrinth.platform === 'newgrounds' ) { handleFrame ( document.getElementById ( 'iframe_embed' ) ); @@ -568,7 +577,7 @@ idrinth.ui = { name: 'windows', rType: '#input', type: 'number', - platforms: [ 'dawnofthedragons' ], + platforms: [ 'dawnofthedragons', 'facebook' ], label: 'Maximum Popups/Frames for joining raids' }, { name: 'alarmTime', @@ -812,4 +821,4 @@ idrinth.ui = { } ) ); build (); } -}; \ No newline at end of file +}; diff --git a/src/stable.js b/src/stable.js index 5c0e529..247b698 100644 --- a/src/stable.js +++ b/src/stable.js @@ -93,7 +93,7 @@ var idrinth = { idrinth.ui.removeElement ( 'idrinth-chat' ); idrinth.ui.removeElement ( 'idrinth-war' ); var sc = document.createElement ( 'script' ); - sc.setAttribute ( 'src', 'https://dotd.idrinth.de/static/userscript/' + Math.random () + '/' ); + sc.setAttribute ( 'src', 'https://dotd.idrinth.de/static/userscript/###RELOAD-VERSION###/' + Math.random () + '/' ); document.getElementsByTagName ( 'body' )[0].appendChild ( sc ); window.setTimeout ( function () { idrinth = { };