Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #124 from Idrinth/idrinth
Browse files Browse the repository at this point in the history
preparation multiple tickets
Merging because approval was given in the wrong way :P
  • Loading branch information
Idrinth authored Sep 15, 2016
2 parents 2551ee6 + 63c07d0 commit b78b6ea
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 24 deletions.
2 changes: 1 addition & 1 deletion MAINTAINERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
idrinth
Idrinth
w20k
20 changes: 20 additions & 0 deletions src/libs/matches-selector-polyfill.js
Original file line number Diff line number Diff line change
@@ -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;
};
43 changes: 43 additions & 0 deletions src/mods/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
}
}
};
42 changes: 26 additions & 16 deletions src/mods/land.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ) {
Expand Down
4 changes: 2 additions & 2 deletions src/mods/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
};
Expand Down
17 changes: 13 additions & 4 deletions src/mods/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
},
Expand Down Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -812,4 +821,4 @@ idrinth.ui = {
} ) );
build ();
}
};
};
2 changes: 1 addition & 1 deletion src/stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { };
Expand Down

0 comments on commit b78b6ea

Please sign in to comment.