Standing orders: a NOC intern runs the player's orders once a day (#98)

This commit is contained in:
Frederic Jung 2026-10-28 17:40:22 +01:00
parent fa4e30692e
commit 7c8fa68659
No known key found for this signature in database
18 changed files with 351 additions and 41 deletions

View file

@ -1,17 +1,5 @@
/* Optional operators use the same sales and repair actions as the player. */
const AUTO_SALES_MS=5000;
function autoSalesSeconds(now=performance.now()){if(!S||!S.autoSales)return null;
const left=UI.autoGame===S&&autoActive()?AUTO_SALES_MS-(now-UI.autoSalesAt):AUTO_SALES_MS;
return Math.max(1,Math.min(AUTO_SALES_MS/1000,Math.ceil(left/1000)));}
function autoCountdownReady(o){if(!S||!S.autoSales||!o||!(o.exp>S.day)||!bCities(o).every(popUp)||!reqOk(o))return false;
const inc=S.inc.map(i=>i.id).join('|');let c=UI.autoReady;
// Share capacity trials between views and keep them until the next check, action or incident.
if(!c||c.s!==S||c.at!==UI.autoSalesAt||c.inc!==inc||c.event!==S.nextEvent)c=UI.autoReady={s:S,at:UI.autoSalesAt,inc,event:S.nextEvent,offers:new Map()};
if(!c.offers.has(o))c.offers.set(o,!!autoOffer(o));return c.offers.get(o);}
function autoAcceptLabel(o){return tr('common.accept')+(S.autoSales?` <small data-auto-sales="${esc(o.id)}" title="${esc(tr('automation.next_auto_check'))}">${autoCountdownReady(o)?`(${autoSalesSeconds()})`:''}</small>`:'');}
function renderAutoCountdown(now=performance.now()){const n=autoSalesSeconds(now);if(UI.autoCount===n&&UI.autoCountResult===R)return;UI.autoCount=n;UI.autoCountResult=R;
const offers=new Map((S?S.offers:[]).map(o=>[o.id,o]));
for(const el of document.querySelectorAll('[data-auto-sales]'))el.textContent=n!==null&&autoCountdownReady(offers.get(el.dataset.autoSales))?`(${n})`:'';}
/* The sales engine of #185: would this offer work if signed now? The intern's sales order runs it once a day.
It trials the new demand on a copy of the game, so a failed trial leaves the live game untouched. */
function offerCustomer(o){return {id:o.id,city:o.city,city2:o.city2||null,reqs:o.reqs||[],latMax:o.latMax||null,route:BIZ_T[o.type].scenic?'scenic':BIZ_T[o.type].lat?'fast':'normal',name:o.name,type:o.type,bw:o.bw,rate:o.rate,mood:100,since:S.day,sites:o.sites||null,start:o.start,dur:o.dur,fee:o.fee,link:o.link,slots:o.slots||0,what:o.what,prov:o.prov,due:BIZ_T[o.type].cap?S.day+o.win:undefined};}
function autoOffer(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.rate<0||!bCities(o).length||!bCities(o).every(popUp)||!reqOk(o))return null;
const T=BIZ_T[o.type],b=offerCustomer(o);if(T.event&&(!(o.start>S.day)||!(o.dur>0)))return null;
@ -27,10 +15,3 @@ function autoOffer(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.ra
const healthy=S.biz.filter(x=>!bizWhy(x).length);if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=S.biz.concat(b);compute();
return !bizWhy(b).length&&healthy.every(x=>!bizWhy(x).length)?b:null;
}finally{S=state;R=result;UI.hotMap=hot;b.start=o.start;if(T.cap)delete b.live;}}
function autoActive(){return !!(S&&UI.started&&!S.lost&&UI.speed&&$('modal').hidden&&document.visibilityState==='visible');}
function automationTick(now){if(UI.autoGame!==S){UI.autoGame=S;UI.autoSalesAt=now;}
if(!autoActive()){UI.autoSalesAt=now;return;}
if(S.autoRepair&&S.inc.length){purgeInc();for(const i of S.inc.slice()){if(!autoActive())break;
if(i.type!=='ddos'&&i.until>S.day&&i.cost>0&&i.cost<=S.cash&&!(i.slow&&i.rushed))ACT.fixInc({id:i.id});}}
if(!S.autoSales||!autoActive()){UI.autoSalesAt=now;return;}if(now-UI.autoSalesAt<AUTO_SALES_MS)return;
UI.autoSalesAt=now;for(const o of S.offers.slice()){if(!autoActive())break;ACT.accept({id:o.id,auto:true});}}