/
plugin.js
192 lines (162 loc) · 5.82 KB
/
plugin.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
// Hunt Artifacts
import { EMPTY_ADDRESS } from "https://cdn.skypack.dev/@darkforest_eth/constants";
import {
PlanetType,
PlanetLevel,
PlanetLevelNames
} from "https://cdn.skypack.dev/@darkforest_eth/types";
class Plugin {
constructor() {
this.maxEnergyPercent = 50;
this.minPlanetLevel = 2;
}
render(container) {
container.style.width = '200px';
let stepperLabel = document.createElement('label');
stepperLabel.innerText = 'Max % energy to spend';
stepperLabel.style.display = 'block';
let stepper = document.createElement('input');
stepper.type = 'range';
stepper.min = '0';
stepper.max = '100';
stepper.step = '5';
stepper.value = `${this.maxEnergyPercent}`;
stepper.style.width = '80%';
stepper.style.height = '24px';
let percent = document.createElement('span');
percent.innerText = `${stepper.value}%`;
percent.style.float = 'right';
stepper.onchange = (evt) => {
percent.innerText = `${evt.target.value}%`;
try {
this.maxEnergyPercent = parseInt(evt.target.value, 10);
} catch (e) {
console.error('could not parse energy percent', e);
}
}
let levelLabel = document.createElement('label');
levelLabel.innerText = 'Min. level to capture';
levelLabel.style.display = 'block';
let level = document.createElement('select');
level.style.background = 'rgb(8,8,8)';
level.style.width = '100%';
level.style.marginTop = '10px';
level.style.marginBottom = '10px';
Object.values(PlanetLevel).forEach(lvl => {
let opt = document.createElement('option');
opt.value = `${lvl}`;
opt.innerText = PlanetLevelNames[lvl];
level.appendChild(opt);
});
level.value = `${this.minPlanetLevel}`;
level.onchange = (evt) => {
try {
this.minPlanetLevel = parseInt(evt.target.value, 10);
} catch (e) {
console.error('could not parse planet level', e);
}
}
let message = document.createElement('div');
let button = document.createElement('button');
button.style.width = '100%';
button.style.marginBottom = '10px';
button.innerHTML = 'Hunt from selected'
button.onclick = () => {
let planet = ui.getSelectedPlanet();
if (planet) {
message.innerText = 'Please wait...';
// TODO: Min planet level?
let moves = captureArtifacts(
planet.locationId,
this.maxEnergyPercent,
this.minPlanetLevel
);
message.innerText = `Capturing ${moves} planets.`;
} else {
message.innerText = 'No planet selected.';
}
}
let globalButton = document.createElement('button');
globalButton.style.width = '100%';
globalButton.style.marginBottom = '10px';
globalButton.innerHTML = 'Globally hunt!'
globalButton.onclick = () => {
message.innerText = 'Please wait...';
let moves = 0;
for (let planet of df.getMyPlanets()) {
// TODO: Make asteroid check configurable
if (!isAsteroid(planet)) {
setTimeout(() => {
moves += captureArtifacts(planet.locationId, this.maxEnergyPercent, this.minPlanetLevel);
message.innerText = `Capturing ${moves} planets.`;
}, 0);
}
}
}
container.appendChild(stepperLabel);
container.appendChild(stepper);
container.appendChild(percent);
container.appendChild(levelLabel);
container.appendChild(level);
container.appendChild(button);
container.appendChild(globalButton);
container.appendChild(message);
}
}
export default Plugin;
function isAsteroid(planet) {
return planet.planetType === PlanetType.SILVER_MINE;
}
function captureArtifacts(fromId, maxDistributeEnergyPercent, minCaptureLevel) {
const to = df.getPlanetWithId(fromId);
const from = df.getPlanetWithId(fromId);
// Rejected if has pending outbound moves
const unconfirmed = df.getUnconfirmedMoves().filter(move => move.from === fromId)
if (unconfirmed.length !== 0) {
return 0;
}
const candidates_ = df.getPlanetsInRange(fromId, maxDistributeEnergyPercent)
.filter(p => df.isPlanetMineable(p) && p.owner === EMPTY_ADDRESS && p.planetLevel >= minCaptureLevel)
.map(to => {
return [to, distance(from, to)]
})
.sort((a, b) => a[1] - b[1]);
let i = 0;
const energyBudget = Math.floor((maxDistributeEnergyPercent / 100) * to.energy);
let energySpent = 0;
let moves = 0;
while (energyBudget - energySpent > 0 && i < candidates_.length) {
const energyLeft = energyBudget - energySpent;
// Remember its a tuple of candidates and their distance
const candidate = candidates_[i++][0];
// Rejected if has unconfirmed pending arrivals
const unconfirmed = df.getUnconfirmedMoves().filter(move => move.to === candidate.locationId)
if (unconfirmed.length !== 0) {
continue;
}
// Rejected if has pending arrivals
const arrivals = getArrivalsForPlanet(candidate.locationId);
if (arrivals.length !== 0) {
continue;
}
const energyArriving = (candidate.energyCap * 0.15) + (candidate.energy * (candidate.defense / 100));
// needs to be a whole number for the contract
const energyNeeded = Math.ceil(df.getEnergyNeededForMove(fromId, candidate.locationId, energyArriving));
if (energyLeft - energyNeeded < 0) {
continue;
}
df.move(fromId, candidate.locationId, energyNeeded, 0);
energySpent += energyNeeded;
moves += 1;
}
return moves;
}
function getArrivalsForPlanet(planetId) {
return df.getAllVoyages().filter(arrival => arrival.toPlanet === planetId).filter(p => p.arrivalTime > Date.now() / 1000);
}
//returns tuples of [planet,distance]
function distance(from, to) {
let fromloc = from.location;
let toloc = to.location;
return Math.sqrt((fromloc.coords.x - toloc.coords.x) ** 2 + (fromloc.coords.y - toloc.coords.y) ** 2);
}