-
Notifications
You must be signed in to change notification settings - Fork 42
/
plant.js
111 lines (93 loc) · 3.17 KB
/
plant.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
import { cropclaim, queryData, recover, tables } from "./farmersworld.js";
import { delay, calcNextClaim, getClaimableAssets, parseBalance, toCamelCase } from "./utils.js";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const accounts = require("./accounts.json");
const { TIMEZONE, PAYBW } = require("./config.json");
const ENERGY_PER_FOOD = 5;
const PLANT_CHARGE_TIME = 14400; // 4 hours
let cropconf;
async function water(plant, paybw = null) {
console.log("Watering", plant.assetId);
await cropclaim(plant, paybw);
}
async function getPlantConf() {
const data = await queryData("cropconf", null, 1);
return (
data.rows
.map((r) => toCamelCase(r))
// map table
.reduce((a, c) => {
a[c.templateId] = c;
return a;
}, {})
);
}
async function recoverEnergy(plants, paybw = null) {
if (plants.length === 0) return;
const wallets = new Set(plants.map((r) => r.wallet));
for (const wallet of wallets) {
const data = await queryData("accounts", wallet, 1);
if (!data || data.rows.length === 0) continue;
const account = toCamelCase(data.rows[0]);
const food = parseBalance(
account.balances.find((r) => r.toUpperCase().endsWith("FOOD"))
);
const consumedEnergy = plants
.filter((r) => r.wallet === wallet)
.reduce((a, c) => a + cropconf[c.templateId].energyConsumed, 0);
console.log("Watering consume", consumedEnergy, "energy");
if (account.energy < consumedEnergy) {
let consumedFood = Math.floor(
(account.maxEnergy - account.energy) / ENERGY_PER_FOOD
);
consumedFood =
consumedFood < food ? consumedFood : Math.floor(food);
const energy = consumedFood * ENERGY_PER_FOOD;
console.log("Recover", wallet, energy, "energy");
await recover(
accounts.find((r) => r.wallet === wallet),
energy,
paybw
);
await delay(400);
}
}
}
async function main(paybw = null) {
const plants = await tables("crops", accounts);
const nextClaim = calcNextClaim(plants, PLANT_CHARGE_TIME);
const difftime = Math.ceil(nextClaim - Date.now() / 1000);
if (difftime > 0) {
console.log(
"Next cropclaim at",
new Date(nextClaim * 1000).toLocaleString("en-US", {
timeZoneName: "short",
timeZone: TIMEZONE,
})
);
await delay(difftime * 1000);
}
const claimable = getClaimableAssets(plants);
await recoverEnergy(claimable, paybw);
for (const plant of claimable) {
await water(plant, paybw);
await delay(400);
}
}
export default async function () {
let paybw = null;
if (PAYBW) {
paybw = require("./paybw.json");
}
// load cropconf
cropconf = await getPlantConf();
while (true) {
try {
await main(paybw);
} catch (e) {
// an error occus
console.log("[Error] -", e);
}
}
}