The routers order reads its model from the build standard (bstd().g, 'keep' is the old next-model-up rule) and the optical upgrade order its line system (bstd().sys). The Ops sheet shows "From the build standard: X" with a button to the sheet. Old saves move a model or line system from an order that was running into the standard when it has none of its own; a paused order's or an unhired intern's setting is dropped. opsParam refuses model/sys and points at bstdSet. The build standard's order is a generator now: every first job that takes a fiber dark goes through the optical order's routing test (opsDark, in the route worker in play), under one shared budget of dark swaps a night (two for Kevin) and the week-long memory for a failed fiber. A fiber one order worked on is the other's no more that shift, and decom keeps waves the standard just upgraded. The plan is checked at every job, since the shift rests between them: a POP gone or changed by hand meanwhile is skipped. The button still runs the whole plan at once on the page.
237 lines
25 KiB
JavaScript
237 lines
25 KiB
JavaScript
/* ---------- The build standard (#240, grown into a target state for every POP, port and fiber) ----------
|
||
Big networks run one design everywhere. The standard says what every POP, port and fiber should have: the router
|
||
model and count, a second building and a generator, LAGs, an exchange port, PNIs spread over cities, the line
|
||
system, the optics and the waves. A new POP gets its POP part at once, while the box is still idle (no maintenance
|
||
window). The rest is brought up by the intern's "Keep to the build standard" order, or in one tap from the sheet. */
|
||
// Every target, with the values its list offers ("keep" first: no target) and its default. Saved sparse: a target left
|
||
// at its default is not in the save, so an old {g, rt} standard is exactly the routers it always was.
|
||
const BSTD=[
|
||
{k:'g',grp:'pop',l:N_('bstd.l_model'),d:'keep',v:()=>['keep',...RG.map((r,i)=>i).sort((a,b)=>RG[a].cls-RG[b].cls||RG[a].cap-RG[b].cap||a-b)],ok:v=>v==='keep'||(Number.isInteger(v)&&!!RG[v])},
|
||
{k:'rt',grp:'pop',l:N_('common.routers'),d:1,v:()=>[1,2,3,4,6,8,12,16],ok:v=>Number.isInteger(v)&&v>=1&&v<=RMAX},
|
||
{k:'site2',grp:'pop',l:N_('bstd.l_site2'),d:false,v:()=>[false,true]},
|
||
{k:'gen',grp:'pop',l:N_('common.backup_generator'),d:false,v:()=>[false,true]},
|
||
{k:'lag',grp:'port',l:N_('bstd.l_lag'),d:1,v:()=>[1,2,4]},
|
||
{k:'split',grp:'port',l:N_('bstd.l_split'),d:false,v:()=>[false,true]},
|
||
{k:'ix',grp:'port',l:N_('bstd.l_ix'),d:false,v:()=>[false,true]},
|
||
{k:'pni',grp:'port',l:N_('bstd.l_pni'),d:0,v:()=>[0,2,3]},
|
||
{k:'sys',grp:'fiber',l:N_('common.line_system'),d:'keep',v:()=>['keep','roadm','cl']},
|
||
{k:'opt',grp:'fiber',l:N_('common.optics'),d:'keep',v:()=>['keep','fast']},
|
||
{k:'wv',grp:'fiber',l:N_('common.waves'),d:'keep',v:()=>['keep',...LT.map((x,i)=>i).filter(i=>i>0&&waveShown(i))],ok:v=>v==='keep'||(Number.isInteger(v)&&v>0&&!!LT[v])},
|
||
];
|
||
const BSTDK=Object.fromEntries(BSTD.map(x=>[x.k,x])),BSTD_MAX=16,BSTD_SYS={lite:0,roadm:1,cl:2};
|
||
const bstdOk=(x,v)=>x.ok?x.ok(v):x.v().includes(v);
|
||
// The whole standard, every target filled in. d: a saved one (untrusted: anything odd reads as "keep").
|
||
function bstd(d=S&&S.popStd){const o={},ob=d&&typeof d==='object'&&!Array.isArray(d)?d:{};
|
||
for(const x of BSTD){const v=ob[x.k];o[x.k]=x.k==='rt'?(own(ob,'rt')?int(v,1,RMAX,1):1):own(ob,x.k)&&bstdOk(x,v)?v:x.d;}return o;}
|
||
// Back to the saved form: the model and router count, and any other target that is not "keep". Nothing set: null.
|
||
function bstdPut(o){const d={};for(const x of BSTD)if(x.k==='rt'||o[x.k]!==x.d)d[x.k]=o[x.k];return Object.keys(d).length>1||d.rt>1?d:null;}
|
||
function popStdSanitize(s){s.popStd=s.popStd&&typeof s.popStd==='object'&&!Array.isArray(s.popStd)?bstdPut(bstd(s.popStd)):null;}
|
||
// Saves from when the intern's orders had their own router model (routers) and line system (optup): the standard takes
|
||
// them where it has none of its own; one that names its own keeps it. 'next' was no choice at all: nothing moves.
|
||
// opsSanitize() passes only what an order that was running used (undefined otherwise): the rest is dropped.
|
||
function bstdAdopt(s,g,sys){const D=bstd(s.popStd);let n=0;
|
||
if(D.g==='keep'&&Number.isInteger(g)&&bstdOk(BSTDK.g,g)){D.g=g;n++;}
|
||
if(D.sys==='keep'&&sys!=='keep'&&bstdOk(BSTDK.sys,sys)){D.sys=sys;n++;}
|
||
if(n)s.popStd=bstdPut(D);}
|
||
// A bank that forbids generators (Stormy Weather's covenant): the standard never installs one while that loan runs.
|
||
const bstdNoGen=()=>(S.loans||[]).some(l=>(l.covs||[]).includes('nogen'));
|
||
// The model a POP should get: the standard's if you can buy it, else the best main-line model that is unlocked and no
|
||
// bigger. Never a step down, never one that does not fit, and never one that would drop a POP out of the backbone core.
|
||
// c: an existing POP, or null for a new, empty one. -1: nothing to do. A vendor's demo unit (at its markup) only ever
|
||
// goes into a new POP, as before: swapping every POP onto one is the player's call, not the intern's. A new POP takes the
|
||
// model as chosen, even one smaller than the basic router (the Mikrowtik CCR has more ports): nothing is replaced there.
|
||
function bstdModel(g,c){if(g==='keep'||!RG[g])return -1;const p=c&&S.pops[c],cur=p?p.rgen||0:0,core=!!p&&coreRun()>0&&inCore(c);
|
||
const ok=i=>i>=0&&RG[i].cap<=RG[g].cap&&(!p||(RG[i].cap>RG[cur].cap&&!modelFits(c,i)&&(!core||rgCore(i,coreRun()))));
|
||
if((p?rgOpen(g):techOk('rg',g,RG[g].lvl))&&ok(g))return g;let b=-1;
|
||
for(let k=0;k<=4;k++){const i=mainRG(k);if(i!==g&&rgOpen(i)&&ok(i)&&(b<0||RG[i].cap>RG[b].cap))b=i;}return b;}
|
||
// What the standard adds to a fresh POP, priced the way popStdApply pays for it, so it can be on the button (#261).
|
||
function popStdQuote(){if(!S||!S.popStd||pzOn())return null;const D=bstd(),m=bstdModel(D.g,null),g=m>0?m:0,rt=D.rt;
|
||
let cost=g?swapCostOf(g,0,1):0;for(let n=1;n<rt;n++)cost+=routerCost(g,n);
|
||
let mo=(rt-1)*ROUTER_MO+cageMo(rt)+rt*RG[g].mo-RG[0].mo;const site2=D.site2,gen=D.gen&&!bstdNoGen();
|
||
if(site2){cost+=SITE2_COST;mo+=SITE2_MO;}if(gen){cost+=GEN_COST;mo+=GEN_MO;}
|
||
return cost||mo?{cost,mo,g,rt,site2,gen}:null;}
|
||
// A new POP's setup with the standard, for every way a POP gets built (links, cables, seats).
|
||
function newPopCost(c){const q=popStdQuote();return popCost(c)+(q?q.cost:0);}
|
||
function popStdApply(city){const p=S.pops[city];if(!S.popStd||!p||pzOn())return;const D=bstd(),m=bstdModel(D.g,null),c0=S.cash;
|
||
// Short of the model asked for (it is locked): the best that is unlocked goes in, and the toast says so.
|
||
let miss=D.g!=='keep'&&RG[m>0?m:0].cap<RG[D.g].cap;
|
||
if(m>0&&m!==(p.rgen||0)){if(S.cash>=swapCost(city,m)){spend(swapCost(city,m));p.rgen=m;}else miss=true;}
|
||
while((p.routers||1)<D.rt){const k=routerCost(p.rgen||0,p.routers||1);if(S.cash<k){miss=true;break;}spend(k);p.routers=(p.routers||1)+1;}
|
||
if(D.site2&&!p.site2){if(S.cash>=SITE2_COST){spend(SITE2_COST);p.site2=true;p.site2Day=S.day;}else miss=true;}
|
||
if(D.gen&&!p.gen&&!bstdNoGen()){if(S.cash>=GEN_COST){spend(GEN_COST);p.gen=true;}else miss=true;}
|
||
if(c0===S.cash&&!miss)return;
|
||
toast(miss?tr('pop_std.not_all_of_the_standard',{city:CITY[city].name}):tr('pop_std.city_gets_the_standard_cost',{city:CITY[city].name,n:p.routers||1,model:esc(RG[p.rgen||0].n),cost:fmt$(c0-S.cash)}),miss?'bad':'good');}
|
||
// The line in a POP's router section: the standard, a button to edit it, and one to make this POP's build the standard.
|
||
function popStdHtml(c){const p=S.pops[c];if(!p||pzOn())return '';const D=bstd(),on=!!S.popStd,mine=bstd(bstdPut({...D,g:p.rgen||0,rt:p.routers||1,site2:!!p.site2,gen:!!p.gen})),
|
||
same=on&&BSTD.every(x=>D[x.k]===mine[x.k]);
|
||
return `<div class="kv"><span>${tr('bstd.title')}</span><b>${on?esc(bstdLine(D)):tr('pop_std.none')}</b></div><div class="btns">${btn(tr('bstd.edit'),'bstdOpen',{},'sm')}${same?'':btn(tr('pop_std.make_this_the_standard'),'popStdSet',{city:c},'ghost sm')}${on?btn(tr('pop_std.clear'),'popStdClear',{},'ghost sm'):''}</div>`;}
|
||
// "2 × Absolute Unit 3.2T, second building, generator": the POP part, what a new POP gets.
|
||
function bstdPopParts(D){const a=[];if(D.g!=='keep'||D.rt>1)a.push(`${D.rt} × ${D.g==='keep'?tr('bstd.any_model'):RG[D.g].n}`);if(D.site2)a.push(tr('bstd.second_building'));if(D.gen)a.push(tr('bstd.generator'));return a;}
|
||
// The POP part first, then how many other targets.
|
||
function bstdLine(D){const a=bstdPopParts(D),n=['lag','split','ix','pni','sys','opt','wv'].filter(k=>D[k]!==BSTDK[k].d).length;if(n)a.push(trn('bstd.n_more_targets',n));return a.join(', ')||tr('pop_std.none');}
|
||
|
||
/* What the standard still asks for, as jobs in the order they get done: POPs busiest first, then ports hottest first,
|
||
then fiber and waves hottest first. Pure: it reads S and R and changes nothing. Each job is priced with the game's
|
||
own helpers on a running copy of the few numbers earlier jobs change (routers, spares), so the sum is what the clicks
|
||
cost. Jobs that build on each other form a chain (ch: a POP's routers, a port, a fiber): when one cannot be done, the
|
||
rest of that chain waits. A job that needs another chain's work first (optics only the new router model takes, a split
|
||
that needs the second building) names that job in dep and waits for it too. Whether a fiber may go dark is settled
|
||
here, not on the way, so the plan is what the button does and its sum is the quote.
|
||
intern: only where the intern may work (per-POP exceptions). J.held: fibers whose line system has to wait. */
|
||
function bstdPlan(intern){const J=[];J.held=[];if(!S||!S.popStd||pzOn())return J;const D=bstd(),here=c=>!intern||opsHere('std',c);
|
||
const load=c=>Math.max((R.nodeLoad[c]||0)/(R.nodeCap[c]||nodeCap(c)||1),portsUsed(c)/Math.max(1,portsMax(c)));
|
||
const cs=Object.keys(S.pops).filter(here),ld={};for(const c of cs)ld[c]=load(c);cs.sort((a,b)=>ld[b]-ld[a]||(a<b?-1:1));
|
||
// Router ports: what the POP will have once its own jobs are done, less what pending PNI augments will take.
|
||
const V={},rmn={},resv=c=>S.pni.reduce((a,p)=>p.up&&p.city===c?a+Math.max(0,PORT_SLOT(p.up.tier)*(p.up.n||1)-PORT_SLOT(p.tier)*(p.n||1)):a,0);
|
||
const room=c=>rmn[c]??(rmn[c]=(V[c]?portsMax(c,V[c].g,V[c].rt)-portsUsed(c,V[c].g):portsMax(c)-portsUsed(c))-resv(c)+1e-9);
|
||
const take=(c,n,dry)=>{if(n<=0)return true;if(room(c)<n)return false;if(!dry)rmn[c]-=n;return true;};
|
||
const job=(grp,k,ch,w,cost,mo,go,x)=>{const j={grp,k,ch,w,cost,mo,go,...x};J.push(j);return j;};
|
||
for(const c of cs){const p=S.pops[c],ch='p:'+c;let g=p.rgen||0,rt=p.routers||1,sw=null,s2=null;
|
||
// A router job checks the POP still has what the plan counted on: the intern's shift rests between jobs, and the player
|
||
// may click meanwhile (the button, a router by hand). So does a PNI job: one ordered meanwhile is not upgraded.
|
||
const add=()=>{const g0=g,r0=rt;job('pop','rt',ch,c+' +1',routerCost(g0,r0),routerMo(g0)+cageMo(r0+1)-cageMo(r0),()=>{const n=S.pops[c].routers||1;if(n!==r0)return false;ACT.addRouter({city:c});return (S.pops[c].routers||1)>n;},{c});rt++;};
|
||
const m=p.rupDay===S.day?-1:bstdModel(D.g,c);
|
||
if(m>=0){
|
||
// One router carrying traffic: a second goes in first, so the swap rolls through with no maintenance window.
|
||
if(rt===1&&!popIdle(c))add();
|
||
const old=g,r=rt;sw=job('pop','g',ch,c+' ⇧'+RG[m].pn,swapCostOf(m,old,r),r*(RG[m].mo-RG[old].mo),()=>{if((S.pops[c].rgen||0)!==old)return false;ACT.upRouter({city:c,g:m});return (S.pops[c].rgen||0)===m;},{c});g=m;}
|
||
while(rt<D.rt&&rt<RMAX)add();
|
||
// A building and a generator do not wait for the routers, nor for each other.
|
||
if(D.site2&&!p.site2)s2=job('pop','site2','s:'+c,c,SITE2_COST,SITE2_MO,()=>{ACT.addSite({city:c});return !!S.pops[c].site2;},{c});
|
||
if(D.gen&&!p.gen&&!bstdNoGen())job('pop','gen','e:'+c,c,GEN_COST,GEN_MO,()=>{if(bstdNoGen())return false;ACT.addGen({city:c});return !!S.pops[c].gen;},{c});
|
||
V[c]={g,rt,site2:!!p.site2||D.site2,sw,s2};}
|
||
// Ports: LAG members up to the standard, then the members split over both buildings.
|
||
if(D.lag>1||D.split){const P=[];
|
||
for(const t of S.transit)if(S.pops[t.city]&&here(t.city))P.push({k:'t',c:t.city,id:t.id,o:t,n:t.n||1,tier:t.tier,u:(R.useT[t.id]||0)/lagCap(PORT[t.tier].g,t.n)});
|
||
for(const x of S.pni)if(S.pops[x.city]&&here(x.city)&&pniOn(x)&&!x.up)P.push({k:'p',c:x.city,id:x.id,o:x,n:x.n||1,tier:x.tier,u:(R.usePni[x.id]||0)/lagCap(pniG(x),x.n)});
|
||
for(const x in S.ixp){const c=ixCity(x);if(!S.myIx[x]&&S.pops[c]&&here(c))P.push({k:'ix',c,id:x,n:(S.ixn||{})[x]||1,tier:S.ixp[x],u:(R.useIx[x]||0)/Math.max(1e-9,lagCap(PORT[S.ixp[x]].g,(S.ixn||{})[x]))});}
|
||
P.sort((a,b)=>b.u-a.u||(a.id<b.id?-1:1));
|
||
for(const o of P)bstdPort(o,D,V[o.c],take,job);}
|
||
// An exchange port at every POP in a city with an exchange (not DE-KICKS: they want a sales call first).
|
||
if(D.ix)for(const c of cs){if(hasIxAt(c))continue;const x=ixIdsIn(c).find(x=>!isDk(x)&&!S.myIx[x]&&!!ixName(x)&&S.ixp[x]===undefined);if(!x||!take(c,PORT_SLOT(0)))continue;
|
||
const ch='i:'+x,pm=ixPortMo(x,0);job('port','ix',ch,x+' +'+PORT[0].n,pm,pm,()=>{ACT.buyIx({city:x,tier:0});return S.ixp[x]!==undefined;},{c});
|
||
bstdPort({k:'ix',c,id:x,n:1,tier:0,fresh:true},D,V[c],take,job);}
|
||
// PNIs spread out: every network you have a PNI with gets one in at least that many cities, where one is worth it.
|
||
if(D.pni){const by={};for(const p of S.pni)(by[p.net]||(by[p.net]=new Set())).add(p.city);
|
||
for(const net of Object.keys(by).sort()){let k=D.pni-by[net].size;if(k<=0||!isNet(net))continue;
|
||
const opts=NET[net].pops.filter(c=>S.pops[c]&&here(c)&&!by[net].has(c)).map(c=>({c,v:pniVal(net,c)})).filter(o=>o.v.worth).sort((a,b)=>b.v.want-a.v.want||(a.c<b.c?-1:1));
|
||
for(const {c,v} of opts){if(k<=0)break;let q=null;for(let t=Math.max(v.tier,pniMin(net));t>=pniMin(net)&&!q;t--){const x=pniQuote(c,net,t,resv(c));if(x&&!x.why)q=x;}
|
||
if(!q||!take(c,q.slots))continue;const tier=q.tier;k--;
|
||
job('port','pni','n:'+net+'@'+c,c+' '+net,q.cost,pniMo({net,tier,n:1},0),()=>{const x=pniQuote(c,net,tier,resv(c));if(!x||x.why||x.ex||!spend(x.cost))return false;pniPlace(x);return true;},{c});}}}
|
||
// Fiber and waves, hottest first. A POP excepted from the order keeps its links as they are too.
|
||
if(D.sys!=='keep'||D.opt!=='keep'||D.wv!=='keep'){const sp={...(S.spares||{})},dk=new Map(),wv=new Map();
|
||
const ls=S.links.filter(l=>(l.kind==='df'||l.kind==='wave')&&here(l.a)&&here(l.b)&&S.pops[l.a]&&S.pops[l.b]).map(l=>({l,u:(R.load[l.id]||0)/Math.max(1e-9,capOf(l))})).sort((a,b)=>b.u-a.u||(a.l.id<b.l.id?-1:1));
|
||
// Waves: the size each goes up to. The decom order hands back the smaller waves a pair no longer needs, judged with
|
||
// the sizes these upgrades give: never pay a month's rent on one it is about to return.
|
||
if(D.wv!=='keep'){for(const {l} of ls)if(l.kind==='wave'&&l.tier<D.wv)for(let t=D.wv;t>l.tier;t--){if(subWaveWhy(t,l.km,l.sub)||soldOut(l.a,l.b,t,l.cable)||!waveOk(l.a,l.b,t))continue;wv.set(l,t);break;}
|
||
if(wv.size&&opsHired()&&S.ops.on.decom){const by=new Map(),cf=x=>wv.has(x)?capOf(x)*LT[wv.get(x)].g/LT[x.tier].g:capOf(x);
|
||
for(const x of S.links)if(x.kind!=='vxc'&&x.kind!=='sat'){const k=edgeKey(x);(by.get(k)||by.set(k,[]).get(k)).push(x);}
|
||
for(const k of new Set([...wv.keys()].map(edgeKey)))for(const x of opsDecomPick(by.get(k),cf))wv.delete(x);}}
|
||
for(const {l} of ls){const ch='l:'+l.id;
|
||
if(l.kind==='wave'){const t=wv.get(l);if(t){const nm=Math.round(waveMo(l.km,l.sub,t)*(l.mult||1));job('fiber','wv',ch,l.a+'|'+l.b,nm,nm-(l.mo||0),()=>{ACT.upLink({id:l.id,tier:t});return l.tier===t;},{l});}continue;}
|
||
if(l.eqDown>S.day)continue;
|
||
let sys=l.sys;const lit=l.ready<=S.day,nx=D.sys!=='keep'&&BSTD_SYS[sys]<BSTD_SYS[D.sys]&&sysNext({sys});
|
||
// A lit fiber goes dark for a day. Busy with nothing parallel to carry it: it keeps its line system for now, and
|
||
// gets the best optics that one takes. A parallel fiber goes dark first in this plan: it waits for next time.
|
||
// The intern's plan leaves the rest to the shift's own test (opsDark(): a routing without the fiber), as the optical
|
||
// order does: here only what is known without one (opsDarkPre()), and a fiber that failed the test this week.
|
||
if(nx&&techOk('sys',nx,DF_SYS[nx].lvl)){
|
||
if(lit&&!(intern?!(OPTUP_NO.get(l.id)>S.day)&&opsDarkPre(l)!==false:bstdDarkOk(l)))J.held.push(l);
|
||
else if(lit&&(intern?pairLinks(l.a,l.b).some(x=>x!==l&&dk.get(x.id)):!bstdDarkOk(l,dk))){J.held.push(l);continue;}
|
||
else{const busy=lit&&(intern?!opsUnused(l):bstdBusy(l));
|
||
while(BSTD_SYS[sys]<BSTD_SYS[D.sys]){const to=sysNext({sys});if(!to||!techOk('sys',to,DF_SYS[to].lvl))break;
|
||
const s0=sys;job('fiber','sys',ch,l.a+'|'+l.b,sysCost(to)*(to==='cl'&&l.own?Math.max(1,l.pairs|0):1),0,()=>{if(l.sys!==s0)return false;ACT.upSys({id:l.id});return l.sys===to;},{l,dark:lit,busy});sys=to;}
|
||
if(lit)dk.set(l.id,busy);}}
|
||
// Optics for the line system the fiber will have.
|
||
if(D.opt==='fast'&&lit&&(l.ch||[]).length)J.push(...bstdOptJobs(l,sys,sp,V,take,ch));}}
|
||
return J;}
|
||
// One port's LAG members up to the standard, then split over both buildings. A PNI's new member is an augment that
|
||
// lands later; it remembers to split when it does. o.fresh: a port the plan is buying (the IX job before it).
|
||
function bstdPort(o,D,v,take,job){const {k,c,id}=o,ch=(o.fresh?'i:':'x:'+k+':')+id;let n=o.n;
|
||
const site2=v?v.site2:!!(S.pops[c]&&S.pops[c].site2),split=D.split&&site2,
|
||
// A second building still to buy is a job of its own: the split waits for it, or it would be tried every shift.
|
||
sx={c,...(v&&v.s2&&!S.pops[c].site2?{dep:[v.s2]}:{})};
|
||
if(k==='p'){const x=o.o;
|
||
if(n<D.lag&&n<LAG_MAX&&!msfNope(x.net,c)&&take(c,PORT_SLOT(x.tier))){const n0=n,mo=x.g1?Math.round(PORT[0].pni/2):PORT[x.tier].pni;
|
||
job('port','lag',ch,c+' +1×'+pniPN(x),1500+PORT[x.tier].setup/2,mo,()=>{if((x.n||1)!==n0||x.up)return false;ACT.lagP({id,d:1});return !!x.up||(x.n||1)>n0;},{c});n++;}
|
||
if(split&&n>=2&&x.site!=='ab')job('port','split',ch,c,0,0,()=>{if(!S.pops[c].site2)return false;if(x.up){if(x.up.n<2)return false;x.up.site='ab';return true;}
|
||
if((x.n||1)<2)return false;ACT.siteSet({k:'p',id,s:'ab'});return x.site==='ab';},sx);
|
||
return;}
|
||
const t=k==='t'?o.o:null,tier=o.tier,each=k==='t'?PORT[tier].setup:ixPortMo(id,tier);
|
||
while(n<D.lag&&n<LAG_MAX&&(k!=='t'||portOk(c,tier))&&take(c,PORT_SLOT(tier))){const n0=n,mo=k==='t'?transitMo({...t,n:n0+1},0)-transitMo({...t,n:n0},0):each;
|
||
job('port','lag',ch,(k==='t'?c:id)+' +1×'+PORT[tier].n,each,mo,k==='t'?()=>{if((t.n||1)!==n0)return false;ACT.lagT({id,d:1});return (t.n||1)>n0;}
|
||
:()=>{const m=(S.ixn||{})[id]||1;if(m!==n0)return false;ACT.lagIx({city:id,d:1});return ((S.ixn||{})[id]||1)>m;},{c});n++;}
|
||
const cur=k==='t'?t.site:(S.ixSite||{})[id];
|
||
if(split&&n>=2&&cur!=='ab')job('port','split',ch,k==='t'?c:id,0,0,()=>{if(!S.pops[c].site2)return false;
|
||
if(k==='t'){if((t.n||1)<2)return false;ACT.siteSet({k:'t',id,s:'ab'});return t.site==='ab';}
|
||
if(((S.ixn||{})[id]||1)<2)return false;ACT.siteSet({k:'ix',id,s:'ab'});return (S.ixSite||{})[id]==='ab';},sx);}
|
||
// Can it see the fastest optic that reaches, ignoring the free-slot check (a swap frees its own slot first)? rg: each
|
||
// end's router model once the plan's swaps are in, so the optics a new model takes are part of the plan.
|
||
const bstdOptFits=(l,o,sys,rg)=>{const O=OPT[o];return techOk('opt',o,O.lvl)&&(O.sys!=='roadm'||isRdm(sys))&&!!optMode(o,l)&&[l.a,l.b].every(c=>!S.pops[c]||RG[rg(c)].cls>=O.rg);};
|
||
const bstdCnt=(l,o)=>(l.ch||[]).filter(k=>k===o).length;
|
||
// Swap a fiber's slower optics for the fastest that reaches on it, one job per kind, slowest first: the old ones go on
|
||
// the spares shelf (setChCount), the new ones come from the shelf first. sp: spares as earlier jobs leave them.
|
||
function bstdOptJobs(l,sys,sp,V,take,ch){const rg=c=>V[c]?V[c].g:(S.pops[c].rgen||0),fits=Object.keys(OPT).filter(o=>bstdOptFits(l,o,sys,rg));if(!fits.length)return [];
|
||
const best=fits.sort((a,b)=>optG(b,l)-optG(a,l)||optCost(a)-optCost(b))[0],bg=optG(best,l),g=c=>V[c]?V[c].g:undefined,out=[];
|
||
// Optics only the new router model takes wait for its swap: until it is in, nothing is pulled.
|
||
const low=c=>!!S.pops[c]&&RG[S.pops[c].rgen||0].cls<OPT[best].rg,dep=[l.a,l.b].filter(low).map(c=>V[c]&&V[c].sw).filter(Boolean);
|
||
for(const o0 of [...new Set(l.ch||[])].filter(o=>OPT[o]&&optG(o,l)<bg).sort((a,b)=>optG(a,l)-optG(b,l)||(a<b?-1:1))){
|
||
// A faster optic can take more router ports than the one it replaces: as many as both ends have room for.
|
||
const per=c=>Math.max(0,chPorts(best,c,g(c))-chPorts(o0,c,g(c)));let k=bstdCnt(l,o0);
|
||
while(k>0&&!(take(l.a,per(l.a)*k,true)&&take(l.b,per(l.b)*k,true)))k--;if(!k)continue;take(l.a,per(l.a)*k);take(l.b,per(l.b)*k);
|
||
const r=REGEN_CH*(l.regen|0),c=optCost(best),s=Math.max(0,Math.min(k,sp[best]|0)),cost=s*Math.round(c*r)+(k-s)*Math.round(c*(1+r));
|
||
sp[best]=(sp[best]|0)-s;sp[o0]=(sp[o0]|0)+k;
|
||
// price: what it costs when it runs. The plan's price counts on the shelf as the jobs before it leave it; one of those
|
||
// refused (budget, cash) leaves the shelf emptier, and the budget and the floor must see the real bill.
|
||
out.push({grp:'fiber',k:'opt',ch,w:l.a+'|'+l.b,cost,mo:0,l,...(dep.length?{dep}:{}),price:()=>opticCountCost(l,best,bstdCnt(l,best)+k),
|
||
go:()=>{const n0=bstdCnt(l,o0),h0=bstdCnt(l,best);if(n0<k||[l.a,l.b].some(low))return false;
|
||
ACT.setChCount({id:l.id,o:o0,n:n0-k});if(bstdCnt(l,o0)!==n0-k)return false;ACT.setChCount({id:l.id,o:best,n:h0+k});if(bstdCnt(l,best)===h0+k)return true;
|
||
// The new ones could not go in (ports, cash): the old optics go back in from the shelf.
|
||
(l.ch=l.ch||[]).push(...Array(k).fill(o0));S.spares[o0]-=k;return false;}});}
|
||
return out;}
|
||
// The button's test of a line-system swap, which darkens the fiber for a day: only while it carries little, or its
|
||
// parallel links can take its load. No routing, so the quote is what the click does. (The intern routes: opsDark().)
|
||
// ex: fibers already going dark today (id → whether they carry traffic): they carry nothing for this one, and one whose
|
||
// traffic leans on its parallel links keeps them lit.
|
||
const bstdBusy=l=>((R.load&&R.load[l.id])||0)>Math.max(1,capOf(l)*0.1);
|
||
// Express lightpaths (#58) ride the fiber's glass: its own load says nothing about them, and its parallel links do not
|
||
// carry them, so an express with any traffic at all keeps the fiber lit (the line system waits, as for a busy fiber).
|
||
// The intern's routing test needs no such rule: an express goes dark with its fiber there (opsDarkTrial()).
|
||
const bstdXpBusy=l=>xpSlots(l)>0&&xpOver(l.id).some(x=>((R.load&&R.load[x.id])||0)>1e-6);
|
||
function bstdDarkOk(l,ex){if(bstdXpBusy(l))return false;const P=pairLinks(l.a,l.b).filter(x=>x!==l);if(ex&&P.some(x=>ex.get(x.id)))return false;if(!bstdBusy(l))return true;
|
||
return P.filter(x=>!linkDown(x)&&!x.standby&&!(ex&&ex.has(x.id))).reduce((a,x)=>a+Math.max(0,capOf(x)-((R.load&&R.load[x.id])||0)),0)>=((R.load&&R.load[l.id])||0);}
|
||
/* Do the plan's jobs in order. can(j): may it be paid for (budget, floor, cash); pay(j): run it, true if it took.
|
||
dark(j, ex): may the first job that darkens a fiber go ahead (ex: fibers gone dark so far, as bstdDarkOk() takes them),
|
||
a generator, so a test can route (yields included); done1(j, first): after each job done (first: it took a fiber dark).
|
||
max: at most that many jobs tried, done or failed, so a shift's work stays bounded whatever goes wrong. A bare yield
|
||
after each job is a place to let a frame through. Returns what was done. */
|
||
function* bstdRun(J,can,pay,dark,done1,max=1e9){const stop=new Set(),done=new Set(),dk=new Map();let n=0;
|
||
for(const j of J){if(n>=max)break;if(stop.has(j.ch))continue;if(j.dep&&!j.dep.every(d=>done.has(d))){stop.add(j.ch);continue;}
|
||
// The money first: no fiber is tested for a job the budget cannot pay, and asked again after a test that routed.
|
||
const first=!!j.dark&&!dk.has(j.l.id);
|
||
if(!can(j)||first&&(!(yield* dark(j,dk))||!can(j))){stop.add(j.ch);continue;}n++;
|
||
if(!pay(j)){stop.add(j.ch);continue;}
|
||
if(j.dark)dk.set(j.l.id,!!j.busy);done.add(j);if(done1)done1(j,first);yield;}
|
||
return [...done];}
|
||
// The button: the whole plan at once, on the page, every fiber judged as the plan judged it.
|
||
function bstdDo(J,can,pay){const g=bstdRun(J,can,pay,function*(j,ex){return bstdDarkOk(j.l,ex);});for(;;){const s=g.next();if(s.done)return s.value;}}
|
||
// What a job costs as it runs (an optics swap is priced again then: see bstdOptJobs).
|
||
const bstdPrice=j=>j.price?j.price():j.cost;
|
||
// The sheet's summary and the MCP quote: how much is below the standard, and what bringing it all up costs. The plan is
|
||
// what the button does, so its sum is the bill. held: the fibers whose line system waits ("fra|ams"); they are below it.
|
||
function bstdQuote(intern){const J=bstdPlan(intern),P=new Set(),Q=new Set(),L=new Set(J.held.map(l=>l.id));let cost=0,mo=0;
|
||
for(const j of J){cost+=j.cost;mo+=j.mo;if(j.grp==='pop')P.add(j.c);else if(j.grp==='port')Q.add(j.ch);else L.add(j.l.id);}
|
||
return {cost:Math.round(cost),mo:Math.round(mo),pops:P.size,ports:Q.size,links:L.size,n:J.length,held:J.held.map(l=>l.a+'|'+l.b)};}
|
||
// The optics order lights the standard's fastest optics while this order keeps to a standard that wants them.
|
||
const bstdFast=()=>!!(S.popStd&&opsHired()&&S.ops.on.std&&bstd().opt==='fast');
|
||
/* The intern's shift: the same plan, every job through the budget and the cash floor at what it costs then, a few at a
|
||
time. A fiber goes dark only on the optical order's test (opsDark(): a routing without it, in play in the route
|
||
worker), within the night's dark swaps that order shares (opsDarkRoom()), and a fiber that fails waits a week. One
|
||
owner per fiber: a fiber or wave the optical order worked on this shift is left alone, and what this order works on
|
||
is its own until tomorrow (X.fib: that order and decom leave it alone). The plan is made once a shift, and the shift
|
||
rests between jobs: a POP decommissioned or a link given up meanwhile is not worked on (the links last: a walk of them,
|
||
only for a job the budget lets through). */
|
||
function* bstdShift(X){const mine=j=>(!j.c||!!S.pops[j.c])&&(!j.l||(X.fib.get(j.l.id)||'std')==='std');
|
||
yield* bstdRun(bstdPlan(true),j=>mine(j)&&X.can('std',bstdPrice(j),j.mo)&&(!j.l||S.links.includes(j.l)),j=>X.pay('std',()=>j.go())&&(X.did('std',j.w),true),
|
||
function*(j){const l=j.l;if(OPTUP_NO.get(l.id)>S.day||!opsDarkRoom(X))return false;const ok=yield* opsDark(l,X);if(!ok)OPTUP_NO.set(l.id,S.day+OPTUP_WAIT);return ok;},
|
||
(j,first)=>{if(j.l)X.fib.set(j.l.id,'std');if(first)X.dk.n++;},BSTD_MAX);}
|