Save slots: several networks per browser, one per window, with a cross-window guard
This commit is contained in:
parent
39ef7c13bb
commit
da8150b7c4
29 changed files with 595 additions and 124 deletions
|
|
@ -8,20 +8,20 @@ function homePick(){const cs=Object.keys(CITY).filter(homeOk),cur=START5.include
|
|||
// A dropdown can't wrap, so long names (Yuzhno-Sakhalinsk) get cut on phones: the line above it says the whole name.
|
||||
const homeWhat=()=>START5.includes(UI.startCity)?trn('title.n_more_cities_with_transit',Object.keys(CITY).filter(c=>homeOk(c)&&!START5.includes(c)).length):`${esc(CITY[UI.startCity].name)} · ${esc(REG[CITY[UI.startCity].reg])}`;
|
||||
function showTitle(){
|
||||
const sv=loadSave();
|
||||
const list=slotIdx(true),top=slotTop(list),many=list.length>1||(list.length===1&&list[0]!==top);
|
||||
if(UI.nm==null)UI.nm=randomNetworkName();
|
||||
const starts=[['fra',tr('title.crossroads_of_europe_de_kicks_ix')],['nyc',tr('title.huge_eyeball_market_pricey')],['sin',tr('title.asia_hub_great_peering_far_from')],['sao',tr('title.big_hungry_market_few_providers')],['syd',tr('title.hard_mode_pricey_everything')]];
|
||||
openModal(`<div class="title-art"><svg width="74" height="74" viewBox="0 0 74 74" aria-hidden="true"><path d="M10 60V40c0-14 10-24 24-24h18" fill="none" stroke="#1E2640" stroke-width="16" stroke-linecap="round"/><path d="M10 60V40c0-14 10-24 24-24h18" fill="none" stroke="#FFD23F" stroke-width="9" stroke-linecap="round"/><circle cx="58" cy="16" r="11" fill="#FF4F9A" stroke="#1E2640" stroke-width="4"/><circle cx="21" cy="44" r="3" fill="#fff"/><circle cx="30" cy="27" r="3" fill="#fff"/></svg>
|
||||
<h1>Series<br>of <span>Tubes</span></h1></div>
|
||||
<p class="tagline">${tr('title.build_an_internet_lay_fiber_between')}</p>${plugHtml(false)}${isPhone()?installRow():''}
|
||||
${sv?`<div class="btns" style="margin-bottom:6px">${btn(tr('title.continue_name_date',{name:esc(sv.name),date:dateStr(sv.day)}),'cont',{},'pink big')}</div>`:''}
|
||||
${top?`<div class="btns" style="margin-bottom:6px">${btn(tr('title.continue_name_date',{name:esc(top.name||tr('common.unnamed_network')),date:dateStr(top.day)}),'cont',{id:top.id},'pink big')}</div>`:''}
|
||||
${many?slotsHtml():''}
|
||||
${doorsHtml()}
|
||||
<label class="f" for="nmIn">${tr('title.name_your_network')}</label><div class="network-name"><input class="t" id="nmIn" maxlength="24" value="${esc(UI.nm)}" autocomplete="off">${btn(tr('common.another_one'),'randomName',{},'ghost sm')}</div>
|
||||
<label class="f" id="homeLbl">${tr('title.pick_your_home_city')}</label><div class="starts homes">${starts.map(([c,d])=>`<button class="start ${UI.startCity===c?'on':''}" data-city="${c}" data-act="pickStart" data-args='{"city":"${c}"}'><b>${CITY[c].name}</b><span>${d}</span></button>`).join('')}<label class="start own ${START5.includes(UI.startCity)?'':'on'}" for="homeSel"><b>${tr('title.anywhere_else')}</b><span id="homeWhat">${homeWhat()}</span>${homePick()}</label></div>
|
||||
<label class="f" for="langTitle">${tr('common.language')}</label>${langPick('langTitle')}
|
||||
<div class="btns cta tcta" style="margin-top:16px">${btn(sv?tr('common.start_a_new_network'):tr('common.start_building'),'start',{},'big')}${btn(tr('common.how_to_play'),'help',{},'ghost')}${btn(tr('common.import_a_save'),'importPick',{},'ghost')}${btn(tr('common.settings'),'settings',{},'ghost')}</div><p class="ver">v${APP_VERSION} · <a href="https://github.com/fjaeckel/seriesoftubes" target="_blank" rel="noopener">${tr('title.source_on_github')}</a> · <a href="${SIGNAL}" target="_blank" rel="noopener">${tr('title.chat_on_signal')}</a> · ${tr('title.cookieless_stats_no_tracking')}</p>`);
|
||||
<div class="btns cta tcta" style="margin-top:16px">${btn(list.length?tr('common.start_a_new_network'):tr('common.start_building'),'start',{},'big')}${btn(tr('common.how_to_play'),'help',{},'ghost')}${btn(tr('common.import_a_save'),'importPick',{},'ghost')}${btn(tr('common.settings'),'settings',{},'ghost')}</div><p class="ver">v${APP_VERSION} · <a href="https://github.com/fjaeckel/seriesoftubes" target="_blank" rel="noopener">${tr('title.source_on_github')}</a> · <a href="${SIGNAL}" target="_blank" rel="noopener">${tr('title.chat_on_signal')}</a> · ${tr('title.cookieless_stats_no_tracking')}</p>`);
|
||||
$('nmIn').addEventListener('input',e=>{UI.nm=e.target.value;});
|
||||
if(sv){S=migrate(sv);compute();}
|
||||
}
|
||||
// "What's new": shown once to returning players after an update, or on demand.
|
||||
function whatsNew(returning,force){
|
||||
|
|
|
|||
37
src/game/95-modals/45-slots.js
Normal file
37
src/game/95-modals/45-slots.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* "Your networks" on the title screen: drawn from the index alone, so it stays instant however big the saves are. */
|
||||
function slotAgo(t){const m=Math.max(0,Math.round((Date.now()-t)/60000));
|
||||
return m<1?tr('slots.just_now'):m<60?trn('notifications.n_min_ago',m):m<1440?trn('slots.n_h_ago',Math.round(m/60)):trn('peering_inbox.n_day_ago',Math.round(m/1440));}
|
||||
const slotIcon=e=>e.broken?'🩹':e.mode==='scenario'?'🏆':e.mode==='sandbox'?'🏖️':(PRESETS[e.preset]||PRESETS.oncall).i;
|
||||
// The network to continue: the most recent one that is not open in another window (or damaged).
|
||||
function slotTop(list){return slotByAt(list).find(e=>!e.broken&&!slotBusy(e.id))||null;}
|
||||
function slotLine(e){if(e.broken)return tr('slots.damaged_export_or_delete');
|
||||
const bits=[e.asn?'AS'+e.asn:'',e.name||e.gz?dateStr(e.day):''];if(e.mode==='scenario'&&e.scn)bits.push(esc(e.scn));else if(e.tier)bits.push(e.tier);
|
||||
return bits.filter(Boolean).join(' · ');}
|
||||
function slotRow(e,top){const busy=slotBusy(e.id),nm=esc(e.name||tr('common.unnamed_network'));
|
||||
const tag=busy?`<span class="tag">${tr('slots.open_in_another_window')}</span>`:top?`<span class="tag">▶ ${tr('slots.continue')}</span>`:'';
|
||||
return `<div class="slot${top?' top':''}${busy?' busy':''}${e.broken?' broken':''}" data-slot="${e.id}"><button class="slot-go" data-act="${e.broken?'slotMenu':'cont'}" data-args='{"id":"${e.id}"}'><i aria-hidden="true">${slotIcon(e)}</i><span class="sn">${tag}<b>${nm}</b><small>${slotLine(e)}</small></span><span class="sm">${e.broken?'':`<em>${fmt$(e.cash)}</em>`}<small>${e.at?slotAgo(e.at):''}</small></span></button><button class="slot-more" data-act="slotMenu" data-args='{"id":"${e.id}"}' aria-label="${tr('slots.more_for_name',{name:nm})}">⋯</button></div>`;}
|
||||
function slotListHtml(){const list=slotByAt(slotIdx()),top=slotTop(list);
|
||||
return list.map(e=>slotRow(e,e===top)).join('');}
|
||||
function slotsHtml(){const list=slotIdx(true);if(!list.length)return '';
|
||||
SLOT.html=slotListHtml();return `<label class="f">${tr('slots.your_networks')}</label><div class="slots" id="slotList">${SLOT.html}</div>`;}
|
||||
// Another window saved, opened or closed a network: the list follows (only when it is on screen and changed).
|
||||
function slotRefresh(){const el=$('slotList');if(!el||UI.started)return;const h=slotListHtml();if(h!==SLOT.html){SLOT.html=h;el.innerHTML=h;}}
|
||||
function slotMenuHtml(id){const e=slotIdx().find(x=>x.id===id);if(!e)return '';const nm=esc(e.name||tr('common.unnamed_network'));
|
||||
return `<div style="display:flex;align-items:center"><div class="grow"><div class="eyebrow">${slotIcon(e)} ${slotLine(e)}</div><h2 style="font-size:26px">${nm}</h2></div><button class="x" data-act="slotBack" aria-label="${tr('common.close')}">×</button></div>
|
||||
<p class="tagline">${e.at?tr('slots.last_played_ago',{ago:slotAgo(e.at)}):''}${e.size?' · '+tr('slots.size_on_this_device',{size:slotSize(e.size)}):''}</p>
|
||||
<div class="menu-list">${e.broken?'':btn(tr('slots.continue_this_network'),'cont',{id},'pink big')}${btn(tr('game_and_settings.export_save_download_a_file'),'slotExport',{id},'')}${btn(tr('slots.delete_this_network'),'slotDel',{id},'red')}${btn(tr('common.back'),'slotBack',{},'ghost')}</div>`;}
|
||||
const slotSize=n=>n>=1e6?(n/1e6).toFixed(1)+' MB':Math.max(1,Math.round(n/1e3))+' kB';
|
||||
function slotBusyModal(id){const e=slotIdx().find(x=>x.id===id),nm=esc(e&&e.name||tr('common.unnamed_network'));
|
||||
openModal(`<div class="eyebrow">🪟 ${tr('slots.two_windows_one_network')}</div><h2 style="font-size:26px">${tr('slots.name_is_open_in_another_window',{name:nm})}</h2>
|
||||
<p class="tagline">${tr('slots.only_one_window_can_play_a')}</p><div class="btns">${btn(tr('slots.switch_to_that_window'),'slotSwitch',{id},'pink big')}${btn(tr('slots.take_over_here'),'slotTake',{id},'big')}${btn(tr('slots.your_networks'),'slotBack',{},'ghost')}</div>`);}
|
||||
// Shown in the window that stepped aside: calm, and nothing here writes.
|
||||
function slotLostModal(id,name,why){const nm=esc(name||tr('common.unnamed_network'));
|
||||
openModal(`<div class="eyebrow">🪟 ${tr('slots.two_windows_one_network')}</div><h2 style="font-size:26px">${why==='newer'?tr('slots.name_was_saved_in_another_window',{name:nm}):tr('slots.name_opened_in_another_window',{name:nm})}</h2>
|
||||
<p class="tagline">${why==='newer'?tr('slots.this_window_stopped_saving_so_it'):tr('slots.this_window_stepped_aside_so_the')}</p><div class="btns">${btn(why==='newer'?tr('slots.load_the_newest_version'):tr('slots.take_it_back_here'),why==='newer'?'cont':'slotTake',{id},'pink big')}${btn(tr('slots.your_networks'),'slotBack',{},'ghost big')}</div>`);}
|
||||
const slotOthers=()=>slotIdx().filter(e=>e.id!==UI.slot);
|
||||
// Out of room: which other networks take space, with export and delete right there.
|
||||
function slotSpaceHtml(){const l=slotOthers().sort((a,b)=>b.size-a.size);
|
||||
return `<div style="display:flex;align-items:center"><div class="grow"><div class="eyebrow">💾 ${tr('slots.storage_on_this_device')}</div><h2 style="font-size:26px">${tr('slots.free_up_space')}</h2></div><button class="x" data-act="closeModal" aria-label="${tr('common.close')}">×</button></div>
|
||||
<p class="tagline">${tr('slots.your_other_networks_share_this_browser')}</p>
|
||||
${l.length?l.map(e=>`<div class="ibx space"><div class="grow"><b>${slotIcon(e)} ${esc(e.name||tr('common.unnamed_network'))}</b><div class="meta">${slotSize(e.size)}${e.at?' · '+slotAgo(e.at):''}</div></div><div class="btns">${btn(tr('common.export_save'),'slotExport',{id:e.id},'ghost sm')}${btn(tr('slots.delete'),'slotDel',{id:e.id,back:'space'},'red sm')}</div></div>`).join(''):`<p class="note">${tr('slots.no_other_networks_here')}</p>`}
|
||||
<div class="btns" style="margin-top:10px">${btn(tr('common.back_to_the_map'),'closeModal',{},'pink big')}</div>`;}
|
||||
|
|
@ -21,4 +21,4 @@ function showHelp(){
|
|||
</dl><h3 style="margin:16px 0 0">${tr('help.want_to_know_how_it_really')}</h3>${plugHtml(true)}${chatHtml()}${issueHtml()}</dl><div class="btns" style="margin-top:14px">${btn(UI.started?tr('common.back_to_the_map'):tr('common.got_it'),'helpClose',{},'big')}${RELEASE_NOTES.length?btn(tr('common.what_s_new_in_v',{v:esc(APP_VERSION)}),'whatsNewShow',{},'ghost'):''}${UI.started&&S&&!S.lost?btn(tr('common.start_a_new_network'),'resetAsk',{},'ghost sm'):''}</div><div class="btns" style="margin-top:6px">${UI.started&&S&&!S.lost?btn(tr('common.export_save'),'exportSave',{},'ghost sm'):''}${btn(tr('common.import_a_save'),'importPick',{},'ghost sm')}${btn(tr('common.settings'),'settings',{},'ghost sm')}</div>`);
|
||||
}
|
||||
function showWin(){setTimeout(()=>{const m=$('modalCard');if(m&&!m.querySelector('[data-act=share]'))m.insertAdjacentHTML('beforeend',`<div class="btns" style="margin-top:8px">${btn(tr('help.share_my_network'),'share',{},'pink')}</div><p class="meta" style="margin:12px 0 0">${tr('help.you_built_a_cartoon_internet_now')}</p>${plugHtml(true)}`);},0);openModal(`<h1 style="font-family:var(--display);font-size:44px;margin:0">${tr('help.you_re_a_tier_1')}</h1><p class="tagline">${tr('help.name_as_asn_is_transit_free',{name:esc(S.name),asn:S.asn,days:S.day,n:S.stats.incidents})}</p>${btn(tr('common.keep_building'),'keepPlaying',{},'pink big')}`);sfx('coin');}
|
||||
function showLose(){openModal(`<h1 style="font-family:var(--display);font-size:40px;margin:0">${tr('common.bankrupt')}</h1><p class="tagline">${tr('help.the_bank_repossessed_your_routers',{name:esc(S.name),days:S.day,peak:fmtG(S.stats.peak)})}</p>${btn(tr('common.try_again'),'newgame',{},'pink big')}`);try{localStorage.removeItem('sot-save-v1');}catch(e){}}
|
||||
function showLose(){openModal(`<h1 style="font-family:var(--display);font-size:40px;margin:0">${tr('common.bankrupt')}</h1><p class="tagline">${tr('help.the_bank_repossessed_your_routers',{name:esc(S.name),days:S.day,peak:fmtG(S.stats.peak)})}</p>${btn(tr('common.try_again'),'newgame',{},'pink big')}`);if(UI.slot){const id=UI.slot;slotDrop(id);slotFree(id);}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue