Merge #177: Add editable optic counts for land and subsea fiber (LookedPath)

This commit is contained in:
Frederic Jung 2026-10-28 15:17:42 +01:00
commit 327749e0cf
No known key found for this signature in database
12 changed files with 158 additions and 1 deletions

View file

@ -0,0 +1,27 @@
// Quote the whole change before spending: shelf optics still need regen cards.
function opticCountCost(l,o,n){
const add=Math.max(0,n-(l.ch||[]).filter(k=>k===o).length),sp=Math.min(add,spareN(o)),r=REGEN_CH*(l.regen|0),c=optCost(o);
return sp*Math.round(c*r)+(add-sp)*Math.round(c*(1+r));
}
Object.assign(ACT,{
setChCount:({id,o,n})=>{
const l=S.links.find(x=>x.id===id);
if(!l||l.kind!=='df'||!own(OPT,o)||!Number.isSafeInteger(n)||n<0||n>chMax(l))return;
const old=(l.ch||[]).filter(k=>k===o).length,d=n-old;
if(!d)return;
if(d>0){
const why=chWhy(l,o);
if(why){toast(tr('fiber_and_hardware.can_t_add_optic_why',{optic:OPT[o].n,why:esc(why)}),'bad');sfx('err');return;}
const free=chMax(l)-(l.ch||[]).length-soldSlots(l);
if(d>free){toast(tr('optic_counts.only_n_channels_free',{n:free}),'bad');sfx('err');return;}
if(!portGate(l.a,d)||!portGate(l.b,d)||!spend(opticCountCost(l,o,n)))return;
const sp=Math.min(d,spareN(o));if(sp)S.spares[o]-=sp;
(l.ch=l.ch||[]).push(...Array(d).fill(o));
}else{
let left=-d;l.ch=l.ch.filter(k=>k!==o||left--<=0);
S.spares=S.spares||{};S.spares[o]=(S.spares[o]||0)-d;
}
if(UI.opticCounts)delete UI.opticCounts[JSON.stringify([id,o])];
after();
},
});

View file

@ -109,7 +109,7 @@ function dfPanel(l){
if(l.own&&l.sub)h+=subUpgradePanel(l);
for(const k of Object.keys(OPT)){const O=OPT[k],n=(l.ch||[]).filter(x=>x===k).length,why=chWhy(l,k);
const md=optMode(k,l),cc=chCost(l,k),sp=spareN(k);
h+=`<div class="row"><div class="grow"><div class="nm">${O.n} <span class="meta">${optModes(k,l).map(([g,r])=>tr('link_panels.g_to_km_km',{g:fmtG(g),km:r.toLocaleString()})).join(' · ')}</span></div><div class="meta">${esc(O.blurb)}${n?` · <b>${tr('link_panels.n_lit',{n})}</b>`:''}${sp?` · ${trn('link_panels.n_spare_on_the_shelf',sp)}`:''}</div>${md&&!md.full?`<div class="meta">⚠️ ${tr('link_panels.runs_at_g_per_channel_on',{g:fmtG(md.g)})} ${!l.sub&&regenNeed(k,l)>(l.regen|0)?trn('link_panels.n_regen_site_would_bring_it',regenNeed(k,l),{g:fmtG(O.g)}):''}</div>`:''}${why?`<div class="why">${esc(why)}</div>`:''}</div>${n?btn('−','rmCh',{id:l.id,o:k},'ghost sm'):''}${btn(sp?`+ <small>${cc?tr('link_panels.spare_price',{price:fmt$(cc)}):tr('common.spare')}</small>`:`+ <small>${fmt$(cc)}</small>`,'addCh',{id:l.id,o:k},'sm',!!why||S.cash<cc)}</div>`;}
h+=`<div class="row"><div class="grow"><div class="nm">${O.n} <span class="meta">${optModes(k,l).map(([g,r])=>tr('link_panels.g_to_km_km',{g:fmtG(g),km:r.toLocaleString()})).join(' · ')}</span></div><div class="meta">${esc(O.blurb)}${n?` · <b>${tr('link_panels.n_lit',{n})}</b>`:''}${sp?` · ${trn('link_panels.n_spare_on_the_shelf',sp)}`:''}</div>${md&&!md.full?`<div class="meta">⚠️ ${tr('link_panels.runs_at_g_per_channel_on',{g:fmtG(md.g)})} ${!l.sub&&regenNeed(k,l)>(l.regen|0)?trn('link_panels.n_regen_site_would_bring_it',regenNeed(k,l),{g:fmtG(O.g)}):''}</div>`:''}${why?`<div class="why">${esc(why)}</div>`:''}${opticCountControl(l,k,n)}</div>${n?btn('−','rmCh',{id:l.id,o:k},'ghost sm'):''}${btn(sp?`+ <small>${cc?tr('link_panels.spare_price',{price:fmt$(cc)}):tr('common.spare')}</small>`:`+ <small>${fmt$(cc)}</small>`,'addCh',{id:l.id,o:k},'sm',!!why||S.cash<cc)}</div>`;}
{const st=Object.keys(OPT).filter(k=>spareN(k)>0);h+=`<p class="note">${tr('link_panels.unplugged_optics_go_on_the_spares')}</p>`;
{const used=[...new Set(l.ch||[])].filter(k=>OPT[k]&&OPT[k].lvl<=lvl());if(used.length)h+=`<div class="btns">${used.map(k=>btn(tr('link_panels.stock_a_spare_optic_price',{optic:OPT[k].n,price:fmt$(optCost(k))}),'buySpare',{o:k},'ghost sm',S.cash<optCost(k)||spareN(k)>=64)).join('')}</div><p class="meta">${tr('link_panels.spares_bought_now_are_already_paid')}</p>`;}if(st.length)h+=`<h4 class="sub4">${tr('common.spares_shelf')}</h4>`+st.map(k=>`<div class="row"><div class="grow"><div class="nm">${spareN(k)} × ${OPT[k].n}</div></div>${btn(tr('link_panels.sell_one_price',{price:fmt$(spareValue(k))}),'sellSpare',{o:k},'ghost sm')}</div>`).join('');}
const rg=l.regen|0,rgAdd=REGEN_COST+Math.round((l.ch||[]).reduce((a,c)=>a+optCost(c),0)*REGEN_CH);

View file

@ -0,0 +1,20 @@
function opticCountControl(l,o,n){
const key=JSON.stringify([l.id,o]);let draft=UI.opticCounts&&UI.opticCounts[key];
if(draft&&draft.base!==n){delete UI.opticCounts[key];draft=null;}
const v=draft?draft.n:n,value=draft?draft.value:n;
const price=Number.isSafeInteger(v)&&v>=0&&v<=chMax(l)?opticCountCost(l,o,v):0;
return `<form class="optic-count" data-link="${esc(l.id)}" data-optic="${o}"><label>${tr('optic_counts.target_count')}<input class="t" type="number" name="count" min="0" max="${chMax(l)}" step="1" inputmode="numeric" required value="${esc(value)}" aria-label="${esc(tr('optic_counts.count_for_model',{model:OPT[o].n}))}"></label><button class="b sm" type="submit">${tr('optic_counts.apply_price',{price:fmt$(price)})}</button></form>`;
}
document.addEventListener('input',e=>{
const f=e.target.closest('form.optic-count');if(!f||!S)return;
const l=S.links.find(x=>x.id===f.dataset.link),o=f.dataset.optic,n=f.elements.count.valueAsNumber;
if(!l||!own(OPT,o))return;
UI.opticCounts=UI.opticCounts||{};UI.opticCounts[JSON.stringify([l.id,o])]={base:(l.ch||[]).filter(k=>k===o).length,n,value:e.target.value};
e.target.setAttribute('value',e.target.value);
f.querySelector('button').textContent=tr('optic_counts.apply_price',{price:fmt$(Number.isSafeInteger(n)&&n>=0&&n<=chMax(l)?opticCountCost(l,o,n):0)});
});
document.addEventListener('submit',e=>{
const f=e.target.closest('form.optic-count');if(!f)return;e.preventDefault();
if(!S||!f.reportValidity()||!$('modal').hidden)return;
ACT.setChCount({id:f.dataset.link,o:f.dataset.optic,n:f.elements.count.valueAsNumber});
});

View file

@ -2517,6 +2517,10 @@
"notifications.n_min_ago":{"one":"{n} min ago","other":"{n} min ago"},
"notifications.nothing_in_the_last_10_minutes":"Nothing in the last 10 minutes.",
"notifications.show_all_n":"Show all {n}",
"optic_counts.apply_price":"Apply · {price}",
"optic_counts.count_for_model":"Number of {model} channels",
"optic_counts.only_n_channels_free":"Only {n} channels are free on this fiber.",
"optic_counts.target_count":"Target count",
"own_cable.a_and_b":"{a} and {b}",
"own_cable.a_fishing_fleet_is_trawling_exactly":"A fishing fleet is trawling exactly where you need to lay.",
"own_cable.a_leased_wave_wave_on_this":"A leased {wave} wave on this route costs {price}/mo.",

View file

@ -0,0 +1,4 @@
.optic-count{display:flex;flex-wrap:wrap;align-items:end;gap:8px;margin-top:8px}
.optic-count label{display:flex;flex-direction:column;gap:3px;font-size:12px;font-weight:800}
.optic-count input.t{width:88px;font-size:16px;min-height:44px}
.optic-count .b{min-height:44px}