Skip to content

Instantly share code, notes, and snippets.

@nahkd123
Last active January 4, 2022 12:50
Show Gist options
  • Save nahkd123/b34e37ce23b33b1ccc071e9733d00f01 to your computer and use it in GitHub Desktop.
Save nahkd123/b34e37ce23b33b1ccc071e9733d00f01 to your computer and use it in GitHub Desktop.
Gift and Destroy (DF plugin)

Gift and Destroy

Gift then destroy planets. Created for use with d_fdao community round.

Warning

Plugins can have access to game private informations (private key for example). Make sure to validate the code before using it (just in case...)

This is also why you should only import private key with little to no funds, besides from localStorage being insecure.

Changelogs

  • 2022/1/3: The beginning of this plugin
  • 2022/1/4: Swapped planet #1 and planet #2 at ownership transfering part
// Gift and Destroy
// Gift then destroy planets
let createElement = (type, text = "", className = "") => {
let e = document.createElement(type);
e.textContent = text;
e.className = className;
return e;
};
let createButton = (text = "") => {
let e = createElement("button", text);
e.style.border = "1px solid white";
return e;
};
let createPlanetEntry = () => {
let entryInfo = { planet: null };
let parent = createElement("div");
parent.style.padding = "8px";
parent.style.background = "#7f7f7f1e";
parent.style.marginBottom = "6px";
parent.style.borderRadius = "4px";
let label = createElement("div", `Planet ID: Not Selected`);
label.style.textOverflow = "ellipsis";
label.style.whiteSpace = "nowrap";
label.style.overflow = "hidden";
let btn = createButton(`Select Planet`);
parent.append(label, btn);
btn.onclick = () => {
let planet = ui.getSelectedPlanet();
if (!planet || planet.owner != df.account) return;
entryInfo.planet = planet;
label.textContent = `Planet ID: ${planet.locationId}`;
};
return [parent, entryInfo];
};
class Plugin {
container;
constructor() {}
/**
* Called when plugin is launched with the "run" button.
*/
async render(container) {
this.container = container;
let addressInput = createElement("input");
addressInput.style.color = "black";
addressInput.style.padding = "3px";
addressInput.style.width = "100%";
addressInput.style.outline = "none";
addressInput.style.marginBottom = "6px";
addressInput.placeholder = "0xdeadbeef...";
let randomAddress = createButton(`Random Whitelisted Address`);
randomAddress.onclick = () => {
let allPlayers = df.getAllPlayers();
let randomPlayer = allPlayers[Math.floor(allPlayers.length * Math.random())];
addressInput.value = randomPlayer.address;
};
randomAddress.style.marginBottom = "6px";
let execDestruction = createButton(`Execute Destruction`);
execDestruction.style.marginBottom = "6px";
let planets = [
createPlanetEntry(),
createPlanetEntry()
];
container.append(
createElement("div", `Type address that will receive planets:`),
addressInput,
randomAddress,
createElement("div", `Select 2 planets:`),
planets[0][0],
planets[1][0],
createElement("div", `Click "Execute Destruction" to begin`),
execDestruction,
createElement("div", `For best result, make sure the planets have max energy, low energy growth and good enough speed.`)
);
let running = false;
execDestruction.onclick = () => {
if (!planets[0][1].planet || !planets[1][1].planet) {
execDestruction.textContent = "Planets not selected. Click to try again";
return;
}
if (running) return;
let recipient = addressInput.value;
if (!recipient.startsWith("0x")) return execDestruction.textContent = "Recipient address is invaild. Click to try again";
execDestruction.textContent = "Please wait...";
let p1Energy = Math.floor(planets[0][1].planet.energy * 0.98);
let p2Energy = Math.floor(planets[1][1].planet.energy * 0.98);
df.move(planets[0][1].planet.locationId, planets[1][1].planet.locationId, p1Energy, 0);
df.move(planets[1][1].planet.locationId, planets[0][1].planet.locationId, p2Energy, 0);
let transfered = [false, false];
Promise.all([
df.waitForPlanet(planets[0][1].planet.locationId, ({current}) => current.energy < p1Energy).then(() => {
if (transfered[0]) return;
df.transferOwnership(planets[0][1].planet.locationId, recipient);
transfered[0] = true;
}),
df.waitForPlanet(planets[1][1].planet.locationId, ({current}) => current.energy < p2Energy).then(() => {
if (transfered[1]) return;
df.transferOwnership(planets[1][1].planet.locationId, recipient);
transfered[1] = true;
})
]).then(() => {
execDestruction.textContent = "Execute Destruction";
running = false;
});
};
}
/**
* Called when plugin modal is closed.
*/
destroy() {}
}
export default Plugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment