Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions js/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const SAVED_TARGETS_KEY =
const SAVE_ARTILLERY_KEY =
'wardogs-save-artillery-position';

const MAP_POINTS_KEY =
'wardogs-map-points';


/* =========================
ZOOM
Expand Down
7 changes: 7 additions & 0 deletions js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ function bindEvents() {
'custom';
}

if (
typeof loadMapPoints ===
'function'
) {
loadMapPoints();
}

clamp(
S.origin
);
Expand Down
157 changes: 157 additions & 0 deletions js/features/saved-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,163 @@ function persistSavedTargets() {
);
}

/* =========================
ARTILLERY / TARGET POSITIONS
========================= */

/*
* Where the two points sit is worth keeping across a reload: coming back
* to a gun laid on the wrong side of the map means placing it again every
* single time.
*
* Every map keeps its own entry, keyed by map id, because the coordinates
* are meaningless on a different map. Switching maps restores that map's
* pair and leaves the others untouched.
*/
const MAP_POINTS_WRITE_DELAY_MS = 300;

let mapPointsWriteTimer = null;

function persistMapPoints() {

/*
* inputs() runs on every frame of a drag, so the write trails the
* gesture instead of hitting localStorage a hundred times across it.
*/
if (mapPointsWriteTimer) {
return;
}

mapPointsWriteTimer = setTimeout(
() => {
mapPointsWriteTimer = null;
writeMapPoints();
},
MAP_POINTS_WRITE_DELAY_MS
);
}

function readMapPointsStore() {

const raw =
localStorage.getItem(
MAP_POINTS_KEY
);

if (!raw) {
return {};
}

let parsed = null;

try {
parsed =
JSON.parse(raw);
} catch (error) {
return {};
}

if (
!parsed ||
typeof parsed !== 'object'
) {
return {};
}

/*
* The first release stored a single { map, origin, target } object;
* fold that lone map into the keyed shape instead of dropping it.
*/
if (
typeof parsed.map === 'string'
) {

return {
[parsed.map]: {
origin: parsed.origin,
target: parsed.target
}
};
}

return parsed;
}

function writeMapPoints() {

try {
const store =
readMapPointsStore();

store[S.map] = {
origin: {
x: S.origin.x,
y: S.origin.y
},
target: {
x: S.target.x,
y: S.target.y
}
};

localStorage.setItem(
MAP_POINTS_KEY,
JSON.stringify(store)
);
} catch (error) {
console.warn(
'Failed to save map points:',
error
);
}
}

function readStoredPoint(value) {

return (
value &&
Number.isFinite(Number(value.x)) &&
Number.isFinite(Number(value.y))
)
? {
x: Number(value.x),
y: Number(value.y)
}
: null;
}

function loadMapPoints() {

try {
const stored =
readMapPointsStore()[S.map];

if (!stored) {
return;
}

const origin =
readStoredPoint(stored.origin);

const target =
readStoredPoint(stored.target);

if (origin) {
S.origin = origin;
}

if (target) {
S.target = target;
}

} catch (error) {
console.warn(
'Failed to load map points:',
error
);
}
}

function getSaveArtilleryPreference() {

return (
Expand Down
6 changes: 6 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ async function init() {

initLayout();

/*
* Before the clamp below, so points restored from a previous
* visit are pulled inside the map's bounds like any other.
*/
loadMapPoints();

/*
* Sync initial state with the
* selected preset map after the
Expand Down
13 changes: 13 additions & 0 deletions js/ui/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ function inputs() {
$('h').value =
S.h;

/*
* Origin and target are written from six different places (map drags,
* the coordinate inputs, saved-target restore, undo, coordinate
* search). They all land here, so one throttled write covers them all
* instead of a hook at each site.
*/
if (
typeof persistMapPoints ===
'function'
) {
persistMapPoints();
}

/*
* The saved-target highlight is derived from where the target sits,
* so every writer of S.target refreshes it by arriving here.
Expand Down