Skip to content

Commit

Permalink
feat: add details for current liberation progress (#5)
Browse files Browse the repository at this point in the history
* deps: bump sdk to version 1.1.0

* feat: add liberation progress fields

* chore(release): bump package version to 1.2.0
  • Loading branch information
fabio-nettis committed Apr 5, 2024
1 parent 6fd2305 commit 0f1da1d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
91 changes: 86 additions & 5 deletions commands/planets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ export default function planets(program: Command) {
const handler = async (...args: any[]) => {
const [id, query] = parseListOptions<Planet>(...args);

const { data, url } = await request<Planet>(HellHub.planets, id, query);
const { data, url } = await request<Planet>(HellHub.planets, id, {
...query,
include: [
...(Array.isArray(query.include) ? query.include : [] ?? []),
"sector",
],
});

if (!!args[1].raw) {
console.log(data);
Expand All @@ -48,10 +54,13 @@ export default function planets(program: Command) {
style: { head: ["white", "bold"] },
head: [
"ID",
"Name",
...(!!entries?.[0]?.sector ? ["Sector"] : []),
"Index",
"Name",
"Liberation",
"Rate",
"Prediction",
"Players",
...(!!entries?.[0]?.sector ? ["Sector"] : []),
"Max Health",
"Health",
"HP",
Expand All @@ -72,12 +81,84 @@ export default function planets(program: Command) {
return chalk.red(str);
})();

const liberation = (() => {
const value = p.liberation;
const str = formatMoney(value, "%", 5, "'", ".", "%v%s");
return chalk.bold(str);
})();

const rate = (() => {
const value = p.liberationRate;
const str = `${p.liberationRate > 0 ? "+" : ""}${formatMoney(value, "%", 2, "'", ".", "%v%s")} / h`;
switch (p.liberationState) {
case "WINNING": {
return chalk.green(str);
}

case "DRAW": {
return chalk.yellow(str);
}

case "LOSING": {
return chalk.red(str);
}

default: {
return chalk.gray(str);
}
}
})();

const status = (() => {
switch (p.liberationState) {
case "WINNING": {
const now_h = p.health;
const max_h = p.maxHealth;
const now_p = (now_h / max_h) * 100;
const lib_r = p.liberationRate;

// show the time to liberation in minutes, hours and minutes or
// in days and hours if the time is greater than 24 hours
const lib_h = (100 - now_p) / lib_r;
const lib_m = lib_h * 60;
const lib_d = lib_h / 24;

if (lib_h < 1) {
return chalk.green(`${Math.round(lib_m)}m`);
} else if (lib_h < 24) {
return chalk.green(
`${Math.round(lib_h)}h ${Math.round(lib_m % 60)}m`,
);
} else {
return chalk.green(
`${Math.round(lib_d)}d ${Math.round(lib_h % 24)}h`,
);
}
}

case "DRAW": {
return chalk.yellow("Draw");
}

case "LOSING": {
return chalk.red("Losing");
}

default: {
return chalk.gray("N/A");
}
}
})();

return [
p.id,
p.name,
...(p.sector ? [p.sector.name] : []),
p.index,
p.name,
liberation,
rate,
status,
formatMoney(p.players, "", 0, "'", "."),
...(p.sector ? [p.sector.name] : []),
formatMoney(p.maxHealth, "", 0, "'", "."),
formatMoney(p.health, "", 0, "'", "."),
hp,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"private": false,
"description": "The official CLI for HellHub API. Stay updated about the current war right without leaving your terminal.",
"version": "1.1.0",
"version": "1.2.0",
"main": "build/index.mjs",
"types": "build/index.d.ts",
"keywords": [
Expand Down Expand Up @@ -55,7 +55,7 @@
"email": "[email protected]"
},
"dependencies": {
"@hellhub-collective/sdk": "^1.0.5",
"@hellhub-collective/sdk": "latest",
"accounting": "^0.4.1",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
Expand Down

0 comments on commit 0f1da1d

Please sign in to comment.