-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
263 lines (226 loc) · 9.05 KB
/
main.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// =======================================================================
// ========================== PhantomJS Exports ==========================
// =======================================================================
// Execute this on PhantomJS...
// const main = require('/Users/victor/Desktop/Extra/DragonBound/main.js')
const clothes = require('/Users/victor/Desktop/Extra/DragonBound/Clothes/clothes_lib.js')
var heads = clothes.heads
var bodies = clothes.bodies
var glasses = clothes.glasses
var flags = clothes.flags
exports.findItems = findItems // Greedy Main Algorithmn
exports.findItems2 = findItems2 // Bruteforce Main Algorithmn
exports.findPages = findPages // Find Avatar Shop Page Number (not so precise because some items are deleted)
// =======================================================================
// =======================================================================
// =======================================================================
Object.defineProperty(Array.prototype, 'combinations', {
value: function () {
if (this == null) {
throw new TypeError('Array.prototype.combinations called on null or undefined')
}
var i, j, subresult
var result = []
var length = this.length
var combinations = Math.pow(2, length)
for (i = 0; i < combinations; i++) {
subresult = []
for (j = 0; j < length; j++) {
// & is bitwise AND
if ((i & Math.pow(2, j))) {
subresult.push(this[j])
}
}
result.push(subresult)
}
return result
}
})
// Find the Page Number where the item is located on the Avatar Shop
// Usage: findPage(heads, 'Indian Fighter')
function findPage (items, name) {
return Math.trunc(items.findIndex(function (item) { return item.name === name }) / 9) + 1
}
// Find the Pages Numbers where the all the items are located on the Avatar Shop
// names: The array of item names [headName, bodyName, glassName, flagName]
// Usage: findPages(['Pikachu Costume', 'Matrix', 'Fire Phoenix', 'Neo Observador'])
function findPages (names) {
var pages = []
var headPage = Math.trunc(heads.findIndex(function (item) { return item.name === names[0] }) / 9) + 1
var bodyPage = Math.trunc(bodies.findIndex(function (item) { return item.name === names[1] }) / 9) + 1
var glassPage = Math.trunc(glasses.findIndex(function (item) { return item.name === names[2] }) / 9) + 1
var flagPage = Math.trunc(flags.findIndex(function (item) { return item.name === names[3] }) / 9) + 1
return [headPage, bodyPage, glassPage, flagPage]
}
// Sort the items based on the following criterias...
// The first criteria is the main sort, the next ones are considered as tiebreaker criterias
// Usage: sortItems(heads, ['attack', 'defense', 'dig', 'life'])
function sortItems (items, criterias) {
items.sort(function (item1, item2) {
for (i = 0; i < criterias.length; i++) {
if (item1[criterias[i]] < item2[criterias[i]]) return 1
if (item1[criterias[i]] > item2[criterias[i]]) return -1
}
})
}
// Sum array elements
function sum (numbers) {
const add = function (a, b) { return a + b }
return numbers.reduce(add, 0)
}
// Find the best item considering the sum of the considered item statuses values
// Usage: bestItem(heads, ['attack', 'defense'])
// Usage: bestItem(heads, ['attack', 'defense', 'dig', 'life', 'delay', 'delay_item', 'popularity'])
function bestItem (items, statuses) {
return items.reduce(function (item1, item2) {
var sum1 = sum(statuses.map(function (status) {
return item1[status]
}))
var sum2 = sum(statuses.map(function (status) {
return item2[status]
}))
return (sum1 > sum2 ? item1 : item2)
})
}
// Possible to sort the above function? Will be amazing
// =======================================================================
// ========== Main Algorithmns to find the Best Avatar Shop Set ==========
// =======================================================================
// Usage 1:
// findItems(['attack', 'defense'])
// findItems(['attack', 'defense', 'life', 'dig'])
// findItems(['attack', 'delay', 'delay_item', 'defense'])
// findItems(['attack', 'delay', 'delay_item', 'dig', 'defense', 'life'])
// findItems(['attack', 'delay', 'delay_item', 'defense', 'life'])
// Usage 2:
// findItems2(['attack', 'defense'])
// findItems2(['attack', 'defense', 'life', 'dig'])
// findItems2(['attack', 'delay', 'delay_item', 'defense'])
// findItems2(['attack', 'delay', 'delay_item', 'dig', 'defense', 'life'])
// findItems2(['attack', 'delay', 'delay_item', 'defense', 'life'])
// Caching Best Items Method
// bestItem(heads, ['attack'])
// bestItem(bodies, ['attack', 'defense'])
cachedBestItems = {
heads: {}, // heads: { "attack,defense": { name: "Skeleton", ... } }
bodies: {},
glasses: {},
flags: {}
}
function accessBestItem (clothType, statuses) {
var statuses = statuses.sort()
var statusesKey = statuses.join(',')
if (cachedBestItems[clothType][statusesKey]) {
return cachedBestItems[clothType][statusesKey]
}
cachedBestItems[clothType][statusesKey] = bestItem(window[clothType], statuses)
return cachedBestItems[clothType][statusesKey]
}
// Greedy Version
function findItems (statuses) {
var resultSet = []
var head = {}
var body = {}
var glass = {}
var flag = {}
var resultStats = {}
var allResultStats = []
var combinations = statuses.combinations()
var length = combinations.length
for (var i=0; i < length; ++i) {
head = accessBestItem('heads', combinations[i])
for (var j=0; j < length; ++j) {
body = accessBestItem('bodies', combinations[j])
for (var k=0; k < length; ++k) {
glass = accessBestItem('glasses', combinations[k])
for (var l=0; l < length; ++l) {
flag = accessBestItem('flags', combinations[l])
resultSet.push(head)
resultSet.push(body)
resultSet.push(glass)
resultSet.push(flag)
var resultStats = {
'names': [],
'attack': 0,
'popularity': 0,
'defense': 0,
'dig': 0,
'life': 0,
'delay': 0,
'delay_item': 0,
'shield': 0
}
resultSet.forEach(function (item) {
resultStats['names'] = resultStats['names'].concat(item['name'])
resultStats['attack'] = Math.min(50, resultStats['attack'] + item['attack'])
resultStats['popularity'] = Math.min(50, resultStats['popularity'] + item['popularity'])
resultStats['defense'] = Math.min(50, resultStats['defense'] + item['defense'])
resultStats['dig'] = Math.min(50, resultStats['dig'] + item['dig'])
resultStats['life'] = Math.min(50, resultStats['life'] + item['life'])
resultStats['delay'] = Math.min(50, resultStats['delay'] + item['delay'])
resultStats['delay_item'] = Math.min(50, resultStats['delay_item'] + item['delay_item'])
resultStats['shield'] = Math.min(50, resultStats['shield'] + item['shield'])
})
allResultStats.push(resultStats)
resultSet = []
}
}
}
}
return bestItem(allResultStats, statuses)
}
// Bruteforce Version
function findItems2 (statuses) {
var resultSet = []
var head = {}
var body = {}
var glass = {}
var flag = {}
var resultStats = {}
var allResultStats = []
var headsLength = heads.length
var bodiesLength = bodies.length
var glassesLength = glasses.length
var flagsLength = flags.length
for (var i=0; i < headsLength; ++i) {
head = heads[i]
for (var j=0; j < bodiesLength; ++j) {
body = bodies[j]
for (var k=0; k < glassesLength; ++k) {
glass = glasses[k]
for (var l=0; l < flagsLength; ++l) {
flag = flags[l]
resultSet.push(head)
resultSet.push(body)
resultSet.push(glass)
resultSet.push(flag)
var resultStats = {
'names': [],
'attack': 0,
'popularity': 0,
'defense': 0,
'dig': 0,
'life': 0,
'delay': 0,
'delay_item': 0,
'shield': 0
}
resultSet.forEach(function (item) {
resultStats['names'] = resultStats['names'].concat(item['name'])
resultStats['attack'] = Math.min(50, resultStats['attack'] + item['attack'])
resultStats['popularity'] = Math.min(50, resultStats['popularity'] + item['popularity'])
resultStats['defense'] = Math.min(50, resultStats['defense'] + item['defense'])
resultStats['dig'] = Math.min(50, resultStats['dig'] + item['dig'])
resultStats['life'] = Math.min(50, resultStats['life'] + item['life'])
resultStats['delay'] = Math.min(50, resultStats['delay'] + item['delay'])
resultStats['delay_item'] = Math.min(50, resultStats['delay_item'] + item['delay_item'])
resultStats['shield'] = Math.min(50, resultStats['shield'] + item['shield'])
})
allResultStats.push(resultStats)
resultSet = []
}
}
}
}
return bestItem(allResultStats, statuses)
}