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

Commit db6e6b6

Browse files
authored
Merge pull request #115 from Idrinth/idrinth
Cleanup raids
2 parents 40f79c2 + b0b5449 commit db6e6b6

10 files changed

+370
-428
lines changed

mods/chat.js

+49-30
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ idrinth.chat = {
1212
refreshChats: function () {
1313
idrinth.chat.oldMessages = JSON.parse ( JSON.stringify ( idrinth.chat.messages ) );
1414
idrinth.chat.messages = [ ];
15-
idrinth.runAjax (
15+
idrinth.core.ajax.run (
1616
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/update/',
1717
idrinth.chat.applyMessages,
1818
idrinth.chat.returnMessages,
@@ -28,7 +28,7 @@ idrinth.chat = {
2828
idrinth.chat.refreshCount++;
2929
},
3030
refreshMembers: function () {
31-
idrinth.runAjax (
31+
idrinth.core.ajax.run (
3232
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/accounts/',
3333
idrinth.chat.applyMembers,
3434
function () {
@@ -144,21 +144,21 @@ idrinth.chat = {
144144
} ) );
145145
},
146146
useroptions: function ( chat, user, rank ) {
147-
idrinth.runAjax (
147+
idrinth.core.ajax.run (
148148
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/rank/',
149149
function ( reply ) {
150150
try {
151151
reply = JSON.parse ( reply );
152-
idrinth.alert ( reply.message );
152+
idrinth.core.alert ( reply.message );
153153
} catch ( e ) {
154-
idrinth.log ( e );
154+
idrinth.core.log ( e );
155155
}
156156
},
157157
function ( reply ) {
158-
idrinth.alert ( this.getMsg ( 'modify.fail' ) );
158+
idrinth.core.alert ( this.getMsg ( 'modify.fail' ) );
159159
},
160160
function ( reply ) {
161-
idrinth.alert ( this.getMsg ( 'modify.fail' ) );
161+
idrinth.core.alert ( this.getMsg ( 'modify.fail' ) );
162162
},
163163
JSON.stringify ( {
164164
chat: chat,
@@ -224,7 +224,10 @@ idrinth.chat = {
224224
if ( !idrinth.chat.emotes.lookup ) {
225225
return message;
226226
}
227-
var part = idrinth.escapeRegExp ( Object.keys ( idrinth.chat.emotes.lookup ).join ( 'TTTT' ) );
227+
var escapeRegExp = function ( str ) {
228+
return str.replace ( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&" );
229+
};
230+
var part = escapeRegExp ( Object.keys ( idrinth.chat.emotes.lookup ).join ( 'TTTT' ) );
228231
var reg = new RegExp ( '(^| )(' + part.replace ( /TTTT/g, '|' ) + ')($| )', 'g' );
229232
return idrinth.chat.replaceInText ( message, reg, [ function ( match ) {
230233
var el = idrinth.chat.emotes.positions[idrinth.chat.emotes.lookup[match.replace ( / /g, '' )]];
@@ -281,10 +284,26 @@ idrinth.chat = {
281284
var processMessages = function ( messages ) {
282285
var addMessages = function ( chatMessages, chatId, chatElement ) {
283286
var buildMessage = function ( message, chat, chatId, messageId ) {
287+
var getfullDateInt = function () {
288+
function addZero ( x, n ) {
289+
while ( x.toString ().length < n ) {
290+
x = "0" + x;
291+
}
292+
return x;
293+
}
294+
var d = new Date ();
295+
return addZero ( d.getFullYear (), 2 ) +
296+
addZero ( d.getMonth (), 2 ) +
297+
addZero ( d.getDate (), 2 ) +
298+
addZero ( d.getHours (), 2 ) +
299+
addZero ( d.getMinutes (), 2 ) +
300+
addZero ( d.getSeconds (), 2 ) +
301+
addZero ( d.getMilliseconds (), 3 );
302+
};
284303
chat.appendChild ( idrinth.ui.buildElement (
285304
{
286305
type: 'li',
287-
id: 'idrinth-single-chat-message-' + messageId + ( parseInt ( messageId, 10 ) < 1 ? '-' + idrinth.getfullDateInt () : '' ),
306+
id: 'idrinth-single-chat-message-' + messageId + ( parseInt ( messageId, 10 ) < 1 ? '-' + getfullDateInt () : '' ),
288307
css: ( parseInt ( message.user, 10 ) === parseInt ( idrinth.chat.self, 10 ) ? 'self-written ' : '' ),
289308
children: [
290309
{
@@ -352,7 +371,7 @@ idrinth.chat = {
352371
try {
353372
chat.lastChild.scrollIntoView ( false );
354373
} catch ( e ) {
355-
idrinth.log ( e );
374+
idrinth.core.log ( e );
356375
}
357376
chat.lastChild.scrollTop = chat.lastChild.scrollHeight;
358377
}
@@ -448,7 +467,7 @@ idrinth.chat = {
448467
window.setTimeout ( idrinth.chat.start, 1000 );
449468
}
450469
window.setTimeout ( function () {
451-
idrinth.runAjax (
470+
idrinth.core.ajax.run (
452471
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/login/',
453472
idrinth.chat.startLoginCallback,
454473
function ( reply ) {
@@ -463,7 +482,7 @@ idrinth.chat = {
463482
);
464483
}, 2500 );
465484
window.setTimeout ( function () {
466-
idrinth.runAjax (
485+
idrinth.core.ajax.run (
467486
'https://dotd.idrinth.de/static/emoticons/data/',
468487
function ( reply ) {
469488
idrinth.chat.emotes = JSON.parse ( reply );
@@ -477,33 +496,33 @@ idrinth.chat = {
477496
}, 1 );
478497
},
479498
create: function () {
480-
idrinth.runAjax (
499+
idrinth.core.ajax.run (
481500
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/create/',
482501
idrinth.chat.joinCallback,
483502
function ( reply ) {
484-
idrinth.alert ( this.getMsg ( 'create.fail' ) );
503+
idrinth.core.alert ( this.getMsg ( 'create.fail' ) );
485504
},
486505
function ( reply ) {
487-
idrinth.alert ( this.getMsg ( 'create.fail' ) );
506+
idrinth.core.alert ( this.getMsg ( 'create.fail' ) );
488507
},
489508
document.getElementById ( 'idrinth-make-chat' ).getElementsByTagName ( 'input' )[0].value
490509
);
491510
},
492511
joinCallback: function ( reply ) {
493512
if ( !reply ) {
494-
idrinth.alert ( this.getMsg ( 'join.fail' ) );
513+
idrinth.core.alert ( this.getMsg ( 'join.fail' ) );
495514
return;
496515
}
497516
reply = JSON.parse ( reply );
498517
if ( !reply ) {
499-
idrinth.alert ( this.getMsg ( 'join.fail' ) );
518+
idrinth.core.alert ( this.getMsg ( 'join.fail' ) );
500519
return;
501520
}
502521
if ( !reply.success ) {
503522
if ( reply.message ) {
504-
idrinth.alert ( reply.message );
523+
idrinth.core.alert ( reply.message );
505524
} else {
506-
idrinth.alert ( this.getMsg ( 'join.notwork' ) );
525+
idrinth.core.alert ( this.getMsg ( 'join.notwork' ) );
507526
}
508527
return;
509528
}
@@ -515,14 +534,14 @@ idrinth.chat = {
515534
users: { },
516535
updateTimeout: null,
517536
add: function () {
518-
idrinth.runAjax (
537+
idrinth.core.ajax.run (
519538
'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/join/',
520539
idrinth.chat.joinCallback,
521540
function ( reply ) {
522-
idrinth.alert ( this.getMsg ( 'join.fail' ) );
541+
idrinth.core.alert ( this.getMsg ( 'join.fail' ) );
523542
},
524543
function ( reply ) {
525-
idrinth.alert ( this.getMsg ( 'join.fail' ) );
544+
idrinth.core.alert ( this.getMsg ( 'join.fail' ) );
526545
},
527546
JSON.stringify ( {
528547
id: document.getElementById ( 'idrinth-add-chat' ).getElementsByTagName ( 'input' )[0].value,
@@ -562,20 +581,20 @@ idrinth.chat = {
562581
},
563582
loginCallback: function ( data ) {
564583
if ( !data ) {
565-
idrinth.alert ( this.getMsg ( 'login.fail' ) );
584+
idrinth.core.alert ( this.getMsg ( 'login.fail' ) );
566585
return;
567586
}
568587
data = JSON.parse ( data );
569588
if ( !data ) {
570-
idrinth.alert ( this.getMsg ( 'login.fail' ) );
589+
idrinth.core.alert ( this.getMsg ( 'login.fail' ) );
571590
return;
572591
}
573592
if ( !data.success && data.message && data['allow-reg'] ) {
574-
idrinth.confirm ( this.getMsg ( 'user.unknown' ), 'idrinth.chat.register();' );
593+
idrinth.core.confirm ( this.getMsg ( 'user.unknown' ), 'idrinth.chat.register();' );
575594
return;
576595
}
577596
if ( !data.success && data.message ) {
578-
idrinth.alert ( data.message );
597+
idrinth.core.alert ( data.message );
579598
return;
580599
}
581600
if ( data.success ) {
@@ -586,7 +605,7 @@ idrinth.chat = {
586605
idrinth.chat.join ( data.data );
587606
return;
588607
}
589-
idrinth.alert ( this.getMsg ( 'login.fail' ) );
608+
idrinth.core.alert ( this.getMsg ( 'login.fail' ) );
590609
},
591610
register: function () {
592611
this.loginActions ( 'register' );
@@ -622,7 +641,7 @@ idrinth.chat = {
622641
type: 'li',
623642
attributes: [ {
624643
name: 'onclick',
625-
value: 'idrinth.runAjax(\'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/delete/' + element.getAttribute ( 'data-id' ) + '/\',idrinth.alert,idrinth.alert,idrinth.alert);this.parentNode.parentNode.removeChild(this.parentNode);'
644+
value: 'idrinth.core.ajax.run(\'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/delete/' + element.getAttribute ( 'data-id' ) + '/\',idrinth.core.alert,idrinth.core.alert,idrinth.core.alert);this.parentNode.parentNode.removeChild(this.parentNode);'
626645
} ]
627646
}, {
628647
type: 'li',
@@ -690,7 +709,7 @@ idrinth.chat = {
690709
'relogin': 'https://dotd.idrinth.de/' + idrinth.platform + '/chat-service/login/'
691710
},
692711
fail = function () {
693-
idrinth.alert ( this.getMsg ( 'login.fail' ) );
712+
idrinth.core.alert ( this.getMsg ( 'login.fail' ) );
694713
},
695714
timeout = function () {
696715
window.setTimeout ( idrinth.chat.login, 1 );
@@ -716,6 +735,6 @@ idrinth.chat = {
716735
headers.pass = chatLogin[1].value;
717736
success = idrinth.chat.loginCallback;
718737
}
719-
idrinth.runAjax ( urls[key], success, fail, timeout, JSON.stringify ( headers ) );
738+
idrinth.core.ajax.run ( urls[key], success, fail, timeout, JSON.stringify ( headers ) );
720739
}
721740
};

mods/core.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
idrinth.core = {
2+
ajax: {
3+
active: { },
4+
run: function ( url, success, failure, timeout, additionalHeader ) {
5+
'use strict';
6+
var requestHandler = new XMLHttpRequest ( );
7+
requestHandler.onreadystatechange = function ( event ) {
8+
var request = ( event || window.event ).target;
9+
var call = function ( func, value ) {
10+
if ( typeof func !== 'function' ) {
11+
return;
12+
}
13+
try {
14+
return func ( value );
15+
} catch ( e ) {
16+
return null;
17+
}
18+
};
19+
if ( request.readyState === 4 ) {
20+
var status = ( request.status > 199 && request.status < 300 ) || request.status === 0;
21+
call ( status ? success : failure, status ? request.responseText : request );
22+
delete idrinth.core.ajax.active[request._url];
23+
}
24+
};
25+
requestHandler.timeout = 30000;
26+
requestHandler.ontimeout = function ( event ) {
27+
var request = ( event || window.event ).target;
28+
timeout.bind ( request );
29+
delete idrinth.core.ajax.active[request._url];
30+
idrinth.core.log ( 'Request to ' + request._url + ' failed.' );
31+
};
32+
var error = function ( event ) {
33+
delete idrinth.core.ajax.active[( event || window.event ).target._url];
34+
idrinth.core.log ( 'Request to ' + ( event || window.event ).target._url + ' failed.' );
35+
};
36+
requestHandler.onerror = error;
37+
requestHandler.onabort = error;
38+
requestHandler._url = url;
39+
requestHandler.open ( "GET", url, true );
40+
if ( url.match ( '/dotd\.idrinth\.de/' ) ) {
41+
if ( additionalHeader ) {
42+
requestHandler.setRequestHeader ( "Idrinth-Addition", additionalHeader );
43+
}
44+
requestHandler.withCredentials = true;
45+
}
46+
idrinth.core.ajax.active[url] = requestHandler;
47+
idrinth.core.ajax.active[url].send ( );
48+
}
49+
},
50+
copyToClipboard: function ( text ) {
51+
var success;
52+
try {
53+
var textAreaElement = idrinth.ui.buildElement ( {
54+
type: 'textarea',
55+
id: "idrinth-copy-helper"
56+
} );
57+
textAreaElement.value = text;
58+
idrinth.ui.body.appendChild ( textAreaElement );
59+
textAreaElement.select ();
60+
success = document.execCommand ( 'copy' );
61+
} catch ( exception ) {
62+
idrinth.core.log ( exception.getMessage () );
63+
success = false;
64+
}
65+
idrinth.ui.removeElement ( "idrinth-copy-helper" );
66+
return success;
67+
},
68+
sendNotification: function ( title, content ) {
69+
if ( !( "Notification" in window ) ) {
70+
return false;
71+
}
72+
if ( window.Notification.permission === "default" ) {
73+
window.Notification.requestPermission ();
74+
}
75+
if ( window.Notification.permission === "denied" ) {
76+
return false;
77+
}
78+
return new window.Notification ( title, {
79+
icon: "https://dotd.idrinth.de/Resources/Images/logo.png",
80+
body: content
81+
} );
82+
},
83+
log: function ( string ) {
84+
'use strict';
85+
console.log ( '[IDotDS] ' + string );
86+
},
87+
alert: function ( text ) {
88+
idrinth.ui.buildModal ( 'Info', text );
89+
},
90+
confirm: function ( text, callback ) {
91+
idrinth.ui.buildModal ( 'Do you?', text, callback );
92+
}
93+
};

mods/land.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ idrinth.land = {
5757
}
5858
var results = baseCalculator ( getRequirements () );
5959
if ( Object.keys ( results ).length === 0 ) {
60-
idrinth.alert ( 'You lack gold to buy any more buildings at the moment.' );
60+
idrinth.core.alert ( 'You lack gold to buy any more buildings at the moment.' );
6161
}
6262
putResults ( results );
6363
},

mods/names.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
idrinth.names = {
22
load: function ( added ) {
33
'use strict';
4-
idrinth.runAjax (
4+
idrinth.core.ajax.run (
55
'https://dotd.idrinth.de/' + idrinth.platform + '/users-service/' + added,
66
function ( text ) {
77
idrinth.names.import ( text );
@@ -33,7 +33,7 @@ idrinth.names = {
3333
idrinth.names.add ( );
3434
}
3535
} catch ( e ) {
36-
idrinth.log ( e );
36+
idrinth.core.log ( e );
3737
}
3838
idrinth.names.counter = idrinth.names.counter + 1;
3939
idrinth.names.ownTimeout = window.setTimeout ( idrinth.names.run, 6666 );
@@ -58,7 +58,7 @@ idrinth.names = {
5858
}
5959
if ( !idrinth.names.users[name.toLowerCase ( )] && name.length > 0 ) {
6060
idrinth.names.users[name.toLowerCase ()] = { };
61-
idrinth.runAjax (
61+
idrinth.core.ajax.run (
6262
'https://dotd.idrinth.de/' + idrinth.platform + '/users-service/add/' + encodeURIComponent ( name ) + '/'
6363
);
6464
}

0 commit comments

Comments
 (0)