-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNetwork.js
More file actions
86 lines (68 loc) · 2.32 KB
/
Network.js
File metadata and controls
86 lines (68 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { RateLimiter } from '/twcheese/src/Models/RateLimiter.js';
let throttle = new RateLimiter(5);
let originalFetch = window.fetch;
window.fetch = function() {
throttle.requestWasMade();
return originalFetch.apply(this, arguments);
};
/**
* requests the document from a url
* @param {string} url the url of the page to get the document from
* @return {Promise}
* @resolve {HTMLDocment}
*/
async function requestDocument(url) {
await throttle.sleepIfNeeded();
let response = await fetch(url);
let responseText = await response.text();
let doc = (new DOMParser()).parseFromString(responseText, 'text/html');
Object.defineProperty(doc, 'URL', {
get: () => url
});
return doc;
};
/**
* requests xml document from a url
* @param {string} url the url of the page to get the cml document from
* @return {Promise}
* @resolve {XMLDocument}
*/
async function requestXml(url) {
await throttle.sleepIfNeeded();
let response = await fetch(url);
let responseText = await response.text();
let xmlDoc = (new DOMParser()).parseFromString(responseText, 'text/xml');
Object.defineProperty(xmlDoc, 'URL', {
get: () => url
});
return xmlDoc;
};
/**
* make a POST request to the game
* @param {string} screen
* @param {object} uriParams
* @param {object} data
* @return {Promise}
* @resolve {object} response data from the game
*/
async function postToGame(screen, uriParams, data) {
await throttle.sleepIfNeeded();
return new Promise(function(resolve, reject) {
window.TribalWars.post(screen, uriParams, data, resolve, reject);
});
}
function gameUrl(screen, uriParams, method = 'GET') {
return 'https://' + document.domain + window.TribalWars.buildURL(method, screen, uriParams);
}
function attackPrepUrl(unitCounts, targetVillageId, originVillageId = window.game_data.village.id) {
let uriParams = {
from: 'simulator',
village: originVillageId,
target_village_id: targetVillageId
};
for (let [unitType, count] of Object.entries(unitCounts)) {
uriParams['att_' + unitType] = count;
}
return gameUrl('place', uriParams);
}
export { requestDocument, requestXml, postToGame, gameUrl, attackPrepUrl };