diff --git a/scripts/lib/scenario-ref.js b/scripts/lib/scenario-ref.js index 2733491..6164e8f 100644 --- a/scripts/lib/scenario-ref.js +++ b/scripts/lib/scenario-ref.js @@ -9,6 +9,7 @@ // ['hire', orders, params] the NOC intern on standing orders, as soon as the level allows // ['wait', day] until that day ['served', g] until g Gbps are served ['prep', c, g] capacity for an event // ['scrub', c, tier] a scrubbing center ['pnis'] a PNI with every unreached network that takes one +// ['pni', c, net, tier] one PNI with that network there ['last', c] transit there only as a last resort // ['loans'] take every loan the banks offer, from now on (the cheese plans) const REF = String.raw`(()=>{ const Q=window.__ref={log:[]}; @@ -19,6 +20,7 @@ Q.cheap=c=>NETS.filter(n=>n.price&&sellsAt(n,c)&&!(Q.avoid||[]).includes(n.id)&& Q.pop=c=>{if(!S.pops[c])ACT.buildPop({city:c});return !!S.pops[c];}; Q.bb=c=>{Q.pop(c);if(S.pops[c]&&!S.pops[c].bb)ACT.launchBB({city:c});return !!(S.pops[c]&&S.pops[c].bb);}; Q.tr=(c,tier=1)=>{Q.pop(c);const n=Q.cheap(c);if(!n)return true;ACT.buyTransit({city:c,net:n.id,tier});return true;}; +Q.last=c=>{for(const t of S.transit)if(t.city===c)ACT.setPref({kind:'t',id:t.id,v:'low'});return true;}; Q.ix=(c,tier=1)=>{if(!S.pops[c])return false;for(const x of ixIdsIn(c)){if(!ixName(x))continue;if(S.ixp[x]===undefined)ACT.buyIx({city:x,tier,signed:true});if(S.ixp[x]===undefined)continue;if(!S.rs[x])ACT.rs({city:x});ACT.peerAll({city:x});}return true;}; // A wave: big waves need big routers at both ends, and a router swap takes a day, so the step waits (null) until they fit. Q.link=(a,b,tier=1,via)=>{for(const c of [a,b])Q.pop(c);if(!S.pops[a]||!S.pops[b])return false; @@ -28,6 +30,7 @@ Q.hire=(orders,par)=>{Q.wantHire=[orders||['transit','routers','repair'],par||{} Q.hire0=(orders,par)=>{if(!opsCanHire()||!Q.can(20000))return false;if(!opsHired()){ACT.opsHire({id:'kevin'});closeModal();}if(!opsHired())return false; for(const k of orders)if(!S.ops.on[k])ACT.opsToggle({k});for(const k in par)for(const [p,v] of Object.entries(par[k]))for(let i=0;i<8&&opsP(k,p)!==v;i++)ACT.opsParam({k,p});closeModal();return true;}; Q.pnis=(tier=0)=>{for(const n of NETS.filter(n=>!R.reachable[n.id]).sort((a,b)=>NSH[b.id]-NSH[a.id])){const b=pniBatch(n.id,tier);if(!b||!b.orders.length||!Q.can(b.cost))continue;ACT.pniAll({net:n.id,tier});}return true;}; +Q.pni=(c,net,tier=0)=>{if(!S.pni.some(p=>p.city===c&&p.net===net))ACT.pni({city:c,net,tier});closeModal();return S.pni.some(p=>p.city===c&&p.net===net);}; Q.scrub=(c,tier=0)=>{ACT.buildScrub({city:c,tier});return true;}; Q.loans=()=>{Q.loan=true;return true;}; Q.served=g=>R.served>=g||null; @@ -38,7 +41,7 @@ Q.prep=(c,g)=>{const need=(S.stats.peak||R.served||0)*1.2+g*1.15,cap=()=>S.trans if(t.tier=need/4&&portOk(c,t.tier+1))ACT.buyTransit({city:c,net:t.net,tier:t.tier+1});else ACT.lagT({id:t.id,d:1}); if(cap()===c0){if(t.tier=RMAX)break;const r0=p.routers||1;ACT.addRouter({city:c});if((p.routers||1)===r0)break;}return true;}; -Q.est={pop:c=>S.pops[c]?0:popCost(c),bb:c=>(S.pops[c]?0:popCost(c))+bbCost(c),tr:()=>20000,ix:()=>25000,hire:()=>0,scrub:(c,t)=>SCRUB_TIERS[t||0].cost, +Q.est={pop:c=>S.pops[c]?0:popCost(c),bb:c=>(S.pops[c]?0:popCost(c))+bbCost(c),tr:()=>20000,pni:()=>20000,ix:()=>25000,hire:()=>0,scrub:(c,t)=>SCRUB_TIERS[t||0].cost, link:(a,b,t)=>(S.pops[a]?0:popCost(a))+(S.pops[b]?0:popCost(b))+waveMo(kmBetween(a,b),isSub(a,b),t)*2+20000}; Q.upkeep=()=>{ for(const l of S.links){if(l.kind!=='wave'||l.tier>=LT.length-1||linkDown(l)||(R.load[l.id]||0) ev(`beginGame('T','icn',{scn:SCN.find(x=>x.id==='gangnam')});if(UI.pendGame)ACT.pitchSkip();closeModal();S.cash=1e8`); +const seoul = "ACT.launchBB({city:'icn'});ACT.buyTransit({city:'icn',net:'cog',tier:1});compute();for(let i=0;i<20;i++)dayTick();compute()"; + +test('sender pays: fee on Seoul transit, incumbents only over a paid PNI, cheap broadband', () => { + const { ev, errors } = load(); + start(ev); + ev(seoul); + assert.ok(ev("scnRule('sender')")); + assert.equal(ev("R.reachable.ktt||R.reachable.skb||R.reachable.lgu"), false, 'transit does not carry the incumbents'); + assert.ok(ev('R.reach') < 0.99); + const t = JSON.parse(ev("(()=>{const t=S.transit[0],q=R.p95[t.id];return JSON.stringify({fee:krFee(t,q),q,mo:transitMo(t,q)})})()")); + assert.ok(t.q > 0 && t.fee === Math.round(t.q * ev('KR_FEE')), 'a sender-pays fee per Gbps of transit'); + assert.ok(ev("Math.abs(R.rev.eyeballs-R.cityEye.icn*sfOf(R,'icn')*EYE_RATE*KR_ARPU)<1")); + ev("ACT.pni({city:'icn',net:'ktt',tier:0});closeModal();for(let i=0;i<90&&!S.pni.every(pniOn);i++)dayTick();compute()"); + assert.ok(ev('R.reachable.ktt') && ev('R.reach') >= 0.99, 'a paid PNI with Kay-Tea brings the reach'); + assert.match(ev('scnCard()'), /Seoul served well/); + assert.match(ev('scnCard()'), /Monthly profit/); + assert.deepEqual(errors, []); +}); + +test('sender pays: Korean users mind a haul to Tokyo even over peering', () => { + const { ev, errors } = load(); + start(ev); + ev("ACT.launchBB({city:'icn'});UI.via=null;UI.route=null;openLinkModal('icn','tok');ACT.buildLink({tier:1});closeModal();for(const x of ixIdsIn('tok')){ACT.buyIx({city:x,tier:1,signed:true});ACT.rs({city:x});}compute()"); + assert.ok(ev('R.egT.ix') > 0 && !ev('R.egT.t'), 'only peering in Tokyo'); + assert.ok(ev('R.latF.icn') < 0.95, 'the haul costs service'); + assert.match(ev("latNote('icn')"), /Korean users notice/); + assert.deepEqual(errors, []); +}); + +test('Korea outside the scenario keeps its usual prices', () => { + const { ev, errors } = load(); + ev("UI.startCity='icn';ACT.start({});closeModal();S.cash=1e8"); + ev(seoul); + assert.ok(!ev("scnRule('sender')")); + assert.ok(ev('R.reachable.ktt'), 'transit reaches the incumbents'); + assert.equal(ev("krFee(S.transit[0],100)"), 0); + assert.ok(ev("Math.abs(R.rev.eyeballs-R.cityEye.icn*sfOf(R,'icn')*EYE_RATE)<1")); + assert.deepEqual(errors, []); +}); + +test('an older Gangnam save picks up the sender-pays rule', () => { + const { ev, errors } = load(); + start(ev); + const s = ev("(()=>{const o=JSON.parse(JSON.stringify(S));o.scn.rules={};return JSON.stringify(o);})()"); + ev(`S=migrate(JSON.parse(${JSON.stringify(s)}))`); + assert.ok(ev("S.scn.rules.sender===true")); + assert.deepEqual(errors, []); +}); diff --git a/tests/scenarios-playtest.test.js b/tests/scenarios-playtest.test.js index 47cd222..663fb11 100644 --- a/tests/scenarios-playtest.test.js +++ b/tests/scenarios-playtest.test.js @@ -9,6 +9,8 @@ const { play } = require('../scripts/lib/scenario-ref'); const HIRE = ['hire', ['transit', 'routers', 'repair', 'sales', 'ix']]; const home = (c) => [['bb', c], ['tr', c, 1], ['ix', c]]; +const GN_H = ['hire', ['transit', 'routers', 'repair', 'sales', 'ix', 'caches', 'pni']]; +const GN_REF = [['bb', 'icn'], ['ix', 'icn'], ['tr', 'icn', 1], ['pni', 'icn', 'ktt', 0], GN_H, ['served', 120], ['link', 'icn', 'tok', 1], ['tr', 'tok', 1], ['ix', 'tok'], ['last', 'icn'], ['pop', 'hkg']]; // ref: the reference plan; win: [earliest, latest] day it may win on; cheese: the shortcut, and `by`, the day before // which it must not win (null: never within the scenario's days). const PLAY = { @@ -25,8 +27,9 @@ const PLAY = { win: [70, 200], cheese: [...home('cpt'), ['link', 'cpt', 'cai', 0], ['pop', 'lag'], ['pop', 'nbo'], ['hire']], by: null }, ixbr: { ref: [...home('sao'), ...[['sao', 'rio', 1], ['sao', 'cpq', 0], ['sao', 'cwb', 0], ['cwb', 'poa', 0], ['rio', 'bhz', 0], ['bhz', 'bsb', 0], ['bsb', 'gyn', 0], ['rio', 'ssa', 0], ['ssa', 'rec', 0], ['rec', 'for', 0], ['rec', 'nat', 0], ['for', 'bel', 0], ['bel', 'mao', 0], ['bsb', 'cgb', 0], ['sao', 'vix', 0]].flatMap(([a, b, t]) => [['link', a, b, t], ['ix', b, 0]]), ['bb', 'rio'], ['tr', 'rio', 1], ['bb', 'bsb'], ['tr', 'bsb', 1], ['bb', 'for'], ['tr', 'for', 1], HIRE], win: [40, 120], cheese: [['bb', 'sao'], ['tr', 'sao', 1], ...['sao', 'rio', 'bsb', 'bhz', 'cwb', 'poa', 'for', 'rec', 'ssa', 'cpq', 'gyn', 'bel', 'mao', 'nat', 'vix', 'cgb'].flatMap((c) => [['pop', c], ['ix', c, 0]]), ['hire']], by: null }, - // Seoul at home is enough here: the cheese is just playing, so it only must not be instant. - gangnam: { ref: [...home('icn'), ['link', 'icn', 'tok', 1], ['ix', 'tok'], ['tr', 'tok', 1], ['link', 'icn', 'hkg', 1], ['ix', 'hkg'], ['tr', 'hkg', 1], HIRE], win: [50, 120], cheese: [...home('icn'), ['loans']], by: 50 }, + // Sender pays: Seoul transit to start (dear, but fast), one paid PNI with Kay-Tea for reach, then haul the bulk from + // Tokyo and keep Seoul transit as the last resort. Seoul alone loses money on the sender-pays fee and never wins. + gangnam: { ref: GN_REF, win: [130, 190], cheese: [...home('icn'), ['loans']], by: null }, // Bigger routers bring Flowspec, which also takes floods: without a scrubbing center it takes twice as long. flood: { ref: [...home('ams'), ['scrub', 'ams', 1], ['tr', 'ams', 1], HIRE], win: [80, 200], cheese: [...home('ams'), ['tr', 'ams', 1], ['loans']], by: 180 }, fttb: { ref: [...home('syd'), ['link', 'syd', 'akl', 0, 'tasman'], ['link', 'syd', 'nou', 0, 'gond'], ...['mel', 'bne', 'adl', 'per'].flatMap((c) => [['bb', c], ['tr', c, 1]]), ['link', 'syd', 'mel', 1], ['link', 'syd', 'bne', 1], ['link', 'mel', 'adl', 1], ['link', 'adl', 'per', 0], HIRE], @@ -58,3 +61,20 @@ for (const [id, P] of Object.entries(PLAY)) { assert.deepEqual(g.errors, []); }); } + +// Gangnam Style Peering bites every way (2.0.0 playtest): paying the sender-pays fee on everything loses money, hauling +// everything from Tokyo is too slow for Korean users, and the old plan (transit everywhere, no Kay-Tea) has neither +// the reach nor the profit. The reference's Seoul transit as first choice, not last resort, also stays in the red. +for (const [name, plan] of Object.entries({ + 'Seoul transit and Kay-Tea': [['bb', 'icn'], ['ix', 'icn'], ['tr', 'icn', 1], ['pni', 'icn', 'ktt', 0], GN_H], + 'everything from Tokyo': [['bb', 'icn'], ['link', 'icn', 'tok', 1], ['tr', 'tok', 1], ['ix', 'tok'], ['pni', 'tok', 'ktt', 0], ['pop', 'hkg'], GN_H], + 'the 1.x reference': [...home('icn'), ['link', 'icn', 'tok', 1], ['ix', 'tok'], ['tr', 'tok', 1], ['link', 'icn', 'hkg', 1], ['ix', 'hkg'], ['tr', 'hkg', 1], HIRE], + 'Tokyo haul, Seoul transit first': GN_REF.filter((s) => s[0] !== 'last'), +})) { + test(`gangnam: ${name} does not win`, () => { + const g = load(); + const r = play(g.ev, 'gangnam', plan); + assert.equal(r.won, null, `${name} won on day ${r.won}`); + assert.deepEqual(g.errors, []); + }); +}