Tier 1 by customer cone (#85), and badge variants per ruleset preset
Cones: a PNI or IX session reaches the peer and its transitive customer cone, precomputed once at load (CONE/CONE_S/DOWN/ANC). Cone routes rank after direct peering and before transit (CONE_RANK 2.5), so peering widely still pays. The long tail and several real eyeballs get real-world upstreams. Cones only add reach, so the rule is easier for every save and needs no version flag. Badges: S.achP[id] records the preset variant (freeze/oncall/bgp), sanitized, default 'oncall' for old saves; the device hall of fame records every variant earned. Harder presets upgrade a badge; toast, list, collection shelf and the share card show it. Sandbox and puzzles earn none.
This commit is contained in:
parent
c011022203
commit
6489db146e
27 changed files with 493 additions and 60 deletions
137
tests/badges-variants.test.js
Normal file
137
tests/badges-variants.test.js
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
// Badge variants by ruleset preset (2.0): On-call earns the standard badge, Change Freeze a frosted one and BGP at
|
||||
// 3 a.m. a fiery one; Sandbox and puzzles earn none. A harder preset upgrades a badge you already have.
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { load } = require('./harness');
|
||||
|
||||
// The preset is read from S.rules.preset until the ruleset feature lands with rulePreset().
|
||||
const start = (ev, preset) => {
|
||||
ev("localStorage.removeItem('sot-badges');ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'})");
|
||||
if (preset) ev(`S.rules={preset:'${preset}'}`);
|
||||
ev("S.ach={};S.achP={};compute()");
|
||||
};
|
||||
const earnHello = (ev) => ev("S.pni.push({id:'x',net:'nfx',city:'fra',tier:0});compute();checkBadges()");
|
||||
const hof = (ev) => JSON.parse(ev("localStorage.getItem('sot-badges')||'{}'"));
|
||||
|
||||
test('every save without a preset plays On-call and earns the standard badge', () => {
|
||||
const { ev, errors } = load();
|
||||
start(ev);
|
||||
assert.equal(ev('badgeVar()'), 'oncall');
|
||||
earnHello(ev);
|
||||
assert.ok(ev('S.ach.hello!==undefined'));
|
||||
assert.equal(ev('S.achP.hello'), 'oncall');
|
||||
assert.deepEqual(hof(ev).hello.vs && Object.keys(hof(ev).hello.vs), ['oncall']);
|
||||
assert.equal(hof(ev).hello.v, 'oncall');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('Change Freeze earns frosted badges and BGP at 3 a.m. fiery ones, shown in the toast and the list', () => {
|
||||
for (const [p, name, cls] of [['freeze', /Freeze-dried/, 'v-freeze'], ['bgp', /Forged at 3 a\.m\./, 'v-bgp']]) {
|
||||
const { ev, dom, errors } = load();
|
||||
start(ev, p);
|
||||
ev("window.__t=[];const _t=toast;toast=(h,...a)=>{window.__t.push(h);return _t(h,...a);}");
|
||||
earnHello(ev);
|
||||
assert.equal(ev('S.achP.hello'), p);
|
||||
assert.match(ev("window.__t.join(' ')"), name, `${p}: the toast names the variant`);
|
||||
ev("UI.left='goals';UI.leftOpen=true;UI.sel=null;renderLeft()");
|
||||
const card = dom.window.document.querySelector(`#leftBody .bdg.on.${cls}`);
|
||||
assert.ok(card, `${p}: the badge list shows the variant`);
|
||||
assert.match(card.textContent, name);
|
||||
assert.equal(hof(ev).hello.v, p, 'remembered on this device');
|
||||
assert.deepEqual(errors, []);
|
||||
}
|
||||
});
|
||||
|
||||
test('Sandbox and puzzles earn no badges', () => {
|
||||
for (const p of ['sandbox', 'puzzle']) {
|
||||
const { ev, errors } = load();
|
||||
start(ev, p);
|
||||
earnHello(ev);
|
||||
assert.equal(ev('badgeVar()'), null);
|
||||
assert.equal(ev('Object.keys(S.ach).length'), 0, p);
|
||||
assert.deepEqual(errors, []);
|
||||
}
|
||||
const { ev, errors } = load();
|
||||
start(ev);
|
||||
ev("S.mode='sandbox'");
|
||||
earnHello(ev);
|
||||
assert.equal(ev('Object.keys(S.ach).length'), 0, 'the old sandbox mode too');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a harder preset upgrades a badge, an easier one never downgrades it', () => {
|
||||
const { ev, errors } = load();
|
||||
start(ev, 'freeze');
|
||||
earnHello(ev);
|
||||
const day = ev('S.ach.hello');
|
||||
ev("window.__t=[];const _t=toast;toast=(h,...a)=>{window.__t.push(h);return _t(h,...a);}");
|
||||
ev("S.rules.preset='oncall';checkBadges()");
|
||||
assert.equal(ev('S.achP.hello'), 'oncall', 'On-call beats Change Freeze');
|
||||
assert.match(ev("window.__t.join(' ')"), /Badge upgraded/);
|
||||
ev("S.day+=10;S.rules.preset='bgp';checkBadges()");
|
||||
assert.equal(ev('S.achP.hello'), 'bgp');
|
||||
assert.equal(ev('S.ach.hello'), day, 'the day it was first earned stays');
|
||||
ev("window.__t=[];S.rules.preset='freeze';checkBadges()");
|
||||
assert.equal(ev('S.achP.hello'), 'bgp', 'never downgraded');
|
||||
assert.equal(ev("window.__t.length"), 0, 'and no toast');
|
||||
const h = hof(ev).hello;
|
||||
assert.deepEqual(Object.keys(h.vs).sort(), ['bgp', 'freeze', 'oncall'], 'the collection holds every variant earned');
|
||||
assert.equal(h.v, 'bgp', 'the shelf shows the best one');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('the collection counts badges per variant on this device, and fits a phone', () => {
|
||||
const { ev, dom, errors } = load();
|
||||
ev('window.innerWidth=360');
|
||||
start(ev, 'bgp');
|
||||
earnHello(ev);
|
||||
ev("UI.left='goals';UI.leftOpen=true;UI.sel=null;renderLeft()");
|
||||
const shelf = dom.window.document.querySelector('#leftBody .bshelf');
|
||||
assert.ok(shelf);
|
||||
const total = ev('BADGES.length');
|
||||
assert.match(shelf.textContent, new RegExp(`Forged at 3 a\\.m\\. 1/${total}`));
|
||||
assert.match(shelf.textContent, new RegExp(`Freeze-dried 0/${total}`));
|
||||
assert.ok(dom.window.document.querySelector('#leftBody .bsh.v-bgp.cur'), 'the variant this game earns is marked');
|
||||
assert.match(dom.window.document.getElementById('leftBody').textContent, /forged in fire/);
|
||||
assert.deepEqual(JSON.parse(ev('JSON.stringify(badgeShelf(hof()))')), { freeze: 0, oncall: 0, bgp: 1 });
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('old saves and old halls of fame keep loading, as standard badges', () => {
|
||||
const { ev, errors } = load();
|
||||
start(ev);
|
||||
// a 1.x save: S.ach holds days, no S.achP; a 1.x hall of fame: {d, n} per badge
|
||||
ev("localStorage.setItem('sot-badges',JSON.stringify({hello:{d:'2025-01-02',n:'Old'},t1:{d:'2025-03-04',n:'Old'},junk:5}))");
|
||||
ev("const o=JSON.parse(JSON.stringify(S));o.ach={hello:12,t1:'400'};delete o.achP;S=migrate(o)");
|
||||
assert.equal(ev('S.achP.hello'), 'oncall');
|
||||
assert.equal(ev('S.ach.t1'), 400, 'days are numbers');
|
||||
assert.deepEqual(JSON.parse(ev("JSON.stringify(Object.keys(S.achP).sort())")), ['hello', 't1']);
|
||||
const h = JSON.parse(ev('JSON.stringify(hof())'));
|
||||
assert.equal(h.hello.v, 'oncall');
|
||||
assert.equal(h.hello.vs.oncall, '2025-01-02');
|
||||
assert.equal(h.junk, undefined, 'broken entries are dropped');
|
||||
// a hand-edited variant is not trusted
|
||||
ev("const o=JSON.parse(JSON.stringify(S));o.achP={hello:'lava',t1:'bgp'};S=migrate(o)");
|
||||
assert.equal(ev('S.achP.hello'), 'oncall');
|
||||
assert.equal(ev('S.achP.t1'), 'bgp');
|
||||
ev("localStorage.setItem('sot-badges','[1,2]')");
|
||||
assert.equal(ev('JSON.stringify(hof())'), '{}');
|
||||
ev("UI.left='goals';UI.leftOpen=true;renderLeft()");
|
||||
// new games start with an empty variant map
|
||||
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip()");
|
||||
assert.equal(ev('JSON.stringify(S.achP)'), '{}');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('the share card rings the variant badges', () => {
|
||||
const { ev, errors } = load();
|
||||
start(ev, 'bgp');
|
||||
earnHello(ev);
|
||||
// count the strokes the card draws with a variant badge and with the same badge in its standard look
|
||||
const calls = () => ev("(()=>{const cv=shareCanvas();return cv.getContext('2d').calls.n;})()");
|
||||
const fiery = calls();
|
||||
ev("S.achP.hello='oncall'");
|
||||
const plain = calls();
|
||||
assert.ok(fiery > plain, `the ring and the tally are drawn (${fiery} > ${plain} calls)`);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
@ -6,7 +6,7 @@ module.exports = `
|
|||
if(f.rem<=1e-6||(rank===0&&!f.cache)||(pass&&!pass(f)))continue;
|
||||
Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++;
|
||||
const candidates=E.filter(e=>e.used<e.cap&&e.ok(f.n)&&
|
||||
(TR?eff(e,f.n):base(e))===rank&&Dv[CIDX[e.city]]<Infinity)
|
||||
(TR?eff(e,f.n):base(e,f.n))===rank&&Dv[CIDX[e.city]]<Infinity)
|
||||
.sort((a,b)=>(a.pref==='high'?0:1)-(b.pref==='high'?0:1)||Dv[CIDX[a.city]]-Dv[CIDX[b.city]]);
|
||||
for(let i=0;i<candidates.length&&f.rem>1e-6;){
|
||||
let j=i+1;while(j<candidates.length&&(candidates[j].pref==='high')===(candidates[i].pref==='high')&&
|
||||
|
|
|
|||
148
tests/tier1-cone.test.js
Normal file
148
tests/tier1-cone.test.js
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
// Tier 1 by customer cone (#85): peering with every Tier 1 reaches their customers and their customers' customers,
|
||||
// so the long tail stops mattering for the win; networks in nobody's cone still need peering of their own.
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { load } = require('./harness');
|
||||
|
||||
// A transit-free network in Frankfurt with a PNI to every Tier 1 it needs.
|
||||
const t1Club = (ev) => {
|
||||
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.cash=1e9;S.lvl=6;for(const g of GOALS)S.done[g.id]=0;delete S.done.t1;ACT.launchBB({city:'fra'});S.pops.fra.bb.d=500");
|
||||
ev("S.transit=[];S.ixp={};S.peers={};S.pni=[];S.grace=null;for(const n of t1Need())S.pni.push({id:'p'+n.id,net:n.id,city:'fra',tier:2});compute()");
|
||||
};
|
||||
|
||||
test('cones are built once at load and hold customers of customers', () => {
|
||||
const { ev, errors } = load();
|
||||
assert.equal(ev("CONE.voc.includes('ddo')&&CONE.voc.includes('spn')"), true, 'Hocus Pocus brings its own customers');
|
||||
// Doodoo buys from Hocus Pocus, which buys from Not That Telecom: two levels down
|
||||
assert.equal(ev("NET.ddo.up.includes('voc')&&NET.voc.up.includes('ntt')"), true);
|
||||
assert.equal(ev("CONE_S.ntt.has('ddo')"), true, 'the Tier 1 cone is transitive');
|
||||
assert.equal(ev("CONE.ntt[0]"), 'ntt', 'a network is first in its own cone');
|
||||
assert.equal(ev("PNI_NETS===CONE"), true);
|
||||
assert.equal(ev("ANC.ddo[0]"), 'voc', 'nearest upstream first');
|
||||
assert.equal(ev("NETS.filter(n=>n.tail).every(n=>n.up&&n.up.length&&n.up.every(u=>NET[u]))"), true, 'the long tail buys transit');
|
||||
assert.equal(ev("NETS.filter(n=>n.tail).every(n=>ANC[n.id].some(u=>NET[u].type==='t1'))"), true, 'from a Tier 1, directly or through a carrier');
|
||||
assert.equal(ev("NET.tail45.up.join()"), 'mag', 'Auckland has no Tier 1: Hobbit Hole buys from a carrier that is there');
|
||||
assert.equal(ev("NETS.filter(n=>n.tail).map(n=>n.up.join('+')).join()"), ev("NETS.filter(n=>n.tail).map(n=>tailUp(n.name,n.pops).join('+')).join()"), 'stable: picked by name');
|
||||
// Australians buy only from Australian carriers, so a Tier 1 PNI is still no back door into Australia.
|
||||
assert.equal(ev("['opt','tpr','abb','npt','lpt','lnt','pnt','stb','blg'].some(id=>t1Need().some(t=>CONE_S[t.id].has(id)))"), false);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a transit-free network with PNIs to every Tier 1 reaches the long tail through their cones', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
const tail = ev("NETS.find(n=>n.tail&&n.pops.includes('akl')).id");
|
||||
for (const id of ['tail0', 'tail38', tail, 'sky', 'kys', 'tim']) {
|
||||
assert.equal(ev(`!!R.reachable.${id}`), true, `${id} is reachable`);
|
||||
assert.equal(ev(`!!R.nt.${id}`), true, `${id} without transit`);
|
||||
assert.equal(ev(`!!R.cone.${id}`), true, `${id} through a peer's cone`);
|
||||
}
|
||||
// the traffic really goes over the PNI with the upstream, not some made-up exit
|
||||
const up = ev("NET.tail0.up[0]");
|
||||
assert.ok(ev("(R.netEg.tail0||{}).p") > 0, 'over a PNI');
|
||||
assert.ok(ev(`R.usePni['p${up}']`) > 0, 'the PNI with its upstream carries it');
|
||||
// the traceroute shows the hand-off to the customer and an AS path one hop longer
|
||||
const tr = JSON.parse(ev("JSON.stringify(traceroute('fra',{net:'tail0'}))"));
|
||||
assert.match(tr.text, /hands it to its customer/);
|
||||
assert.equal(tr.asPath.length, 3, 'you, the Tier 1, the customer');
|
||||
assert.equal(tr.asPath[1], ev(`NET.${up}.asn`), 'through the upstream it buys transit from');
|
||||
assert.match(tr.why, /customers come after direct peers/);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a network in nobody\'s cone still needs reaching directly', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
// Doitsche Telekomm buys from no one: no Tier 1 PNI reaches it
|
||||
assert.equal(ev("(NET.dtk.up||[]).length"), 0);
|
||||
assert.equal(ev("R.reachable.dtk"), false);
|
||||
assert.equal(ev("isTier1()"), false);
|
||||
assert.match(ev('t1Why()'), /nobody you peer with sells transit to/);
|
||||
// peer with every island directly and the win comes
|
||||
ev("for(const n of NETS)if(!R.reachable[n.id])S.pni.push({id:'i'+n.id,net:n.id,city:n.pops[0],tier:0});for(const n of NETS)if(n.pops.length&&!S.pops[n.pops[0]])S.pops[n.pops[0]]={day:0,bb:null,boost:0,down:0,routers:1,gen:false};compute()");
|
||||
assert.ok(ev("R.reach") > 0.999, `reach ${ev('R.reach')}`);
|
||||
assert.equal(ev("isTier1()"), true);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('the long tail no longer stands between you and Tier 1', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
// peer directly only with the networks in nobody's cone, never with the long tail itself
|
||||
ev("for(const n of NETS)if(!R.reachable[n.id]){if(!S.pops[n.pops[0]])S.pops[n.pops[0]]={day:0,bb:null,boost:0,down:0,routers:1,gen:false};S.pni.push({id:'i'+n.id,net:n.id,city:n.pops[0],tier:0});}compute()");
|
||||
assert.equal(ev("S.pni.some(p=>NET[p.net].tail)"), false, 'no session with any long-tail network');
|
||||
assert.equal(ev("NETS.filter(n=>n.tail).every(n=>R.reachable[n.id])"), true);
|
||||
assert.equal(ev("isTier1()"), true);
|
||||
// the peers you need directly are all real networks of some size, and fewer than before
|
||||
const islands = ev("S.pni.filter(p=>p.id[0]==='i').length");
|
||||
assert.ok(islands < 70, `${islands} networks to peer with directly`);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a peer\'s cone comes after direct peering and before transit, so peering widely still pays', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
ev("ACT.buyTransit({city:'fra',net:'cog',tier:2});S.pni=S.pni.filter(p=>p.net!=='cog');compute()");
|
||||
// a Cogentle customer is reached through another Tier 1's cone only if it buys from one; transit is the fallback
|
||||
const id = ev("NETS.find(n=>n.tail&&n.up.length===1&&n.up[0]==='tly').id");
|
||||
assert.ok(ev(`(R.netEg.${id}||{}).p`) > 0, 'over the PNI with its upstream, not over transit');
|
||||
assert.equal(ev(`(R.netEg.${id}||{}).t||0`), 0);
|
||||
// a direct session beats the cone: peer with it at the exchange and its traffic moves there
|
||||
ev(`S.pops.fra.bb.d=500;ACT.buyIx({city:'fra',tier:1,signed:true});ACT.rs({city:'fra'});compute()`);
|
||||
if (ev(`ixSessions('fra').has('${id}')`)) {
|
||||
assert.ok(ev(`(R.netEg.${id}||{}).ix`) > 0, 'direct peering wins over the cone');
|
||||
assert.equal(ev(`(R.netEg.${id}||{}).p||0`), 0);
|
||||
}
|
||||
// an IX session also brings the member's customers along
|
||||
assert.equal(ev("ixSessions('fra').has('gsl')"), true);
|
||||
const down = ev("DOWN.gsl.find(id=>!t1Need().some(t=>CONE_S[t.id].has(id))&&!ixSessions('fra').has(id))||''");
|
||||
if (down) assert.equal(ev(`R.reachable.${down}`), true, `${down} comes along with Glowball Sakura Lager's session`);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('old saves keep their win: cones only ever add reach', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
ev("for(const n of NETS)if(!R.reachable[n.id]){if(!S.pops[n.pops[0]])S.pops[n.pops[0]]={day:0,bb:null,boost:0,down:0,routers:1,gen:false};S.pni.push({id:'i'+n.id,net:n.id,city:n.pops[0],tier:0});}compute()");
|
||||
ev("S.won=true;S.done.t1=S.day;S.t1Now=true");
|
||||
// a save from before 2.0, with an older Tier 1 set and no record of cones, loads and is still Tier 1
|
||||
ev("const o=JSON.parse(JSON.stringify(S));o.t1v=2;S=migrate(o);compute()");
|
||||
assert.equal(ev("S.won"), true);
|
||||
assert.equal(ev("isTier1()"), true);
|
||||
assert.equal(ev("tierLabel()"), 'Tier 1');
|
||||
// and reach with cones is never lower than reach without: every exit still reaches what it did
|
||||
const before = ev("R.reach");
|
||||
ev("for(const id of NET_IDS){CONE_S[id]=new Set([id]);DOWN[id]=[];}compute()");
|
||||
assert.ok(ev("R.reach") <= before + 1e-9, 'switching cones off can only lose reach');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('the Road to Tier 1 card and the network list explain the cone, on a phone too', () => {
|
||||
for (const w of [1280, 360]) {
|
||||
const { ev, dom, errors } = load();
|
||||
ev(`window.innerWidth=${w}`);
|
||||
t1Club(ev);
|
||||
ev("UI.left='goals';UI.leftOpen=true;UI.sel=null;renderLeft()");
|
||||
const txt = dom.window.document.getElementById('leftBody').textContent;
|
||||
assert.match(txt, /Road to Tier 1/);
|
||||
assert.match(txt, /their customers come along for free/);
|
||||
assert.match(txt, /\d+ networks already come along with a peer/);
|
||||
ev("ACT.netFocus({id:'tail0'})");
|
||||
assert.match(dom.window.document.getElementById('leftBody').textContent, /Reached through the customer cone of/);
|
||||
ev("S.pni=[];compute();ACT.netFocus({id:'tail0'})");
|
||||
assert.match(dom.window.document.getElementById('leftBody').textContent, /Buys transit from .*Peer with/);
|
||||
assert.match(ev("GOALS.find(g=>g.id==='t1').d"), /customers come along for free/);
|
||||
assert.deepEqual(errors, [], `${w}px`);
|
||||
}
|
||||
});
|
||||
|
||||
test('routing with cones costs about what it did: exits are asked once, cones are built at load', () => {
|
||||
const { ev, errors } = load();
|
||||
t1Club(ev);
|
||||
ev("for(const n of NETS.filter(n=>!n.tail&&!n.cn).slice(0,40))for(const c of ['fra','ams','lon']){if(!S.pops[c])S.pops[c]={day:0,bb:{d:400},boost:0,down:0,routers:4,gen:true};S.pni.push({id:n.id+c,net:n.id,city:c,tier:2});}compute()");
|
||||
const t = ev("(()=>{for(let i=0;i<3;i++)compute();const a=performance.now();for(let i=0;i<10;i++)compute();return (performance.now()-a)/10;})()");
|
||||
const plain = ev(`(()=>{const keep={};for(const id of NET_IDS){keep[id]=[CONE_S[id],DOWN[id]];CONE_S[id]=new Set([id]);DOWN[id]=[];}
|
||||
for(let i=0;i<3;i++)compute();const a=performance.now();for(let i=0;i<10;i++)compute();const r=(performance.now()-a)/10;for(const id of NET_IDS){CONE_S[id]=keep[id][0];DOWN[id]=keep[id][1];}return r;})()`);
|
||||
assert.ok(t < plain * 2 + 20, `compute() with cones ${t.toFixed(1)} ms vs without ${plain.toFixed(1)} ms`);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue