import * as THREE from 'https://unpkg.com/three@0.126.1/build/three.module.js' import { OrbitControls } from 'https://unpkg.com/three@0.126.1/examples/jsm/controls/OrbitControls.js' import { CSS2DRenderer, CSS2DObject } from 'https://unpkg.com/three@0.126.1/examples/jsm/renderers/CSS2DRenderer.js' const SCALE = 10000 const METERS_IN_AU = 149597871000 const EXPIRY = 1000 * 60 * 10 const GRID_EXTENT = 100 * SCALE class Timeline { start finish current simple_list = [] register(wreck) { if (this.start === undefined || wreck.timestamp - EXPIRY < this.start) { this.start = wreck.timestamp - EXPIRY if (this.current === undefined || this.current == this.start) this.current = this.start } if (this.finish === undefined || wreck.timestamp + EXPIRY > this.finish) this.finish = wreck.timestamp + EXPIRY // TODO: Implement more reliable and optimized data structure for timeline. this.simple_list.push(wreck) } update() { this.simple_list.forEach(wreck => wreck.toggleKilled(this.current)) } setTo(time) { this.current = time } } class Grid { scene = new THREE.Scene() wrecks = [] constructor(wreck, {container, renderer, renderer2d}) { this.container = container this.renderer = renderer this.renderer2d = renderer2d const aspect = this.container.clientWidth / this.container.clientHeight this.camera = new THREE.PerspectiveCamera(80, aspect, 0.1, 1000) this.camera.position.set(8, 8, 8) this.camera.lookAt(0, 0, 0) this.controls = new OrbitControls(this.camera, this.renderer2d.domElement) this.controls.minDistance = 2 this.controls.maxDistance = 30 this.controls.enableDamping = true this.controls.dampingFactor = 0.4 const helper = new THREE.GridHelper(50, 50, 0x303030, 0x222222) this.scene.add(helper) this.positionOfReference = wreck.killmail.victim.position this.locationOfReference = wreck.location this.scene.add(wreck.object) this.wrecks.push(wreck) } tryAdding(wreck) { if (wreck.location == this.locationOfReference) { const distance = wreck.killmail.victim.position.distanceTo(this.positionOfReference) if (distance < GRID_EXTENT) { this.scene.add(wreck.object) this.wrecks.push(wreck) return true } } return false } draw() { this.renderer.render(this.scene, this.camera) this.renderer2d.render(this.scene, this.camera) this.controls.update() if (this.active) requestAnimationFrame(() => this.draw()) else this.renderer2d.domElement.innerHTML = '' } onresize() { this.renderer.setSize(this.container.clientWidth, this.container.clientHeight) this.renderer2d.setSize(this.container.clientWidth, this.container.clientHeight) this.camera.aspect = this.container.clientWidth / this.container.clientHeight this.camera.updateProjectionMatrix() } enable() { this.active = true this.controls.enabled = true } disable() { this.active = false this.controls.enabled = false } recenter() { const center = averagePosition(this.wrecks.map(wreck => wreck.killmail.victim.position)) this.wrecks.forEach(wreck => wreck.object.position.sub(center).divideScalar(SCALE)) // TODO: rescale } } class Battle { timeline = new Timeline() grids = [] constructor(grid_initializer) { this.grid_initializer = grid_initializer } add(wreck) { this.timeline.register(wreck) let added = false for (const grid of this.grids) { added = grid.tryAdding(wreck) if (added) break } if (!added) { const grid = new Grid(wreck, this.grid_initializer) this.grids.push(grid) } } } class Wreck { domElement = document.createElement('div') object = new THREE.Object3D() killmail constructor({killmail, battle}) { const labelElement = document.createElement('div') const object2d = new CSS2DObject(this.domElement) const ownerId = "alliance_id" in killmail.victim ? killmail.victim.alliance_id : killmail.victim.corporation_id const team = battle.teams.findIndex(team => undefined !== team.find(id => id == ownerId)) == 0 ? "teamA" : "teamB" const shipTypeId = killmail.victim.ship_type_id const typeName = battle.ships[shipTypeId].name const groupName = battle.ships[shipTypeId].group this.domElement.dataset.type = typeName this.domElement.dataset.group = groupName labelElement.textContent = typeName labelElement.classList.add('label') this.killmail = killmail this.timestamp = Date.parse(killmail.killmail_time) this.object.position.copy(killmail.victim.position) this.object.add(object2d) this.domElement.classList.add('wreck', team) this.domElement.appendChild(labelElement) this.domElement.onclick = () => window.open(`https://zkillboard.com/kill/${this.killmail.killmail_id}/`) } toggleKilled(timestamp) { const timeDifference = this.timestamp - timestamp this.domElement.classList.remove('future', 'alive', 'killed', 'expired') if (timeDifference < -EXPIRY) { this.domElement.classList.add('expired') } else if (timeDifference <= 0) { this.domElement.classList.add('killed') } else if (timeDifference < EXPIRY) { this.domElement.classList.add('alive') } else { this.domElement.classList.add('future') } } } function vec3FromXYZ({x, y, z}) { return new THREE.Vector3(x, y, z) } function averagePosition(positions) { const sum = positions.reduce((sum, pos) => sum.add(pos), new THREE.Vector3()) return sum.divideScalar(positions.length) } function processBattle({battle, renderer, renderer2d, container, toolbar, skybox}) { const battle_obj = new Battle({container, renderer, renderer2d}) const wrecks = battle.killmails.map(killmail => new Wreck({killmail, battle})) wrecks.forEach(wreck => wreck.killmail.victim.position = vec3FromXYZ(wreck.killmail.victim.position)) wrecks.forEach(wreck => battle_obj.add(wreck)) battle_obj.grids.forEach(grid => window.addEventListener('resize', () => grid.onresize())) battle_obj.grids.forEach(grid => grid.recenter()) const gridSelection = document.getElementById("grid") battle_obj.grids.forEach(grid => { const option = document.createElement("option") const origin = new THREE.Vector3() origin.copy(grid.wrecks[0].killmail.victim.position) origin.divideScalar(METERS_IN_AU) option.text = `${origin.x.toFixed(1)} AU, ${origin.y.toFixed(1)} AU, ${origin.z.toFixed(1)} AU` gridSelection.appendChild(option) }) gridSelection.oninput = () => { battle_obj.grids.forEach(g => g.disable()) battle_obj.grids.at(gridSelection.selectedIndex).enable() battle_obj.grids.at(gridSelection.selectedIndex).draw() } gridSelection.oninput() skybox.then(skybox => { const rt = new THREE.WebGLCubeRenderTarget(skybox.image.height) rt.fromEquirectangularTexture(renderer, skybox) battle_obj.grids.forEach(g => g.scene.background = rt) }) const start = battle_obj.timeline.start - EXPIRY const end = battle_obj.timeline.finish + EXPIRY const bottomBar = document.createElement("div") const seekbar = document.createElement("div") const buttons = document.createElement("div") const progress = document.createElement("div") const tip = document.createElement("div") const play = document.createElement("button") const time = document.createElement("span") bottomBar.classList.add("bottom-bar") seekbar.classList.add("seekbar") seekbar.draggable = true buttons.classList.add("buttons") progress.classList.add("progress") progress.style.width = "0%" tip.classList.add("tip") play.textContent = "▶️" container.appendChild(bottomBar) bottomBar.appendChild(seekbar) bottomBar.appendChild(buttons) buttons.appendChild(play) buttons.appendChild(time) seekbar.appendChild(progress) progress.appendChild(tip) const writeTime = stamp => { let date = new Date(stamp) time.textContent = date.toLocaleString() } let current = start let isPlaying = false let callbackId play.onclick = () => { isPlaying = !isPlaying if (isPlaying) { play.textContent = "⏸️" callbackId = setInterval(() => { if (current + 5000 > end) { current = end isPlaying = false clearInterval(callbackId) play.textContent = "▶️" } else { current += 5000 } battle_obj.timeline.simple_list.forEach(item => item.toggleKilled(current)) const norm = (current - start) / (end - start) progress.style.width = `${norm * 100}%` writeTime(current) }, 100) } else { clearInterval(callbackId) play.textContent = "▶️" } } const seekWithBar = e => { const rect = e.srcElement.getBoundingClientRect() const norm = Math.min(1, Math.max(0, (e.clientX - rect.left) / e.srcElement.clientWidth)) const lim = Math.floor(norm * e.srcElement.clientWidth) / e.srcElement.clientWidth const timestamp = start + (end - start) * lim if (e.clientX != 0 && current != timestamp) { current = timestamp battle_obj.timeline.simple_list.forEach(item => item.toggleKilled(timestamp)) progress.style.width = `${lim * 100}%` writeTime(current) } } battle_obj.timeline.simple_list.forEach(item => item.toggleKilled(start)) writeTime(current) seekbar.onclick = seekWithBar seekbar.ondrag = seekWithBar seekbar.ondragstart = e => e.dataTransfer.setDragImage(new Image(0, 0), 0, 0) } export function init({id, container, toolbar}) { const renderer = new THREE.WebGLRenderer({antialias: true}) const renderer2d = new CSS2DRenderer() renderer.setSize(container.clientWidth, container.clientHeight) renderer2d.setSize(container.clientWidth, container.clientHeight) renderer2d.domElement.style.position = 'absolute' renderer2d.domElement.style.top = '0px' container.appendChild(renderer.domElement) container.appendChild(renderer2d.domElement) const loader = new THREE.TextureLoader() const skybox = loader.loadAsync("https://i.imgur.com/rDGOLFC.jpg") // TODO: Don't use imgur as CDN. fetch(`/battles/${id}`) .then(response => response.json()) .then(battle => processBattle({battle, renderer, renderer2d, container, toolbar, skybox})) }