From e27e9e306660f8f465b3da84973294c5eac67a58 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Wed, 11 Nov 2026 12:06:23 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 2 + src/game/60-features/37-fiber-market.js | 14 ++++-- src/game/90-actions/50-customers.js | 2 +- src/game/90-actions/53-market.js | 9 ++-- tests/fiber-broker.test.js | 2 +- tests/issues.test.js | 2 +- tests/manual-listing.test.js | 66 +++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 tests/manual-listing.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b31070..43a6467 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/src/game/60-features/37-fiber-market.js b/src/game/60-features/37-fiber-market.js index 6236f09..3bad457 100644 --- a/src/game/60-features/37-fiber-market.js +++ b/src/game/60-features/37-fiber-market.js @@ -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)*100l.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. */ diff --git a/src/game/90-actions/50-customers.js b/src/game/90-actions/50-customers.js index aa5e4c8..eb5810e 100644 --- a/src/game/90-actions/50-customers.js +++ b/src/game/90-actions/50-customers.js @@ -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;} diff --git a/src/game/90-actions/53-market.js b/src/game/90-actions/53-market.js index c9e593b..a362177 100644 --- a/src/game/90-actions/53-market.js +++ b/src/game/90-actions/53-market.js @@ -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();}, }); diff --git a/tests/fiber-broker.test.js b/tests/fiber-broker.test.js index 7248473..1344982 100644 --- a/tests/fiber-broker.test.js +++ b/tests/fiber-broker.test.js @@ -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'})"); diff --git a/tests/issues.test.js b/tests/issues.test.js index d09c13b..9eac260 100644 --- a/tests/issues.test.js +++ b/tests/issues.test.js @@ -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); diff --git a/tests/manual-listing.test.js b/tests/manual-listing.test.js new file mode 100644 index 0000000..66b48f6 --- /dev/null +++ b/tests/manual-listing.test.js @@ -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, []); +});