Files
Battle-for-Tismo/shared/game_clock.js
adrien d73ea4735d Grew the economy at a constant rate and forecast the budget
GDP per capita had no natural growth, so a nation's economy only moved
when population, buildings or war did. The summary stats also offered
inconsistent hover text: a native browser tooltip for popularity and none
at all for the budget.

- Every land tile now gains a constant 0.01% of the GDP per capita it
  opened with, once a day. Because the amount is fixed the relative growth
  rate tapers over the years. Each tile's opening figure is snapshotted
  once and shipped to the browser, so the tile panel and the GDP-per-capita
  map mode stay in step with the server.
- The population, GDP and GDP/capita tooltips show the relative growth:
  against 1 January 2000 through the first year, then year on year. The
  monthly captures now keep a rolling thirteen months for the baseline.
- Every summary stat explains itself through the same custom bubble.
  Popularity's native title moved onto it, and Budget forecasts the next
  calendar month at the current hourly net, as a compact figure such as
  "+€3.28B/mo" (hoursInNextMonth follows the real calendar).
- The tile GDP breakdown shows gains with a plus sign (natural growth,
  ports) rather than the old minus.

Added tests for the constant daily gain, the falling relative rate, the
first-year and year-on-year baselines, the snapshot baseline, the budget
forecast and the calendar month length.
2026-09-21 12:27:11 +02:00

38 lines
1.4 KiB
JavaScript

// Global game clock constants and date formatting, ported from
// scripts/classes/game_clock.gd. One real second is one in-game hour.
export const SECONDS_PER_HOUR = 1.0;
export const HOURS_PER_DAY = 24;
export const DAYS_PER_YEAR = 365;
export const HOURS_PER_YEAR = HOURS_PER_DAY * DAYS_PER_YEAR;
export const UNIX_SECONDS_PER_HOUR = 3600;
export const START_UNIX_TIME = 946684800;
export const MONTH_NAMES = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
];
export function unixTime(totalHours) {
return START_UNIX_TIME + totalHours * UNIX_SECONDS_PER_HOUR;
}
export function dateString(totalHours) {
const date = new Date(unixTime(totalHours) * 1000);
const day = date.getUTCDate();
const month = MONTH_NAMES[date.getUTCMonth()];
const year = date.getUTCFullYear();
const hour = date.getUTCHours();
return `${day} ${month} ${year}, ${String(hour).padStart(2, "0")}:00`;
}
// How many in-game hours there are from `totalHours` to the same moment one
// calendar month later. Months differ in length, so a forecast that speaks of
// "the next month" should not assume a fixed 30 days.
export function hoursInNextMonth(totalHours) {
const date = new Date(unixTime(totalHours) * 1000);
const later = new Date(date.getTime());
later.setUTCMonth(later.getUTCMonth() + 1);
return Math.round((later.getTime() - date.getTime()) / (UNIX_SECONDS_PER_HOUR * 1000));
}