From 3b3519105707985544f7345961b0286a6123cb30 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 8 Aug 2021 21:50:12 +0200 Subject: Separated data source into a class --- derelict.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/derelict.js b/derelict.js index a91b1a8..9bd77bf 100644 --- a/derelict.js +++ b/derelict.js @@ -185,15 +185,15 @@ class Wreck { object = new THREE.Object3D() killmail - constructor({killmail, battle}) { + constructor({killmail, snapshot}) { 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 team = snapshot.getTeam(ownerId) const shipTypeId = killmail.victim.ship_type_id - const typeName = battle.ships[shipTypeId].name - const groupName = battle.ships[shipTypeId].group + const typeName = snapshot.getShipName(shipTypeId) + const groupName = snapshot.getShipGroup(shipTypeId) this.domElement.dataset.type = typeName this.domElement.dataset.group = groupName @@ -297,6 +297,29 @@ class StatusBar { } } +class Snapshot { + constructor(json) { + this.json = json + } + + getKillmails() { + return this.json.killmails + } + + getShipName(id) { + return this.json.ships[id.toString()].name + } + + getShipGroup(id) { + return this.json.ships[id.toString()].group + } + + getTeam(lookupId) { + const containsLookupId = team => undefined !== team.find(id => id == lookupId) + return this.json.teams.findIndex(containsLookupId) == 0 ? "teamA" : "teamB" + } +} + function vec3FromXYZ({x, y, z}) { return new THREE.Vector3(x, y, z) } @@ -306,9 +329,9 @@ function averagePosition(positions) { return sum.divideScalar(positions.length) } -function processBattle({battle, renderer, renderer2d, container, toolbar, skybox}) { +function processBattle({snapshot, renderer, renderer2d, container, toolbar, skybox}) { const battle_obj = new Battle({container, renderer, renderer2d}) - const wrecks = battle.killmails.map(killmail => new Wreck({killmail, battle})) + const wrecks = snapshot.getKillmails().map(killmail => new Wreck({killmail, snapshot})) 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())) @@ -359,5 +382,6 @@ export function init({id, container, toolbar}) { fetch(`/battles/${id}`) .then(response => response.json()) - .then(battle => processBattle({battle, renderer, renderer2d, container, toolbar, skybox})) + .then(json => new Snapshot(json)) + .then(snapshot => processBattle({snapshot, renderer, renderer2d, container, toolbar, skybox})) } -- cgit v1.1