Find a city: a search box in the HUD and on / that jumps the map there (closes #218)

This commit is contained in:
Frederic Jung 2026-10-31 13:57:35 +01:00
parent d50fc201bb
commit 7756d89d41
No known key found for this signature in database
15 changed files with 162 additions and 0 deletions

View file

@ -45,6 +45,8 @@ const ACT={
if(o.a==='cust'){if(S.biz.some(b=>b.id===o.v)){ACT.custGo({id:o.v});return;}toast(tr('interface.that_customer_is_gone_the_contract'),'');return;}
if(o.a==='bank'){ACT.bank({});return;}if(o.a==='ops'){ACT.opsOpen();return;}if(o.a==='tab'&&LOG_TABS.includes(o.v))ACT.leftTab({t:o.v});},
logAll:()=>{UI.logAll=!UI.logAll;renderLeft();},
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),
};

View file

@ -0,0 +1,30 @@
/* ---------- Find a city (#218): a search box that jumps the map there ----------
Matches the city's name, its country (in the player's language and in English) and its exchange and data center
names, so "Deutschland", "Germany" or "DE-CIX" all find Frankfurt. Press / anywhere, or the 🔍 chip in the HUD. */
const FIND_N=8;
const fold=s=>String(s||'').normalize('NFD').replace(/[̀-ͯ]/g,'').toLowerCase().trim();
// Country names, built once per page: they never change while playing.
let FIND_IX=null;
function findIndex(){if(FIND_IX)return FIND_IX;let dn=null,en=null;try{dn=new Intl.DisplayNames([LANG],{type:'region'});en=new Intl.DisplayNames(['en'],{type:'region'});}catch(e){}
const cn=(d,cc)=>{try{return d?d.of(cc)||'':'';}catch(e){return '';}};
FIND_IX=Object.values(CITY).map(C=>{const land=cn(dn,C.cc)||C.cc||'';return {id:C.id,name:fold(C.name),land,ctry:fold(land+' '+cn(en,C.cc)+' '+(C.cc||'')),xtra:fold((C.ix||'')+' '+(C.dc||''))};});return FIND_IX;}
// Best matches first: the name starts with it, a word of the name does, the name has it, then country, then exchange or DC.
function findCities(q){q=fold(q);const mine=c=>S&&S.pops&&S.pops[c]?0:1;
if(!q)return S&&S.pops?Object.keys(S.pops).filter(isCity).sort((a,b)=>CITY[a].name.localeCompare(CITY[b].name)).slice(0,FIND_N):[];
const out=[];for(const e of findIndex()){const n=e.name;
const s=n.startsWith(q)?0:n.split(/[\s\-.'()]+/).some(w=>w.startsWith(q))?1:n.includes(q)?2:e.ctry.includes(q)?3:e.xtra.includes(q)?4:-1;
if(s>=0)out.push([s,mine(e.id),-(CITY[e.id].P||0),e.id]);}
return out.sort((a,b)=>a[0]-b[0]||a[1]-b[1]||a[2]-b[2]).slice(0,FIND_N).map(r=>r[3]);}
function findRows(q){const ids=findCities(q),land=c=>(findIndex().find(e=>e.id===c)||{}).land||'';
if(!ids.length)return `<p class="meta" style="margin:10px 0">${q.trim()?tr('find.no_city_matches_q',{q:esc(q.trim())}):tr('find.type_a_city_a_country')}</p>`;
return ids.map((c,i)=>{const C=CITY[c],p=S&&S.pops&&S.pops[c];
return `<button class="row findrow${i?'':' on'}" data-act="findGo" data-args="${esc(JSON.stringify({city:c}))}"><span class="grow"><span class="nm">${p?'📍 ':''}${esc(C.name)}</span><span class="meta">${esc([land(c),C.ix].filter(Boolean).join(' · '))}${p?' · '+tr('find.your_pop'):''}</span></span><span aria-hidden="true">›</span></button>`;}).join('');}
function findOpen(){if(!S||!UI.started)return;
openModal(`<h2 style="font-size:24px;margin:0 0 10px">🔍 ${tr('find.find_a_city')}</h2><input class="t" id="findIn" type="search" enterkeyhint="go" autocomplete="off" autocapitalize="off" spellcheck="false" placeholder="${esc(tr('find.city_country_or_exchange'))}" aria-label="${esc(tr('find.find_a_city'))}" aria-controls="findList">
<div id="findList" class="findlist" role="list">${findRows('')}</div>${isPhone()?'':`<p class="meta" style="margin:8px 0 0">${tr('find.press_slash_to_search_from')}</p>`}`,'find');
const el=$('findIn');if(el)el.focus();}
document.addEventListener('input',e=>{if(e.target.id!=='findIn')return;const L=$('findList');if(L)L.innerHTML=findRows(e.target.value);});
document.addEventListener('keydown',e=>{
if(e.target.id==='findIn'){if(e.key==='Escape'){closeModal();return;}if(e.key==='Enter'){e.preventDefault();const c=findCities(e.target.value)[0];if(c)ACT.findGo({city:c});}return;}
if(e.key!=='/'||e.ctrlKey||e.metaKey||e.altKey||(e.target.closest&&e.target.closest('input,textarea,select,[contenteditable]')))return;
if(!S||!UI.started||!$('modal').hidden)return;e.preventDefault();findOpen();});

View file

@ -2029,6 +2029,13 @@
"fiber_modals.you_need_a_fabric_port_at":"You need a fabric port at one of your POPs first. Open a POP's Infra tab and buy a port on {list}.",
"fiber_modals.your_own_trench_almost_no_rent":"Your own trench: almost no rent, but permits and digging take forever, and delays happen.",
"fiber_modals.your_pop_needs_a_fabric_port":"Your POP · needs a fabric port there",
"find.city_country_or_exchange":"City, country or exchange",
"find.find_a_city":"Find a city",
"find.find_a_city_slash":"Find a city (/)",
"find.no_city_matches_q":"No city matches “{q}”.",
"find.press_slash_to_search_from":"Tip: press <b>/</b> anywhere on the map to search.",
"find.type_a_city_a_country":"Type a city, a country or an exchange.",
"find.your_pop":"your POP",
"game_and_settings.delete_and_start_over":"Delete and start over",
"game_and_settings.export_save_download_a_file":"Export save <small>download a file</small>",
"game_and_settings.import_failed_that_save_is_damaged":"<b>Import failed.</b><br>That save is damaged and could not be loaded. Your current game is untouched.",

View file

@ -117,6 +117,7 @@
<button class="chip noc" id="hNoc" data-act="leftTab" data-args='{"t":"noc"}'><span class="lbl">NOC</span><span class="val num" id="hNocVal" data-i18n="common.all_quiet">All quiet</span><span class="badge" id="hNocNew" hidden></span></button>
<div class="spacer"></div>
<div class="chip"><div class="lbl" data-i18n="common.date">Date</div><div class="val num" id="hDate">Y1 · Jan 1</div></div>
<button class="chip find" data-act="findOpen" aria-label="Find a city" data-i18n-aria-label="find.find_a_city" title="Find a city (/)" data-i18n-title="find.find_a_city_slash"><span aria-hidden="true">🔍</span></button>
<div class="chip speed" role="group" aria-label="Game speed" data-i18n-aria-label="common.game_speed">
<button data-act="speed" data-args='{"v":0}' id="sp0" aria-label="Pause" data-i18n-aria-label="common.pause">❚❚</button>
<button data-act="speed" data-args='{"v":0.5}' id="sp0.5" aria-label="Half speed" data-i18n-aria-label="common.half_speed">½×</button>

View file

@ -29,3 +29,5 @@
.minibar{height:6px;border-radius:4px;background:var(--line);overflow:hidden;margin-top:2px;width:100%}
.minibar i{display:block;height:100%;background:var(--ok)}
/* Find a city: a square chip with just the magnifier. */
#hud .chip.find{justify-content:center;align-items:center;padding:0;width:44px;min-width:44px;font-size:19px;cursor:pointer}

View file

@ -196,3 +196,9 @@ h2 .rn{font-size:18px;vertical-align:middle;padding:2px 4px}
.menu-links .lnk{background:none;border:0;padding:4px 2px;font-weight:700;font-size:14px;font-family:inherit;color:var(--ink2);text-decoration:underline;text-underline-offset:3px;cursor:pointer}
.menu-plugs{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px;margin-top:12px}
.mplug{border:2px solid var(--line);border-radius:12px;padding:8px 10px;font-weight:700;font-size:13px;line-height:1.25;color:var(--ink);text-decoration:none;background:var(--soft);display:flex;align-items:center}
/* Find a city: big tap targets, the first result (what Enter picks) highlighted. */
.findlist{margin-top:8px}
.findrow{width:100%;text-align:left;background:none;border:0;border-top:1px solid var(--line);font:inherit;color:var(--ink);min-height:48px;padding:6px 8px;cursor:pointer;border-radius:0}
.findrow .nm,.findrow .meta{display:block}
.findrow.on{background:var(--soft);border-radius:10px}
.findrow:hover{background:var(--soft)}

View file

@ -36,3 +36,8 @@
.chip{padding:2px 7px}
}
/* Narrow tablets: a slim find chip and slimmer speed buttons keep the HUD on one row. */
@media (min-width:761px) and (max-width:860px){
#hud .chip.find{width:32px;min-width:32px;font-size:15px}
.speed button{min-width:23px;padding-left:2px;padding-right:2px}
}

View file

@ -119,3 +119,5 @@ button,.chip,.b,a{touch-action:manipulation;-webkit-tap-highlight-color:transpar
}
@media (prefers-reduced-motion:reduce){:root:not(.anim-force) *{animation:none !important;transition:none !important}}
:root.no-anim *{animation:none !important;transition:none !important}
/* The narrowest phones: a slimmer find chip so the date still fits. */
@media (max-width:340px){#hud .chip.find{width:34px;min-width:34px;font-size:15px}#hud .menubtn .caret{display:none}}