Files
pages/static/js/city-geometry.js
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

49 lines
3.1 KiB
JavaScript

(() => {
const VERSION = 1;
const hash = value => {
let h = 2166136261;
for (const char of String(value)) { h ^= char.charCodeAt(0); h = Math.imul(h, 16777619); }
return h >>> 0;
};
const randomFrom = seed => {
let state = hash(seed) || 1;
return () => { state += 0x6D2B79F5; let t = state; t = Math.imul(t ^ t >>> 15, t | 1); t ^= t + Math.imul(t ^ t >>> 7, t | 61); return ((t ^ t >>> 14) >>> 0) / 4294967296; };
};
const jitter = (random, amount) => (random() * 2 - 1) * amount;
const cacheKey = city => `pageswiki:city-geometry:v${VERSION}:${city.domain_uuid}:${city.version}`;
const signature = city => city.quartiers.map(q => `${q.uuid}:${q.streets.map(s => `${s.uuid}:${s.spots.map(p => p.uuid).join(',')}`).join('|')}`).join(';');
function generate(city) {
const random = randomFrom(`${city.domain_uuid}:${city.version}:${signature(city)}`);
const geometry = { version:VERSION, domain:{width:1000,height:Math.max(560,Math.ceil(city.quartiers.length/3)*250+80)}, quartiers:{}, streets:{}, spots:{} };
city.quartiers.forEach((quartier,index) => {
const col=index%3,row=Math.floor(index/3),x=55+col*315+jitter(random,18),y=55+row*235+jitter(random,14);
const width=255+jitter(random,22),height=170+jitter(random,18),cut=18+random()*24;
geometry.quartiers[quartier.uuid]={x,y,width,height,polygon:[[x+cut,y],[x+width-cut*.3,y+4],[x+width,y+cut],[x+width-7,y+height-cut*.4],[x+width-cut,y+height],[x+cut*.4,y+height-5],[x,y+height-cut],[x+5,y+cut]]};
const mapHeight=Math.max(480,quartier.streets.length*125+110);
quartier.streets.forEach((street,streetIndex) => {
const sy=90+streetIndex*120+jitter(random,22),bend=jitter(random,85),startY=sy+jitter(random,18),endY=sy+jitter(random,28);
geometry.streets[street.uuid]={width:920,height:mapHeight,path:[[35,startY],[260,sy+bend],[635,sy-bend*.65],[885,endY]],laneWidth:26+random()*14};
street.spots.forEach((spot,spotIndex) => {
const side=spotIndex%2===0?-1:1,slot=Math.floor(spotIndex/2),sx=65+slot*210+jitter(random,18),baseY=side<0?62:330;
geometry.spots[spot.uuid]={x:sx,y:baseY+jitter(random,16),width:160+jitter(random,15),height:112+Math.min(4,(spot.pages||[]).length)*19+jitter(random,10),side,roof:random()>.55?'gable':'flat'};
});
});
});
return geometry;
}
function load(city) {
const key=cacheKey(city),expected=signature(city);
try { const cached=JSON.parse(localStorage.getItem(key)); if(cached?.version===VERSION&&cached.signature===expected)return cached.geometry; } catch (_) {}
const geometry=generate(city);
try {
localStorage.setItem(key,JSON.stringify({version:VERSION,signature:expected,geometry}));
Object.keys(localStorage).filter(item=>item.startsWith(`pageswiki:city-geometry:v${VERSION}:${city.domain_uuid}:`)&&item!==key).slice(0,-2).forEach(item=>localStorage.removeItem(item));
} catch (_) {}
return geometry;
}
window.PagesWikiCityGeometry={version:VERSION,load,generate,register(adapter){if(adapter?.load&&adapter?.generate)window.PagesWikiCityGeometry=adapter;}};
})();