Merge #250: Inspecting a link moves the map to it

Thanks to ddryden.
This commit is contained in:
Frederic Jung 2026-11-01 07:57:49 +01:00 • committed by GitHub
commit 061acfe9f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 2 deletions

View file

@ -17,6 +17,7 @@ All notable changes to this project are documented here. The project follows
- Waves between Philippine islands now cost more than waves out of the country: Manila to Cebu costs about twice Manila to Hong Kong.
- Manila, Cebu and Davao have more people online, in line with their real populations.
- Cable names on the map no longer pile on top of each other when you zoom in. Where short cables crowd together, a name that would cover another is left out, and selecting a city shows the names of all its cables.
- Inspecting a congested or busy link now moves the map to it, zoomed so the whole line fits beside the open panel.
## [2.2.0] — 2026-10-31

View file

@ -20,7 +20,7 @@ const ACT={
if(UI.confirm!==id){UI.confirm=id;renderBank();return;}UI.confirm=null;if(!spend(cost)){renderBank();return;}S.loans=S.loans.filter(x=>x!==l);track('loan_repaid');
toast(B.equity?tr('interface.you_bought_out_bank_they_flew',{bank:esc(B.name)}):tr('interface.loan_from_bank_repaid_early',{bank:esc(B.name)}),'good');after();renderBank();},
leaseSwap:({a,b,src})=>{if(!S.pops[a]||!S.pops[b]||a===b)return;$('dash').hidden=true;UI.lmode='df';UI.dfSrc=['lease','build','cable'].includes(src)?src:isSub(a,b)?'cable':'build';UI.dfSys=lvl()>=3?'roadm':'lite';openLinkModal(a,b);},
dashLink:({id})=>{$('dash').hidden=true;selectLink(id);},
dashLink:({id})=>{$('dash').hidden=true;selectLink(id);focusLink(id);},
animSet:({v})=>{if(!['auto','on','off'].includes(v))return;UI.anim=v;try{localStorage.setItem('sot-anim',v);}catch(e){}applyAnim();refreshSettings();},
soundSet:({v})=>{UI.muted=v==='off';try{localStorage.setItem('sot-mute',UI.muted?'1':'0');}catch(e){}renderHUD();refreshSettings();},
langSet:({v})=>{if(!Object.hasOwn(LANGS,v)||v===LANG)return;try{localStorage.setItem('sot-lang',v);}catch(e){}save(true);location.reload();},
@ -48,5 +48,5 @@ const ACT={
findOpen:()=>findOpen(),
findGo:({city})=>{if(!isCity(city))return;closeModal();$('dash').hidden=true;ACT.focusCity({city});},
focusCity:({city})=>{selectCity(city);const C=CITY[city];cam.x=C.x;cam.y=C.y;cam.z=Math.max(cam.z,1.2);if(innerWidth<760)UI.leftOpen=false;renderLeft();},
selLink:({id})=>selectLink(id),
selLink:({id})=>{selectLink(id);focusLink(id);},
};

View file

@ -27,5 +27,15 @@ function bbCost(c){return 15000+CITY[c].P*20;}
function after(){if(pzOn()){pzAfter();return;}if(S)purgeInc();compute();checkGoals();renderAll();if(S&&!$('dash').hidden)renderDash();save();}
const isTablet=()=>innerWidth>760&&innerWidth<=1200;
function selectLink(id){UI.sel={type:'link',id};UI.confirm=null;if(isTablet()||isPhone())UI.leftOpen=false;$('panel').classList.remove('full');renderPanel();renderLeft();}
// Frame the whole drawn line (cables bend far from the straight one) in the part of the map the panel leaves free:
// above a phone's bottom sheet, left of the side panel. Long links zoom out to fit; short ones zoom in like a city.
// A puzzle keeps its own framing beside the puzzle card (pzCam).
function focusLink(id){if(pzOn())return;const l=S.links.find(x=>x.id===id);if(!l||!CITY[l.a]||!CITY[l.b])return;const pts=linkGeom(l).pts;
let x0=Infinity,x1=-Infinity,y0=Infinity,y1=-Infinity;for(const [x,y] of pts){if(x<x0)x0=x;if(x>x1)x1=x;if(y<y0)y0=y;if(y>y1)y1=y;}
const P=$('panel'),r=P&&!P.hidden&&P.getBoundingClientRect();let fl=0,fr=CW,ft=70,fb=CH;
if(r&&r.width>0){if(r.width>CW*0.6)fb=Math.min(fb,r.top);else fr=Math.min(fr,r.left);}
// Phones leave a thin strip above the sheet: a fixed margin would eat most of it.
const m=(a,b)=>Math.max(40,b-a-Math.min(100,(b-a)*0.3)),fw=m(fl,fr),fh=m(ft,fb),z=clamp(Math.min(fw/Math.max(1,x1-x0),fh/Math.max(1,y1-y0),Math.max(cam.z,1.2)),ZMIN,ZMAX);
cam.z=z;cam.x=(x0+x1)/2-((fl+fr)/2-CW/2)/z;cam.y=(y0+y1)/2-((ft+fb)/2-CH/2)/z;wrapCam();}
function selectCity(c){UI.sel={type:'city',id:c};UI.confirm=null;if(isTablet()||isPhone())UI.leftOpen=false;const P=$('panel');if(P)P.classList.remove('full');if(!S.pops[c])UI.tab='cust';renderPanel();}

51
tests/focus-link.test.js Normal file
View file

@ -0,0 +1,51 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const start = "ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'})";
// Where a world point lands on screen, in whichever wrapped copy of the map is nearest the middle.
const onScreen = (ev, x, y) => JSON.parse(ev(`JSON.stringify([-1,0,1].map(k=>toS(${x},${y},k)).sort((p,q)=>Math.abs(p[0]-CW/2)-Math.abs(q[0]-CW/2))[0])`));
const ends = (ev, id) => JSON.parse(ev(`JSON.stringify((()=>{const l=S.links.find(x=>x.id==='${id}');return [CITY[l.a],CITY[l.b]].map(c=>[c.x,c.y]);})())`));
const inBox = (p, b, why) => assert.ok(p[0] >= b[0] && p[0] <= b[2] && p[1] >= b[1] && p[1] <= b[3], `${why}: ${p} not in ${b}`);
test('Inspect on a congested link moves the map to that link', () => {
const { ev, errors, dom } = load();
ev(start);
ev("S.links.push({id:'hot',a:'fra',b:'ams',kind:'wave',tier:0,km:400,mo:100,cut:0});R.load.hot=capOf(S.links.find(l=>l.id==='hot'))*0.95");
ev("const C=CITY.syd;cam.x=C.x;cam.y=C.y;cam.z=0.8;UI.left='noc';UI.leftOpen=true;renderLeft()");
ev("document.querySelector('#leftBody [data-act=selLink][data-args*=hot]').click()");
assert.equal(ev('UI.sel.id'), 'hot');
assert.ok(ev('cam.z') >= 1.2, 'a short link zooms in like a city does');
const box = [0, 0, ev('CW'), ev('CH')];
for (const [x, y] of ends(ev, 'hot')) inBox(onScreen(ev, x, y), box, 'both cities are on screen');
assert.deepEqual(errors, []);
dom.window.close();
});
test('a long link zooms out to fit, and stays clear of a bottom sheet', () => {
const { ev, errors, dom } = load();
ev(start);
ev("S.links.push({id:'far',a:'fra',b:'sin',kind:'wave',tier:0,km:10000,mo:100,cut:0})");
// jsdom lays nothing out, so stand in for a phone's bottom sheet covering the lower 60% of the map.
ev("$('panel').getBoundingClientRect=()=>({left:0,top:CH*0.4,width:CW,height:CH*0.6,right:CW,bottom:CH});cam.z=3");
ev("ACT.selLink({id:'far'})");
assert.ok(ev('cam.z') < 1.2, 'zoomed out for a long link');
const box = [0, 0, ev('CW'), ev('CH*0.4')];
for (const [x, y] of ends(ev, 'far')) inBox(onScreen(ev, x, y), box, 'both cities are above the sheet');
// The drawn line, not just its ends, fits.
for (const [x, y] of JSON.parse(ev("JSON.stringify(linkGeom(S.links.find(l=>l.id==='far')).pts)"))) inBox(onScreen(ev, x, y), box, 'the whole line is above the sheet');
assert.deepEqual(errors, []);
dom.window.close();
});
test('a puzzle keeps its own framing when a link is opened', () => {
const { ev, errors, dom } = load();
ev("ACT.pzStart({id:'hello'})");
ev("S.links.push({id:'pz',a:'nyc',b:'lon',kind:'wave',tier:0,km:5600,mo:100,cut:0})");
const before = ev('JSON.stringify(cam)');
ev("ACT.selLink({id:'pz'})");
assert.equal(ev('UI.sel.id'), 'pz');
assert.equal(ev('JSON.stringify(cam)'), before, 'the camera stays on the whole puzzle');
assert.deepEqual(errors, []);
dom.window.close();
});