Architect runs: a generated run every week
scripts/weekly-architect.js picks an HQ in a region the last weeks did not have and designs each brief against the network a reference architect has built so far, so every brief asks for something new; the game's own checklist and chaos monkey review judge it, and the whole run is replayed from the file for three stars. The game fetches architect/weekly/<key>.json when Puzzles opens, checks it field by field and writes the briefs' text itself. The Sunday job and the release deploy carry it like the weekly puzzles and scenarios. Later briefs' cities no longer show early.
This commit is contained in:
parent
601f7028bd
commit
4e6e8294d2
21 changed files with 461 additions and 17 deletions
|
|
@ -41,17 +41,17 @@ const AR_RUNS=[
|
|||
]},
|
||||
];
|
||||
const arOn=()=>pzOn()&&!!S.ar;
|
||||
const arRunDef=id=>AR_RUNS.find(r=>r.id===id)||null;
|
||||
const arRunDef=id=>AR_RUNS.find(r=>r.id===id)||arWkRun(id);
|
||||
const arRun=()=>arRunDef(S.ar.id);
|
||||
const arBrief=()=>arRun().briefs[S.ar.bi];
|
||||
const arLast=()=>S.ar.bi>=arRun().briefs.length-1;
|
||||
// ---------- progress: best stars, spend and briefs cleared per run, under its own key ----------
|
||||
function arLoad(){let j=null;try{j=JSON.parse(localStorage.getItem(AR_KEY)||'{}');}catch(e){}const o={runs:{}};
|
||||
if(j&&j.runs&&typeof j.runs==='object')for(const k of Object.keys(j.runs)){const r=j.runs[k];if(!arRunDef(k)||!r||typeof r!=='object')continue;
|
||||
o.runs[k]={st:int(r.st,0,3,0),br:int(r.br,0,99,0),sp:r.sp==null||!isFinite(+r.sp)||+r.sp<0?null:Math.round(+r.sp)};}
|
||||
if(j&&j.runs&&typeof j.runs==='object')for(const k of Object.keys(j.runs)){const r=j.runs[k];if((!arRunDef(k)&&!AR_WK_ID.test(k))||!r||typeof r!=='object')continue;
|
||||
o.runs[k]={st:int(r.st,0,3,0),br:int(r.br,0,99,0),sp:r.sp==null||!isFinite(+r.sp)||+r.sp<0?null:Math.round(+r.sp)};if(AR_WK_ID.test(k))o.runs[k].rv=int(r.rv,1,999,1);}
|
||||
return o;}
|
||||
function arStore(p){try{localStorage.setItem(AR_KEY,JSON.stringify(p));}catch(e){}}
|
||||
function arRecord(st,br,sp){const p=arLoad(),r=p.runs[S.ar.id]||{st:0,br:0,sp:null};
|
||||
function arRecord(st,br,sp){const p=arLoad(),run=arRun(),old=p.runs[S.ar.id],r=old&&(!run.wk||(old.rv||1)===run.rev)?old:{st:0,br:0,sp:null};if(run.wk)r.rv=run.rev;
|
||||
if(st>r.st||(st===r.st&&st>0&&(r.sp==null||sp<r.sp))){r.st=st;r.sp=st?sp:r.sp;}r.br=Math.max(r.br,br);p.runs[S.ar.id]=r;arStore(p);}
|
||||
// ---------- money ----------
|
||||
// What a POP or a link costs for the year, as the puzzles count it.
|
||||
|
|
@ -121,7 +121,8 @@ function arApply(i){const A=S.ar,P=S.pz,b=arRun().briefs[i];A.bi=i;
|
|||
for(const c of b.gold||[])if(!A.gold.includes(c))A.gold.push(c);
|
||||
if(b.ring)A.rings.push(b.ring.slice());Object.assign(A.dem,b.dem||{});
|
||||
if(b.lat)A.lat=b.lat.slice();A.passed=null;
|
||||
P.via=[...new Set(arRun().briefs.slice(i+1).flatMap(x=>[...(x.add||[]),...(x.gold||[]),...(x.ring||[])]))].filter(c=>!P.cities.includes(c)&&!P.avoid.includes(c));
|
||||
// Later briefs stay a surprise: their cities do not show until they come (Show all cities shows everything).
|
||||
P.via=(arRun().via||[]).filter(c=>!P.cities.includes(c)&&!P.avoid.includes(c));
|
||||
arDemand();A.start=arSnapStr();UI.pzHist=[];UI.sel=null;UI.arFail=null;
|
||||
compute();pzCam();renderAll();
|
||||
toast(`<b>${tr('architect.brief_n_of_m',{n:i+1,m:arRun().briefs.length})}</b><br>${esc(b.t)}`,'goal');sfx('click');}
|
||||
|
|
|
|||
59
src/game/60-features/98-architect-weekly.js
Normal file
59
src/game/60-features/98-architect-weekly.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* ---------- the published weekly Architect run ----------
|
||||
Every Sunday scripts/weekly-architect.js publishes architect/weekly/<key>.json next to the page: an HQ, a budget and
|
||||
the briefs, as data. Fetched only when Puzzles opens, checked field by field, and played like the handmade runs
|
||||
(AR_RUNS) under the id 'wk-<key>'. A brief's text is written here from what it asks for, in the player's language, so
|
||||
the file can never say one thing and want another. When the file is missing, offline or wrong there is simply no
|
||||
weekly run that week. */
|
||||
const AR_WK_V=1,AR_WK_FIELDS=['v','key','hq','budget','name','joke','briefs','ref','rev'],AR_WK_BRIEF=['add','gold','ring','drop','dem','lat','tubes'];
|
||||
// The labels a file may name (N_: kept whole in the shipped page); their only placeholder is {hq}, a city.
|
||||
const AR_WK_NAMES=[N_('architect_weekly.name_all_roads_lead_to_hq'),N_('architect_weekly.name_the_hq_job'),N_('architect_weekly.name_hq_and_friends')];
|
||||
const AR_WK_JOKES=[N_('architect_weekly.joke_hq_signed_the_lease'),N_('architect_weekly.joke_one_network_a_handful')];
|
||||
const arWkId=key=>'wk-'+key,AR_WK_ID=/^wk-\d{4}-W\d{2}$/;
|
||||
function arWkTxt(o,list){if(!pzObj(o)||!list.includes(o.l))return null;const p=o.p==null?{}:o.p;if(!pzObj(p))return null;
|
||||
const need=[...new Set([...String(EN[o.l]).matchAll(/\{(\w+)\}/g)].map(m=>m[1]))];
|
||||
if(Object.keys(p).length!==need.length||!need.every(k=>k==='hq'&&pzCityOk(p[k])))return null;return {l:o.l,p:Object.assign({},p)};}
|
||||
const arWkSay1=o=>tr(o.l,o.p.hq?{hq:CITY[o.p.hq].name}:{});
|
||||
// What a brief says, built from what it wants.
|
||||
function arWkSay(b){const n=c=>CITY[c].name,t=[];
|
||||
if(b.drop)t.push(tr('architect_weekly.t_drop',{a:n(b.drop[0])}));
|
||||
const ag=b.add&&b.gold&&b.add.length===1&&b.gold.length===1&&b.add[0]===b.gold[0];
|
||||
if(ag)t.push(tr('architect_weekly.t_add_gold',{a:n(b.add[0])}));
|
||||
else if(b.add)t.push(b.add.length>1?tr('architect_weekly.t_add_two',{a:n(b.add[0]),b:n(b.add[1])}):tr('architect_weekly.t_add_one',{a:n(b.add[0])}));
|
||||
if(b.gold&&!ag)t.push(b.gold.length>1?tr('architect_weekly.t_gold_two',{a:n(b.gold[0]),b:n(b.gold[1])}):tr('architect_weekly.t_gold_one',{a:n(b.gold[0])}));
|
||||
if(b.ring)t.push(tr('architect_weekly.t_ring',{list:b.ring.map(n).join(', ')}));
|
||||
if(b.lat)t.push(tr('architect_weekly.t_lat',{a:n(b.lat[0]),b:n(b.lat[1]),ms:+b.lat[2].toFixed(1)}));
|
||||
return t.join(' ');}
|
||||
// The briefs, in order: a site exists from the brief that adds it until one drops it; every brief asks for something.
|
||||
function arWkBriefs(a,hq){if(!Array.isArray(a)||a.length<3||a.length>7)return null;const sites=new Set([hq]),gone=new Set(),out=[];
|
||||
const ids=(x,lo,hi)=>x==null?[]:Array.isArray(x)&&x.length>=lo&&x.length<=hi&&x.every(pzCityOk)&&new Set(x).size===x.length?x.slice():null;
|
||||
for(const b of a){if(!pzObj(b)||Object.keys(b).some(k=>!AR_WK_BRIEF.includes(k)))return null;
|
||||
const drop=ids(b.drop,1,1),add=ids(b.add,1,2),gold=ids(b.gold,1,2),ring=b.ring==null?[]:ids(b.ring,3,4);if(!drop||!add||!gold||!ring)return null;
|
||||
for(const c of drop){if(c===hq||!sites.has(c))return null;sites.delete(c);gone.add(c);}
|
||||
for(const c of add){if(sites.has(c)||gone.has(c))return null;sites.add(c);}
|
||||
if(![...gold,...ring].every(c=>sites.has(c))||gold.includes(hq))return null;
|
||||
const dem={};if(b.dem!=null){if(!pzObj(b.dem))return null;for(const c of Object.keys(b.dem)){const g=b.dem[c];if(!sites.has(c)||c===hq||!Number.isInteger(g)||g<10||g>400)return null;dem[c]=g;}}
|
||||
let lat=null;if(b.lat!=null){const l=b.lat;if(!Array.isArray(l)||l.length!==3||!sites.has(l[0])||!sites.has(l[1])||l[0]===l[1]||typeof l[2]!=='number'||!isFinite(l[2])||l[2]<1||l[2]>300)return null;lat=l.slice();}
|
||||
if(!Number.isInteger(b.tubes)||b.tubes<0||b.tubes>9)return null;
|
||||
if(!add.length&&!gold.length&&!ring.length&&!lat)return null;
|
||||
const o={tubes:b.tubes};if(add.length)o.add=add;if(gold.length)o.gold=gold;if(ring.length)o.ring=ring;if(drop.length)o.drop=drop;if(Object.keys(dem).length)o.dem=dem;if(lat)o.lat=lat;
|
||||
o.t=arWkSay(o);out.push(o);}
|
||||
return out[0].add?out:null;}
|
||||
// The file, never trusted: a run definition like AR_RUNS's, or null.
|
||||
function arWkValid(j,key){if(!pzObj(j)||Object.keys(j).some(k=>!AR_WK_FIELDS.includes(k))||j.v!==AR_WK_V||j.key!==key||pzWeekStart(key)==null||!pzCityOk(j.hq))return null;
|
||||
if(!Number.isInteger(j.budget)||j.budget<50000||j.budget>2e7)return null;
|
||||
if(j.rev!==undefined&&!(Number.isInteger(j.rev)&&j.rev>=2&&j.rev<=999))return null;if(j.ref!==undefined&&!pzObj(j.ref))return null;
|
||||
const name=arWkTxt(j.name,AR_WK_NAMES),joke=arWkTxt(j.joke,AR_WK_JOKES),briefs=arWkBriefs(j.briefs,j.hq);if(!name||!joke||!briefs)return null;
|
||||
return {id:arWkId(key),key,wk:true,n:arWkSay1(name),j:arWkSay1(joke),hq:j.hq,via:[],budget:j.budget,briefs,rev:j.rev||1};}
|
||||
// The run behind an id, when it is this week's.
|
||||
function arWkRun(id){const w=UI.arWk;return w&&w.def&&w.def.id===id?w.def:null;}
|
||||
// ---------- fetching it: when Puzzles opens, never on page load ----------
|
||||
function arWkEnsure(){const key=pzWeekKey(new Date()),w=UI.arWk;if(w&&w.key===key&&(w.def||Date.now()-w.at<5*60e3))return;
|
||||
const st=UI.arWk={key,def:null,at:Date.now()};let done=false;
|
||||
const fin=def=>{if(done)return;done=true;clearTimeout(to);if(UI.arWk!==st)return;st.def=def;st.at=Date.now();const el=$('arWk');if(el)el.outerHTML=arWkRow();};
|
||||
const ac=window.AbortController?new AbortController():null,to=setTimeout(()=>{if(ac)ac.abort();fin(null);},5000);
|
||||
if(!window.fetch||location.protocol==='file:'){fin(null);return;}
|
||||
try{fetch(`architect/weekly/${key}.json`,ac?{signal:ac.signal}:{}).then(r=>r.ok?r.json():null).then(j=>arWkValid(j,key),()=>null).then(fin,()=>fin(null));}catch(e){fin(null);}}
|
||||
// Its row in the Puzzles menu, above the handmade runs; nothing while there is none.
|
||||
function arWkRow(){const w=UI.arWk,r=w&&w.key===pzWeekKey(new Date())?w.def:null;if(!r)return '<div id="arWk"></div>';
|
||||
const x=arLoad().runs[r.id],ok=x&&(x.rv||1)===r.rev?x:null;
|
||||
return `<div class="scn pzwk" id="arWk"><div class="grow"><div class="eyebrow">🗓️ ${tr('architect_weekly.weekly_run')}</div><div class="nm">${esc(r.n)} <span class="pzst">${pzStarStr(ok?ok.st:0)}</span></div><div class="meta">${esc(r.j)}</div><div class="meta">${tr('architect.n_briefs',{n:r.briefs.length})} · 🏠 ${esc(CITY[r.hq].name)} · ${tr('puzzles.budget_x',{x:fmt$(r.budget)})}${ok&&ok.sp!=null?' · '+tr('puzzles.best_x',{x:fmt$(ok.sp)}):''}</div></div>${btn(tr('common.play'),'pzArStart',{id:r.id},ok&&ok.st>=3?'ghost':'pink')}</div>`;}
|
||||
|
|
@ -65,7 +65,7 @@ function arLinkModal(a,b,keep){const kb=pzKind(b);
|
|||
// ---------- results ----------
|
||||
const arLine=st=>st>=3?tr('architect.line_3'):st===2?tr('architect.line_2'):tr('architect.line_1');
|
||||
function arResults(done){const A=S.ar,run=arRun(),sp=arSpend(),br=done?run.briefs.length:A.bi,st=done?arStars(sp):0,sh=arShape();arRecord(st,br,sp);
|
||||
const nx=AR_RUNS[AR_RUNS.indexOf(run)+1]||null,stars=[0,1,2].map(i=>`<span class="pzbig${i<st?' on pop':''}" style="--d:${i*0.45}s">⭐</span>`).join('');
|
||||
const ri=AR_RUNS.indexOf(run),nx=ri>=0?AR_RUNS[ri+1]||null:null,stars=[0,1,2].map(i=>`<span class="pzbig${i<st?' on pop':''}" style="--d:${i*0.45}s">⭐</span>`).join('');
|
||||
openModal(`<div class="pzres">${st>=3?pzConfetti():''}<div style="display:flex;align-items:center"><div class="grow"><div class="eyebrow">🏗️ ${done?tr('architect.run_complete'):tr('architect.run_over')}</div><h2 style="font-size:28px">${esc(run.n)}</h2></div><button class="x" data-act="closeModal" aria-label="${tr('common.close')}">×</button></div>
|
||||
<div class="pzstars">${stars}</div><p class="tagline" style="text-align:center">${done?arLine(st):tr('architect.the_cfo_pulled_the_plug')}</p>
|
||||
<div class="kv"><span>${done?'⭐':'☆'} ${tr('architect.briefs_done')}</span><b>${br} / ${run.briefs.length}</b></div>
|
||||
|
|
@ -75,7 +75,7 @@ function arResults(done){const A=S.ar,run=arRun(),sp=arSpend(),br=done?run.brief
|
|||
<div class="btns cta" style="margin-top:12px">${nx&&done?btn(tr('architect.next_run')+' →','pzArStart',{id:nx.id},'pink big'):btn(tr('puzzles.more_puzzles'),'puzzleMenu',{},'pink big')}${btn(tr('architect.try_again'),'pzRetry',{},'')}${done?btn(tr('common.share_2cd9'),'pzShare',{},'ghost'):btn(tr('architect.keep_going'),'closeModal',{},'ghost')}</div></div>`);
|
||||
for(let i=0;i<st;i++)setTimeout(()=>{sfx('star');haptic(15);},i*450+150);if(st>=3)setTimeout(()=>sfx('fanfare'),3*450+250);}
|
||||
// ---------- the list ----------
|
||||
function arMenuRows(){const p=arLoad();let h=`<h3 class="pzpack">🏗️ ${tr('architect.architect_runs')} <span class="pznew">${tr('architect.new')}</span></h3><p class="meta" style="margin:0 0 2px">${tr('architect.runs_lead')}</p>`;
|
||||
function arMenuRows(){const p=arLoad();let h=`<h3 class="pzpack">🏗️ ${tr('architect.architect_runs')} <span class="pznew">${tr('architect.new')}</span></h3><p class="meta" style="margin:0 0 2px">${tr('architect.runs_lead')}</p>`;arWkEnsure();h+=arWkRow();
|
||||
for(const r of AR_RUNS){const x=p.runs[r.id];
|
||||
h+=`<div class="scn"><div class="grow"><div class="nm">${esc(r.n)} <span class="pzst">${pzStarStr(x?x.st:0)}</span></div><div class="meta">${esc(r.j)}</div><div class="meta">${tr('architect.n_briefs',{n:r.briefs.length})} · 🏠 ${esc(CITY[r.hq].name)}${' · '+tr('puzzles.budget_x',{x:fmt$(r.budget)})}${x&&x.sp!=null?' · '+tr('puzzles.best_x',{x:fmt$(x.sp)}):x&&x.br?' · '+tr('architect.best_n_briefs',{n:x.br}):''}</div></div>${btn(tr('common.play'),'pzArStart',{id:r.id},x&&x.st>=3?'ghost':'pink')}</div>`;}
|
||||
return h;}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue