-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGetInventory.html
169 lines (143 loc) · 5.61 KB
/
GetInventory.html
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="lib/jquery-2.2.1.min.js"></script>
</head>
<body>
<form>
<input type="text" id="user_id" name="user_id" placeholder="user_id" />
<input type="text" id="password" name="password" placeholder="password" />
<input type="button" value="Submit" onclick="getInventoryHash()" />
</form>
<hr>
<div id="output" style="word-wrap:break-word"></div>
<script>
'user strict';
init();
function getInventoryHash() {
var userID = $('#user_id').val();
var password = $('#password').val();
$('#output').html('');
var url = "https://spellstone.synapse-games.com/api.php?message=update&user_id=" + userID + "&password=" + password;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
var response = JSON.parse(this.responseText);
if (response.user_units) {
var hash = hashInventory(response.user_units);
var output = $('#output');
output.append(createLink("View as Deck Hash", hash, ''));
output.append($('<br/>'));
output.append(createLink("View as Inventory", '', hash));
} else {
throw "Invalid Credentials";
}
} else {
displayError(this.status + '; ' + this.responseText);
}
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function createLink(text, hash, inventory) {
var a = $('<a>');
a.attr("href", "javascript:void(0);");
a.attr("onclick", "openDeckBuilder('" + hash + "', '" + inventory + "')");
a.text(text); //add the text node to the newly created div.
return a;
}
function hashInventory(units) {
var inventory = [];
for (var uid in units) {
var unit = units[uid];
unitInfo = { id: unit.unit_id, level: unit.level, runes: [] };
for (var key in unit.runes) {
unitInfo.runes.push({ id: unit.runes[key].item_id });
}
inventory.push(unitInfo);
}
return hashEncode({ commander: null, deck: inventory });
}
//Returns hash built from deck array
function hashEncode(deck, noIndexes) {
var encoded = [];
var has_priorities = false;
var has_indexes = false;
var indexes = [];
if (deck.commander) {
encoded.push(unitInfoToBase64(deck.commander));
}
var copies = [1];
for (var k in deck.deck) {
var unitInfo = deck.deck[k];
var unitHash = unitInfoToBase64(unitInfo);
encoded.push(unitHash);
}
return encoded.join("");
}
// Used to determine how to hash runeIDs
var maxRuneID = 1000;
function unitInfoToBase64(unitInfo) {
var baseID = parseInt(unitInfo.id);
var level = (parseInt(unitInfo.level) - 1);
var fusion = Math.floor(baseID / 10000);
baseID %= 10000;
var runeID = 0;
if (unitInfo.runes.length) {
runeID = parseInt(unitInfo.runes[0].id);
runeID %= 5000; // Runes IDs are all in the range of 5000 - 5999
}
var base10 = baseID;
base10 = base10 * 3 + fusion;
base10 = base10 * 7 + level;
base10 = base10 * maxRuneID + runeID;
return base10ToBase64(base10, 5);
}
var base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!~";
function base10ToBase64(base10, len) {
var base64 = '';
for (var i = 0; i < len; i++) {
var part = base10 % 64;
base64 += base64chars[part];
base10 = (base10 - part) / 64;
}
return base64;
}
function displayResponse(response) {
document.getElementById("output").innerHTML = response;
}
function displayError(response) {
displayResponse('<font style="color:red;">' + response + '</font>');
}
// GET variables from query string
function _GET(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1] ? pair[1] : '');
}
}
return undefined;
}
function init() {
$('#user_id').val(_GET('userID'));
$('#password').val(_GET('password'));
}
function openDeckBuilder(hash, inventory) {
var win = window.open("DeckBuilder.html");
// Push values to window once it has loaded
$(win).load((function (hash, inventory) {
return function () {
win.externalData(hash, inventory);
}
})(hash, inventory));
}
</script>
</body>
</html>