diff options
author | Aki <please@ignore.pl> | 2021-08-08 21:50:12 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-08-08 21:50:12 +0200 |
commit | 3b3519105707985544f7345961b0286a6123cb30 (patch) | |
tree | c07b430d5dfec5b4d1a140c211a99ff09cc7714f | |
parent | 2d97c40e874927267e9cbadbb5b367004df58b89 (diff) | |
download | derelict-prototype-3b3519105707985544f7345961b0286a6123cb30.zip derelict-prototype-3b3519105707985544f7345961b0286a6123cb30.tar.gz derelict-prototype-3b3519105707985544f7345961b0286a6123cb30.tar.bz2 |
Separated data source into a class
-rw-r--r-- | derelict.js | 38 |
1 files 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})) } |