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 #248 from IDotD/idrinth
Browse files Browse the repository at this point in the history
Idrinth
  • Loading branch information
Idrinth authored Jan 12, 2017
2 parents 5507f0a + c98cf7d commit 2bf45b4
Show file tree
Hide file tree
Showing 12 changed files with 1,102 additions and 398 deletions.
26 changes: 11 additions & 15 deletions src/mods/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -703,26 +703,26 @@ 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];
}
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" ),
pass: idrinth.settings.get ( "chatpass" )
} )
);
}, 2500 );
window.setTimeout ( function () {
idrinth.core.timeouts.add ( 'chat.emoticons', function () {
idrinth.core.ajax.runHome (
'emoticons/data/',
function ( reply ) {
Expand Down Expand Up @@ -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/',
Expand Down Expand Up @@ -804,10 +803,7 @@ idrinth.chat = {
idrinth.ui.buildChat ( chatId, list[chatId].name, list[chatId].access, list[chatId].pass );
}
}
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 ) {
Expand Down Expand Up @@ -859,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: [ {
Expand Down Expand Up @@ -939,7 +935,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: '',
Expand All @@ -952,7 +948,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" );
Expand Down
192 changes: 189 additions & 3 deletions src/mods/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
idrinth.core = {
/**
*
* @param {string} str
* @returns {string}
*/
escapeRegExp: function ( str ) {
return str.replace ( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&" );
},
Expand All @@ -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 ( );
Expand Down Expand Up @@ -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 {
Expand All @@ -77,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 ) {
Expand All @@ -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' ) );
Expand All @@ -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;
Expand All @@ -112,33 +165,166 @@ idrinth.core = {
body: content
} );
},
/**
*
* @type {object}
*/
timeouts: {
/**
*
* @type {object}
*/
next: null,
/**
*
* @type {object}
*/
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 ) {
idrinth.core.timeouts.list[identifier].repeats = 1;
idrinth.core.timeouts.list[identifier].func = function () {
};
}
},
/**
*
* @param {string} identifier
* @param {function} func
* @param {int} time in milliseconds
* @param {Number} [1] maxRepeats -1 will repeat until the end of time
* @returns {undefined}
*/
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,
repeats: maxRepeats ? maxRepeats : 1
};
},
/**
* activates all relevant timeouts and intervals
* @returns {undefined}
*/
process: function () {
'use strict';
var date = ( new Date () ).getTime () + ( new Date () ).getMilliseconds () / 1000;
var min = 10;
/**
*
* @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 ();
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].next = date + idrinth.core.timeouts.list[property].duration / 1000;
} 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, Math.ceil ( 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] : { };
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] ) {
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 );
} );
}
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;
Expand Down
21 changes: 21 additions & 0 deletions src/mods/facebook.js
Original file line number Diff line number Diff line change
@@ -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 );
}
};
Loading

0 comments on commit 2bf45b4

Please sign in to comment.