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

@ -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, []);
});