Keep hand-listed alien waves on the market (#340)

With the capacity market selling alien waves (or pairs), its daily pass set
every fiber's listing to what the thresholds allowed: 128 waves listed by hand
came back as 96 (after the 25% kept back), or were taken off entirely when
the route's demand was below the minimum or the price below the floor.

A listing now remembers how much was listed by hand (l.list.hw / hp). The
broker lists its own share on top, capped at what the fiber has free; sales
use up the broker's share first; switching a product or route off keeps the
hand-listed part. cmClean() checks the new fields, so old saves load as before.

Fixes #340
This commit is contained in:
Frederic Jung 2026-11-11 12:06:23 +01:00
parent c137d3552d
commit e27e9e3066
No known key found for this signature in database
7 changed files with 87 additions and 10 deletions

View file

@ -6,6 +6,8 @@ All notable changes to this project are documented here. The project follows
## [Unreleased]
- Alien waves you list on a fiber yourself now stay listed. With the capacity market selling for you, the broker used to cut your listing down to what its settings allowed the next day, or take it off altogether when the route's demand was below your minimum. Your own listing now stays put, and the broker adds what its settings allow on top (#340).
## [3.4.0] — 2026-11-10
- A PNI can now grow past eight ports: each router at the POP takes eight members of a PNI, up to 32, so a busier interconnect costs another router and its ports. Saves keep every LAG they have, and a router a PNI still needs stays until the PNI shrinks (#323).

View file

@ -21,10 +21,15 @@ const CM_HMAX=2.5;
const cmNew=()=>({hold:25,p:JSON.parse(JSON.stringify(CM_DEF)),off:{},sh:[],news:[],seen:[]});
const cmCut=()=>broker()?0.97:0.9;
/* A fiber's listing, l.list: {w: spectrum slots, p: pairs} as it always was, plus m1/m4/m8/m16 (managed 100G to 1.6T
waves) when any are listed. cmClean() checks one (a save, or after a day of sales) and returns null when nothing is left. */
waves) when any are listed, and hw/hp: how many of w/p you listed by hand, which the broker's thresholds never take
back (#340). cmClean() checks one (a save, or after a day of sales) and returns null when nothing is left. */
const cmLs=l=>l.list||(l.list={w:0,p:0});
const CM_HAND={w:'hw',p:'hp'};
function cmClean(L){if(!L||typeof L!=='object'||Array.isArray(L))return null;const x={w:int(L.w,0,256,0),p:int(L.p,0,2,0)};let any=x.w||x.p;
for(const k in CM_HAND){const v=Math.min(x[k],int(L[CM_HAND[k]],0,256,0));if(v)x[CM_HAND[k]]=v;}
for(const p of CM_W){const v=int(L[CM_L[p]],0,256,0);if(v){x[CM_L[p]]=v;any=1;}}return any?x:null;}
// What you listed by hand of a product on a fiber (only alien waves and pairs can be).
const cmHand=(l,lk)=>(CM_HAND[lk]&&l.list&&l.list[CM_HAND[lk]])||0;
// A stable number in [0,1) for a route and a question, different in every game.
const cmU=(k,q)=>hstr(k+'|'+q+'|'+((S&&S.asn)||0))%100000/100000;
/* How hot a route is (1 = its usual self). Its own slow cycle (two waves of 4 to 10 and 1.5 to 3 months) is what a
@ -130,7 +135,8 @@ function cmShocks(d){const C=S.cm;C.sh=C.sh.filter(x=>x.until>d);
while demand is at least your minimum and the price at least your floor) or takes it off, then each listing may
find a buyer: the more room the route has left and the hotter it is, the sooner; the fastest fiber sells first. */
function cmList1(r,p,s,d){const c=S.cm.p[p],lk=CM_L[p];if(!c.on||S.cm.off[r.k])return;const ok=cmDem(r,p,d)>=c.min&&cmIdx(r,p,s,0,d)*100>=c.floor;
for(const l of r.ls){const n=ok?cmSpare(l,p):0;if(n>0)cmLs(l)[lk]=n;else if(l.list)l.list[lk]=0;}}
// What you listed by hand stays listed on top of what the thresholds allow, up to what the fiber has free.
for(const l of r.ls){const h=cmHand(l,lk),n=Math.min(ok?cmSpare(l,p)+h:h,h?(p==='spec'?freeCh(l):1):Infinity);if(n>0)cmLs(l)[lk]=n;else if(l.list)l.list[lk]=0;}}
// The listings as the thresholds want them right now (after you change a setting), without selling anything.
function cmRelist(){const rs=cmRoutes(),sold=cmSold(rs);for(const r of rs.values())for(const p of CM_P)cmList1(r,p,sold.get(r.k)[p],S.day);cmTidy();}
const cmTidy=()=>{for(const l of S.links)if(l.list){const L=cmClean(l.list);if(L)l.list=L;else delete l.list;}};
@ -146,7 +152,9 @@ function cmDay(d=S.day){const C=S.cm;cmShocks(d);const rs=cmRoutes(),sold=cmSold
while(n>0&&cmIdx(r,p,s[p],n-1,d)*100<C.p[p].floor)n--;if(!n)continue;
const cut=cmCut()*mkt(),L=cmList(r,p);let rate=0;for(let i=0;i<n;i++)rate+=L*cmIdx(r,p,s[p],i,d);
const up=p==='iru'?Math.round(cmUpList(r)*cmIdx(r,p,s[p],0,d)*cut):0;
cmSell(l,p,n,Math.round(rate*cut),up);s[p]+=n;l.list[lk]-=n;}}
cmSell(l,p,n,Math.round(rate*cut),up);s[p]+=n;l.list[lk]-=n;
// Sales take from the broker's share first: what you listed by hand goes only once that is sold.
if(cmHand(l,lk)>l.list[lk])l.list[CM_HAND[lk]]=l.list[lk];}}
cmGrow(d);cmTidy();}
/* What the next 30 and 90 days look like on a route, for a Cable broker: the route's own cycle and the news already
out, but not the noise or news still to come. */

View file

@ -1,7 +1,7 @@
Object.assign(ACT,{
// List spare capacity with a capacity broker (#203): w waves and p spare pairs, sold as the route's market wants.
listFiber:({id,w,p})=>{const l=S.links.find(x=>x.id===id);if(!l||l.kind!=='df'||l.ready>S.day)return;w=Math.max(0,Math.min(freeCh(l)-((l.list&&l.list.w)||0),w|0));p=l.own?Math.max(0,Math.min((pairFree(l)?1:0)-((l.list&&l.list.p)||0),p|0)):0;
if(!w&&!p){toast(tr('customers.nothing_spare_to_sell_on_this'),'bad');sfx('err');return;}const L=l.list||(l.list={w:0,p:0});L.w+=w;L.p+=p;
if(!w&&!p){toast(tr('customers.nothing_spare_to_sell_on_this'),'bad');sfx('err');return;}const L=l.list||(l.list={w:0,p:0});L.w+=w;L.p+=p;if(w)L.hw=(L.hw||0)+w;if(p)L.hp=(L.hp||0)+p;
toast(tr('broker.listed',{a:CITY[l.a].name,b:CITY[l.b].name}),'good');sfx('click');after();},
unlistFiber:({id})=>{const l=S.links.find(x=>x.id===id);if(!l||!l.list)return;delete l.list;sfx('click');after();},
accept:({id,auto=false})=>{const o=S.offers.find(x=>x.id===id);if(!o)return;const customer=auto?autoOffer(o):offerCustomer(o);if(!customer)return;if(!bHas(o)){toast(tr('customers.you_need_a_pop_in_every'),'bad');return;}

View file

@ -2,13 +2,14 @@
Object.assign(ACT,{
marketOpen:()=>{if(!S||pzOn())return;UI.dtab='market';$('dash').hidden=false;renderDash();},
mktHold:({v})=>{if(!CM_HOLD.includes(v))return;S.cm.hold=v;cmRelist();sfx('click');after();},
// Switching a product off takes what the broker listed of it off the market; what it already sold stays sold.
// Switching a product off takes what the broker listed of it off the market (what you listed by hand stays); what it
// already sold stays sold.
mktCfg:({p,k,v})=>{const c=CM_P.includes(p)&&S.cm.p[p];if(!c)return;
if(k==='on'){c.on=!!v;if(!c.on)for(const l of S.links)if(l.list&&l.list[CM_L[p]])l.list[CM_L[p]]=0;}
if(k==='on'){c.on=!!v;if(!c.on)for(const l of S.links)if(l.list&&l.list[CM_L[p]])l.list[CM_L[p]]=cmHand(l,CM_L[p]);}
else if(k==='min'&&CM_MIN[p].includes(v))c.min=v;else if(k==='floor'&&CM_FLOOR.includes(v))c.floor=v;else return;
cmRelist();sfx('click');after();},
// A route the broker leaves alone keeps nothing listed by the thresholds.
// A route the broker leaves alone keeps nothing listed by the thresholds, only what you listed by hand.
mktRoute:({k})=>{const r=typeof k==='string'&&cmRoutes().get(k);if(!r)return;if(S.cm.off[k]){delete S.cm.off[k];}else{S.cm.off[k]=1;
for(const l of r.ls)if(l.list)for(const p of CM_P)if(S.cm.p[p].on)l.list[CM_L[p]]=0;}
for(const l of r.ls)if(l.list)for(const p of CM_P)if(S.cm.p[p].on)l.list[CM_L[p]]=cmHand(l,CM_L[p]);}
cmRelist();sfx('click');after();},
});

View file

@ -43,7 +43,7 @@ test('a listing is kept in the save, checked, and can be taken off the market',
fiber(ev, 'big', 'fra', 'ams', 450);
ev("ACT.listFiber({id:'big',w:4})");
ev("const t=JSON.stringify(packSave(S));S=migrate(JSON.parse(t))");
assert.deepEqual(JSON.parse(ev("JSON.stringify(S.links.find(l=>l.id==='big').list)")), { w: 4, p: 0 });
assert.deepEqual(JSON.parse(ev("JSON.stringify(S.links.find(l=>l.id==='big').list)")), { w: 4, p: 0, hw: 4 });
ev("const o=JSON.parse(JSON.stringify(packSave(S)));o.links.find(l=>l.id==='big').list={w:'lots',p:-3};S=migrate(o)");
assert.equal(ev("S.links.find(l=>l.id==='big').list===undefined"), true, 'junk is dropped');
ev("ACT.listFiber({id:'big',w:2});ACT.unlistFiber({id:'big'})");

View file

@ -1279,7 +1279,7 @@ test('#51 a dark fiber shows what is rented out, and spare capacity can be liste
assert.equal(ev("S.links[0].list?S.links[0].list.w:0"), 4 - b.slots, 'the listing shrinks by what sold');
// when every channel is lit or sold, only the spare pair in your own trench can be listed; then nothing is left
ev("const l=S.links[0];delete l.list;l.ch=Array(chMax(l)-soldSlots(l)).fill('tp16');after();ACT.listFiber({id:'df1',w:4,p:1})");
assert.deepEqual(JSON.parse(ev("JSON.stringify(S.links[0].list)")), { w: 0, p: 1 });
assert.deepEqual(JSON.parse(ev("JSON.stringify(S.links[0].list)")), { w: 0, p: 1, hp: 1 });
ev("S.biz.push({id:'p1',type:'dfpair',link:'df1',city:'fra',city2:'ams',name:'X',bw:1,rate:1,reqs:[]},{id:'p2',type:'dfpair',link:'df1',city:'fra',city2:'ams',name:'Y',bw:1,rate:1,reqs:[]});delete S.links[0].list;S.offers=[];renderPanel()");
assert.match(txt(), /Nothing spare to sell on this fiber right now/);
ev("ACT.listFiber({id:'df1',w:1,p:1})"); assert.equal(ev("S.links[0].list===undefined"), true);

View file

@ -0,0 +1,66 @@
// #340: alien waves you list by hand stay listed, whatever the capacity market's thresholds say. The broker's daily pass
// used to overwrite the listing with what the thresholds allowed, so 128 listed waves came back as 96, or as nothing.
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const setup = () => {
const r = load();
r.ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.cash=1e9;S.lvl=6;S.biz=[];S.offers=[];for(const c of ['lon','nyc'])if(!S.pops[c])ACT.buildPop({city:c})");
r.ev("S.links.push({id:'f',a:'lon',b:'nyc',kind:'df',own:true,sys:'cl',ch:[],km:5570,ready:0,day:0,cut:0,mo:1,trench:'own:f',pairs:1});after()");
return r;
};
// Objects from the page come from another realm: compare them as JSON.
const J = (ev, x) => JSON.parse(ev(`JSON.stringify(${x})`));
const L = ev => J(ev, "S.links.find(l=>l.id==='f').list||null");
// A day of the market with no buyer (so nothing sells and only the listing logic runs).
const quietDay = ev => ev("(()=>{const r=Math.random;Math.random=()=>0.99;try{cmDay(S.day)}finally{Math.random=r}})()");
test('#340 a hand-made listing survives the broker when demand is below your minimum or the price below your floor', () => {
const { ev, errors } = setup();
ev("S.cm.p.spec.on=true;S.cm.p.spec.min=8;S.cm.p.spec.floor=120");
ev("ACT.listFiber({id:'f',w:128})");
assert.equal(L(ev).w, 128);
quietDay(ev);
assert.equal(L(ev).w, 128, 'still listed the next day');
// Changing a setting relists right away; it must not take the hand-made listing away either.
ev("ACT.mktCfg({p:'spec',k:'floor',v:100})");
assert.equal(L(ev).w, 128);
ev("ACT.mktCfg({p:'spec',k:'on',v:0})");
assert.equal(L(ev).w, 128, 'switching the product off keeps what you listed by hand');
ev("ACT.mktCfg({p:'spec',k:'on',v:1});ACT.mktRoute({k:'lon|nyc'})");
assert.equal(L(ev).w, 128, 'leaving the route alone keeps it too');
ev("ACT.unlistFiber({id:'f'})");
assert.equal(L(ev), null, 'unlisting still clears it');
assert.deepEqual(errors, []);
});
test('#340 the broker adds its share on top of yours, and sales take from its share first', () => {
const { ev, errors } = setup();
ev("S.cm.p.spec.on=true;S.cm.p.spec.min=1;S.cm.p.spec.floor=60;S.cm.hold=25");
quietDay(ev);
const auto = L(ev).w;
assert.equal(auto, 96, 'the broker lists what is spare after the 25% kept back');
ev("ACT.listFiber({id:'f',w:4})");
quietDay(ev);
assert.equal(L(ev).w, 100, 'your 4 come out of what is kept back');
// A buyer takes a wave: the broker's share shrinks, yours stays.
ev("(()=>{const r=Math.random;Math.random=()=>0;try{cmDay(S.day)}finally{Math.random=r}})()");
assert.equal(ev("S.biz.filter(b=>b.type==='wwave').reduce((a,b)=>a+b.slots,0)"), 1);
assert.equal(L(ev).hw, 4);
quietDay(ev);
assert.equal(L(ev).w, 99, '95 spare after the reserve, plus your 4');
assert.equal(L(ev).hw, 4);
assert.deepEqual(errors, []);
});
test('#340 the hand-made part of a listing round-trips through a save, and old saves load as before', () => {
const { ev, errors } = setup();
ev("ACT.listFiber({id:'f',w:12})");
const back = J(ev, "((()=>{const s=parseSave(JSON.stringify(S)).s;migrate(s);return s.links.find(l=>l.id==='f').list})())");
assert.deepEqual(back, { w: 12, p: 0, hw: 12 });
assert.deepEqual(J(ev, "(cmClean({w:5,p:0,hw:40}))"), { w: 5, p: 0, hw: 5 }, 'never more by hand than listed');
assert.deepEqual(J(ev, "(cmClean({w:5,p:0}))"), { w: 5, p: 0 }, 'an old listing has nothing by hand');
assert.equal(ev("cmClean({w:0,p:0,hw:3})"), null);
assert.deepEqual(errors, []);
});