-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulkAbandon.js
413 lines (362 loc) · 13.2 KB
/
BulkAbandon.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Abandon Planet
import {
PlanetLevel,
PlanetLevelNames,
} from "https://cdn.skypack.dev/@darkforest_eth/types";
import { getPlanetName } from "https://cdn.skypack.dev/@darkforest_eth/procedural";
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
const OFF_LIMITS = [
'0000ff381808f9e5549b0a06fb40b83b46b2988603241ca756eede057c6b3d8b',
'000026100002a129b93550ffd73470da9ae9b8db6bd18d5d3a49b53a5d45a01e'
]
function getArrivalsForPlanet(planetId) {
return df.getAllVoyages().filter(arrival => arrival.toPlanet == planetId).filter(p => p.arrivalTime > Date.now() / 1000);
}
import {
canStatUpgrade,
canPlanetUpgrade,
getPlanetRank,
//@ts-ignore
} from "https://plugins.zkga.me/utils/utils.js";
class Plugin {
constructor() {
this.maxLevel = 5;
this.timeLevel = 120;
this.whitelist = [];
this.junkLostLabel = document.createElement("div");
this.junkLostLabel.innerText = "Remove 0 Junk";
this.autorun = false;
}
planetLink = (locationId, clickable = true) => {
const planet = df.getPlanetWithId(locationId);
if(!planet) return;
const planetElement = document.createElement(clickable ? "button" : "span");
planetElement.innerText = `L${
planet.planetLevel
}J${df.getDefaultSpaceJunkForPlanetLevel(
planet.planetLevel
)} ${getPlanetName(planet)}`;
planetElement.title = locationId;
planetElement.style.textDecoration = "underline";
planetElement.style.background = "none";
planetElement.style.border = "none";
planetElement.style.color = "white";
planetElement.style.outline = "none";
planetElement.style.padding = "0";
if (clickable) {
planetElement.addEventListener("click", () => {
ui.centerLocationId(locationId);
});
}
return planetElement;
};
invaded(p) {
return p.invader !== ZERO_ADDRESS;
}
captured(p) {
return p.capturer !== ZERO_ADDRESS;
}
invadedAndNotCaptured(p) {
return this.invaded(p) && !this.captured(p);
}
planetIsInsideCaptureZone(p, captureZones) {
if (!p.location || !p.location.coords) return false;
for (let zone of captureZones) {
let zCoords = zone.coords;
let zRadius = zone.radius;
let x = zCoords.x - p.location.coords.x;
let y = zCoords.y - p.location.coords.y;
let dist = Math.sqrt(x * x + y * y);
if (dist < zRadius) return true;
}
return false;
}
removeAllChildNodes = (parent) => {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
};
getOwnedPlanetsInRange = (source) => {
if (!source) return;
const ownedPlanetsInRange = df
.getPlanetsInRange(source, 100)
.filter(
(planet) =>
planet.owner != ZERO_ADDRESS &&
planet.locationId != source &&
!OFF_LIMITS.includes(planet.locationId) &&
getArrivalsForPlanet(planet.locationId).length == 0
)
.sort(
(a, b) => b.planetLevel - a.planetLevel
);
// if (ownedPlanetsInRange.length === 0) return undefined;
return ownedPlanetsInRange;
};
getNearestOwnedPlanetInRange = (source) => {
const nearestPlanets = this.getOwnedPlanetsInRange(source);
return nearestPlanets ? nearestPlanets[0] : undefined;
};
abandon = (evt) => {
let sources = this.source;
console.log('sources for abandon', sources);
if (!sources) return;
const acct = df.getAccount();
if(!acct) return;
sources.forEach((source) => {
const src = df.getPlanetWithId(source);
if(!src) return;
if (df.getPlayerSpaceJunk(acct) < 1000) return;
const target = this.getNearestOwnedPlanetInRange(source);
if (!target) {
console.log('target not found')
return
};
if (getArrivalsForPlanet(target.locationId).length != 0) {
console.log(`target has arrivals`);
return;
}
console.log(`abandon source: ${getPlanetName(src)}, target: ${getPlanetName(target)}`);
df.move(src.locationId, target.locationId, src.energy, 0, false, true);
});
this.removeAllChildNodes(this.sourcePlanetContainer);
};
updatePlanets(numPlanets) {
if (!numPlanets) {
alert("input a number");
return;
}
this.source = [];
this.removeAllChildNodes(this.sourcePlanetContainer);
this.sourcePlanetContainer.append("Planets to abandon:");
const planets = this.getPlanetsToAbandon(numPlanets);
let junk = 0;
// console.log(planets);
for (let planet of planets) {
if (!planet) {
this.sourcePlanetContainer.append("?????");
continue;
}
const linkContainer = document.createElement("div");
linkContainer.style.marginBottom = `4px`;
const planetLink = this.planetLink(planet.locationId);
const removeButton = document.createElement("button");
removeButton.innerText = `X`;
removeButton.style.float = "right";
removeButton.addEventListener("click", (evt) => {
this.source = this.source.filter(
(p) =>
p != planet.locationId &&
planet.locationId != df.getHomeHash() &&
df.getPlanetsInRange(p).length != 0
);
this.sourcePlanetContainer.removeChild(linkContainer);
console.log(`length: ${this.source.length}`);
});
linkContainer.appendChild(planetLink);
linkContainer.appendChild(removeButton);
this.sourcePlanetContainer.append(linkContainer);
this.source.push(planet.locationId);
junk += df.getDefaultSpaceJunkForPlanetLevel(planet.planetLevel);
}
this.junkLostLabel.innerText = `Remove ${junk} Junk`;
}
clearSendTimer() {
console.log("check clear timer")
if (this.sendTimer) {
console.log("clearing interval")
clearInterval(this.sendTimer);
}
}
getPlanetsToAbandon = (n) => {
let captureZones = df.getCaptureZones();
// console.log(JSON.stringify(df.getMyPlanets().map(p => p.planetLevel)))
let sortedPlanets = df
.getMyPlanets()
.filter(
(planet) =>
planet.locationId != df.getHomeHash() &&
!this.planetIsInsideCaptureZone(planet, captureZones) &&
!this.invadedAndNotCaptured(planet) &&
planet.planetLevel <= this.maxLevel &&
getPlanetRank(planet) == 0 &&
df.entityStore.getArrivalIdsForLocation(planet.locationId) &&
df.entityStore.getArrivalIdsForLocation(planet.locationId).length == 0 &&
this.getNearestOwnedPlanetInRange(planet.locationId)
)
.sort((a, b) => a.planetLevel - b.planetLevel);
// console.log(sortedPlanets.map((p) => p.planetLevel));
if (sortedPlanets.length < n) return sortedPlanets;
return sortedPlanets.slice(0, n);
};
render(container) {
container.parentElement.style.minHeight = "unset";
container.style.minHeight = "unset";
container.style.width = "300px";
const firstTextDiv = document.createElement("div");
firstTextDiv.innerText = "Choose a planet and abandon it";
this.sourcePlanetContainer = document.createElement("div");
this.sourcePlanetContainer.innerText = "Current source: none";
const addPlanetSourceButton = document.createElement("button");
addPlanetSourceButton.innerText = "Choose Planet";
addPlanetSourceButton.style.marginInline = "2px";
addPlanetSourceButton.style.marginBottom = "10px";
addPlanetSourceButton.addEventListener("click", () => {
this.removeAllChildNodes(this.sourcePlanetContainer);
this.sourcePlanetContainer.append("Planet to abandon:");
const sourcePlanet = ui.getSelectedPlanet();
if (sourcePlanet) {
if (sourcePlanet.owner !== df.getAccount()) {
alert("planet must be owned by player");
return;
}
if (sourcePlanet.locationId == df.getHomeHash()) {
alert("cannot abandon home planet");
return;
}
this.source = [sourcePlanet.locationId];
this.source.forEach((planet) => {
if (!planet) {
this.sourcePlanetContainer.append("?????");
return;
}
const linkContainer = document.createElement("div");
const planetLink = this.planetLink(planet);
const removeButton = document.createElement("button");
removeButton.innerText = `X`;
removeButton.style.float = "right";
removeButton.addEventListener("click", (evt) => {
this.source.filter((p) => p !== planet);
this.sourcePlanetContainer.removeChild(linkContainer);
});
linkContainer.appendChild(this.planetLink(planet));
linkContainer.appendChild(removeButton);
// console.log(linkContainer);
this.sourcePlanetContainer.append(linkContainer);
});
}
});
let autoSendLabel = document.createElement("label");
autoSendLabel.innerHTML = "Auto Abandon (Every 120 seconds)";
autoSendLabel.style.paddingRight = "10px";
let autoSendCheck = document.createElement("input");
autoSendCheck.type = "checkbox";
autoSendCheck.style.marginRight = "10px";
autoSendCheck.checked = false;
autoSendCheck.onchange = (evt) => {
if (evt.target.checked) {
console.log(`Starting abandon...`);
this.updatePlanets(10);
this.abandon();
this.sendTimer = setInterval(() => {
console.log(`console log in interval...`);
this.updatePlanets(5);
this.abandon();
}, 1000 * this.timeLevel);
} else {
this.clearSendTimer();
}
};
let inputLabel = document.createElement("span");
inputLabel.innerText = "Choose N smallest planets to abandon:";
this.userInput = document.createElement("input");
this.userInput.placeholder = "# of planets";
this.userInput.style.color = "black";
this.userInput.style.padding = "5px";
this.userInput.style.marginRight = "5px";
this.userInput.style.width = "50%";
const updateButton = document.createElement("button");
updateButton.color = "red";
updateButton.innerText = "update list";
updateButton.addEventListener("click", (evt) => {
let numPlanets = parseInt(this.userInput.value);
this.updatePlanets(numPlanets);
});
let stepperLabel = document.createElement("label");
stepperLabel.innerText = "Max level";
stepperLabel.style.display = "block";
let maxLevelRange = createRange(this.maxLevel);
let maxLevelLabel = document.createElement("span");
maxLevelLabel.innerText = `${maxLevelRange.value}`;
maxLevelLabel.style.float = "right";
maxLevelRange.onchange = (evt) => {
maxLevelLabel.innerText = `${evt.target.value}`;
try {
this.maxLevel = parseInt(evt.target.value, 10);
} catch (e) {
console.error("could not parse level", e);
}
};
let timeStepperLabel = document.createElement("label");
timeStepperLabel.innerText = "Time";
timeStepperLabel.style.display = "block";
let timeRange = createTimeRange(this.timeLevel);
let timeLabel = document.createElement("span");
timeLabel.innerText = `${timeRange.value} seconds`;
timeLabel.style.float = "right";
timeRange.onchange = (evt) => {
timeLabel.innerText = `${evt.target.value} seconds`;
autoSendLabel.innerHTML = `Auto Abandon (Every ${evt.target.value} seconds)`;
try {
this.timeLevel = parseInt(evt.target.value, 120);
} catch (e) {
console.error("could not parse level", e);
}
};
let inputWrapper = document.createElement("div");
inputWrapper.style.marginBottom = "10px";
inputWrapper.appendChild(inputLabel);
inputWrapper.appendChild(this.userInput);
inputWrapper.appendChild(updateButton);
const myButton = document.createElement("button");
myButton.color = "red";
myButton.margin = "2px";
myButton.innerText = "ABANDON";
myButton.addEventListener("click", this.abandon);
container.appendChild(firstTextDiv);
container.appendChild(addPlanetSourceButton);
container.appendChild(document.createElement("hr"));
container.appendChild(inputWrapper);
container.appendChild(stepperLabel);
container.appendChild(maxLevelRange);
container.appendChild(maxLevelLabel);
container.appendChild(stepperLabel);
container.appendChild(maxLevelRange);
container.appendChild(maxLevelLabel);
container.appendChild(document.createElement("br"));
container.appendChild(this.sourcePlanetContainer);
container.appendChild(myButton);
container.appendChild(this.junkLostLabel);
container.appendChild(timeStepperLabel);
container.appendChild(timeRange);
container.appendChild(timeLabel);
container.appendChild(autoSendLabel);
container.appendChild(autoSendCheck);
}
destroy() {
clearInterval(this.sendTimer);
}
}
export default Plugin;
function createRange(value) {
let range = document.createElement("input");
range.type = "range";
range.min = "0";
range.max = "8";
range.step = "1";
range.value = value;
range.style.width = "80%";
range.style.height = "24px";
return range;
}
function createTimeRange(value) {
let range = document.createElement("input");
range.type = "range";
range.min = "30";
range.max = "1800";
range.step = "30";
range.value = value;
range.style.width = "80%";
range.style.height = "24px";
return range;
}